blob: 8b7a519c08006eaacc7d8fe874928eb00fbc46fe [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200110#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
111static int text_to_screenline(win_T *wp, char_u *text, int col);
112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_FOLDING
114static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
117
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200118/* Flag that is set when drawing for a callback, not from the main command
119 * loop. */
120static int redrawing_for_callback = 0;
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122/*
123 * Buffer for one screen line (characters and attributes).
124 */
125static schar_T *current_ScreenLine;
126
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void win_update(win_T *wp);
128static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
131static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
132static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200134static int win_line(win_T *, linenr_T, int, int, int nochange);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200141# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void start_search_hl(void);
143static void end_search_hl(void);
144static void init_search_hl(win_T *wp);
145static void prepare_search_hl(win_T *wp, linenr_T lnum);
146static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
147static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screen_start_highlight(int attr);
150static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200155static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void lineinvalid(unsigned off, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void linecopy(int to, int from, win_T *wp);
158static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200159static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void win_rest_invalid(win_T *wp);
161static void msg_pos_mode(void);
162static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200164static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200166#ifdef FEAT_MENU
167static void redraw_win_toolbar(win_T *wp);
168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#endif
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176/* Ugly global: overrule attribute used by screen_char() */
177static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200179#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
180/* Can limit syntax highlight time to 'redrawtime'. */
181# define SYN_TIME_LIMIT 1
182#endif
183
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200184#ifdef FEAT_RIGHTLEFT
185# define HAS_RIGHTLEFT(x) x
186#else
187# define HAS_RIGHTLEFT(x) FALSE
188#endif
189
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190/*
191 * Redraw the current window later, with update_screen(type).
192 * Set must_redraw only if not already set to a higher value.
193 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
194 */
195 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100196redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197{
198 redraw_win_later(curwin, type);
199}
200
201 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100202redraw_win_later(
203 win_T *wp,
204 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200206 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
208 wp->w_redr_type = type;
209 if (type >= NOT_VALID)
210 wp->w_lines_valid = 0;
211 if (must_redraw < type) /* must_redraw is the maximum of all windows */
212 must_redraw = type;
213 }
214}
215
216/*
217 * Force a complete redraw later. Also resets the highlighting. To be used
218 * after executing a shell command that messes up the screen.
219 */
220 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100221redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000224#ifdef FEAT_GUI
225 if (gui.in_use)
226 /* Use a code that will reset gui.highlight_mask in
227 * gui_stop_highlight(). */
228 screen_attr = HL_ALL + 1;
229 else
230#endif
231 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200232 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233}
234
235/*
236 * Mark all windows to be redrawn later.
237 */
238 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100239redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240{
241 win_T *wp;
242
243 FOR_ALL_WINDOWS(wp)
244 {
245 redraw_win_later(wp, type);
246 }
247}
248
249/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000250 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 */
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 redraw_buf_later(curbuf, type);
256}
257
258 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100259redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 win_T *wp;
262
263 FOR_ALL_WINDOWS(wp)
264 {
265 if (wp->w_buffer == buf)
266 redraw_win_later(wp, type);
267 }
268}
269
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200270 void
271redraw_buf_and_status_later(buf_T *buf, int type)
272{
273 win_T *wp;
274
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200275#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200276 if (wild_menu_showing != 0)
277 /* Don't redraw while the command line completion is displayed, it
278 * would disappear. */
279 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200280#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281 FOR_ALL_WINDOWS(wp)
282 {
283 if (wp->w_buffer == buf)
284 {
285 redraw_win_later(wp, type);
286 wp->w_redr_status = TRUE;
287 }
288 }
289}
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200292 * Redraw as soon as possible. When the command line is not scrolled redraw
293 * right away and restore what was on the command line.
294 * Return a code indicating what happened.
295 */
296 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100297redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200298{
299 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200300 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200301 int r;
302 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 schar_T *screenline; /* copy from ScreenLines[] */
304 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200305#ifdef FEAT_MBYTE
306 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200307 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200309 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310#endif
311
312 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 return ret;
315
316 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 if (screenline == NULL || screenattr == NULL)
323 ret = 2;
324#ifdef FEAT_MBYTE
325 if (enc_utf8)
326 {
327 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenlineUC == NULL)
330 ret = 2;
331 for (i = 0; i < p_mco; ++i)
332 {
333 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200334 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (screenlineC[i] == NULL)
336 ret = 2;
337 }
338 }
339 if (enc_dbcs == DBCS_JPNU)
340 {
341 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200342 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenline2 == NULL)
344 ret = 2;
345 }
346#endif
347
348 if (ret != 2)
349 {
350 /* Save the text displayed in the command line area. */
351 for (r = 0; r < rows; ++r)
352 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200353 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200354 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200355 (size_t)cols * sizeof(schar_T));
356 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359#ifdef FEAT_MBYTE
360 if (enc_utf8)
361 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 mch_memmove(screenlineC[i] + r * cols,
367 ScreenLinesC[i] + LineOffset[cmdline_row + r],
368 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200369 }
370 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200373 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374#endif
375 }
376
377 update_screen(0);
378 ret = 3;
379
380 if (must_redraw == 0)
381 {
382 int off = (int)(current_ScreenLine - ScreenLines);
383
384 /* Restore the text displayed in the command line area. */
385 for (r = 0; r < rows; ++r)
386 {
387 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200388 screenline + r * cols,
389 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenattr + r * cols,
392 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393#ifdef FEAT_MBYTE
394 if (enc_utf8)
395 {
396 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenlineUC + r * cols,
398 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 for (i = 0; i < p_mco; ++i)
400 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200401 screenlineC[i] + r * cols,
402 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200403 }
404 if (enc_dbcs == DBCS_JPNU)
405 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200406 screenline2 + r * cols,
407 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200408#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200409 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 }
411 ret = 4;
412 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413 }
414
415 vim_free(screenline);
416 vim_free(screenattr);
417#ifdef FEAT_MBYTE
418 if (enc_utf8)
419 {
420 vim_free(screenlineUC);
421 for (i = 0; i < p_mco; ++i)
422 vim_free(screenlineC[i]);
423 }
424 if (enc_dbcs == DBCS_JPNU)
425 vim_free(screenline2);
426#endif
427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
435
436/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100437 * Invoked after an asynchronous callback is called.
438 * If an echo command was used the cursor needs to be put back where
439 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200440 * If "call_update_screen" is FALSE don't call update_screen() when at the
441 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100442 */
443 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200444redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200446 ++redrawing_for_callback;
447
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100448 if (State == HITRETURN || State == ASKMORE)
449 ; /* do nothing */
450 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200451 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200452 /* Redrawing only works when the screen didn't scroll. Don't clear
453 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454 if (msg_scrolled == 0
455#ifdef FEAT_WILDMENU
456 && wild_menu_showing == 0
457#endif
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200458 && call_update_screen)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200459 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200460 /* Redraw in the same position, so that the user can continue
461 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200462 redrawcmdline_ex(FALSE);
463 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200464 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100465 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 /* keep the command line if possible */
467 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100468 setcursor();
469 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100471#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100472 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200473 /* Don't update the cursor when it is blinking and off to avoid
474 * flicker. */
Bram Moolenaara338adc2018-01-31 20:51:47 +0100475 out_flush_cursor(FALSE, FALSE);
476 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100478 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200479
480 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100481}
482
483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 * Changed something in the current window, at buffer line "lnum", that
485 * requires that line and possibly other lines to be redrawn.
486 * Used when entering/leaving Insert mode with the cursor on a folded line.
487 * Used to remove the "$" from a change command.
488 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
489 * may become invalid and the whole window will have to be redrawn.
490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100492redrawWinline(
493 linenr_T lnum,
494 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496#ifdef FEAT_FOLDING
497 int i;
498#endif
499
500 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
501 curwin->w_redraw_top = lnum;
502 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
503 curwin->w_redraw_bot = lnum;
504 redraw_later(VALID);
505
506#ifdef FEAT_FOLDING
507 if (invalid)
508 {
509 /* A w_lines[] entry for this lnum has become invalid. */
510 i = find_wl_entry(curwin, lnum);
511 if (i >= 0)
512 curwin->w_lines[i].wl_valid = FALSE;
513 }
514#endif
515}
516
517/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200518 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 */
520 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100521update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523 redraw_curbuf_later(type);
524 update_screen(type);
525}
526
527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * Based on the current value of curwin->w_topline, transfer a screenfull
529 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200530 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200532 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200533update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200535 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 win_T *wp;
537 static int did_intro = FALSE;
538#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
539 int did_one;
540#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200541#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200542 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200543 int gui_cursor_col;
544 int gui_cursor_row;
545#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000548 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200550 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552 if (type == VALID_NO_UPDATE)
553 {
554 no_update = TRUE;
555 type = 0;
556 }
557
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 if (must_redraw)
559 {
560 if (type < must_redraw) /* use maximal type */
561 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000562
563 /* must_redraw is reset here, so that when we run into some weird
564 * reason to redraw while busy redrawing (e.g., asynchronous
565 * scrolling), or update_topline() in win_update() will cause a
566 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 must_redraw = 0;
568 }
569
570 /* Need to update w_lines[]. */
571 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
572 type = NOT_VALID;
573
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000574 /* Postpone the redrawing when it's not needed and when being called
575 * recursively. */
576 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {
578 redraw_later(type); /* remember type for next time */
579 must_redraw = type;
580 if (type > INVERTED_ALL)
581 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200582 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 }
584
585 updating_screen = TRUE;
586#ifdef FEAT_SYN_HL
587 ++display_tick; /* let syntax code know we're in a next round of
588 * display updating */
589#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200590 if (no_update)
591 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 /*
594 * if the screen was scrolled up when displaying a message, scroll it down
595 */
596 if (msg_scrolled)
597 {
598 clear_cmdline = TRUE;
599 if (msg_scrolled > Rows - 5) /* clearing is faster */
600 type = CLEAR;
601 else if (type != CLEAR)
602 {
603 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200604 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
605 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 type = CLEAR;
607 FOR_ALL_WINDOWS(wp)
608 {
609 if (W_WINROW(wp) < msg_scrolled)
610 {
611 if (W_WINROW(wp) + wp->w_height > msg_scrolled
612 && wp->w_redr_type < REDRAW_TOP
613 && wp->w_lines_valid > 0
614 && wp->w_topline == wp->w_lines[0].wl_lnum)
615 {
616 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
617 wp->w_redr_type = REDRAW_TOP;
618 }
619 else
620 {
621 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200622 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
623 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 }
626 }
627 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200628 if (!no_update)
629 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000630 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 }
632 msg_scrolled = 0;
633 need_wait_return = FALSE;
634 }
635
636 /* reset cmdline_row now (may have been changed temporarily) */
637 compute_cmdrow();
638
639 /* Check for changed highlighting */
640 if (need_highlight_changed)
641 highlight_changed();
642
643 if (type == CLEAR) /* first clear screen */
644 {
645 screenclear(); /* will reset clear_cmdline */
646 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200647 /* must_redraw may be set indirectly, avoid another redraw later */
648 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 }
650
651 if (clear_cmdline) /* going to clear cmdline (done below) */
652 check_for_delay(FALSE);
653
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000654#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200655 /* Force redraw when width of 'number' or 'relativenumber' column
656 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000657 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200658 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
659 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000660 curwin->w_redr_type = NOT_VALID;
661#endif
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 /*
664 * Only start redrawing if there is really something to do.
665 */
666 if (type == INVERTED)
667 update_curswant();
668 if (curwin->w_redr_type < type
669 && !((type == VALID
670 && curwin->w_lines[0].wl_valid
671#ifdef FEAT_DIFF
672 && curwin->w_topfill == curwin->w_old_topfill
673 && curwin->w_botfill == curwin->w_old_botfill
674#endif
675 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000677 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
679 && curwin->w_old_visual_mode == VIsual_mode
680 && (curwin->w_valid & VALID_VIRTCOL)
681 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 ))
683 curwin->w_redr_type = type;
684
Bram Moolenaar5a305422006-04-28 22:38:25 +0000685 /* Redraw the tab pages line if needed. */
686 if (redraw_tabline || type >= NOT_VALID)
687 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#ifdef FEAT_SYN_HL
690 /*
691 * Correct stored syntax highlighting info for changes in each displayed
692 * buffer. Each buffer must only be done once.
693 */
694 FOR_ALL_WINDOWS(wp)
695 {
696 if (wp->w_buffer->b_mod_set)
697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 win_T *wwp;
699
700 /* Check if we already did this buffer. */
701 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
702 if (wwp->w_buffer == wp->w_buffer)
703 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200704 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 syn_stack_apply_changes(wp->w_buffer);
706 }
707 }
708#endif
709
710 /*
711 * Go from top to bottom through the windows, redrawing the ones that need
712 * it.
713 */
714#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
715 did_one = FALSE;
716#endif
717#ifdef FEAT_SEARCH_EXTRA
718 search_hl.rm.regprog = NULL;
719#endif
720 FOR_ALL_WINDOWS(wp)
721 {
722 if (wp->w_redr_type != 0)
723 {
724 cursor_off();
725#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
726 if (!did_one)
727 {
728 did_one = TRUE;
729# ifdef FEAT_SEARCH_EXTRA
730 start_search_hl();
731# endif
732# ifdef FEAT_CLIPBOARD
733 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200734 if (clip_star.available && clip_isautosel_star())
735 clip_update_selection(&clip_star);
736 if (clip_plus.available && clip_isautosel_plus())
737 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738# endif
739#ifdef FEAT_GUI
740 /* Remove the cursor before starting to do anything, because
741 * scrolling may make it difficult to redraw the text under
742 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200743 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200744 {
745 gui_cursor_col = gui.cursor_col;
746 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200748 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750#endif
751 }
752#endif
753 win_update(wp);
754 }
755
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 /* redraw status line after the window to minimize cursor movement */
757 if (wp->w_redr_status)
758 {
759 cursor_off();
760 win_redr_status(wp);
761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 }
763#if defined(FEAT_SEARCH_EXTRA)
764 end_search_hl();
765#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100766#ifdef FEAT_INS_EXPAND
767 /* May need to redraw the popup menu. */
768 if (pum_visible())
769 pum_redraw();
770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 /* Reset b_mod_set flags. Going through all windows is probably faster
773 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200774 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
777 updating_screen = FALSE;
778#ifdef FEAT_GUI
779 gui_may_resize_shell();
780#endif
781
782 /* Clear or redraw the command line. Done last, because scrolling may
783 * mess up the command line. */
784 if (clear_cmdline || redraw_cmdline)
785 showmode();
786
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200787 if (no_update)
788 --no_win_do_lines_ins;
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200791 if (!did_intro)
792 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 did_intro = TRUE;
794
795#ifdef FEAT_GUI
796 /* Redraw the cursor and update the scrollbars when all screen updating is
797 * done. */
798 if (gui.in_use)
799 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200800 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200801 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100802 mch_disable_flush();
803 out_flush(); /* required before updating the cursor */
804 mch_enable_flush();
805
Bram Moolenaar144445d2016-07-08 21:41:54 +0200806 /* Put the GUI position where the cursor was, gui_update_cursor()
807 * uses that. */
808 gui.col = gui_cursor_col;
809 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200810# ifdef FEAT_MBYTE
811 gui.col = mb_fix_col(gui.col, gui.row);
812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100814 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200815 screen_cur_col = gui.col;
816 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200817 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100818 else
819 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 gui_update_scrollbars(FALSE);
821 }
822#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200823 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824}
825
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100826#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
827/*
828 * Prepare for updating one or more windows.
829 * Caller must check for "updating_screen" already set to avoid recursiveness.
830 */
831 static void
832update_prepare(void)
833{
834 cursor_off();
835 updating_screen = TRUE;
836#ifdef FEAT_GUI
837 /* Remove the cursor before starting to do anything, because scrolling may
838 * make it difficult to redraw the text under it. */
839 if (gui.in_use)
840 gui_undraw_cursor();
841#endif
842#ifdef FEAT_SEARCH_EXTRA
843 start_search_hl();
844#endif
845}
846
847/*
848 * Finish updating one or more windows.
849 */
850 static void
851update_finish(void)
852{
853 if (redraw_cmdline)
854 showmode();
855
856# ifdef FEAT_SEARCH_EXTRA
857 end_search_hl();
858# endif
859
860 updating_screen = FALSE;
861
862# ifdef FEAT_GUI
863 gui_may_resize_shell();
864
865 /* Redraw the cursor and update the scrollbars when all screen updating is
866 * done. */
867 if (gui.in_use)
868 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100869 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100870 gui_update_scrollbars(FALSE);
871 }
872# endif
873}
874#endif
875
Bram Moolenaar860cae12010-06-05 23:22:07 +0200876#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200877/*
878 * Return TRUE if the cursor line in window "wp" may be concealed, according
879 * to the 'concealcursor' option.
880 */
881 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100882conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200883{
884 int c;
885
886 if (*wp->w_p_cocu == NUL)
887 return FALSE;
888 if (get_real_state() & VISUAL)
889 c = 'v';
890 else if (State & INSERT)
891 c = 'i';
892 else if (State & NORMAL)
893 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200894 else if (State & CMDLINE)
895 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200896 else
897 return FALSE;
898 return vim_strchr(wp->w_p_cocu, c) != NULL;
899}
900
901/*
902 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
903 */
904 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100905conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200906{
907 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
908 {
909 need_cursor_line_redraw = TRUE;
910 /* Need to recompute cursor column, e.g., when starting Visual mode
911 * without concealing. */
912 curs_columns(TRUE);
913 }
914}
915
Bram Moolenaar860cae12010-06-05 23:22:07 +0200916 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100917update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200918{
919 int row;
920 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200921#ifdef SYN_TIME_LIMIT
922 proftime_T syntax_tm;
923#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200924
Bram Moolenaar908be432016-05-24 10:51:30 +0200925 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100926 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200927 return;
928
Bram Moolenaar860cae12010-06-05 23:22:07 +0200929 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200930 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200931 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200932#ifdef SYN_TIME_LIMIT
933 /* Set the time limit to 'redrawtime'. */
934 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200935 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200936#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100937 update_prepare();
938
Bram Moolenaar860cae12010-06-05 23:22:07 +0200939 row = 0;
940 for (j = 0; j < wp->w_lines_valid; ++j)
941 {
942 if (lnum == wp->w_lines[j].wl_lnum)
943 {
944 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200945# ifdef FEAT_SEARCH_EXTRA
946 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200947 start_search_hl();
948 prepare_search_hl(wp, lnum);
949# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200950 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951# if defined(FEAT_SEARCH_EXTRA)
952 end_search_hl();
953# endif
954 break;
955 }
956 row += wp->w_lines[j].wl_size;
957 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100958
959 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200960
961#ifdef SYN_TIME_LIMIT
962 syn_set_timeout(NULL);
963#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200964 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200965 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967#endif
968
969#if defined(FEAT_SIGNS) || defined(PROTO)
970 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100971update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 win_T *wp;
974 int doit = FALSE;
975
976# ifdef FEAT_FOLDING
977 win_foldinfo.fi_level = 0;
978# endif
979
980 /* update/delete a specific mark */
981 FOR_ALL_WINDOWS(wp)
982 {
983 if (buf != NULL && lnum > 0)
984 {
985 if (wp->w_buffer == buf && lnum >= wp->w_topline
986 && lnum < wp->w_botline)
987 {
988 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
989 wp->w_redraw_top = lnum;
990 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
991 wp->w_redraw_bot = lnum;
992 redraw_win_later(wp, VALID);
993 }
994 }
995 else
996 redraw_win_later(wp, VALID);
997 if (wp->w_redr_type != 0)
998 doit = TRUE;
999 }
1000
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001001 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001002 * happening (recursive call), messages on the screen or still starting up.
1003 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001004 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001005 || State == ASKMORE || State == HITRETURN
1006 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001007#ifdef FEAT_GUI
1008 || gui.starting
1009#endif
1010 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 return;
1012
1013 /* update all windows that need updating */
1014 update_prepare();
1015
Bram Moolenaar29323592016-07-24 22:04:11 +02001016 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {
1018 if (wp->w_redr_type != 0)
1019 win_update(wp);
1020 if (wp->w_redr_status)
1021 win_redr_status(wp);
1022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 update_finish();
1025}
1026#endif
1027
1028
1029#if defined(FEAT_GUI) || defined(PROTO)
1030/*
1031 * Update a single window, its status line and maybe the command line msg.
1032 * Used for the GUI scrollbar.
1033 */
1034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001035updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001037 /* return if already busy updating */
1038 if (updating_screen)
1039 return;
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 update_prepare();
1042
1043#ifdef FEAT_CLIPBOARD
1044 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001045 if (clip_star.available && clip_isautosel_star())
1046 clip_update_selection(&clip_star);
1047 if (clip_plus.available && clip_isautosel_plus())
1048 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001050
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001052
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001053 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001054 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001055 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (wp->w_redr_status
1058# ifdef FEAT_CMDL_INFO
1059 || p_ru
1060# endif
1061# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001062 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063# endif
1064 )
1065 win_redr_status(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
1067 update_finish();
1068}
1069#endif
1070
1071/*
1072 * Update a single window.
1073 *
1074 * This may cause the windows below it also to be redrawn (when clearing the
1075 * screen or scrolling lines).
1076 *
1077 * How the window is redrawn depends on wp->w_redr_type. Each type also
1078 * implies the one below it.
1079 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001080 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1082 * INVERTED redraw the changed part of the Visual area
1083 * INVERTED_ALL redraw the whole Visual area
1084 * VALID 1. scroll up/down to adjust for a changed w_topline
1085 * 2. update lines at the top when scrolled down
1086 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001087 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 * b_mod_top and b_mod_bot.
1089 * - if wp->w_redraw_top non-zero, redraw lines between
1090 * wp->w_redraw_top and wp->w_redr_bot.
1091 * - continue redrawing when syntax status is invalid.
1092 * 4. if scrolled up, update lines at the bottom.
1093 * This results in three areas that may need updating:
1094 * top: from first row to top_end (when scrolled down)
1095 * mid: from mid_start to mid_end (update inversion or changed text)
1096 * bot: from bot_start to last row (when scrolled up)
1097 */
1098 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001099win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100{
1101 buf_T *buf = wp->w_buffer;
1102 int type;
1103 int top_end = 0; /* Below last row of the top area that needs
1104 updating. 0 when no top area updating. */
1105 int mid_start = 999;/* first row of the mid area that needs
1106 updating. 999 when no mid area updating. */
1107 int mid_end = 0; /* Below last row of the mid area that needs
1108 updating. 0 when no mid area updating. */
1109 int bot_start = 999;/* first row of the bot area that needs
1110 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 int scrolled_down = FALSE; /* TRUE when scrolled down when
1112 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001114 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 int top_to_mod = FALSE; /* redraw above mod_top */
1116#endif
1117
1118 int row; /* current window row to display */
1119 linenr_T lnum; /* current buffer lnum to display */
1120 int idx; /* current index in w_lines[] */
1121 int srow; /* starting row of the current line */
1122
1123 int eof = FALSE; /* if TRUE, we hit the end of the file */
1124 int didline = FALSE; /* if TRUE, we finished the last line */
1125 int i;
1126 long j;
1127 static int recursive = FALSE; /* being called recursively */
1128 int old_botline = wp->w_botline;
1129#ifdef FEAT_FOLDING
1130 long fold_count;
1131#endif
1132#ifdef FEAT_SYN_HL
1133 /* remember what happened to the previous line, to know if
1134 * check_visual_highlight() can be used */
1135#define DID_NONE 1 /* didn't update a line */
1136#define DID_LINE 2 /* updated a normal line */
1137#define DID_FOLD 3 /* updated a folded line */
1138 int did_update = DID_NONE;
1139 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1140#endif
1141 linenr_T mod_top = 0;
1142 linenr_T mod_bot = 0;
1143#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1144 int save_got_int;
1145#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001146#ifdef SYN_TIME_LIMIT
1147 proftime_T syntax_tm;
1148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
1150 type = wp->w_redr_type;
1151
1152 if (type == NOT_VALID)
1153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 wp->w_lines_valid = 0;
1156 }
1157
1158 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001159 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
1161 wp->w_redr_type = 0;
1162 return;
1163 }
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 /* Window is zero-width: Only need to draw the separator. */
1166 if (wp->w_width == 0)
1167 {
1168 /* draw the vertical separator right of this window */
1169 draw_vsep_win(wp, 0);
1170 wp->w_redr_type = 0;
1171 return;
1172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001174#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001175 /* If this window contains a terminal, redraw works completely differently.
1176 */
1177 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001178 {
Bram Moolenaar181ca992018-02-13 21:19:21 +01001179# ifdef FEAT_MENU
1180 /* Draw the window toolbar, if there is one. */
1181 if (winbar_height(wp) > 0)
1182 redraw_win_toolbar(wp);
1183# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001184 wp->w_redr_type = 0;
1185 return;
1186 }
1187#endif
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001190 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191#endif
1192
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001193#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001194 /* Force redraw when width of 'number' or 'relativenumber' column
1195 * changes. */
1196 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001197 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001198 {
1199 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001200 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001201 }
1202 else
1203#endif
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1206 {
1207 /*
1208 * When there are both inserted/deleted lines and specific lines to be
1209 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1210 * everything (only happens when redrawing is off for while).
1211 */
1212 type = NOT_VALID;
1213 }
1214 else
1215 {
1216 /*
1217 * Set mod_top to the first line that needs displaying because of
1218 * changes. Set mod_bot to the first line after the changes.
1219 */
1220 mod_top = wp->w_redraw_top;
1221 if (wp->w_redraw_bot != 0)
1222 mod_bot = wp->w_redraw_bot + 1;
1223 else
1224 mod_bot = 0;
1225 wp->w_redraw_top = 0; /* reset for next time */
1226 wp->w_redraw_bot = 0;
1227 if (buf->b_mod_set)
1228 {
1229 if (mod_top == 0 || mod_top > buf->b_mod_top)
1230 {
1231 mod_top = buf->b_mod_top;
1232#ifdef FEAT_SYN_HL
1233 /* Need to redraw lines above the change that may be included
1234 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001235 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001237 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 if (mod_top < 1)
1239 mod_top = 1;
1240 }
1241#endif
1242 }
1243 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1244 mod_bot = buf->b_mod_bot;
1245
1246#ifdef FEAT_SEARCH_EXTRA
1247 /* When 'hlsearch' is on and using a multi-line search pattern, a
1248 * change in one line may make the Search highlighting in a
1249 * previous line invalid. Simple solution: redraw all visible
1250 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001251 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001253 if (search_hl.rm.regprog != NULL
1254 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001256 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001257 {
1258 cur = wp->w_match_head;
1259 while (cur != NULL)
1260 {
1261 if (cur->match.regprog != NULL
1262 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001263 {
1264 top_to_mod = TRUE;
1265 break;
1266 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001267 cur = cur->next;
1268 }
1269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270#endif
1271 }
1272#ifdef FEAT_FOLDING
1273 if (mod_top != 0 && hasAnyFolding(wp))
1274 {
1275 linenr_T lnumt, lnumb;
1276
1277 /*
1278 * A change in a line can cause lines above it to become folded or
1279 * unfolded. Find the top most buffer line that may be affected.
1280 * If the line was previously folded and displayed, get the first
1281 * line of that fold. If the line is folded now, get the first
1282 * folded line. Use the minimum of these two.
1283 */
1284
1285 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1286 * the line below it. If there is no valid entry, use w_topline.
1287 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1288 * to this line. If there is no valid entry, use MAXLNUM. */
1289 lnumt = wp->w_topline;
1290 lnumb = MAXLNUM;
1291 for (i = 0; i < wp->w_lines_valid; ++i)
1292 if (wp->w_lines[i].wl_valid)
1293 {
1294 if (wp->w_lines[i].wl_lastlnum < mod_top)
1295 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1296 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1297 {
1298 lnumb = wp->w_lines[i].wl_lnum;
1299 /* When there is a fold column it might need updating
1300 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001301 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 ++lnumb;
1303 }
1304 }
1305
1306 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1307 if (mod_top > lnumt)
1308 mod_top = lnumt;
1309
1310 /* Now do the same for the bottom line (one above mod_bot). */
1311 --mod_bot;
1312 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1313 ++mod_bot;
1314 if (mod_bot < lnumb)
1315 mod_bot = lnumb;
1316 }
1317#endif
1318
1319 /* When a change starts above w_topline and the end is below
1320 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001321 * If the end of the change is above w_topline: do like no change was
1322 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (mod_top != 0 && mod_top < wp->w_topline)
1324 {
1325 if (mod_bot > wp->w_topline)
1326 mod_top = wp->w_topline;
1327#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001328 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 top_end = 1;
1330#endif
1331 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001332
1333 /* When line numbers are displayed need to redraw all lines below
1334 * inserted/deleted lines. */
1335 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1336 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338
1339 /*
1340 * When only displaying the lines at the top, set top_end. Used when
1341 * window has scrolled down for msg_scrolled.
1342 */
1343 if (type == REDRAW_TOP)
1344 {
1345 j = 0;
1346 for (i = 0; i < wp->w_lines_valid; ++i)
1347 {
1348 j += wp->w_lines[i].wl_size;
1349 if (j >= wp->w_upd_rows)
1350 {
1351 top_end = j;
1352 break;
1353 }
1354 }
1355 if (top_end == 0)
1356 /* not found (cannot happen?): redraw everything */
1357 type = NOT_VALID;
1358 else
1359 /* top area defined, the rest is VALID */
1360 type = VALID;
1361 }
1362
Bram Moolenaar367329b2007-08-30 11:53:22 +00001363 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001364 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1365 * non-zero and thus not FALSE) will indicate that screenclear() was not
1366 * called. */
1367 if (screen_cleared)
1368 screen_cleared = MAYBE;
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /*
1371 * If there are no changes on the screen that require a complete redraw,
1372 * handle three cases:
1373 * 1: we are off the top of the screen by a few lines: scroll down
1374 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1375 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1376 * w_lines[] that needs updating.
1377 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001378 if ((type == VALID || type == SOME_VALID
1379 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380#ifdef FEAT_DIFF
1381 && !wp->w_botfill && !wp->w_old_botfill
1382#endif
1383 )
1384 {
1385 if (mod_top != 0 && wp->w_topline == mod_top)
1386 {
1387 /*
1388 * w_topline is the first changed line, the scrolling will be done
1389 * further down.
1390 */
1391 }
1392 else if (wp->w_lines[0].wl_valid
1393 && (wp->w_topline < wp->w_lines[0].wl_lnum
1394#ifdef FEAT_DIFF
1395 || (wp->w_topline == wp->w_lines[0].wl_lnum
1396 && wp->w_topfill > wp->w_old_topfill)
1397#endif
1398 ))
1399 {
1400 /*
1401 * New topline is above old topline: May scroll down.
1402 */
1403#ifdef FEAT_FOLDING
1404 if (hasAnyFolding(wp))
1405 {
1406 linenr_T ln;
1407
1408 /* count the number of lines we are off, counting a sequence
1409 * of folded lines as one */
1410 j = 0;
1411 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1412 {
1413 ++j;
1414 if (j >= wp->w_height - 2)
1415 break;
1416 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1417 }
1418 }
1419 else
1420#endif
1421 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1422 if (j < wp->w_height - 2) /* not too far off */
1423 {
1424 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1425#ifdef FEAT_DIFF
1426 /* insert extra lines for previously invisible filler lines */
1427 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1428 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1429 - wp->w_old_topfill;
1430#endif
1431 if (i < wp->w_height - 2) /* less than a screen off */
1432 {
1433 /*
1434 * Try to insert the correct number of lines.
1435 * If not the last window, delete the lines at the bottom.
1436 * win_ins_lines may fail when the terminal can't do it.
1437 */
1438 if (i > 0)
1439 check_for_delay(FALSE);
1440 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1441 {
1442 if (wp->w_lines_valid != 0)
1443 {
1444 /* Need to update rows that are new, stop at the
1445 * first one that scrolled down. */
1446 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
1449 /* Move the entries that were scrolled, disable
1450 * the entries for the lines to be redrawn. */
1451 if ((wp->w_lines_valid += j) > wp->w_height)
1452 wp->w_lines_valid = wp->w_height;
1453 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1454 wp->w_lines[idx] = wp->w_lines[idx - j];
1455 while (idx >= 0)
1456 wp->w_lines[idx--].wl_valid = FALSE;
1457 }
1458 }
1459 else
1460 mid_start = 0; /* redraw all lines */
1461 }
1462 else
1463 mid_start = 0; /* redraw all lines */
1464 }
1465 else
1466 mid_start = 0; /* redraw all lines */
1467 }
1468 else
1469 {
1470 /*
1471 * New topline is at or below old topline: May scroll up.
1472 * When topline didn't change, find first entry in w_lines[] that
1473 * needs updating.
1474 */
1475
1476 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1477 j = -1;
1478 row = 0;
1479 for (i = 0; i < wp->w_lines_valid; i++)
1480 {
1481 if (wp->w_lines[i].wl_valid
1482 && wp->w_lines[i].wl_lnum == wp->w_topline)
1483 {
1484 j = i;
1485 break;
1486 }
1487 row += wp->w_lines[i].wl_size;
1488 }
1489 if (j == -1)
1490 {
1491 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1492 * lines */
1493 mid_start = 0;
1494 }
1495 else
1496 {
1497 /*
1498 * Try to delete the correct number of lines.
1499 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1500 */
1501#ifdef FEAT_DIFF
1502 /* If the topline didn't change, delete old filler lines,
1503 * otherwise delete filler lines of the new topline... */
1504 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1505 row += wp->w_old_topfill;
1506 else
1507 row += diff_check_fill(wp, wp->w_topline);
1508 /* ... but don't delete new filler lines. */
1509 row -= wp->w_topfill;
1510#endif
1511 if (row > 0)
1512 {
1513 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001514 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1515 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 bot_start = wp->w_height - row;
1517 else
1518 mid_start = 0; /* redraw all lines */
1519 }
1520 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1521 {
1522 /*
1523 * Skip the lines (below the deleted lines) that are still
1524 * valid and don't need redrawing. Copy their info
1525 * upwards, to compensate for the deleted lines. Set
1526 * bot_start to the first row that needs redrawing.
1527 */
1528 bot_start = 0;
1529 idx = 0;
1530 for (;;)
1531 {
1532 wp->w_lines[idx] = wp->w_lines[j];
1533 /* stop at line that didn't fit, unless it is still
1534 * valid (no lines deleted) */
1535 if (row > 0 && bot_start + row
1536 + (int)wp->w_lines[j].wl_size > wp->w_height)
1537 {
1538 wp->w_lines_valid = idx + 1;
1539 break;
1540 }
1541 bot_start += wp->w_lines[idx++].wl_size;
1542
1543 /* stop at the last valid entry in w_lines[].wl_size */
1544 if (++j >= wp->w_lines_valid)
1545 {
1546 wp->w_lines_valid = idx;
1547 break;
1548 }
1549 }
1550#ifdef FEAT_DIFF
1551 /* Correct the first entry for filler lines at the top
1552 * when it won't get updated below. */
1553 if (wp->w_p_diff && bot_start > 0)
1554 wp->w_lines[0].wl_size =
1555 plines_win_nofill(wp, wp->w_topline, TRUE)
1556 + wp->w_topfill;
1557#endif
1558 }
1559 }
1560 }
1561
1562 /* When starting redraw in the first line, redraw all lines. When
1563 * there is only one window it's probably faster to clear the screen
1564 * first. */
1565 if (mid_start == 0)
1566 {
1567 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001568 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001569 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001570 /* Clear the screen when it was not done by win_del_lines() or
1571 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1572 * then. */
1573 if (screen_cleared != TRUE)
1574 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001575 /* The screen was cleared, redraw the tab pages line. */
1576 if (redraw_tabline)
1577 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001580
1581 /* When win_del_lines() or win_ins_lines() caused the screen to be
1582 * cleared (only happens for the first window) or when screenclear()
1583 * was called directly above, "must_redraw" will have been set to
1584 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1585 if (screen_cleared == TRUE)
1586 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588 else
1589 {
1590 /* Not VALID or INVERTED: redraw all lines. */
1591 mid_start = 0;
1592 mid_end = wp->w_height;
1593 }
1594
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001595 if (type == SOME_VALID)
1596 {
1597 /* SOME_VALID: redraw all lines. */
1598 mid_start = 0;
1599 mid_end = wp->w_height;
1600 type = NOT_VALID;
1601 }
1602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 /* check if we are updating or removing the inverted part */
1604 if ((VIsual_active && buf == curwin->w_buffer)
1605 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1606 {
1607 linenr_T from, to;
1608
1609 if (VIsual_active)
1610 {
1611 if (VIsual_active
1612 && (VIsual_mode != wp->w_old_visual_mode
1613 || type == INVERTED_ALL))
1614 {
1615 /*
1616 * If the type of Visual selection changed, redraw the whole
1617 * selection. Also when the ownership of the X selection is
1618 * gained or lost.
1619 */
1620 if (curwin->w_cursor.lnum < VIsual.lnum)
1621 {
1622 from = curwin->w_cursor.lnum;
1623 to = VIsual.lnum;
1624 }
1625 else
1626 {
1627 from = VIsual.lnum;
1628 to = curwin->w_cursor.lnum;
1629 }
1630 /* redraw more when the cursor moved as well */
1631 if (wp->w_old_cursor_lnum < from)
1632 from = wp->w_old_cursor_lnum;
1633 if (wp->w_old_cursor_lnum > to)
1634 to = wp->w_old_cursor_lnum;
1635 if (wp->w_old_visual_lnum < from)
1636 from = wp->w_old_visual_lnum;
1637 if (wp->w_old_visual_lnum > to)
1638 to = wp->w_old_visual_lnum;
1639 }
1640 else
1641 {
1642 /*
1643 * Find the line numbers that need to be updated: The lines
1644 * between the old cursor position and the current cursor
1645 * position. Also check if the Visual position changed.
1646 */
1647 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1648 {
1649 from = curwin->w_cursor.lnum;
1650 to = wp->w_old_cursor_lnum;
1651 }
1652 else
1653 {
1654 from = wp->w_old_cursor_lnum;
1655 to = curwin->w_cursor.lnum;
1656 if (from == 0) /* Visual mode just started */
1657 from = to;
1658 }
1659
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001660 if (VIsual.lnum != wp->w_old_visual_lnum
1661 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 {
1663 if (wp->w_old_visual_lnum < from
1664 && wp->w_old_visual_lnum != 0)
1665 from = wp->w_old_visual_lnum;
1666 if (wp->w_old_visual_lnum > to)
1667 to = wp->w_old_visual_lnum;
1668 if (VIsual.lnum < from)
1669 from = VIsual.lnum;
1670 if (VIsual.lnum > to)
1671 to = VIsual.lnum;
1672 }
1673 }
1674
1675 /*
1676 * If in block mode and changed column or curwin->w_curswant:
1677 * update all lines.
1678 * First compute the actual start and end column.
1679 */
1680 if (VIsual_mode == Ctrl_V)
1681 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001682 colnr_T fromc, toc;
1683#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1684 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaar404406a2014-10-09 13:24:43 +02001686 if (curwin->w_p_lbr)
1687 ve_flags = VE_ALL;
1688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001690#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1691 ve_flags = save_ve_flags;
1692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 ++toc;
1694 if (curwin->w_curswant == MAXCOL)
1695 toc = MAXCOL;
1696
1697 if (fromc != wp->w_old_cursor_fcol
1698 || toc != wp->w_old_cursor_lcol)
1699 {
1700 if (from > VIsual.lnum)
1701 from = VIsual.lnum;
1702 if (to < VIsual.lnum)
1703 to = VIsual.lnum;
1704 }
1705 wp->w_old_cursor_fcol = fromc;
1706 wp->w_old_cursor_lcol = toc;
1707 }
1708 }
1709 else
1710 {
1711 /* Use the line numbers of the old Visual area. */
1712 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1713 {
1714 from = wp->w_old_cursor_lnum;
1715 to = wp->w_old_visual_lnum;
1716 }
1717 else
1718 {
1719 from = wp->w_old_visual_lnum;
1720 to = wp->w_old_cursor_lnum;
1721 }
1722 }
1723
1724 /*
1725 * There is no need to update lines above the top of the window.
1726 */
1727 if (from < wp->w_topline)
1728 from = wp->w_topline;
1729
1730 /*
1731 * If we know the value of w_botline, use it to restrict the update to
1732 * the lines that are visible in the window.
1733 */
1734 if (wp->w_valid & VALID_BOTLINE)
1735 {
1736 if (from >= wp->w_botline)
1737 from = wp->w_botline - 1;
1738 if (to >= wp->w_botline)
1739 to = wp->w_botline - 1;
1740 }
1741
1742 /*
1743 * Find the minimal part to be updated.
1744 * Watch out for scrolling that made entries in w_lines[] invalid.
1745 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1746 * top_end; need to redraw from top_end to the "to" line.
1747 * A middle mouse click with a Visual selection may change the text
1748 * above the Visual area and reset wl_valid, do count these for
1749 * mid_end (in srow).
1750 */
1751 if (mid_start > 0)
1752 {
1753 lnum = wp->w_topline;
1754 idx = 0;
1755 srow = 0;
1756 if (scrolled_down)
1757 mid_start = top_end;
1758 else
1759 mid_start = 0;
1760 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1761 {
1762 if (wp->w_lines[idx].wl_valid)
1763 mid_start += wp->w_lines[idx].wl_size;
1764 else if (!scrolled_down)
1765 srow += wp->w_lines[idx].wl_size;
1766 ++idx;
1767# ifdef FEAT_FOLDING
1768 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1769 lnum = wp->w_lines[idx].wl_lnum;
1770 else
1771# endif
1772 ++lnum;
1773 }
1774 srow += mid_start;
1775 mid_end = wp->w_height;
1776 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1777 {
1778 if (wp->w_lines[idx].wl_valid
1779 && wp->w_lines[idx].wl_lnum >= to + 1)
1780 {
1781 /* Only update until first row of this line */
1782 mid_end = srow;
1783 break;
1784 }
1785 srow += wp->w_lines[idx].wl_size;
1786 }
1787 }
1788 }
1789
1790 if (VIsual_active && buf == curwin->w_buffer)
1791 {
1792 wp->w_old_visual_mode = VIsual_mode;
1793 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1794 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001795 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 wp->w_old_curswant = curwin->w_curswant;
1797 }
1798 else
1799 {
1800 wp->w_old_visual_mode = 0;
1801 wp->w_old_cursor_lnum = 0;
1802 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001803 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
1806#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1807 /* reset got_int, otherwise regexp won't work */
1808 save_got_int = got_int;
1809 got_int = 0;
1810#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001811#ifdef SYN_TIME_LIMIT
1812 /* Set the time limit to 'redrawtime'. */
1813 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001814 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816#ifdef FEAT_FOLDING
1817 win_foldinfo.fi_level = 0;
1818#endif
1819
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001820#ifdef FEAT_MENU
1821 /*
1822 * Draw the window toolbar, if there is one.
1823 * TODO: only when needed.
1824 */
1825 if (winbar_height(wp) > 0)
1826 redraw_win_toolbar(wp);
1827#endif
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 /*
1830 * Update all the window rows.
1831 */
1832 idx = 0; /* first entry in w_lines[].wl_size */
1833 row = 0;
1834 srow = 0;
1835 lnum = wp->w_topline; /* first line shown in window */
1836 for (;;)
1837 {
1838 /* stop updating when reached the end of the window (check for _past_
1839 * the end of the window is at the end of the loop) */
1840 if (row == wp->w_height)
1841 {
1842 didline = TRUE;
1843 break;
1844 }
1845
1846 /* stop updating when hit the end of the file */
1847 if (lnum > buf->b_ml.ml_line_count)
1848 {
1849 eof = TRUE;
1850 break;
1851 }
1852
1853 /* Remember the starting row of the line that is going to be dealt
1854 * with. It is used further down when the line doesn't fit. */
1855 srow = row;
1856
1857 /*
1858 * Update a line when it is in an area that needs updating, when it
1859 * has changes or w_lines[idx] is invalid.
1860 * bot_start may be halfway a wrapped line after using
1861 * win_del_lines(), check if the current line includes it.
1862 * When syntax folding is being used, the saved syntax states will
1863 * already have been updated, we can't see where the syntax state is
1864 * the same again, just update until the end of the window.
1865 */
1866 if (row < top_end
1867 || (row >= mid_start && row < mid_end)
1868#ifdef FEAT_SEARCH_EXTRA
1869 || top_to_mod
1870#endif
1871 || idx >= wp->w_lines_valid
1872 || (row + wp->w_lines[idx].wl_size > bot_start)
1873 || (mod_top != 0
1874 && (lnum == mod_top
1875 || (lnum >= mod_top
1876 && (lnum < mod_bot
1877#ifdef FEAT_SYN_HL
1878 || did_update == DID_FOLD
1879 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001880 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 && (
1882# ifdef FEAT_FOLDING
1883 (foldmethodIsSyntax(wp)
1884 && hasAnyFolding(wp)) ||
1885# endif
1886 syntax_check_changed(lnum)))
1887#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001888#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001889 /* match in fixed position might need redraw
1890 * if lines were inserted or deleted */
1891 || (wp->w_match_head != NULL
1892 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 )))))
1895 {
1896#ifdef FEAT_SEARCH_EXTRA
1897 if (lnum == mod_top)
1898 top_to_mod = FALSE;
1899#endif
1900
1901 /*
1902 * When at start of changed lines: May scroll following lines
1903 * up or down to minimize redrawing.
1904 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001905 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 */
1907 if (lnum == mod_top
1908 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001909 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 {
1911 int old_rows = 0;
1912 int new_rows = 0;
1913 int xtra_rows;
1914 linenr_T l;
1915
1916 /* Count the old number of window rows, using w_lines[], which
1917 * should still contain the sizes for the lines as they are
1918 * currently displayed. */
1919 for (i = idx; i < wp->w_lines_valid; ++i)
1920 {
1921 /* Only valid lines have a meaningful wl_lnum. Invalid
1922 * lines are part of the changed area. */
1923 if (wp->w_lines[i].wl_valid
1924 && wp->w_lines[i].wl_lnum == mod_bot)
1925 break;
1926 old_rows += wp->w_lines[i].wl_size;
1927#ifdef FEAT_FOLDING
1928 if (wp->w_lines[i].wl_valid
1929 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1930 {
1931 /* Must have found the last valid entry above mod_bot.
1932 * Add following invalid entries. */
1933 ++i;
1934 while (i < wp->w_lines_valid
1935 && !wp->w_lines[i].wl_valid)
1936 old_rows += wp->w_lines[i++].wl_size;
1937 break;
1938 }
1939#endif
1940 }
1941
1942 if (i >= wp->w_lines_valid)
1943 {
1944 /* We can't find a valid line below the changed lines,
1945 * need to redraw until the end of the window.
1946 * Inserting/deleting lines has no use. */
1947 bot_start = 0;
1948 }
1949 else
1950 {
1951 /* Able to count old number of rows: Count new window
1952 * rows, and may insert/delete lines */
1953 j = idx;
1954 for (l = lnum; l < mod_bot; ++l)
1955 {
1956#ifdef FEAT_FOLDING
1957 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1958 ++new_rows;
1959 else
1960#endif
1961#ifdef FEAT_DIFF
1962 if (l == wp->w_topline)
1963 new_rows += plines_win_nofill(wp, l, TRUE)
1964 + wp->w_topfill;
1965 else
1966#endif
1967 new_rows += plines_win(wp, l, TRUE);
1968 ++j;
1969 if (new_rows > wp->w_height - row - 2)
1970 {
1971 /* it's getting too much, must redraw the rest */
1972 new_rows = 9999;
1973 break;
1974 }
1975 }
1976 xtra_rows = new_rows - old_rows;
1977 if (xtra_rows < 0)
1978 {
1979 /* May scroll text up. If there is not enough
1980 * remaining text or scrolling fails, must redraw the
1981 * rest. If scrolling works, must redraw the text
1982 * below the scrolled text. */
1983 if (row - xtra_rows >= wp->w_height - 2)
1984 mod_bot = MAXLNUM;
1985 else
1986 {
1987 check_for_delay(FALSE);
1988 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001989 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 mod_bot = MAXLNUM;
1991 else
1992 bot_start = wp->w_height + xtra_rows;
1993 }
1994 }
1995 else if (xtra_rows > 0)
1996 {
1997 /* May scroll text down. If there is not enough
1998 * remaining text of scrolling fails, must redraw the
1999 * rest. */
2000 if (row + xtra_rows >= wp->w_height - 2)
2001 mod_bot = MAXLNUM;
2002 else
2003 {
2004 check_for_delay(FALSE);
2005 if (win_ins_lines(wp, row + old_rows,
2006 xtra_rows, FALSE, FALSE) == FAIL)
2007 mod_bot = MAXLNUM;
2008 else if (top_end > row + old_rows)
2009 /* Scrolled the part at the top that requires
2010 * updating down. */
2011 top_end += xtra_rows;
2012 }
2013 }
2014
2015 /* When not updating the rest, may need to move w_lines[]
2016 * entries. */
2017 if (mod_bot != MAXLNUM && i != j)
2018 {
2019 if (j < i)
2020 {
2021 int x = row + new_rows;
2022
2023 /* move entries in w_lines[] upwards */
2024 for (;;)
2025 {
2026 /* stop at last valid entry in w_lines[] */
2027 if (i >= wp->w_lines_valid)
2028 {
2029 wp->w_lines_valid = j;
2030 break;
2031 }
2032 wp->w_lines[j] = wp->w_lines[i];
2033 /* stop at a line that won't fit */
2034 if (x + (int)wp->w_lines[j].wl_size
2035 > wp->w_height)
2036 {
2037 wp->w_lines_valid = j + 1;
2038 break;
2039 }
2040 x += wp->w_lines[j++].wl_size;
2041 ++i;
2042 }
2043 if (bot_start > x)
2044 bot_start = x;
2045 }
2046 else /* j > i */
2047 {
2048 /* move entries in w_lines[] downwards */
2049 j -= i;
2050 wp->w_lines_valid += j;
2051 if (wp->w_lines_valid > wp->w_height)
2052 wp->w_lines_valid = wp->w_height;
2053 for (i = wp->w_lines_valid; i - j >= idx; --i)
2054 wp->w_lines[i] = wp->w_lines[i - j];
2055
2056 /* The w_lines[] entries for inserted lines are
2057 * now invalid, but wl_size may be used above.
2058 * Reset to zero. */
2059 while (i >= idx)
2060 {
2061 wp->w_lines[i].wl_size = 0;
2062 wp->w_lines[i--].wl_valid = FALSE;
2063 }
2064 }
2065 }
2066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068
2069#ifdef FEAT_FOLDING
2070 /*
2071 * When lines are folded, display one line for all of them.
2072 * Otherwise, display normally (can be several display lines when
2073 * 'wrap' is on).
2074 */
2075 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2076 if (fold_count != 0)
2077 {
2078 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2079 ++row;
2080 --fold_count;
2081 wp->w_lines[idx].wl_folded = TRUE;
2082 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2083# ifdef FEAT_SYN_HL
2084 did_update = DID_FOLD;
2085# endif
2086 }
2087 else
2088#endif
2089 if (idx < wp->w_lines_valid
2090 && wp->w_lines[idx].wl_valid
2091 && wp->w_lines[idx].wl_lnum == lnum
2092 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002093 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 && srow + wp->w_lines[idx].wl_size > wp->w_height
2095#ifdef FEAT_DIFF
2096 && diff_check_fill(wp, lnum) == 0
2097#endif
2098 )
2099 {
2100 /* This line is not going to fit. Don't draw anything here,
2101 * will draw "@ " lines below. */
2102 row = wp->w_height + 1;
2103 }
2104 else
2105 {
2106#ifdef FEAT_SEARCH_EXTRA
2107 prepare_search_hl(wp, lnum);
2108#endif
2109#ifdef FEAT_SYN_HL
2110 /* Let the syntax stuff know we skipped a few lines. */
2111 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002112 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 syntax_end_parsing(syntax_last_parsed + 1);
2114#endif
2115
2116 /*
2117 * Display one line.
2118 */
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002119 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
2121#ifdef FEAT_FOLDING
2122 wp->w_lines[idx].wl_folded = FALSE;
2123 wp->w_lines[idx].wl_lastlnum = lnum;
2124#endif
2125#ifdef FEAT_SYN_HL
2126 did_update = DID_LINE;
2127 syntax_last_parsed = lnum;
2128#endif
2129 }
2130
2131 wp->w_lines[idx].wl_lnum = lnum;
2132 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002133
2134 /* Past end of the window or end of the screen. Note that after
2135 * resizing wp->w_height may be end up too big. That's a problem
2136 * elsewhere, but prevent a crash here. */
2137 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
2139 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002140 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2142 ++idx;
2143 break;
2144 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002145 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 wp->w_lines[idx].wl_size = row - srow;
2147 ++idx;
2148#ifdef FEAT_FOLDING
2149 lnum += fold_count + 1;
2150#else
2151 ++lnum;
2152#endif
2153 }
2154 else
2155 {
2156 /* This line does not need updating, advance to the next one */
2157 row += wp->w_lines[idx++].wl_size;
2158 if (row > wp->w_height) /* past end of screen */
2159 break;
2160#ifdef FEAT_FOLDING
2161 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2162#else
2163 ++lnum;
2164#endif
2165#ifdef FEAT_SYN_HL
2166 did_update = DID_NONE;
2167#endif
2168 }
2169
2170 if (lnum > buf->b_ml.ml_line_count)
2171 {
2172 eof = TRUE;
2173 break;
2174 }
2175 }
2176 /*
2177 * End of loop over all window lines.
2178 */
2179
2180
2181 if (idx > wp->w_lines_valid)
2182 wp->w_lines_valid = idx;
2183
2184#ifdef FEAT_SYN_HL
2185 /*
2186 * Let the syntax stuff know we stop parsing here.
2187 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002188 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 syntax_end_parsing(syntax_last_parsed + 1);
2190#endif
2191
2192 /*
2193 * If we didn't hit the end of the file, and we didn't finish the last
2194 * line we were working on, then the line didn't fit.
2195 */
2196 wp->w_empty_rows = 0;
2197#ifdef FEAT_DIFF
2198 wp->w_filler_rows = 0;
2199#endif
2200 if (!eof && !didline)
2201 {
2202 if (lnum == wp->w_topline)
2203 {
2204 /*
2205 * Single line that does not fit!
2206 * Don't overwrite it, it can be edited.
2207 */
2208 wp->w_botline = lnum + 1;
2209 }
2210#ifdef FEAT_DIFF
2211 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2212 {
2213 /* Window ends in filler lines. */
2214 wp->w_botline = lnum;
2215 wp->w_filler_rows = wp->w_height - srow;
2216 }
2217#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002218 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2219 {
2220 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2221
2222 /*
2223 * Last line isn't finished: Display "@@@" in the last screen line.
2224 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002225 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002226 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002227 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002228 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002229 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002230 set_empty_rows(wp, srow);
2231 wp->w_botline = lnum;
2232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2234 {
2235 /*
2236 * Last line isn't finished: Display "@@@" at the end.
2237 */
2238 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2239 W_WINROW(wp) + wp->w_height,
2240 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002241 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 set_empty_rows(wp, srow);
2243 wp->w_botline = lnum;
2244 }
2245 else
2246 {
2247 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2248 wp->w_botline = lnum;
2249 }
2250 }
2251 else
2252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 if (eof) /* we hit the end of the file */
2255 {
2256 wp->w_botline = buf->b_ml.ml_line_count + 1;
2257#ifdef FEAT_DIFF
2258 j = diff_check_fill(wp, wp->w_botline);
2259 if (j > 0 && !wp->w_botfill)
2260 {
2261 /*
2262 * Display filler lines at the end of the file
2263 */
2264 if (char2cells(fill_diff) > 1)
2265 i = '-';
2266 else
2267 i = fill_diff;
2268 if (row + j > wp->w_height)
2269 j = wp->w_height - row;
2270 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2271 row += j;
2272 }
2273#endif
2274 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002275 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 wp->w_botline = lnum;
2277
2278 /* make sure the rest of the screen is blank */
2279 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002280 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
2282
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002283#ifdef SYN_TIME_LIMIT
2284 syn_set_timeout(NULL);
2285#endif
2286
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 /* Reset the type of redrawing required, the window has been updated. */
2288 wp->w_redr_type = 0;
2289#ifdef FEAT_DIFF
2290 wp->w_old_topfill = wp->w_topfill;
2291 wp->w_old_botfill = wp->w_botfill;
2292#endif
2293
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002294 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 /*
2297 * There is a trick with w_botline. If we invalidate it on each
2298 * change that might modify it, this will cause a lot of expensive
2299 * calls to plines() in update_topline() each time. Therefore the
2300 * value of w_botline is often approximated, and this value is used to
2301 * compute the value of w_topline. If the value of w_botline was
2302 * wrong, check that the value of w_topline is correct (cursor is on
2303 * the visible part of the text). If it's not, we need to redraw
2304 * again. Mostly this just means scrolling up a few lines, so it
2305 * doesn't look too bad. Only do this for the current window (where
2306 * changes are relevant).
2307 */
2308 wp->w_valid |= VALID_BOTLINE;
2309 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2310 {
2311 recursive = TRUE;
2312 curwin->w_valid &= ~VALID_TOPLINE;
2313 update_topline(); /* may invalidate w_botline again */
2314 if (must_redraw != 0)
2315 {
2316 /* Don't update for changes in buffer again. */
2317 i = curbuf->b_mod_set;
2318 curbuf->b_mod_set = FALSE;
2319 win_update(curwin);
2320 must_redraw = 0;
2321 curbuf->b_mod_set = i;
2322 }
2323 recursive = FALSE;
2324 }
2325 }
2326
2327#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2328 /* restore got_int, unless CTRL-C was hit while redrawing */
2329 if (!got_int)
2330 got_int = save_got_int;
2331#endif
2332}
2333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334/*
2335 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2336 * as the filler character.
2337 */
2338 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002339win_draw_end(
2340 win_T *wp,
2341 int c1,
2342 int c2,
2343 int row,
2344 int endrow,
2345 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346{
2347#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2348 int n = 0;
2349# define FDC_OFF n
2350#else
2351# define FDC_OFF 0
2352#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002353#ifdef FEAT_FOLDING
2354 int fdc = compute_foldcolumn(wp, 0);
2355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
2357#ifdef FEAT_RIGHTLEFT
2358 if (wp->w_p_rl)
2359 {
2360 /* No check for cmdline window: should never be right-left. */
2361# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002362 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
2364 if (n > 0)
2365 {
2366 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002367 if (n > wp->w_width)
2368 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2370 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002371 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373# endif
2374# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002375 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
2377 int nn = n + 2;
2378
2379 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002380 if (nn > wp->w_width)
2381 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2383 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002384 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 n = nn;
2386 }
2387# endif
2388 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002389 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002390 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2392 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002393 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395 else
2396#endif
2397 {
2398#ifdef FEAT_CMDWIN
2399 if (cmdwin_type != 0 && wp == curwin)
2400 {
2401 /* draw the cmdline character in the leftmost column */
2402 n = 1;
2403 if (n > wp->w_width)
2404 n = wp->w_width;
2405 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002406 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002407 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409#endif
2410#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002411 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002413 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002416 if (nn > wp->w_width)
2417 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002419 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002420 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 n = nn;
2422 }
2423#endif
2424#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002425 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427 int nn = n + 2;
2428
2429 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002430 if (nn > wp->w_width)
2431 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002433 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002434 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 n = nn;
2436 }
2437#endif
2438 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002439 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002440 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
2442 set_empty_rows(wp, row);
2443}
2444
Bram Moolenaar1a384422010-07-14 19:53:30 +02002445#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002446static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002447
2448/*
2449 * Advance **color_cols and return TRUE when there are columns to draw.
2450 */
2451 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002452advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002453{
2454 while (**color_cols >= 0 && vcol > **color_cols)
2455 ++*color_cols;
2456 return (**color_cols >= 0);
2457}
2458#endif
2459
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002460#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2461/*
2462 * Copy "text" to ScreenLines using "attr".
2463 * Returns the next screen column.
2464 */
2465 static int
2466text_to_screenline(win_T *wp, char_u *text, int col)
2467{
2468 int off = (int)(current_ScreenLine - ScreenLines);
2469
2470#ifdef FEAT_MBYTE
2471 if (has_mbyte)
2472 {
2473 int cells;
2474 int u8c, u8cc[MAX_MCO];
2475 int i;
2476 int idx;
2477 int c_len;
2478 char_u *p;
2479# ifdef FEAT_ARABIC
2480 int prev_c = 0; /* previous Arabic character */
2481 int prev_c1 = 0; /* first composing char for prev_c */
2482# endif
2483
2484# ifdef FEAT_RIGHTLEFT
2485 if (wp->w_p_rl)
2486 idx = off;
2487 else
2488# endif
2489 idx = off + col;
2490
2491 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2492 for (p = text; *p != NUL; )
2493 {
2494 cells = (*mb_ptr2cells)(p);
2495 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002496 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002497# ifdef FEAT_RIGHTLEFT
2498 - (wp->w_p_rl ? col : 0)
2499# endif
2500 )
2501 break;
2502 ScreenLines[idx] = *p;
2503 if (enc_utf8)
2504 {
2505 u8c = utfc_ptr2char(p, u8cc);
2506 if (*p < 0x80 && u8cc[0] == 0)
2507 {
2508 ScreenLinesUC[idx] = 0;
2509#ifdef FEAT_ARABIC
2510 prev_c = u8c;
2511#endif
2512 }
2513 else
2514 {
2515#ifdef FEAT_ARABIC
2516 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2517 {
2518 /* Do Arabic shaping. */
2519 int pc, pc1, nc;
2520 int pcc[MAX_MCO];
2521 int firstbyte = *p;
2522
2523 /* The idea of what is the previous and next
2524 * character depends on 'rightleft'. */
2525 if (wp->w_p_rl)
2526 {
2527 pc = prev_c;
2528 pc1 = prev_c1;
2529 nc = utf_ptr2char(p + c_len);
2530 prev_c1 = u8cc[0];
2531 }
2532 else
2533 {
2534 pc = utfc_ptr2char(p + c_len, pcc);
2535 nc = prev_c;
2536 pc1 = pcc[0];
2537 }
2538 prev_c = u8c;
2539
2540 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2541 pc, pc1, nc);
2542 ScreenLines[idx] = firstbyte;
2543 }
2544 else
2545 prev_c = u8c;
2546#endif
2547 /* Non-BMP character: display as ? or fullwidth ?. */
2548#ifdef UNICODE16
2549 if (u8c >= 0x10000)
2550 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2551 else
2552#endif
2553 ScreenLinesUC[idx] = u8c;
2554 for (i = 0; i < Screen_mco; ++i)
2555 {
2556 ScreenLinesC[i][idx] = u8cc[i];
2557 if (u8cc[i] == 0)
2558 break;
2559 }
2560 }
2561 if (cells > 1)
2562 ScreenLines[idx + 1] = 0;
2563 }
2564 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2565 /* double-byte single width character */
2566 ScreenLines2[idx] = p[1];
2567 else if (cells > 1)
2568 /* double-width character */
2569 ScreenLines[idx + 1] = p[1];
2570 col += cells;
2571 idx += cells;
2572 p += c_len;
2573 }
2574 }
2575 else
2576#endif
2577 {
2578 int len = (int)STRLEN(text);
2579
Bram Moolenaar02631462017-09-22 15:20:32 +02002580 if (len > wp->w_width - col)
2581 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002582 if (len > 0)
2583 {
2584#ifdef FEAT_RIGHTLEFT
2585 if (wp->w_p_rl)
2586 STRNCPY(current_ScreenLine, text, len);
2587 else
2588#endif
2589 STRNCPY(current_ScreenLine + col, text, len);
2590 col += len;
2591 }
2592 }
2593 return col;
2594}
2595#endif
2596
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597#ifdef FEAT_FOLDING
2598/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002599 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2600 * space is available for window "wp", minus "col".
2601 */
2602 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002603compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002604{
2605 int fdc = wp->w_p_fdc;
2606 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002607 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002608
2609 if (fdc > wwidth - (col + wmw))
2610 fdc = wwidth - (col + wmw);
2611 return fdc;
2612}
2613
2614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 * Display one folded line.
2616 */
2617 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002618fold_line(
2619 win_T *wp,
2620 long fold_count,
2621 foldinfo_T *foldinfo,
2622 linenr_T lnum,
2623 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002625 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 pos_T *top, *bot;
2627 linenr_T lnume = lnum + fold_count - 1;
2628 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002629 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 int col;
2632 int txtcol;
2633 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002634 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
2636 /* Build the fold line:
2637 * 1. Add the cmdwin_type for the command-line window
2638 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002639 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 * 4. Compose the text
2641 * 5. Add the text
2642 * 6. set highlighting for the Visual area an other text
2643 */
2644 col = 0;
2645
2646 /*
2647 * 1. Add the cmdwin_type for the command-line window
2648 * Ignores 'rightleft', this window is never right-left.
2649 */
2650#ifdef FEAT_CMDWIN
2651 if (cmdwin_type != 0 && wp == curwin)
2652 {
2653 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002654 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655#ifdef FEAT_MBYTE
2656 if (enc_utf8)
2657 ScreenLinesUC[off] = 0;
2658#endif
2659 ++col;
2660 }
2661#endif
2662
2663 /*
2664 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002665 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002667 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (fdc > 0)
2669 {
2670 fill_foldcolumn(buf, wp, TRUE, lnum);
2671#ifdef FEAT_RIGHTLEFT
2672 if (wp->w_p_rl)
2673 {
2674 int i;
2675
Bram Moolenaar02631462017-09-22 15:20:32 +02002676 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002677 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 /* reverse the fold column */
2679 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002680 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682 else
2683#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002684 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 col += fdc;
2686 }
2687
2688#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002689# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2690 for (ri = 0; ri < l; ++ri) \
Bram Moolenaar02631462017-09-22 15:20:32 +02002691 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002692 else \
2693 for (ri = 0; ri < l; ++ri) \
2694 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002696# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2697 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698#endif
2699
Bram Moolenaar64486672010-05-16 15:46:46 +02002700 /* Set all attributes of the 'number' or 'relativenumber' column and the
2701 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002702 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
2704#ifdef FEAT_SIGNS
2705 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002706 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002708 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (len > 0)
2710 {
2711 if (len > 2)
2712 len = 2;
2713# ifdef FEAT_RIGHTLEFT
2714 if (wp->w_p_rl)
2715 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002716 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002717 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 else
2719# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002720 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 col += len;
2722 }
2723 }
2724#endif
2725
2726 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002727 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002729 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002731 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 if (len > 0)
2733 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002734 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002735 long num;
2736 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002737
2738 if (len > w + 1)
2739 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002740
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002741 if (wp->w_p_nu && !wp->w_p_rnu)
2742 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002743 num = (long)lnum;
2744 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002745 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002746 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002747 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002748 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002749 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002750 /* 'number' + 'relativenumber': cursor line shows absolute
2751 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002752 num = lnum;
2753 fmt = "%-*ld ";
2754 }
2755 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002756
Bram Moolenaar700e7342013-01-30 12:31:36 +01002757 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#ifdef FEAT_RIGHTLEFT
2759 if (wp->w_p_rl)
2760 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002761 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002762 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 else
2764#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002765 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 col += len;
2767 }
2768 }
2769
2770 /*
2771 * 4. Compose the folded-line string with 'foldtext', if set.
2772 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002773 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774
2775 txtcol = col; /* remember where text starts */
2776
2777 /*
2778 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2779 * Right-left text is put in columns 0 - number-col, normal text is put
2780 * in columns number-col - window-width.
2781 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002782 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
2784 /* Fill the rest of the line with the fold filler */
2785#ifdef FEAT_RIGHTLEFT
2786 if (wp->w_p_rl)
2787 col -= txtcol;
2788#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002789 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790#ifdef FEAT_RIGHTLEFT
2791 - (wp->w_p_rl ? txtcol : 0)
2792#endif
2793 )
2794 {
2795#ifdef FEAT_MBYTE
2796 if (enc_utf8)
2797 {
2798 if (fill_fold >= 0x80)
2799 {
2800 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002801 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002802 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002807 ScreenLines[off + col] = fill_fold;
2808 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002809 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002811 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002813 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 }
2815
2816 if (text != buf)
2817 vim_free(text);
2818
2819 /*
2820 * 6. set highlighting for the Visual area an other text.
2821 * If all folded lines are in the Visual area, highlight the line.
2822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2824 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002825 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 {
2827 /* Visual is after curwin->w_cursor */
2828 top = &curwin->w_cursor;
2829 bot = &VIsual;
2830 }
2831 else
2832 {
2833 /* Visual is before curwin->w_cursor */
2834 top = &VIsual;
2835 bot = &curwin->w_cursor;
2836 }
2837 if (lnum >= top->lnum
2838 && lnume <= bot->lnum
2839 && (VIsual_mode != 'v'
2840 || ((lnum > top->lnum
2841 || (lnum == top->lnum
2842 && top->col == 0))
2843 && (lnume < bot->lnum
2844 || (lnume == bot->lnum
2845 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002846 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 {
2848 if (VIsual_mode == Ctrl_V)
2849 {
2850 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002851 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002853 if (wp->w_old_cursor_lcol != MAXCOL
2854 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002855 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 len = wp->w_old_cursor_lcol;
2857 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002858 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002859 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002860 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
2862 }
2863 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002866 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002871#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002872 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2873 if (wp->w_p_cc_cols)
2874 {
2875 int i = 0;
2876 int j = wp->w_p_cc_cols[i];
2877 int old_txtcol = txtcol;
2878
2879 while (j > -1)
2880 {
2881 txtcol += j;
2882 if (wp->w_p_wrap)
2883 txtcol -= wp->w_skipcol;
2884 else
2885 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002886 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002887 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002888 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002889 txtcol = old_txtcol;
2890 j = wp->w_p_cc_cols[++i];
2891 }
2892 }
2893
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002894 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002895 if (wp->w_p_cuc)
2896 {
2897 txtcol += wp->w_virtcol;
2898 if (wp->w_p_wrap)
2899 txtcol -= wp->w_skipcol;
2900 else
2901 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002902 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002903 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002904 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002905 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
Bram Moolenaar02631462017-09-22 15:20:32 +02002908 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2909 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
2911 /*
2912 * Update w_cline_height and w_cline_folded if the cursor line was
2913 * updated (saves a call to plines() later).
2914 */
2915 if (wp == curwin
2916 && lnum <= curwin->w_cursor.lnum
2917 && lnume >= curwin->w_cursor.lnum)
2918 {
2919 curwin->w_cline_row = row;
2920 curwin->w_cline_height = 1;
2921 curwin->w_cline_folded = TRUE;
2922 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2923 }
2924}
2925
2926/*
2927 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2928 */
2929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002930copy_text_attr(
2931 int off,
2932 char_u *buf,
2933 int len,
2934 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002936 int i;
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 mch_memmove(ScreenLines + off, buf, (size_t)len);
2939# ifdef FEAT_MBYTE
2940 if (enc_utf8)
2941 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2942# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002943 for (i = 0; i < len; ++i)
2944 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945}
2946
2947/*
2948 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002949 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 */
2951 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002952fill_foldcolumn(
2953 char_u *p,
2954 win_T *wp,
2955 int closed, /* TRUE of FALSE */
2956 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957{
2958 int i = 0;
2959 int level;
2960 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002961 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002962 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
2964 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002965 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
2967 level = win_foldinfo.fi_level;
2968 if (level > 0)
2969 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002970 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002971 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002972
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 /* If the column is too narrow, we start at the lowest level that
2974 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002975 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 if (first_level < 1)
2977 first_level = 1;
2978
Bram Moolenaar1c934292015-01-27 16:39:29 +01002979 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {
2981 if (win_foldinfo.fi_lnum == lnum
2982 && first_level + i >= win_foldinfo.fi_low_level)
2983 p[i] = '-';
2984 else if (first_level == 1)
2985 p[i] = '|';
2986 else if (first_level + i <= 9)
2987 p[i] = '0' + first_level + i;
2988 else
2989 p[i] = '>';
2990 if (first_level + i == level)
2991 break;
2992 }
2993 }
2994 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002995 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996}
2997#endif /* FEAT_FOLDING */
2998
2999/*
3000 * Display line "lnum" of window 'wp' on the screen.
3001 * Start at row "startrow", stop when "endrow" is reached.
3002 * wp->w_virtcol needs to be valid.
3003 *
3004 * Return the number of last row the line occupies.
3005 */
3006 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003007win_line(
3008 win_T *wp,
3009 linenr_T lnum,
3010 int startrow,
3011 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003012 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003014 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3016 int c = 0; /* init for GCC */
3017 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003018#ifdef FEAT_LINEBREAK
3019 long vcol_sbr = -1; /* virtual column after showbreak */
3020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 long vcol_prev = -1; /* "vcol" of previous character */
3022 char_u *line; /* current line */
3023 char_u *ptr; /* current position in "line" */
3024 int row; /* row in the window, excl w_winrow */
3025 int screen_row; /* row on the screen, incl w_winrow */
3026
3027 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3028 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003029 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003030 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 int c_extra = NUL; /* extra chars, all the same */
3032 int extra_attr = 0; /* attributes when n_extra != 0 */
3033 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3034 displaying lcs_eol at end-of-line */
3035 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3036 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3037
3038 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3039 int saved_n_extra = 0;
3040 char_u *saved_p_extra = NULL;
3041 int saved_c_extra = 0;
3042 int saved_char_attr = 0;
3043
3044 int n_attr = 0; /* chars with special attr */
3045 int saved_attr2 = 0; /* char_attr saved for n_attr */
3046 int n_attr3 = 0; /* chars with overruling special attr */
3047 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3048
3049 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3050
3051 int fromcol, tocol; /* start/end of inverting */
3052 int fromcol_prev = -2; /* start of inverting after cursor */
3053 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003055 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 pos_T pos;
3057 long v;
3058
3059 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003060 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3062 in this line */
3063 int attr = 0; /* attributes for area highlighting */
3064 int area_attr = 0; /* attributes desired by highlighting */
3065 int search_attr = 0; /* attributes desired by 'hlsearch' */
3066#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003067 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 int syntax_attr = 0; /* attributes desired by syntax */
3069 int has_syntax = FALSE; /* this buffer has syntax highl. */
3070 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003071 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003072 int draw_color_col = FALSE; /* highlight colorcolumn */
3073 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003074#endif
3075#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003076 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003077# define SPWORDLEN 150
3078 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003079 int nextlinecol = 0; /* column where nextline[] starts */
3080 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003081 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003082 int spell_attr = 0; /* attributes desired by spelling */
3083 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003084 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3085 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003086 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003087 static int cap_col = -1; /* column to check for Cap word */
3088 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003089 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090#endif
3091 int extra_check; /* has syntax or linebreak */
3092#ifdef FEAT_MBYTE
3093 int multi_attr = 0; /* attributes desired by multibyte */
3094 int mb_l = 1; /* multi-byte byte length */
3095 int mb_c = 0; /* decoded multi-byte character */
3096 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003097 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098#endif
3099#ifdef FEAT_DIFF
3100 int filler_lines; /* nr of filler lines to be drawn */
3101 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003102 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 int change_start = MAXCOL; /* first col of changed area */
3104 int change_end = -1; /* last col of changed area */
3105#endif
3106 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3107#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003108 int need_showbreak = FALSE; /* overlong line, skipping first x
3109 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003111#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003112 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003114 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#endif
3116#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003117 matchitem_T *cur; /* points to the match list */
3118 match_T *shl; /* points to search_hl or a match */
3119 int shl_flag; /* flag to indicate whether search_hl
3120 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003121 int pos_inprogress; /* marks that position match search is
3122 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003123 int prevcol_hl_flag; /* flag to indicate whether prevcol
3124 equals startcol of search_hl or one
3125 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#endif
3127#ifdef FEAT_ARABIC
3128 int prev_c = 0; /* previous Arabic character */
3129 int prev_c1 = 0; /* first composing char for prev_c */
3130#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003131#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003132 int did_line_attr = 0;
3133#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003134#ifdef FEAT_TERMINAL
3135 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003136 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
3139 /* draw_state: items that are drawn in sequence: */
3140#define WL_START 0 /* nothing done yet */
3141#ifdef FEAT_CMDWIN
3142# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3143#else
3144# define WL_CMDLINE WL_START
3145#endif
3146#ifdef FEAT_FOLDING
3147# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3148#else
3149# define WL_FOLD WL_CMDLINE
3150#endif
3151#ifdef FEAT_SIGNS
3152# define WL_SIGN WL_FOLD + 1 /* column for signs */
3153#else
3154# define WL_SIGN WL_FOLD /* column for signs */
3155#endif
3156#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003157#ifdef FEAT_LINEBREAK
3158# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003160# define WL_BRI WL_NR
3161#endif
3162#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3163# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3164#else
3165# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166#endif
3167#define WL_LINE WL_SBR + 1 /* text in the line */
3168 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003169#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 int feedback_col = 0;
3171 int feedback_old_attr = -1;
3172#endif
3173
Bram Moolenaar860cae12010-06-05 23:22:07 +02003174#ifdef FEAT_CONCEAL
3175 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003176 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003177 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003178 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003179 int is_concealing = FALSE;
3180 int boguscols = 0; /* nonexistent columns added to force
3181 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003182 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003183 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003184 int match_conc = 0; /* cchar for match functions */
3185 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003186 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003187# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003188# define FIX_FOR_BOGUSCOLS \
3189 { \
3190 n_extra += vcol_off; \
3191 vcol -= vcol_off; \
3192 vcol_off = 0; \
3193 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003194 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003195 boguscols = 0; \
3196 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003197#else
3198# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200
3201 if (startrow > endrow) /* past the end already! */
3202 return startrow;
3203
3204 row = startrow;
3205 screen_row = row + W_WINROW(wp);
3206
3207 /*
3208 * To speed up the loop below, set extra_check when there is linebreak,
3209 * trailing white space and/or syntax processing to be done.
3210 */
3211#ifdef FEAT_LINEBREAK
3212 extra_check = wp->w_p_lbr;
3213#else
3214 extra_check = 0;
3215#endif
3216#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003217 if (syntax_present(wp) && !wp->w_s->b_syn_error
3218# ifdef SYN_TIME_LIMIT
3219 && !wp->w_s->b_syn_slow
3220# endif
3221 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
3223 /* Prepare for syntax highlighting in this line. When there is an
3224 * error, stop syntax highlighting. */
3225 save_did_emsg = did_emsg;
3226 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003227 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003229 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 else
3231 {
3232 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003233#ifdef SYN_TIME_LIMIT
3234 if (!wp->w_s->b_syn_slow)
3235#endif
3236 {
3237 has_syntax = TRUE;
3238 extra_check = TRUE;
3239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 }
3241 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003242
3243 /* Check for columns to display for 'colorcolumn'. */
3244 color_cols = wp->w_p_cc_cols;
3245 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003246 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003247#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003248
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003249#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003250 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003251 {
3252 extra_check = TRUE;
3253 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003254 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003255 }
3256#endif
3257
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003258#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003259 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003260 && *wp->w_s->b_p_spl != NUL
3261 && wp->w_s->b_langp.ga_len > 0
3262 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003263 {
3264 /* Prepare for spell checking. */
3265 has_spell = TRUE;
3266 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003267
3268 /* Get the start of the next line, so that words that wrap to the next
3269 * line are found too: "et<line-break>al.".
3270 * Trick: skip a few chars for C/shell/Vim comments */
3271 nextline[SPWORDLEN] = NUL;
3272 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3273 {
3274 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3275 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3276 }
3277
3278 /* When a word wrapped from the previous line the start of the current
3279 * line is valid. */
3280 if (lnum == checked_lnum)
3281 cur_checked_col = checked_col;
3282 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003283
3284 /* When there was a sentence end in the previous line may require a
3285 * word starting with capital in this line. In line 1 always check
3286 * the first word. */
3287 if (lnum != capcol_lnum)
3288 cap_col = -1;
3289 if (lnum == 1)
3290 cap_col = 0;
3291 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293#endif
3294
3295 /*
3296 * handle visual active in this window
3297 */
3298 fromcol = -10;
3299 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3301 {
3302 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003303 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 top = &curwin->w_cursor;
3306 bot = &VIsual;
3307 }
3308 else /* Visual is before curwin->w_cursor */
3309 {
3310 top = &VIsual;
3311 bot = &curwin->w_cursor;
3312 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003313 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 if (VIsual_mode == Ctrl_V) /* block mode */
3315 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003316 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 {
3318 fromcol = wp->w_old_cursor_fcol;
3319 tocol = wp->w_old_cursor_lcol;
3320 }
3321 }
3322 else /* non-block mode */
3323 {
3324 if (lnum > top->lnum && lnum <= bot->lnum)
3325 fromcol = 0;
3326 else if (lnum == top->lnum)
3327 {
3328 if (VIsual_mode == 'V') /* linewise */
3329 fromcol = 0;
3330 else
3331 {
3332 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3333 if (gchar_pos(top) == NUL)
3334 tocol = fromcol + 1;
3335 }
3336 }
3337 if (VIsual_mode != 'V' && lnum == bot->lnum)
3338 {
3339 if (*p_sel == 'e' && bot->col == 0
3340#ifdef FEAT_VIRTUALEDIT
3341 && bot->coladd == 0
3342#endif
3343 )
3344 {
3345 fromcol = -10;
3346 tocol = MAXCOL;
3347 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003348 else if (bot->col == MAXCOL)
3349 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 else
3351 {
3352 pos = *bot;
3353 if (*p_sel == 'e')
3354 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3355 else
3356 {
3357 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3358 ++tocol;
3359 }
3360 }
3361 }
3362 }
3363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 /* Check if the character under the cursor should not be inverted */
3365 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003366#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 )
3370 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
3372 /* if inverting in this line set area_highlighting */
3373 if (fromcol >= 0)
3374 {
3375 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003376 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003378 if ((clip_star.available && !clip_star.owned
3379 && clip_isautosel_star())
3380 || (clip_plus.available && !clip_plus.owned
3381 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003382 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383#endif
3384 }
3385 }
3386
3387 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003388 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003390 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 && wp == curwin
3392 && lnum >= curwin->w_cursor.lnum
3393 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3394 {
3395 if (lnum == curwin->w_cursor.lnum)
3396 getvcol(curwin, &(curwin->w_cursor),
3397 (colnr_T *)&fromcol, NULL, NULL);
3398 else
3399 fromcol = 0;
3400 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3401 {
3402 pos.lnum = lnum;
3403 pos.col = search_match_endcol;
3404 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3405 }
3406 else
3407 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003408 /* do at least one character; happens when past end of line */
3409 if (fromcol == tocol)
3410 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003412 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
3414
3415#ifdef FEAT_DIFF
3416 filler_lines = diff_check(wp, lnum);
3417 if (filler_lines < 0)
3418 {
3419 if (filler_lines == -1)
3420 {
3421 if (diff_find_change(wp, lnum, &change_start, &change_end))
3422 diff_hlf = HLF_ADD; /* added line */
3423 else if (change_start == 0)
3424 diff_hlf = HLF_TXD; /* changed text */
3425 else
3426 diff_hlf = HLF_CHD; /* changed line */
3427 }
3428 else
3429 diff_hlf = HLF_ADD; /* added line */
3430 filler_lines = 0;
3431 area_highlighting = TRUE;
3432 }
3433 if (lnum == wp->w_topline)
3434 filler_lines = wp->w_topfill;
3435 filler_todo = filler_lines;
3436#endif
3437
3438#ifdef LINE_ATTR
3439# ifdef FEAT_SIGNS
3440 /* If this line has a sign with line highlighting set line_attr. */
3441 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3442 if (v != 0)
3443 line_attr = sign_get_attr((int)v, TRUE);
3444# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003445# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003447 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003448 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449# endif
3450 if (line_attr != 0)
3451 area_highlighting = TRUE;
3452#endif
3453
3454 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3455 ptr = line;
3456
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003457#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003458 if (has_spell)
3459 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003460 /* For checking first word with a capital skip white space. */
3461 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003462 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003463
Bram Moolenaar30abd282005-06-22 22:35:10 +00003464 /* To be able to spell-check over line boundaries copy the end of the
3465 * current line into nextline[]. Above the start of the next line was
3466 * copied to nextline[SPWORDLEN]. */
3467 if (nextline[SPWORDLEN] == NUL)
3468 {
3469 /* No next line or it is empty. */
3470 nextlinecol = MAXCOL;
3471 nextline_idx = 0;
3472 }
3473 else
3474 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003475 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003476 if (v < SPWORDLEN)
3477 {
3478 /* Short line, use it completely and append the start of the
3479 * next line. */
3480 nextlinecol = 0;
3481 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003482 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003483 nextline_idx = v + 1;
3484 }
3485 else
3486 {
3487 /* Long line, use only the last SPWORDLEN bytes. */
3488 nextlinecol = v - SPWORDLEN;
3489 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3490 nextline_idx = SPWORDLEN + 1;
3491 }
3492 }
3493 }
3494#endif
3495
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003496 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003498 if (lcs_space || lcs_trail)
3499 extra_check = TRUE;
3500 /* find start of trailing whitespace */
3501 if (lcs_trail)
3502 {
3503 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003504 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003505 --trailcol;
3506 trailcol += (colnr_T) (ptr - line);
3507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
3509
3510 /*
3511 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3512 * first character to be displayed.
3513 */
3514 if (wp->w_p_wrap)
3515 v = wp->w_skipcol;
3516 else
3517 v = wp->w_leftcol;
3518 if (v > 0)
3519 {
3520#ifdef FEAT_MBYTE
3521 char_u *prev_ptr = ptr;
3522#endif
3523 while (vcol < v && *ptr != NUL)
3524 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003525 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 vcol += c;
3527#ifdef FEAT_MBYTE
3528 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003530 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003533 /* When:
3534 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003535 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003536 * - 'virtualedit' is set, or
3537 * - the visual mode is active,
3538 * the end of the line may be before the start of the displayed part.
3539 */
3540 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003541#ifdef FEAT_SYN_HL
3542 wp->w_p_cuc || draw_color_col ||
3543#endif
3544#ifdef FEAT_VIRTUALEDIT
3545 virtual_active() ||
3546#endif
3547 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
3552 /* Handle a character that's not completely on the screen: Put ptr at
3553 * that character but skip the first few screen characters. */
3554 if (vcol > v)
3555 {
3556 vcol -= c;
3557#ifdef FEAT_MBYTE
3558 ptr = prev_ptr;
3559#else
3560 --ptr;
3561#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003562 /* If the character fits on the screen, don't need to skip it.
3563 * Except for a TAB. */
3564 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003565#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003566 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003567#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003568 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003569 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571
3572 /*
3573 * Adjust for when the inverted text is before the screen,
3574 * and when the start of the inverted text is before the screen.
3575 */
3576 if (tocol <= vcol)
3577 fromcol = 0;
3578 else if (fromcol >= 0 && fromcol < vcol)
3579 fromcol = vcol;
3580
3581#ifdef FEAT_LINEBREAK
3582 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3583 if (wp->w_p_wrap)
3584 need_showbreak = TRUE;
3585#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003586#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003587 /* When spell checking a word we need to figure out the start of the
3588 * word and if it's badly spelled or not. */
3589 if (has_spell)
3590 {
3591 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003592 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003593 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003594
3595 pos = wp->w_cursor;
3596 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003597 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003598 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003599
3600 /* spell_move_to() may call ml_get() and make "line" invalid */
3601 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3602 ptr = line + linecol;
3603
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003604 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003605 {
3606 /* no bad word found at line start, don't check until end of a
3607 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003608 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003609 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003610 }
3611 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003612 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003613 /* bad word found, use attributes until end of word */
3614 word_end = wp->w_cursor.col + len + 1;
3615
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003616 /* Turn index into actual attributes. */
3617 if (spell_hlf != HLF_COUNT)
3618 spell_attr = highlight_attr[spell_hlf];
3619 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003620 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003621
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003622# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003623 /* Need to restart syntax highlighting for this line. */
3624 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003625 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003626# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003627 }
3628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
3630
3631 /*
3632 * Correct highlighting for cursor that can't be disabled.
3633 * Avoids having to check this for each character.
3634 */
3635 if (fromcol >= 0)
3636 {
3637 if (noinvcur)
3638 {
3639 if ((colnr_T)fromcol == wp->w_virtcol)
3640 {
3641 /* highlighting starts at cursor, let it start just after the
3642 * cursor */
3643 fromcol_prev = fromcol;
3644 fromcol = -1;
3645 }
3646 else if ((colnr_T)fromcol < wp->w_virtcol)
3647 /* restart highlighting after the cursor */
3648 fromcol_prev = wp->w_virtcol;
3649 }
3650 if (fromcol >= tocol)
3651 fromcol = -1;
3652 }
3653
3654#ifdef FEAT_SEARCH_EXTRA
3655 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003656 * Handle highlighting the last used search pattern and matches.
3657 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003659 cur = wp->w_match_head;
3660 shl_flag = FALSE;
3661 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003663 if (shl_flag == FALSE)
3664 {
3665 shl = &search_hl;
3666 shl_flag = TRUE;
3667 }
3668 else
3669 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003670 shl->startcol = MAXCOL;
3671 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003673 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003674 v = (long)(ptr - line);
3675 if (cur != NULL)
3676 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003677 next_search_hl(wp, shl, lnum, (colnr_T)v,
3678 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003679
3680 /* Need to get the line again, a multi-line regexp may have made it
3681 * invalid. */
3682 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3683 ptr = line + v;
3684
3685 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003687 if (shl->lnum == lnum)
3688 shl->startcol = shl->rm.startpos[0].col;
3689 else
3690 shl->startcol = 0;
3691 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3692 - shl->rm.startpos[0].lnum)
3693 shl->endcol = shl->rm.endpos[0].col;
3694 else
3695 shl->endcol = MAXCOL;
3696 /* Highlight one character for an empty match. */
3697 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003700 if (has_mbyte && line[shl->endcol] != NUL)
3701 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3702 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003704 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003706 if ((long)shl->startcol < v) /* match at leftcol */
3707 {
3708 shl->attr_cur = shl->attr;
3709 search_attr = shl->attr;
3710 }
3711 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003713 if (shl != &search_hl && cur != NULL)
3714 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716#endif
3717
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003718#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003719 /* Cursor line highlighting for 'cursorline' in the current window. Not
3720 * when Visual mode is active, because it's not clear what is selected
3721 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003722 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003723 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003724 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003725 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003726 area_highlighting = TRUE;
3727 }
3728#endif
3729
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003730 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 col = 0;
3732#ifdef FEAT_RIGHTLEFT
3733 if (wp->w_p_rl)
3734 {
3735 /* Rightleft window: process the text in the normal direction, but put
3736 * it in current_ScreenLine[] from right to left. Start at the
3737 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003738 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 off += col;
3740 }
3741#endif
3742
3743 /*
3744 * Repeat for the whole displayed line.
3745 */
3746 for (;;)
3747 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003748#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003749 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 /* Skip this quickly when working on the text. */
3752 if (draw_state != WL_LINE)
3753 {
3754#ifdef FEAT_CMDWIN
3755 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3756 {
3757 draw_state = WL_CMDLINE;
3758 if (cmdwin_type != 0 && wp == curwin)
3759 {
3760 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003762 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003763 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 }
3765 }
3766#endif
3767
3768#ifdef FEAT_FOLDING
3769 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3770 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003771 int fdc = compute_foldcolumn(wp, 0);
3772
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003774 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003776 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003777 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003778 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003779 p_extra_free = alloc(12 + 1);
3780
3781 if (p_extra_free != NULL)
3782 {
3783 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3784 n_extra = fdc;
3785 p_extra_free[n_extra] = NUL;
3786 p_extra = p_extra_free;
3787 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003788 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791 }
3792#endif
3793
3794#ifdef FEAT_SIGNS
3795 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3796 {
3797 draw_state = WL_SIGN;
3798 /* Show the sign column when there are any signs in this
3799 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003800 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003802 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003804 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805# endif
3806
3807 /* Draw two cells with the sign value or blank. */
3808 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003809 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 n_extra = 2;
3811
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003812 if (row == startrow
3813#ifdef FEAT_DIFF
3814 + filler_lines && filler_todo <= 0
3815#endif
3816 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 {
3818 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3819 SIGN_TEXT);
3820# ifdef FEAT_SIGN_ICONS
3821 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3822 SIGN_ICON);
3823 if (gui.in_use && icon_sign != 0)
3824 {
3825 /* Use the image in this position. */
3826 c_extra = SIGN_BYTE;
3827# ifdef FEAT_NETBEANS_INTG
3828 if (buf_signcount(wp->w_buffer, lnum) > 1)
3829 c_extra = MULTISIGN_BYTE;
3830# endif
3831 char_attr = icon_sign;
3832 }
3833 else
3834# endif
3835 if (text_sign != 0)
3836 {
3837 p_extra = sign_get_text(text_sign);
3838 if (p_extra != NULL)
3839 {
3840 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003841 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843 char_attr = sign_get_attr(text_sign, FALSE);
3844 }
3845 }
3846 }
3847 }
3848#endif
3849
3850 if (draw_state == WL_NR - 1 && n_extra == 0)
3851 {
3852 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003853 /* Display the absolute or relative line number. After the
3854 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3855 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 && (row == startrow
3857#ifdef FEAT_DIFF
3858 + filler_lines
3859#endif
3860 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3861 {
3862 /* Draw the line number (empty space after wrapping). */
3863 if (row == startrow
3864#ifdef FEAT_DIFF
3865 + filler_lines
3866#endif
3867 )
3868 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003869 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003870 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003871
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003872 if (wp->w_p_nu && !wp->w_p_rnu)
3873 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003874 num = (long)lnum;
3875 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003876 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003877 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003878 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003879 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003880 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003881 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003882 num = lnum;
3883 fmt = "%-*ld ";
3884 }
3885 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003886
Bram Moolenaar700e7342013-01-30 12:31:36 +01003887 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003888 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (wp->w_skipcol > 0)
3890 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3891 *p_extra = '-';
3892#ifdef FEAT_RIGHTLEFT
3893 if (wp->w_p_rl) /* reverse line numbers */
3894 rl_mirror(extra);
3895#endif
3896 p_extra = extra;
3897 c_extra = NUL;
3898 }
3899 else
3900 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003901 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003902 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003903#ifdef FEAT_SYN_HL
3904 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003905 * the current line differently.
3906 * TODO: Can we use CursorLine instead of CursorLineNr
3907 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003908 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003909 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003910 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913 }
3914
Bram Moolenaar597a4222014-06-25 14:39:50 +02003915#ifdef FEAT_LINEBREAK
3916 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3917 && n_extra == 0 && *p_sbr != NUL)
3918 /* draw indent after showbreak value */
3919 draw_state = WL_BRI;
3920 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3921 /* After the showbreak, draw the breakindent */
3922 draw_state = WL_BRI - 1;
3923
3924 /* draw 'breakindent': indent wrapped text accordingly */
3925 if (draw_state == WL_BRI - 1 && n_extra == 0)
3926 {
3927 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003928 /* if need_showbreak is set, breakindent also applies */
3929 if (wp->w_p_bri && n_extra == 0
3930 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003931# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003932 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003933# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003934 )
3935 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003936 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003937# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003938 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003939 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003940 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003941# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003942 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3943 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003944 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003945# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003946 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003947# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003948 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003949 c_extra = ' ';
3950 n_extra = get_breakindent_win(wp,
3951 ml_get_buf(wp->w_buffer, lnum, FALSE));
3952 /* Correct end of highlighted area for 'breakindent',
3953 * required when 'linebreak' is also set. */
3954 if (tocol == vcol)
3955 tocol += n_extra;
3956 }
3957 }
3958#endif
3959
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3961 if (draw_state == WL_SBR - 1 && n_extra == 0)
3962 {
3963 draw_state = WL_SBR;
3964# ifdef FEAT_DIFF
3965 if (filler_todo > 0)
3966 {
3967 /* Draw "deleted" diff line(s). */
3968 if (char2cells(fill_diff) > 1)
3969 c_extra = '-';
3970 else
3971 c_extra = fill_diff;
3972# ifdef FEAT_RIGHTLEFT
3973 if (wp->w_p_rl)
3974 n_extra = col + 1;
3975 else
3976# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02003977 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003978 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
3980# endif
3981# ifdef FEAT_LINEBREAK
3982 if (*p_sbr != NUL && need_showbreak)
3983 {
3984 /* Draw 'showbreak' at the start of each broken line. */
3985 p_extra = p_sbr;
3986 c_extra = NUL;
3987 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003988 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003990 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 /* Correct end of highlighted area for 'showbreak',
3992 * required when 'linebreak' is also set. */
3993 if (tocol == vcol)
3994 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003995#ifdef FEAT_SYN_HL
3996 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003997 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003998 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003999 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 }
4002# endif
4003 }
4004#endif
4005
4006 if (draw_state == WL_LINE - 1 && n_extra == 0)
4007 {
4008 draw_state = WL_LINE;
4009 if (saved_n_extra)
4010 {
4011 /* Continue item from end of wrapped line. */
4012 n_extra = saved_n_extra;
4013 c_extra = saved_c_extra;
4014 p_extra = saved_p_extra;
4015 char_attr = saved_char_attr;
4016 }
4017 else
4018 char_attr = 0;
4019 }
4020 }
4021
4022 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004023 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004024 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025#ifdef FEAT_DIFF
4026 && filler_todo <= 0
4027#endif
4028 )
4029 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004030 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004031 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004032 /* Pretend we have finished updating the window. Except when
4033 * 'cursorcolumn' is set. */
4034#ifdef FEAT_SYN_HL
4035 if (wp->w_p_cuc)
4036 row = wp->w_cline_row + wp->w_cline_height;
4037 else
4038#endif
4039 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 break;
4041 }
4042
4043 if (draw_state == WL_LINE && area_highlighting)
4044 {
4045 /* handle Visual or match highlighting in this line */
4046 if (vcol == fromcol
4047#ifdef FEAT_MBYTE
4048 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4049 && (*mb_ptr2cells)(ptr) > 1)
4050#endif
4051 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004052 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 && vcol < tocol))
4054 area_attr = attr; /* start highlighting */
4055 else if (area_attr != 0
4056 && (vcol == tocol
4057 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060#ifdef FEAT_SEARCH_EXTRA
4061 if (!n_extra)
4062 {
4063 /*
4064 * Check for start/end of search pattern match.
4065 * After end, check for start/end of next match.
4066 * When another match, have to check for start again.
4067 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004068 * Do this for 'search_hl' and the match list (ordered by
4069 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004071 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004072 cur = wp->w_match_head;
4073 shl_flag = FALSE;
4074 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004076 if (shl_flag == FALSE
4077 && ((cur != NULL
4078 && cur->priority > SEARCH_HL_PRIORITY)
4079 || cur == NULL))
4080 {
4081 shl = &search_hl;
4082 shl_flag = TRUE;
4083 }
4084 else
4085 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004086 if (cur != NULL)
4087 cur->pos.cur = 0;
4088 pos_inprogress = TRUE;
4089 while (shl->rm.regprog != NULL
4090 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004092 if (shl->startcol != MAXCOL
4093 && v >= (long)shl->startcol
4094 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004096#ifdef FEAT_MBYTE
4097 int tmp_col = v + MB_PTR2LEN(ptr);
4098
4099 if (shl->endcol < tmp_col)
4100 shl->endcol = tmp_col;
4101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004103#ifdef FEAT_CONCEAL
4104 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4105 == cur->hlg_id)
4106 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004107 has_match_conc =
4108 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004109 match_conc = cur->conceal_char;
4110 }
4111 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004112 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004115 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 {
4117 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004118 next_search_hl(wp, shl, lnum, (colnr_T)v,
4119 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004120 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004121 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123 /* Need to get the line again, a multi-line regexp
4124 * may have made it invalid. */
4125 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4126 ptr = line + v;
4127
4128 if (shl->lnum == lnum)
4129 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004130 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004132 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004134 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004136 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 {
4138 /* highlight empty match, try again after
4139 * it */
4140#ifdef FEAT_MBYTE
4141 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004142 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004143 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 else
4145#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004146 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
4148
4149 /* Loop to check if the match starts at the
4150 * current position */
4151 continue;
4152 }
4153 }
4154 break;
4155 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004156 if (shl != &search_hl && cur != NULL)
4157 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004159
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004160 /* Use attributes from match with highest priority among
4161 * 'search_hl' and the match list. */
4162 search_attr = search_hl.attr_cur;
4163 cur = wp->w_match_head;
4164 shl_flag = FALSE;
4165 while (cur != NULL || shl_flag == FALSE)
4166 {
4167 if (shl_flag == FALSE
4168 && ((cur != NULL
4169 && cur->priority > SEARCH_HL_PRIORITY)
4170 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004171 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004172 shl = &search_hl;
4173 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004174 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004175 else
4176 shl = &cur->hl;
4177 if (shl->attr_cur != 0)
4178 search_attr = shl->attr_cur;
4179 if (shl != &search_hl && cur != NULL)
4180 cur = cur->next;
4181 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004182 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004183 if (*ptr == NUL && (did_line_attr >= 1
4184 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004185 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187#endif
4188
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004190 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004192 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4193 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004195 if (diff_hlf == HLF_TXD && ptr - line > change_end
4196 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004198 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004199 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004200 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004203
4204 /* Decide which of the highlight attributes to use. */
4205 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004206#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004207 if (area_attr != 0)
4208 char_attr = hl_combine_attr(line_attr, area_attr);
4209 else if (search_attr != 0)
4210 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004211 /* Use line_attr when not in the Visual or 'incsearch' area
4212 * (area_attr may be 0 when "noinvcur" is set). */
4213 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004214 || vcol < fromcol || vcol_prev < fromcol_prev
4215 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004216 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004217#else
4218 if (area_attr != 0)
4219 char_attr = area_attr;
4220 else if (search_attr != 0)
4221 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004222#endif
4223 else
4224 {
4225 attr_pri = FALSE;
4226#ifdef FEAT_SYN_HL
4227 if (has_syntax)
4228 char_attr = syntax_attr;
4229 else
4230#endif
4231 char_attr = 0;
4232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234
4235 /*
4236 * Get the next character to put on the screen.
4237 */
4238 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004239 * The "p_extra" points to the extra stuff that is inserted to
4240 * represent special characters (non-printable stuff) and other
4241 * things. When all characters are the same, c_extra is used.
4242 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4243 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4245 */
4246 if (n_extra > 0)
4247 {
4248 if (c_extra != NUL)
4249 {
4250 c = c_extra;
4251#ifdef FEAT_MBYTE
4252 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004253 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004256 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004257 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259 else
4260 mb_utf8 = FALSE;
4261#endif
4262 }
4263 else
4264 {
4265 c = *p_extra;
4266#ifdef FEAT_MBYTE
4267 if (has_mbyte)
4268 {
4269 mb_c = c;
4270 if (enc_utf8)
4271 {
4272 /* If the UTF-8 character is more than one byte:
4273 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004274 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 mb_utf8 = FALSE;
4276 if (mb_l > n_extra)
4277 mb_l = 1;
4278 else if (mb_l > 1)
4279 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004280 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004282 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 }
4285 else
4286 {
4287 /* if this is a DBCS character, put it in "mb_c" */
4288 mb_l = MB_BYTE2LEN(c);
4289 if (mb_l >= n_extra)
4290 mb_l = 1;
4291 else if (mb_l > 1)
4292 mb_c = (c << 8) + p_extra[1];
4293 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004294 if (mb_l == 0) /* at the NUL at end-of-line */
4295 mb_l = 1;
4296
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 /* If a double-width char doesn't fit display a '>' in the
4298 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004299 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300# ifdef FEAT_RIGHTLEFT
4301 wp->w_p_rl ? (col <= 0) :
4302# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004303 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 && (*mb_char2cells)(mb_c) == 2)
4305 {
4306 c = '>';
4307 mb_c = c;
4308 mb_l = 1;
4309 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004310 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 /* put the pointer back to output the double-width
4312 * character at the start of the next line. */
4313 ++n_extra;
4314 --p_extra;
4315 }
4316 else
4317 {
4318 n_extra -= mb_l - 1;
4319 p_extra += mb_l - 1;
4320 }
4321 }
4322#endif
4323 ++p_extra;
4324 }
4325 --n_extra;
4326 }
4327 else
4328 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004329#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004330 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004331#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004332
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004333 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004334 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 /*
4336 * Get a character from the line itself.
4337 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004338 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004339#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004340 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342#ifdef FEAT_MBYTE
4343 if (has_mbyte)
4344 {
4345 mb_c = c;
4346 if (enc_utf8)
4347 {
4348 /* If the UTF-8 character is more than one byte: Decode it
4349 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004350 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 mb_utf8 = FALSE;
4352 if (mb_l > 1)
4353 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004354 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 /* Overlong encoded ASCII or ASCII with composing char
4356 * is displayed normally, except a NUL. */
4357 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004358 {
4359 c = mb_c;
4360# ifdef FEAT_LINEBREAK
4361 c0 = mb_c;
4362# endif
4363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004365
4366 /* At start of the line we can have a composing char.
4367 * Draw it as a space with a composing char. */
4368 if (utf_iscomposing(mb_c))
4369 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004370 int i;
4371
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004372 for (i = Screen_mco - 1; i > 0; --i)
4373 u8cc[i] = u8cc[i - 1];
4374 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004375 mb_c = ' ';
4376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
4378
4379 if ((mb_l == 1 && c >= 0x80)
4380 || (mb_l >= 1 && mb_c == 0)
4381 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004382# ifdef UNICODE16
4383 || mb_c >= 0x10000
4384# endif
4385 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
4387 /*
4388 * Illegal UTF-8 byte: display as <xx>.
4389 * Non-BMP character : display as ? or fullwidth ?.
4390 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004391# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
4395 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004396# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 if (wp->w_p_rl) /* reverse */
4398 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004401# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 else if (utf_char2cells(mb_c) != 2)
4403 STRCPY(extra, "?");
4404 else
4405 /* 0xff1f in UTF-8: full-width '?' */
4406 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
4409 p_extra = extra;
4410 c = *p_extra;
4411 mb_c = mb_ptr2char_adv(&p_extra);
4412 mb_utf8 = (c >= 0x80);
4413 n_extra = (int)STRLEN(p_extra);
4414 c_extra = NUL;
4415 if (area_attr == 0 && search_attr == 0)
4416 {
4417 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004418 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 saved_attr2 = char_attr; /* save current attr */
4420 }
4421 }
4422 else if (mb_l == 0) /* at the NUL at end-of-line */
4423 mb_l = 1;
4424#ifdef FEAT_ARABIC
4425 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4426 {
4427 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004428 int pc, pc1, nc;
4429 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
4431 /* The idea of what is the previous and next
4432 * character depends on 'rightleft'. */
4433 if (wp->w_p_rl)
4434 {
4435 pc = prev_c;
4436 pc1 = prev_c1;
4437 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004438 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440 else
4441 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004442 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004444 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446 prev_c = mb_c;
4447
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004448 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
4450 else
4451 prev_c = mb_c;
4452#endif
4453 }
4454 else /* enc_dbcs */
4455 {
4456 mb_l = MB_BYTE2LEN(c);
4457 if (mb_l == 0) /* at the NUL at end-of-line */
4458 mb_l = 1;
4459 else if (mb_l > 1)
4460 {
4461 /* We assume a second byte below 32 is illegal.
4462 * Hopefully this is OK for all double-byte encodings!
4463 */
4464 if (ptr[1] >= 32)
4465 mb_c = (c << 8) + ptr[1];
4466 else
4467 {
4468 if (ptr[1] == NUL)
4469 {
4470 /* head byte at end of line */
4471 mb_l = 1;
4472 transchar_nonprint(extra, c);
4473 }
4474 else
4475 {
4476 /* illegal tail byte */
4477 mb_l = 2;
4478 STRCPY(extra, "XX");
4479 }
4480 p_extra = extra;
4481 n_extra = (int)STRLEN(extra) - 1;
4482 c_extra = NUL;
4483 c = *p_extra++;
4484 if (area_attr == 0 && search_attr == 0)
4485 {
4486 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004487 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 saved_attr2 = char_attr; /* save current attr */
4489 }
4490 mb_c = c;
4491 }
4492 }
4493 }
4494 /* If a double-width char doesn't fit display a '>' in the
4495 * last column; the character is displayed at the start of the
4496 * next line. */
4497 if ((
4498# ifdef FEAT_RIGHTLEFT
4499 wp->w_p_rl ? (col <= 0) :
4500# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004501 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 && (*mb_char2cells)(mb_c) == 2)
4503 {
4504 c = '>';
4505 mb_c = c;
4506 mb_utf8 = FALSE;
4507 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004508 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 /* Put pointer back so that the character will be
4510 * displayed at the start of the next line. */
4511 --ptr;
4512 }
4513 else if (*ptr != NUL)
4514 ptr += mb_l - 1;
4515
4516 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004517 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004518 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004519 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004522 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 c = ' ';
4524 if (area_attr == 0 && search_attr == 0)
4525 {
4526 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004527 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 saved_attr2 = char_attr; /* save current attr */
4529 }
4530 mb_c = c;
4531 mb_utf8 = FALSE;
4532 mb_l = 1;
4533 }
4534
4535 }
4536#endif
4537 ++ptr;
4538
4539 if (extra_check)
4540 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004541#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004542 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004543#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004544
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004545#ifdef FEAT_TERMINAL
4546 if (get_term_attr)
4547 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004548 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004549
4550 if (!attr_pri)
4551 char_attr = syntax_attr;
4552 else
4553 char_attr = hl_combine_attr(syntax_attr, char_attr);
4554 }
4555#endif
4556
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004557#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 /* Get syntax attribute, unless still at the start of the line
4559 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004560 v = (long)(ptr - line);
4561 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
4563 /* Get the syntax attribute for the character. If there
4564 * is an error, disable syntax highlighting. */
4565 save_did_emsg = did_emsg;
4566 did_emsg = FALSE;
4567
Bram Moolenaar217ad922005-03-20 22:37:15 +00004568 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004569# ifdef FEAT_SPELL
4570 has_spell ? &can_spell :
4571# endif
4572 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
4574 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004575 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004576 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004577 has_syntax = FALSE;
4578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 else
4580 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004581#ifdef SYN_TIME_LIMIT
4582 if (wp->w_s->b_syn_slow)
4583 has_syntax = FALSE;
4584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
4586 /* Need to get the line again, a multi-line regexp may
4587 * have made it invalid. */
4588 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4589 ptr = line + v;
4590
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004591 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004593 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004594 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004595# ifdef FEAT_CONCEAL
4596 /* no concealing past the end of the line, it interferes
4597 * with line highlighting */
4598 if (c == NUL)
4599 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004600 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004601 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004602# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004604#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004605
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004606#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004607 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004608 * Only do this when there is no syntax highlighting, the
4609 * @Spell cluster is not used or the current syntax item
4610 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004611 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004612 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004613 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004614# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004615 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004616 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004617# endif
4618 if (c != 0 && (
4619# ifdef FEAT_SYN_HL
4620 !has_syntax ||
4621# endif
4622 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004623 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004624 char_u *prev_ptr, *p;
4625 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004626 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004627# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004628 if (has_mbyte)
4629 {
4630 prev_ptr = ptr - mb_l;
4631 v -= mb_l - 1;
4632 }
4633 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004634# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004635 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004636
4637 /* Use nextline[] if possible, it has the start of the
4638 * next line concatenated. */
4639 if ((prev_ptr - line) - nextlinecol >= 0)
4640 p = nextline + (prev_ptr - line) - nextlinecol;
4641 else
4642 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004643 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004644 len = spell_check(wp, p, &spell_hlf, &cap_col,
4645 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004646 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004647
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004648 /* In Insert mode only highlight a word that
4649 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004650 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004651 && (State & INSERT) != 0
4652 && wp->w_cursor.lnum == lnum
4653 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004654 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004655 && wp->w_cursor.col < (colnr_T)word_end)
4656 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004657 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004658 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004659 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004660
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004661 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004662 && (p - nextline) + len > nextline_idx)
4663 {
4664 /* Remember that the good word continues at the
4665 * start of the next line. */
4666 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004667 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004668 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004669
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004670 /* Turn index into actual attributes. */
4671 if (spell_hlf != HLF_COUNT)
4672 spell_attr = highlight_attr[spell_hlf];
4673
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004674 if (cap_col > 0)
4675 {
4676 if (p != prev_ptr
4677 && (p - nextline) + cap_col >= nextline_idx)
4678 {
4679 /* Remember that the word in the next line
4680 * must start with a capital. */
4681 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004682 cap_col = (int)((p - nextline) + cap_col
4683 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004684 }
4685 else
4686 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004687 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004688 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004689 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004690 }
4691 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004692 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004693 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004694 char_attr = hl_combine_attr(char_attr, spell_attr);
4695 else
4696 char_attr = hl_combine_attr(spell_attr, char_attr);
4697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698#endif
4699#ifdef FEAT_LINEBREAK
4700 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004701 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004703 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004704 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004706# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004707 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004708# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004709 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004711 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004713 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004714
Bram Moolenaar597a4222014-06-25 14:39:50 +02004715 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004716 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004717 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004718 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaara3650912014-11-19 13:21:57 +01004719 n_extra = (int)wp->w_buffer->b_p_ts
4720 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4721
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004722# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004723 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004724# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004726# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004727 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004728 {
4729#ifdef FEAT_CONCEAL
4730 if (c == TAB)
4731 /* See "Tab alignment" below. */
4732 FIX_FOR_BOGUSCOLS;
4733#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004734 if (!wp->w_p_list)
4735 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
4738#endif
4739
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004740 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4741 */
4742 if (wp->w_p_list
4743 && (((c == 160
4744#ifdef FEAT_MBYTE
4745 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4746#endif
4747 ) && lcs_nbsp)
4748 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4749 {
4750 c = (c == ' ') ? lcs_space : lcs_nbsp;
4751 if (area_attr == 0 && search_attr == 0)
4752 {
4753 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004754 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004755 saved_attr2 = char_attr; /* save current attr */
4756 }
4757#ifdef FEAT_MBYTE
4758 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004759 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004760 {
4761 mb_utf8 = TRUE;
4762 u8cc[0] = 0;
4763 c = 0xc0;
4764 }
4765 else
4766 mb_utf8 = FALSE;
4767#endif
4768 }
4769
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4771 {
4772 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004773 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004776 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 saved_attr2 = char_attr; /* save current attr */
4778 }
4779#ifdef FEAT_MBYTE
4780 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004781 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 {
4783 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004784 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004785 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
4787 else
4788 mb_utf8 = FALSE;
4789#endif
4790 }
4791 }
4792
4793 /*
4794 * Handling of non-printable characters.
4795 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004796 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
4798 /*
4799 * when getting a character from the file, we may have to
4800 * turn it into something else on the way to putting it
4801 * into "ScreenLines".
4802 */
4803 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4804 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004805 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004806 long vcol_adjusted = vcol; /* removed showbreak length */
4807#ifdef FEAT_LINEBREAK
4808 /* only adjust the tab_len, when at the first column
4809 * after the showbreak value was drawn */
4810 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4811 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004814 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004815 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4816
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004817#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004818 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004819#endif
4820 /* tab amount depends on current column */
4821 n_extra = tab_len;
4822#ifdef FEAT_LINEBREAK
4823 else
4824 {
4825 char_u *p;
4826 int len = n_extra;
4827 int i;
4828 int saved_nextra = n_extra;
4829
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004830#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004831 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004832 /* there are characters to conceal */
4833 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004834 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4835 */
4836 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4837 && n_extra > tab_len)
4838 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004839#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004840
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004841 /* if n_extra > 0, it gives the number of chars, to
4842 * use for a tab, else we need to calculate the width
4843 * for a tab */
4844#ifdef FEAT_MBYTE
4845 len = (tab_len * mb_char2len(lcs_tab2));
4846 if (n_extra > 0)
4847 len += n_extra - tab_len;
4848#endif
4849 c = lcs_tab1;
4850 p = alloc((unsigned)(len + 1));
4851 vim_memset(p, ' ', len);
4852 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004853 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004854 p_extra_free = p;
4855 for (i = 0; i < tab_len; i++)
4856 {
4857#ifdef FEAT_MBYTE
4858 mb_char2bytes(lcs_tab2, p);
4859 p += mb_char2len(lcs_tab2);
4860 n_extra += mb_char2len(lcs_tab2)
4861 - (saved_nextra > 0 ? 1 : 0);
4862#else
4863 p[i] = lcs_tab2;
4864#endif
4865 }
4866 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004867#ifdef FEAT_CONCEAL
4868 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4869 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004870 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004871 n_extra -= vcol_off;
4872#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004873 }
4874#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004875#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004876 {
4877 int vc_saved = vcol_off;
4878
4879 /* Tab alignment should be identical regardless of
4880 * 'conceallevel' value. So tab compensates of all
4881 * previous concealed characters, and thus resets
4882 * vcol_off and boguscols accumulated so far in the
4883 * line. Note that the tab can be longer than
4884 * 'tabstop' when there are concealed characters. */
4885 FIX_FOR_BOGUSCOLS;
4886
4887 /* Make sure, the highlighting for the tab char will be
4888 * correctly set further below (effectively reverts the
4889 * FIX_FOR_BOGSUCOLS macro */
4890 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004891 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004892 tab_len += vc_saved;
4893 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895#ifdef FEAT_MBYTE
4896 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4897#endif
4898 if (wp->w_p_list)
4899 {
4900 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004901#ifdef FEAT_LINEBREAK
4902 if (wp->w_p_lbr)
4903 c_extra = NUL; /* using p_extra from above */
4904 else
4905#endif
4906 c_extra = lcs_tab2;
4907 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004908 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 saved_attr2 = char_attr; /* save current attr */
4910#ifdef FEAT_MBYTE
4911 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004912 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
4914 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004915 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004916 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918#endif
4919 }
4920 else
4921 {
4922 c_extra = ' ';
4923 c = ' ';
4924 }
4925 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004926 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004927 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004928 || ((fromcol >= 0 || fromcol_prev >= 0)
4929 && tocol > vcol
4930 && VIsual_mode != Ctrl_V
4931 && (
4932# ifdef FEAT_RIGHTLEFT
4933 wp->w_p_rl ? (col >= 0) :
4934# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004935 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004936 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004937 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004938 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004939 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004941 /* Display a '$' after the line or highlight an extra
4942 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4944 /* For a diff line the highlighting continues after the
4945 * "$". */
4946 if (
4947# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004948 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949# ifdef LINE_ATTR
4950 &&
4951# endif
4952# endif
4953# ifdef LINE_ATTR
4954 line_attr == 0
4955# endif
4956 )
4957#endif
4958 {
4959#ifdef FEAT_VIRTUALEDIT
4960 /* In virtualedit, visual selections may extend
4961 * beyond end of line. */
4962 if (area_highlighting && virtual_active()
4963 && tocol != MAXCOL && vcol < tocol)
4964 n_extra = 0;
4965 else
4966#endif
4967 {
4968 p_extra = at_end_str;
4969 n_extra = 1;
4970 c_extra = NUL;
4971 }
4972 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004973 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004974 c = lcs_eol;
4975 else
4976 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 lcs_eol_one = -1;
4978 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004979 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004981 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 n_attr = 1;
4983 }
4984#ifdef FEAT_MBYTE
4985 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004986 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
4988 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004989 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004990 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else
4993 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4994#endif
4995 }
4996 else if (c != NUL)
4997 {
4998 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004999 if (n_extra == 0)
5000 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001#ifdef FEAT_RIGHTLEFT
5002 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5003 rl_mirror(p_extra); /* reverse "<12>" */
5004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005006#ifdef FEAT_LINEBREAK
5007 if (wp->w_p_lbr)
5008 {
5009 char_u *p;
5010
5011 c = *p_extra;
5012 p = alloc((unsigned)n_extra + 1);
5013 vim_memset(p, ' ', n_extra);
5014 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5015 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005016 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005017 p_extra_free = p_extra = p;
5018 }
5019 else
5020#endif
5021 {
5022 n_extra = byte2cells(c) - 1;
5023 c = *p_extra++;
5024 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005025 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
5027 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005028 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 saved_attr2 = char_attr; /* save current attr */
5030 }
5031#ifdef FEAT_MBYTE
5032 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5033#endif
5034 }
5035#ifdef FEAT_VIRTUALEDIT
5036 else if (VIsual_active
5037 && (VIsual_mode == Ctrl_V
5038 || VIsual_mode == 'v')
5039 && virtual_active()
5040 && tocol != MAXCOL
5041 && vcol < tocol
5042 && (
5043# ifdef FEAT_RIGHTLEFT
5044 wp->w_p_rl ? (col >= 0) :
5045# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005046 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
5048 c = ' ';
5049 --ptr; /* put it back at the NUL */
5050 }
5051#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005052#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 else if ((
5054# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005055 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005057# ifdef FEAT_TERMINAL
5058 term_attr != 0 ||
5059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 ) && (
5062# ifdef FEAT_RIGHTLEFT
5063 wp->w_p_rl ? (col >= 0) :
5064# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005065 (col
5066# ifdef FEAT_CONCEAL
5067 - boguscols
5068# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005069 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
5071 /* Highlight until the right side of the window */
5072 c = ' ';
5073 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005074
5075 /* Remember we do the char for line highlighting. */
5076 ++did_line_attr;
5077
5078 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005079 if (line_attr != 0 && char_attr == search_attr
5080 && (did_line_attr > 1
5081 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005082 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083# ifdef FEAT_DIFF
5084 if (diff_hlf == HLF_TXD)
5085 {
5086 diff_hlf = HLF_CHD;
5087 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005088 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005089 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005090 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5091 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005092 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005096# ifdef FEAT_TERMINAL
5097 if (term_attr != 0)
5098 {
5099 char_attr = term_attr;
5100 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5101 char_attr = hl_combine_attr(char_attr,
5102 HL_ATTR(HLF_CUL));
5103 }
5104# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 }
5106#endif
5107 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005108
5109#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005110 if ( wp->w_p_cole > 0
5111 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005112 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005113 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005114 && !(lnum_in_visual_area
5115 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005116 {
5117 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005118 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005119 && (syn_get_sub_char() != NUL || match_conc
5120 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005121 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005122 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005123 /* First time at this concealed item: display one
5124 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005125 if (match_conc)
5126 c = match_conc;
5127 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005128 c = syn_get_sub_char();
5129 else if (lcs_conceal != NUL)
5130 c = lcs_conceal;
5131 else
5132 c = ' ';
5133
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005134 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005135
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005136 if (n_extra > 0)
5137 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005138 vcol += n_extra;
5139 if (wp->w_p_wrap && n_extra > 0)
5140 {
5141# ifdef FEAT_RIGHTLEFT
5142 if (wp->w_p_rl)
5143 {
5144 col -= n_extra;
5145 boguscols -= n_extra;
5146 }
5147 else
5148# endif
5149 {
5150 boguscols += n_extra;
5151 col += n_extra;
5152 }
5153 }
5154 n_extra = 0;
5155 n_attr = 0;
5156 }
5157 else if (n_skip == 0)
5158 {
5159 is_concealing = TRUE;
5160 n_skip = 1;
5161 }
5162# ifdef FEAT_MBYTE
5163 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005164 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005165 {
5166 mb_utf8 = TRUE;
5167 u8cc[0] = 0;
5168 c = 0xc0;
5169 }
5170 else
5171 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5172# endif
5173 }
5174 else
5175 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005176 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005177 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005178 }
5179#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
5181
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005182#ifdef FEAT_CONCEAL
5183 /* In the cursor line and we may be concealing characters: correct
5184 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005185 if (!did_wcol && draw_state == WL_LINE
5186 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005187 && conceal_cursor_line(wp)
5188 && (int)wp->w_virtcol <= vcol + n_skip)
5189 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005190# ifdef FEAT_RIGHTLEFT
5191 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005192 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005193 else
5194# endif
5195 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005196 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005197 did_wcol = TRUE;
5198 }
5199#endif
5200
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 /* Don't override visual selection highlighting. */
5202 if (n_attr > 0
5203 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005204 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 char_attr = extra_attr;
5206
Bram Moolenaar81695252004-12-29 20:58:21 +00005207#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 /* XIM don't send preedit_start and preedit_end, but they send
5209 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5210 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005211 if (p_imst == IM_ON_THE_SPOT
5212 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005213 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 && (State & INSERT)
5215 && !p_imdisable
5216 && im_is_preediting()
5217 && draw_state == WL_LINE)
5218 {
5219 colnr_T tcol;
5220
5221 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005222 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 else
5224 tcol = preedit_end_col;
5225 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5226 {
5227 if (feedback_old_attr < 0)
5228 {
5229 feedback_col = 0;
5230 feedback_old_attr = char_attr;
5231 }
5232 char_attr = im_get_feedback_attr(feedback_col);
5233 if (char_attr < 0)
5234 char_attr = feedback_old_attr;
5235 feedback_col++;
5236 }
5237 else if (feedback_old_attr >= 0)
5238 {
5239 char_attr = feedback_old_attr;
5240 feedback_old_attr = -1;
5241 feedback_col = 0;
5242 }
5243 }
5244#endif
5245 /*
5246 * Handle the case where we are in column 0 but not on the first
5247 * character of the line and the user wants us to show us a
5248 * special character (via 'listchars' option "precedes:<char>".
5249 */
5250 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005251 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5253#ifdef FEAT_DIFF
5254 && filler_todo <= 0
5255#endif
5256 && draw_state > WL_NR
5257 && c != NUL)
5258 {
5259 c = lcs_prec;
5260 lcs_prec_todo = NUL;
5261#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005262 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5263 {
5264 /* Double-width character being overwritten by the "precedes"
5265 * character, need to fill up half the character. */
5266 c_extra = MB_FILLER_CHAR;
5267 n_extra = 1;
5268 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005269 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005272 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
5274 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005275 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005276 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 }
5278 else
5279 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5280#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005281 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
5283 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005284 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 n_attr3 = 1;
5286 }
5287 }
5288
5289 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005290 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005292 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005293#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005294 || did_line_attr == 1
5295#endif
5296 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005298#ifdef FEAT_SEARCH_EXTRA
5299 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005300
5301 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005302 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005303 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005304#endif
5305
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005306 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 * highlight match at end of line. If it's beyond the last
5308 * char on the screen, just overwrite that one (tricky!) Not
5309 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005310#ifdef FEAT_SEARCH_EXTRA
5311 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005312 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005313 prevcol_hl_flag = TRUE;
5314 else
5315 {
5316 cur = wp->w_match_head;
5317 while (cur != NULL)
5318 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005319 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005320 {
5321 prevcol_hl_flag = TRUE;
5322 break;
5323 }
5324 cur = cur->next;
5325 }
5326 }
5327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005329 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005330 && (VIsual_mode != Ctrl_V
5331 || lnum == VIsual.lnum
5332 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005333 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334#ifdef FEAT_SEARCH_EXTRA
5335 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005336 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005337# ifdef FEAT_SYN_HL
5338 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5339 && !(wp == curwin && VIsual_active))
5340# endif
5341# ifdef FEAT_DIFF
5342 && diff_hlf == (hlf_T)0
5343# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005344# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005345 && did_line_attr <= 1
5346# endif
5347 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348#endif
5349 ))
5350 {
5351 int n = 0;
5352
5353#ifdef FEAT_RIGHTLEFT
5354 if (wp->w_p_rl)
5355 {
5356 if (col < 0)
5357 n = 1;
5358 }
5359 else
5360#endif
5361 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005362 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 n = -1;
5364 }
5365 if (n != 0)
5366 {
5367 /* At the window boundary, highlight the last character
5368 * instead (better than nothing). */
5369 off += n;
5370 col += n;
5371 }
5372 else
5373 {
5374 /* Add a blank character to highlight. */
5375 ScreenLines[off] = ' ';
5376#ifdef FEAT_MBYTE
5377 if (enc_utf8)
5378 ScreenLinesUC[off] = 0;
5379#endif
5380 }
5381#ifdef FEAT_SEARCH_EXTRA
5382 if (area_attr == 0)
5383 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005384 /* Use attributes from match with highest priority among
5385 * 'search_hl' and the match list. */
5386 char_attr = search_hl.attr;
5387 cur = wp->w_match_head;
5388 shl_flag = FALSE;
5389 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005390 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005391 if (shl_flag == FALSE
5392 && ((cur != NULL
5393 && cur->priority > SEARCH_HL_PRIORITY)
5394 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005395 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005396 shl = &search_hl;
5397 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005398 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005399 else
5400 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005401 if ((ptr - line) - 1 == (long)shl->startcol
5402 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005403 char_attr = shl->attr;
5404 if (shl != &search_hl && cur != NULL)
5405 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
5408#endif
5409 ScreenAttrs[off] = char_attr;
5410#ifdef FEAT_RIGHTLEFT
5411 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005414 --off;
5415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 else
5417#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005418 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005420 ++off;
5421 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005422 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005423#ifdef FEAT_SYN_HL
5424 eol_hl_off = 1;
5425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428
Bram Moolenaar91170f82006-05-05 21:15:17 +00005429 /*
5430 * At end of the text line.
5431 */
5432 if (c == NUL)
5433 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005434#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005435 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5436 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005437 {
5438 /* highlight last char after line */
5439 --col;
5440 --off;
5441 --vcol;
5442 }
5443
Bram Moolenaar1a384422010-07-14 19:53:30 +02005444 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005445 if (wp->w_p_wrap)
5446 v = wp->w_skipcol;
5447 else
5448 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005449
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005450 /* check if line ends before left margin */
5451 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005452 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005453#ifdef FEAT_CONCEAL
5454 /* Get rid of the boguscols now, we want to draw until the right
5455 * edge for 'cursorcolumn'. */
5456 col -= boguscols;
5457 boguscols = 0;
5458#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005459
5460 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005461 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005462
5463 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005464 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5465 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005466 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005467 && lnum != wp->w_cursor.lnum)
5468 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005469# ifdef FEAT_RIGHTLEFT
5470 && !wp->w_p_rl
5471# endif
5472 )
5473 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005474 int rightmost_vcol = 0;
5475 int i;
5476
5477 if (wp->w_p_cuc)
5478 rightmost_vcol = wp->w_virtcol;
5479 if (draw_color_col)
5480 /* determine rightmost colorcolumn to possibly draw */
5481 for (i = 0; color_cols[i] >= 0; ++i)
5482 if (rightmost_vcol < color_cols[i])
5483 rightmost_vcol = color_cols[i];
5484
Bram Moolenaar02631462017-09-22 15:20:32 +02005485 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005486 {
5487 ScreenLines[off] = ' ';
5488#ifdef FEAT_MBYTE
5489 if (enc_utf8)
5490 ScreenLinesUC[off] = 0;
5491#endif
5492 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005493 if (draw_color_col)
5494 draw_color_col = advance_color_col(VCOL_HLC,
5495 &color_cols);
5496
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005497 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005498 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005499 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005500 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005501 else
5502 ScreenAttrs[off++] = 0;
5503
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005504 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005505 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005506
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005507 ++vcol;
5508 }
5509 }
5510#endif
5511
Bram Moolenaar53f81742017-09-22 14:35:51 +02005512 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005513 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 row++;
5515
5516 /*
5517 * Update w_cline_height and w_cline_folded if the cursor line was
5518 * updated (saves a call to plines() later).
5519 */
5520 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5521 {
5522 curwin->w_cline_row = startrow;
5523 curwin->w_cline_height = row - startrow;
5524#ifdef FEAT_FOLDING
5525 curwin->w_cline_folded = FALSE;
5526#endif
5527 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5528 }
5529
5530 break;
5531 }
5532
5533 /* line continues beyond line end */
5534 if (lcs_ext
5535 && !wp->w_p_wrap
5536#ifdef FEAT_DIFF
5537 && filler_todo <= 0
5538#endif
5539 && (
5540#ifdef FEAT_RIGHTLEFT
5541 wp->w_p_rl ? col == 0 :
5542#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005543 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005545 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5547 {
5548 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005549 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550#ifdef FEAT_MBYTE
5551 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005552 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 {
5554 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005555 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005556 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558 else
5559 mb_utf8 = FALSE;
5560#endif
5561 }
5562
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005563#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005564 /* advance to the next 'colorcolumn' */
5565 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005566 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005567
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005568 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005569 * highlight the cursor position itself.
5570 * Also highlight the 'colorcolumn' if it is different than
5571 * 'cursorcolumn' */
5572 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005573 if (draw_state == WL_LINE && !lnum_in_visual_area
5574 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005575 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005576 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005577 && lnum != wp->w_cursor.lnum)
5578 {
5579 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005580 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005581 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005582 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005583 {
5584 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005585 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005586 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005587 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005588#endif
5589
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 /*
5591 * Store character to be displayed.
5592 * Skip characters that are left of the screen for 'nowrap'.
5593 */
5594 vcol_prev = vcol;
5595 if (draw_state < WL_LINE || n_skip <= 0)
5596 {
5597 /*
5598 * Store the character.
5599 */
5600#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5601 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5602 {
5603 /* A double-wide character is: put first halve in left cell. */
5604 --off;
5605 --col;
5606 }
5607#endif
5608 ScreenLines[off] = c;
5609#ifdef FEAT_MBYTE
5610 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005611 {
5612 if ((mb_c & 0xff00) == 0x8e00)
5613 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 else if (enc_utf8)
5617 {
5618 if (mb_utf8)
5619 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005620 int i;
5621
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005623 if ((c & 0xff) == 0)
5624 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005625 for (i = 0; i < Screen_mco; ++i)
5626 {
5627 ScreenLinesC[i][off] = u8cc[i];
5628 if (u8cc[i] == 0)
5629 break;
5630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 }
5632 else
5633 ScreenLinesUC[off] = 0;
5634 }
5635 if (multi_attr)
5636 {
5637 ScreenAttrs[off] = multi_attr;
5638 multi_attr = 0;
5639 }
5640 else
5641#endif
5642 ScreenAttrs[off] = char_attr;
5643
5644#ifdef FEAT_MBYTE
5645 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5646 {
5647 /* Need to fill two screen columns. */
5648 ++off;
5649 ++col;
5650 if (enc_utf8)
5651 /* UTF-8: Put a 0 in the second screen char. */
5652 ScreenLines[off] = 0;
5653 else
5654 /* DBCS: Put second byte in the second screen char. */
5655 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005656 if (draw_state > WL_NR
5657#ifdef FEAT_DIFF
5658 && filler_todo <= 0
5659#endif
5660 )
5661 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 /* When "tocol" is halfway a character, set it to the end of
5663 * the character, otherwise highlighting won't stop. */
5664 if (tocol == vcol)
5665 ++tocol;
5666#ifdef FEAT_RIGHTLEFT
5667 if (wp->w_p_rl)
5668 {
5669 /* now it's time to backup one cell */
5670 --off;
5671 --col;
5672 }
5673#endif
5674 }
5675#endif
5676#ifdef FEAT_RIGHTLEFT
5677 if (wp->w_p_rl)
5678 {
5679 --off;
5680 --col;
5681 }
5682 else
5683#endif
5684 {
5685 ++off;
5686 ++col;
5687 }
5688 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005689#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005690 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005691 {
5692 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005693 ++vcol_off;
5694 if (n_extra > 0)
5695 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005696 if (wp->w_p_wrap)
5697 {
5698 /*
5699 * Special voodoo required if 'wrap' is on.
5700 *
5701 * Advance the column indicator to force the line
5702 * drawing to wrap early. This will make the line
5703 * take up the same screen space when parts are concealed,
5704 * so that cursor line computations aren't messed up.
5705 *
5706 * To avoid the fictitious advance of 'col' causing
5707 * trailing junk to be written out of the screen line
5708 * we are building, 'boguscols' keeps track of the number
5709 * of bad columns we have advanced.
5710 */
5711 if (n_extra > 0)
5712 {
5713 vcol += n_extra;
5714# ifdef FEAT_RIGHTLEFT
5715 if (wp->w_p_rl)
5716 {
5717 col -= n_extra;
5718 boguscols -= n_extra;
5719 }
5720 else
5721# endif
5722 {
5723 col += n_extra;
5724 boguscols += n_extra;
5725 }
5726 n_extra = 0;
5727 n_attr = 0;
5728 }
5729
5730
5731# ifdef FEAT_MBYTE
5732 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5733 {
5734 /* Need to fill two screen columns. */
5735# ifdef FEAT_RIGHTLEFT
5736 if (wp->w_p_rl)
5737 {
5738 --boguscols;
5739 --col;
5740 }
5741 else
5742# endif
5743 {
5744 ++boguscols;
5745 ++col;
5746 }
5747 }
5748# endif
5749
5750# ifdef FEAT_RIGHTLEFT
5751 if (wp->w_p_rl)
5752 {
5753 --boguscols;
5754 --col;
5755 }
5756 else
5757# endif
5758 {
5759 ++boguscols;
5760 ++col;
5761 }
5762 }
5763 else
5764 {
5765 if (n_extra > 0)
5766 {
5767 vcol += n_extra;
5768 n_extra = 0;
5769 n_attr = 0;
5770 }
5771 }
5772
5773 }
5774#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 else
5776 --n_skip;
5777
Bram Moolenaar64486672010-05-16 15:46:46 +02005778 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5779 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005780 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781#ifdef FEAT_DIFF
5782 && filler_todo <= 0
5783#endif
5784 )
5785 ++vcol;
5786
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005787#ifdef FEAT_SYN_HL
5788 if (vcol_save_attr >= 0)
5789 char_attr = vcol_save_attr;
5790#endif
5791
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 /* restore attributes after "predeces" in 'listchars' */
5793 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5794 char_attr = saved_attr3;
5795
5796 /* restore attributes after last 'listchars' or 'number' char */
5797 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5798 char_attr = saved_attr2;
5799
5800 /*
5801 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005802 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 */
5804 if ((
5805#ifdef FEAT_RIGHTLEFT
5806 wp->w_p_rl ? (col < 0) :
5807#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005808 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 && (*ptr != NUL
5810#ifdef FEAT_DIFF
5811 || filler_todo > 0
5812#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005813 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5815 )
5816 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005817#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005818 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005819 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005820 boguscols = 0;
5821#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005822 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005823 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 ++row;
5826 ++screen_row;
5827
5828 /* When not wrapping and finished diff lines, or when displayed
5829 * '$' and highlighting until last column, break here. */
5830 if ((!wp->w_p_wrap
5831#ifdef FEAT_DIFF
5832 && filler_todo <= 0
5833#endif
5834 ) || lcs_eol_one == -1)
5835 break;
5836
5837 /* When the window is too narrow draw all "@" lines. */
5838 if (draw_state != WL_LINE
5839#ifdef FEAT_DIFF
5840 && filler_todo <= 0
5841#endif
5842 )
5843 {
5844 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 row = endrow;
5847 }
5848
5849 /* When line got too long for screen break here. */
5850 if (row == endrow)
5851 {
5852 ++row;
5853 break;
5854 }
5855
5856 if (screen_cur_row == screen_row - 1
5857#ifdef FEAT_DIFF
5858 && filler_todo <= 0
5859#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005860 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 {
5862 /* Remember that the line wraps, used for modeless copy. */
5863 LineWraps[screen_row - 1] = TRUE;
5864
5865 /*
5866 * Special trick to make copy/paste of wrapped lines work with
5867 * xterm/screen: write an extra character beyond the end of
5868 * the line. This will work with all terminal types
5869 * (regardless of the xn,am settings).
5870 * Only do this on a fast tty.
5871 * Only do this if the cursor is on the current line
5872 * (something has been written in it).
5873 * Don't do this for the GUI.
5874 * Don't do this for double-width characters.
5875 * Don't do this for a window not at the right screen border.
5876 */
5877 if (p_tf
5878#ifdef FEAT_GUI
5879 && !gui.in_use
5880#endif
5881#ifdef FEAT_MBYTE
5882 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005883 && ((*mb_off2cells)(LineOffset[screen_row],
5884 LineOffset[screen_row] + screen_Columns)
5885 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005887 + (int)Columns - 2,
5888 LineOffset[screen_row] + screen_Columns)
5889 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#endif
5891 )
5892 {
5893 /* First make sure we are at the end of the screen line,
5894 * then output the same character again to let the
5895 * terminal know about the wrap. If the terminal doesn't
5896 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005897 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 screen_char(LineOffset[screen_row - 1]
5899 + (unsigned)Columns - 1,
5900 screen_row - 1, (int)(Columns - 1));
5901
5902#ifdef FEAT_MBYTE
5903 /* When there is a multi-byte character, just output a
5904 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005905 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5906 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 out_char(' ');
5908 else
5909#endif
5910 out_char(ScreenLines[LineOffset[screen_row - 1]
5911 + (Columns - 1)]);
5912 /* force a redraw of the first char on the next line */
5913 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5914 screen_start(); /* don't know where cursor is now */
5915 }
5916 }
5917
5918 col = 0;
5919 off = (unsigned)(current_ScreenLine - ScreenLines);
5920#ifdef FEAT_RIGHTLEFT
5921 if (wp->w_p_rl)
5922 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005923 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 off += col;
5925 }
5926#endif
5927
5928 /* reset the drawing state for the start of a wrapped line */
5929 draw_state = WL_START;
5930 saved_n_extra = n_extra;
5931 saved_p_extra = p_extra;
5932 saved_c_extra = c_extra;
5933 saved_char_attr = char_attr;
5934 n_extra = 0;
5935 lcs_prec_todo = lcs_prec;
5936#ifdef FEAT_LINEBREAK
5937# ifdef FEAT_DIFF
5938 if (filler_todo <= 0)
5939# endif
5940 need_showbreak = TRUE;
5941#endif
5942#ifdef FEAT_DIFF
5943 --filler_todo;
5944 /* When the filler lines are actually below the last line of the
5945 * file, don't draw the line itself, break here. */
5946 if (filler_todo == 0 && wp->w_botfill)
5947 break;
5948#endif
5949 }
5950
5951 } /* for every character in the line */
5952
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005953#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005954 /* After an empty line check first word for capital. */
5955 if (*skipwhite(line) == NUL)
5956 {
5957 capcol_lnum = lnum + 1;
5958 cap_col = 0;
5959 }
5960#endif
5961
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005962 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 return row;
5964}
5965
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005966#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005967static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005968
5969/*
5970 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005971 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005972 */
5973 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005974comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005975{
5976 int i;
5977
5978 for (i = 0; i < Screen_mco; ++i)
5979 {
5980 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5981 return TRUE;
5982 if (ScreenLinesC[i][off_from] == 0)
5983 break;
5984 }
5985 return FALSE;
5986}
5987#endif
5988
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989/*
5990 * Check whether the given character needs redrawing:
5991 * - the (first byte of the) character is different
5992 * - the attributes are different
5993 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005994 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 */
5996 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005997char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998{
5999 if (cols > 0
6000 && ((ScreenLines[off_from] != ScreenLines[off_to]
6001 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6002
6003#ifdef FEAT_MBYTE
6004 || (enc_dbcs != 0
6005 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6006 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6007 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6008 : (cols > 1 && ScreenLines[off_from + 1]
6009 != ScreenLines[off_to + 1])))
6010 || (enc_utf8
6011 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6012 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006013 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006014 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6015 && ScreenLines[off_from + 1]
6016 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017#endif
6018 ))
6019 return TRUE;
6020 return FALSE;
6021}
6022
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006023#if defined(FEAT_TERMINAL) || defined(PROTO)
6024/*
6025 * Return the index in ScreenLines[] for the current screen line.
6026 */
6027 int
6028screen_get_current_line_off()
6029{
6030 return (int)(current_ScreenLine - ScreenLines);
6031}
6032#endif
6033
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034/*
6035 * Move one "cooked" screen line to the screen, but only the characters that
6036 * have actually changed. Handle insert/delete character.
6037 * "coloff" gives the first column on the screen for this line.
6038 * "endcol" gives the columns where valid characters are.
6039 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6040 * needs to be cleared, negative otherwise.
6041 * "rlflag" is TRUE in a rightleft window:
6042 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6043 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6044 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006045 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006046screen_line(
6047 int row,
6048 int coloff,
6049 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006050 int clear_width,
6051 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052{
6053 unsigned off_from;
6054 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006055#ifdef FEAT_MBYTE
6056 unsigned max_off_from;
6057 unsigned max_off_to;
6058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 int force = FALSE; /* force update rest of the line */
6062 int redraw_this /* bool: does character need redraw? */
6063#ifdef FEAT_GUI
6064 = TRUE /* For GUI when while-loop empty */
6065#endif
6066 ;
6067 int redraw_next; /* redraw_this for next character */
6068#ifdef FEAT_MBYTE
6069 int clear_next = FALSE;
6070 int char_cells; /* 1: normal char */
6071 /* 2: occupies two display cells */
6072# define CHAR_CELLS char_cells
6073#else
6074# define CHAR_CELLS 1
6075#endif
6076
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006077 /* Check for illegal row and col, just in case. */
6078 if (row >= Rows)
6079 row = Rows - 1;
6080 if (endcol > Columns)
6081 endcol = Columns;
6082
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083# ifdef FEAT_CLIPBOARD
6084 clip_may_clear_selection(row, row);
6085# endif
6086
6087 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6088 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006089#ifdef FEAT_MBYTE
6090 max_off_from = off_from + screen_Columns;
6091 max_off_to = LineOffset[row] + screen_Columns;
6092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093
6094#ifdef FEAT_RIGHTLEFT
6095 if (rlflag)
6096 {
6097 /* Clear rest first, because it's left of the text. */
6098 if (clear_width > 0)
6099 {
6100 while (col <= endcol && ScreenLines[off_to] == ' '
6101 && ScreenAttrs[off_to] == 0
6102# ifdef FEAT_MBYTE
6103 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6104# endif
6105 )
6106 {
6107 ++off_to;
6108 ++col;
6109 }
6110 if (col <= endcol)
6111 screen_fill(row, row + 1, col + coloff,
6112 endcol + coloff + 1, ' ', ' ', 0);
6113 }
6114 col = endcol + 1;
6115 off_to = LineOffset[row] + col + coloff;
6116 off_from += col;
6117 endcol = (clear_width > 0 ? clear_width : -clear_width);
6118 }
6119#endif /* FEAT_RIGHTLEFT */
6120
6121 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6122
6123 while (col < endcol)
6124 {
6125#ifdef FEAT_MBYTE
6126 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006127 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 else
6129 char_cells = 1;
6130#endif
6131
6132 redraw_this = redraw_next;
6133 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6134 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6135
6136#ifdef FEAT_GUI
6137 /* If the next character was bold, then redraw the current character to
6138 * remove any pixels that might have spilt over into us. This only
6139 * happens in the GUI.
6140 */
6141 if (redraw_next && gui.in_use)
6142 {
6143 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006144 if (hl > HL_ALL)
6145 hl = syn_attr2attr(hl);
6146 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 redraw_this = TRUE;
6148 }
6149#endif
6150
6151 if (redraw_this)
6152 {
6153 /*
6154 * Special handling when 'xs' termcap flag set (hpterm):
6155 * Attributes for characters are stored at the position where the
6156 * cursor is when writing the highlighting code. The
6157 * start-highlighting code must be written with the cursor on the
6158 * first highlighted character. The stop-highlighting code must
6159 * be written with the cursor just after the last highlighted
6160 * character.
6161 * Overwriting a character doesn't remove it's highlighting. Need
6162 * to clear the rest of the line, and force redrawing it
6163 * completely.
6164 */
6165 if ( p_wiv
6166 && !force
6167#ifdef FEAT_GUI
6168 && !gui.in_use
6169#endif
6170 && ScreenAttrs[off_to] != 0
6171 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6172 {
6173 /*
6174 * Need to remove highlighting attributes here.
6175 */
6176 windgoto(row, col + coloff);
6177 out_str(T_CE); /* clear rest of this screen line */
6178 screen_start(); /* don't know where cursor is now */
6179 force = TRUE; /* force redraw of rest of the line */
6180 redraw_next = TRUE; /* or else next char would miss out */
6181
6182 /*
6183 * If the previous character was highlighted, need to stop
6184 * highlighting at this character.
6185 */
6186 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6187 {
6188 screen_attr = ScreenAttrs[off_to - 1];
6189 term_windgoto(row, col + coloff);
6190 screen_stop_highlight();
6191 }
6192 else
6193 screen_attr = 0; /* highlighting has stopped */
6194 }
6195#ifdef FEAT_MBYTE
6196 if (enc_dbcs != 0)
6197 {
6198 /* Check if overwriting a double-byte with a single-byte or
6199 * the other way around requires another character to be
6200 * redrawn. For UTF-8 this isn't needed, because comparing
6201 * ScreenLinesUC[] is sufficient. */
6202 if (char_cells == 1
6203 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006204 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 {
6206 /* Writing a single-cell character over a double-cell
6207 * character: need to redraw the next cell. */
6208 ScreenLines[off_to + 1] = 0;
6209 redraw_next = TRUE;
6210 }
6211 else if (char_cells == 2
6212 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006213 && (*mb_off2cells)(off_to, max_off_to) == 1
6214 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 {
6216 /* Writing the second half of a double-cell character over
6217 * a double-cell character: need to redraw the second
6218 * cell. */
6219 ScreenLines[off_to + 2] = 0;
6220 redraw_next = TRUE;
6221 }
6222
6223 if (enc_dbcs == DBCS_JPNU)
6224 ScreenLines2[off_to] = ScreenLines2[off_from];
6225 }
6226 /* When writing a single-width character over a double-width
6227 * character and at the end of the redrawn text, need to clear out
6228 * the right halve of the old character.
6229 * Also required when writing the right halve of a double-width
6230 * char over the left halve of an existing one. */
6231 if (has_mbyte && col + char_cells == endcol
6232 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006233 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006235 && (*mb_off2cells)(off_to, max_off_to) == 1
6236 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 clear_next = TRUE;
6238#endif
6239
6240 ScreenLines[off_to] = ScreenLines[off_from];
6241#ifdef FEAT_MBYTE
6242 if (enc_utf8)
6243 {
6244 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6245 if (ScreenLinesUC[off_from] != 0)
6246 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006247 int i;
6248
6249 for (i = 0; i < Screen_mco; ++i)
6250 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 }
6252 }
6253 if (char_cells == 2)
6254 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6255#endif
6256
6257#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006258 /* The bold trick makes a single column of pixels appear in the
6259 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 * character should be redrawn too. This happens for our own GUI
6261 * and for some xterms. */
6262 if (
6263# ifdef FEAT_GUI
6264 gui.in_use
6265# endif
6266# if defined(FEAT_GUI) && defined(UNIX)
6267 ||
6268# endif
6269# ifdef UNIX
6270 term_is_xterm
6271# endif
6272 )
6273 {
6274 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006275 if (hl > HL_ALL)
6276 hl = syn_attr2attr(hl);
6277 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 redraw_next = TRUE;
6279 }
6280#endif
6281 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6282#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006283 /* For simplicity set the attributes of second half of a
6284 * double-wide character equal to the first half. */
6285 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006287
6288 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 else
6291#endif
6292 screen_char(off_to, row, col + coloff);
6293 }
6294 else if ( p_wiv
6295#ifdef FEAT_GUI
6296 && !gui.in_use
6297#endif
6298 && col + coloff > 0)
6299 {
6300 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6301 {
6302 /*
6303 * Don't output stop-highlight when moving the cursor, it will
6304 * stop the highlighting when it should continue.
6305 */
6306 screen_attr = 0;
6307 }
6308 else if (screen_attr != 0)
6309 screen_stop_highlight();
6310 }
6311
6312 off_to += CHAR_CELLS;
6313 off_from += CHAR_CELLS;
6314 col += CHAR_CELLS;
6315 }
6316
6317#ifdef FEAT_MBYTE
6318 if (clear_next)
6319 {
6320 /* Clear the second half of a double-wide character of which the left
6321 * half was overwritten with a single-wide character. */
6322 ScreenLines[off_to] = ' ';
6323 if (enc_utf8)
6324 ScreenLinesUC[off_to] = 0;
6325 screen_char(off_to, row, col + coloff);
6326 }
6327#endif
6328
6329 if (clear_width > 0
6330#ifdef FEAT_RIGHTLEFT
6331 && !rlflag
6332#endif
6333 )
6334 {
6335#ifdef FEAT_GUI
6336 int startCol = col;
6337#endif
6338
6339 /* blank out the rest of the line */
6340 while (col < clear_width && ScreenLines[off_to] == ' '
6341 && ScreenAttrs[off_to] == 0
6342#ifdef FEAT_MBYTE
6343 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6344#endif
6345 )
6346 {
6347 ++off_to;
6348 ++col;
6349 }
6350 if (col < clear_width)
6351 {
6352#ifdef FEAT_GUI
6353 /*
6354 * In the GUI, clearing the rest of the line may leave pixels
6355 * behind if the first character cleared was bold. Some bold
6356 * fonts spill over the left. In this case we redraw the previous
6357 * character too. If we didn't skip any blanks above, then we
6358 * only redraw if the character wasn't already redrawn anyway.
6359 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006360 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 {
6362 hl = ScreenAttrs[off_to];
6363 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006364 {
6365 int prev_cells = 1;
6366# ifdef FEAT_MBYTE
6367 if (enc_utf8)
6368 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6369 * that its width is 2. */
6370 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6371 else if (enc_dbcs != 0)
6372 {
6373 /* find previous character by counting from first
6374 * column and get its width. */
6375 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006376 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006377
6378 while (off < off_to)
6379 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006380 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006381 off += prev_cells;
6382 }
6383 }
6384
6385 if (enc_dbcs != 0 && prev_cells > 1)
6386 screen_char_2(off_to - prev_cells, row,
6387 col + coloff - prev_cells);
6388 else
6389# endif
6390 screen_char(off_to - prev_cells, row,
6391 col + coloff - prev_cells);
6392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 }
6394#endif
6395 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6396 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 off_to += clear_width - col;
6398 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400 }
6401
6402 if (clear_width > 0)
6403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 /* For a window that's left of another, draw the separator char. */
6405 if (col + coloff < Columns)
6406 {
6407 int c;
6408
6409 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006410 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006411#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006412 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6413 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 || ScreenAttrs[off_to] != hl)
6416 {
6417 ScreenLines[off_to] = c;
6418 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006419#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 if (enc_utf8)
6421 {
6422 if (c >= 0x80)
6423 {
6424 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006425 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 }
6427 else
6428 ScreenLinesUC[off_to] = 0;
6429 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 screen_char(off_to, row, col + coloff);
6432 }
6433 }
6434 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 LineWraps[row] = FALSE;
6436 }
6437}
6438
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006439#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006441 * Mirror text "str" for right-left displaying.
6442 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006444 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006445rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447 char_u *p1, *p2;
6448 int t;
6449
6450 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6451 {
6452 t = *p1;
6453 *p1 = *p2;
6454 *p2 = t;
6455 }
6456}
6457#endif
6458
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459/*
6460 * mark all status lines for redraw; used after first :cd
6461 */
6462 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006463status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
6465 win_T *wp;
6466
Bram Moolenaar29323592016-07-24 22:04:11 +02006467 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 if (wp->w_status_height)
6469 {
6470 wp->w_redr_status = TRUE;
6471 redraw_later(VALID);
6472 }
6473}
6474
6475/*
6476 * mark all status lines of the current buffer for redraw
6477 */
6478 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006479status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480{
6481 win_T *wp;
6482
Bram Moolenaar29323592016-07-24 22:04:11 +02006483 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6485 {
6486 wp->w_redr_status = TRUE;
6487 redraw_later(VALID);
6488 }
6489}
6490
6491/*
6492 * Redraw all status lines that need to be redrawn.
6493 */
6494 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006495redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
6497 win_T *wp;
6498
Bram Moolenaar29323592016-07-24 22:04:11 +02006499 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 if (wp->w_redr_status)
6501 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006502 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006503 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
Bram Moolenaar4033c552017-09-16 20:54:51 +02006506#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507/*
6508 * Redraw all status lines at the bottom of frame "frp".
6509 */
6510 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006511win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512{
6513 if (frp->fr_layout == FR_LEAF)
6514 frp->fr_win->w_redr_status = TRUE;
6515 else if (frp->fr_layout == FR_ROW)
6516 {
6517 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6518 win_redraw_last_status(frp);
6519 }
6520 else /* frp->fr_layout == FR_COL */
6521 {
6522 frp = frp->fr_child;
6523 while (frp->fr_next != NULL)
6524 frp = frp->fr_next;
6525 win_redraw_last_status(frp);
6526 }
6527}
6528#endif
6529
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530/*
6531 * Draw the verticap separator right of window "wp" starting with line "row".
6532 */
6533 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006534draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535{
6536 int hl;
6537 int c;
6538
6539 if (wp->w_vsep_width)
6540 {
6541 /* draw the vertical separator right of this window */
6542 c = fillchar_vsep(&hl);
6543 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6544 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6545 c, ' ', hl);
6546 }
6547}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006550static int status_match_len(expand_T *xp, char_u *s);
6551static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552
6553/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006554 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 */
6556 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006557status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558{
6559 int len = 0;
6560
6561#ifdef FEAT_MENU
6562 int emenu = (xp->xp_context == EXPAND_MENUS
6563 || xp->xp_context == EXPAND_MENUNAMES);
6564
6565 /* Check for menu separators - replace with '|'. */
6566 if (emenu && menu_is_separator(s))
6567 return 1;
6568#endif
6569
6570 while (*s != NUL)
6571 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006572 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006573 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006574 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 }
6576
6577 return len;
6578}
6579
6580/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006581 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006582 * These are backslashes used for escaping. Do show backslashes in help tags.
6583 */
6584 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006585skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006586{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006587 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006588#ifdef FEAT_MENU
6589 || ((xp->xp_context == EXPAND_MENUS
6590 || xp->xp_context == EXPAND_MENUNAMES)
6591 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6592#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006593 )
6594 {
6595#ifndef BACKSLASH_IN_FILENAME
6596 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6597 return 2;
6598#endif
6599 return 1;
6600 }
6601 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006602}
6603
6604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 * Show wildchar matches in the status line.
6606 * Show at least the "match" item.
6607 * We start at item 'first_match' in the list and show all matches that fit.
6608 *
6609 * If inversion is possible we use it. Else '=' characters are used.
6610 */
6611 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006612win_redr_status_matches(
6613 expand_T *xp,
6614 int num_matches,
6615 char_u **matches, /* list of matches */
6616 int match,
6617 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618{
6619#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6620 int row;
6621 char_u *buf;
6622 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006623 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 int fillchar;
6625 int attr;
6626 int i;
6627 int highlight = TRUE;
6628 char_u *selstart = NULL;
6629 int selstart_col = 0;
6630 char_u *selend = NULL;
6631 static int first_match = 0;
6632 int add_left = FALSE;
6633 char_u *s;
6634#ifdef FEAT_MENU
6635 int emenu;
6636#endif
6637#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6638 int l;
6639#endif
6640
6641 if (matches == NULL) /* interrupted completion? */
6642 return;
6643
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006644#ifdef FEAT_MBYTE
6645 if (has_mbyte)
6646 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6647 else
6648#endif
6649 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 if (buf == NULL)
6651 return;
6652
6653 if (match == -1) /* don't show match but original text */
6654 {
6655 match = 0;
6656 highlight = FALSE;
6657 }
6658 /* count 1 for the ending ">" */
6659 clen = status_match_len(xp, L_MATCH(match)) + 3;
6660 if (match == 0)
6661 first_match = 0;
6662 else if (match < first_match)
6663 {
6664 /* jumping left, as far as we can go */
6665 first_match = match;
6666 add_left = TRUE;
6667 }
6668 else
6669 {
6670 /* check if match fits on the screen */
6671 for (i = first_match; i < match; ++i)
6672 clen += status_match_len(xp, L_MATCH(i)) + 2;
6673 if (first_match > 0)
6674 clen += 2;
6675 /* jumping right, put match at the left */
6676 if ((long)clen > Columns)
6677 {
6678 first_match = match;
6679 /* if showing the last match, we can add some on the left */
6680 clen = 2;
6681 for (i = match; i < num_matches; ++i)
6682 {
6683 clen += status_match_len(xp, L_MATCH(i)) + 2;
6684 if ((long)clen >= Columns)
6685 break;
6686 }
6687 if (i == num_matches)
6688 add_left = TRUE;
6689 }
6690 }
6691 if (add_left)
6692 while (first_match > 0)
6693 {
6694 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6695 if ((long)clen >= Columns)
6696 break;
6697 --first_match;
6698 }
6699
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006700 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
6702 if (first_match == 0)
6703 {
6704 *buf = NUL;
6705 len = 0;
6706 }
6707 else
6708 {
6709 STRCPY(buf, "< ");
6710 len = 2;
6711 }
6712 clen = len;
6713
6714 i = first_match;
6715 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6716 {
6717 if (i == match)
6718 {
6719 selstart = buf + len;
6720 selstart_col = clen;
6721 }
6722
6723 s = L_MATCH(i);
6724 /* Check for menu separators - replace with '|' */
6725#ifdef FEAT_MENU
6726 emenu = (xp->xp_context == EXPAND_MENUS
6727 || xp->xp_context == EXPAND_MENUNAMES);
6728 if (emenu && menu_is_separator(s))
6729 {
6730 STRCPY(buf + len, transchar('|'));
6731 l = (int)STRLEN(buf + len);
6732 len += l;
6733 clen += l;
6734 }
6735 else
6736#endif
6737 for ( ; *s != NUL; ++s)
6738 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006739 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 clen += ptr2cells(s);
6741#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006742 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 {
6744 STRNCPY(buf + len, s, l);
6745 s += l - 1;
6746 len += l;
6747 }
6748 else
6749#endif
6750 {
6751 STRCPY(buf + len, transchar_byte(*s));
6752 len += (int)STRLEN(buf + len);
6753 }
6754 }
6755 if (i == match)
6756 selend = buf + len;
6757
6758 *(buf + len++) = ' ';
6759 *(buf + len++) = ' ';
6760 clen += 2;
6761 if (++i == num_matches)
6762 break;
6763 }
6764
6765 if (i != num_matches)
6766 {
6767 *(buf + len++) = '>';
6768 ++clen;
6769 }
6770
6771 buf[len] = NUL;
6772
6773 row = cmdline_row - 1;
6774 if (row >= 0)
6775 {
6776 if (wild_menu_showing == 0)
6777 {
6778 if (msg_scrolled > 0)
6779 {
6780 /* Put the wildmenu just above the command line. If there is
6781 * no room, scroll the screen one line up. */
6782 if (cmdline_row == Rows - 1)
6783 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006784 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 ++msg_scrolled;
6786 }
6787 else
6788 {
6789 ++cmdline_row;
6790 ++row;
6791 }
6792 wild_menu_showing = WM_SCROLLED;
6793 }
6794 else
6795 {
6796 /* Create status line if needed by setting 'laststatus' to 2.
6797 * Set 'winminheight' to zero to avoid that the window is
6798 * resized. */
6799 if (lastwin->w_status_height == 0)
6800 {
6801 save_p_ls = p_ls;
6802 save_p_wmh = p_wmh;
6803 p_ls = 2;
6804 p_wmh = 0;
6805 last_status(FALSE);
6806 }
6807 wild_menu_showing = WM_SHOWN;
6808 }
6809 }
6810
6811 screen_puts(buf, row, 0, attr);
6812 if (selstart != NULL && highlight)
6813 {
6814 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006815 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 }
6817
6818 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6819 }
6820
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 vim_free(buf);
6823}
6824#endif
6825
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826/*
6827 * Redraw the status line of window wp.
6828 *
6829 * If inversion is possible we use it. Else '=' characters are used.
6830 */
6831 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006832win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833{
6834 int row;
6835 char_u *p;
6836 int len;
6837 int fillchar;
6838 int attr;
6839 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006840 static int busy = FALSE;
6841
6842 /* It's possible to get here recursively when 'statusline' (indirectly)
6843 * invokes ":redrawstatus". Simply ignore the call then. */
6844 if (busy)
6845 return;
6846 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
6848 wp->w_redr_status = FALSE;
6849 if (wp->w_status_height == 0)
6850 {
6851 /* no status line, can only be last window */
6852 redraw_cmdline = TRUE;
6853 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006854 else if (!redrawing()
6855#ifdef FEAT_INS_EXPAND
6856 /* don't update status line when popup menu is visible and may be
6857 * drawn over it */
6858 || pum_visible()
6859#endif
6860 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 {
6862 /* Don't redraw right now, do it later. */
6863 wp->w_redr_status = TRUE;
6864 }
6865#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006866 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 {
6868 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006869 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 }
6871#endif
6872 else
6873 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006874 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006876 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 p = NameBuff;
6878 len = (int)STRLEN(p);
6879
Bram Moolenaard85f2712017-07-28 21:51:57 +02006880 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881#ifdef FEAT_QUICKFIX
6882 || wp->w_p_pvw
6883#endif
6884 || bufIsChanged(wp->w_buffer)
6885 || wp->w_buffer->b_p_ro)
6886 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006887 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006889 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 len += (int)STRLEN(p + len);
6891 }
6892#ifdef FEAT_QUICKFIX
6893 if (wp->w_p_pvw)
6894 {
6895 STRCPY(p + len, _("[Preview]"));
6896 len += (int)STRLEN(p + len);
6897 }
6898#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006899 if (bufIsChanged(wp->w_buffer)
6900#ifdef FEAT_TERMINAL
6901 && !bt_terminal(wp->w_buffer)
6902#endif
6903 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {
6905 STRCPY(p + len, "[+]");
6906 len += 3;
6907 }
6908 if (wp->w_buffer->b_p_ro)
6909 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006910 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006911 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 }
6913
Bram Moolenaar02631462017-09-22 15:20:32 +02006914 this_ru_col = ru_col - (Columns - wp->w_width);
6915 if (this_ru_col < (wp->w_width + 1) / 2)
6916 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 if (this_ru_col <= 1)
6918 {
6919 p = (char_u *)"<"; /* No room for file name! */
6920 len = 1;
6921 }
6922 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923#ifdef FEAT_MBYTE
6924 if (has_mbyte)
6925 {
6926 int clen = 0, i;
6927
6928 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006929 clen = mb_string2cells(p, -1);
6930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 /* Find first character that will fit.
6932 * Going from start to end is much faster for DBCS. */
6933 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006934 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 clen -= (*mb_ptr2cells)(p + i);
6936 len = clen;
6937 if (i > 0)
6938 {
6939 p = p + i - 1;
6940 *p = '<';
6941 ++len;
6942 }
6943
6944 }
6945 else
6946#endif
6947 if (len > this_ru_col - 1)
6948 {
6949 p += len - (this_ru_col - 1);
6950 *p = '<';
6951 len = this_ru_col - 1;
6952 }
6953
6954 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006955 screen_puts(p, row, wp->w_wincol, attr);
6956 screen_fill(row, row + 1, len + wp->w_wincol,
6957 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006959 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6961 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006962 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
6964#ifdef FEAT_CMDL_INFO
6965 win_redr_ruler(wp, TRUE);
6966#endif
6967 }
6968
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 /*
6970 * May need to draw the character below the vertical separator.
6971 */
6972 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6973 {
6974 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006975 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 else
6977 fillchar = fillchar_vsep(&attr);
6978 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6979 attr);
6980 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006981 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982}
6983
Bram Moolenaar238a5642006-02-21 22:12:05 +00006984#ifdef FEAT_STL_OPT
6985/*
6986 * Redraw the status line according to 'statusline' and take care of any
6987 * errors encountered.
6988 */
6989 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006990redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006991{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006992 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006993 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006994
6995 /* When called recursively return. This can happen when the statusline
6996 * contains an expression that triggers a redraw. */
6997 if (entered)
6998 return;
6999 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007000
Bram Moolenaara742e082016-04-05 21:10:38 +02007001 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007002 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007003 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007004 {
7005 /* When there is an error disable the statusline, otherwise the
7006 * display is messed up with errors and a redraw triggers the problem
7007 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007008 set_string_option_direct((char_u *)"statusline", -1,
7009 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007010 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007011 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007012 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007013 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007014}
7015#endif
7016
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017/*
7018 * Return TRUE if the status line of window "wp" is connected to the status
7019 * line of the window right of it. If not, then it's a vertical separator.
7020 * Only call if (wp->w_vsep_width != 0).
7021 */
7022 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007023stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
7025 frame_T *fr;
7026
7027 fr = wp->w_frame;
7028 while (fr->fr_parent != NULL)
7029 {
7030 if (fr->fr_parent->fr_layout == FR_COL)
7031 {
7032 if (fr->fr_next != NULL)
7033 break;
7034 }
7035 else
7036 {
7037 if (fr->fr_next != NULL)
7038 return TRUE;
7039 }
7040 fr = fr->fr_parent;
7041 }
7042 return FALSE;
7043}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046/*
7047 * Get the value to show for the language mappings, active 'keymap'.
7048 */
7049 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007050get_keymap_str(
7051 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007052 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007053 char_u *buf, /* buffer for the result */
7054 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 char_u *p;
7057
7058 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7059 return FALSE;
7060
7061 {
7062#ifdef FEAT_EVAL
7063 buf_T *old_curbuf = curbuf;
7064 win_T *old_curwin = curwin;
7065 char_u *s;
7066
7067 curbuf = wp->w_buffer;
7068 curwin = wp;
7069 STRCPY(buf, "b:keymap_name"); /* must be writable */
7070 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007071 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 --emsg_skip;
7073 curbuf = old_curbuf;
7074 curwin = old_curwin;
7075 if (p == NULL || *p == NUL)
7076#endif
7077 {
7078#ifdef FEAT_KEYMAP
7079 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7080 p = wp->w_buffer->b_p_keymap;
7081 else
7082#endif
7083 p = (char_u *)"lang";
7084 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007085 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 buf[0] = NUL;
7087#ifdef FEAT_EVAL
7088 vim_free(s);
7089#endif
7090 }
7091 return buf[0] != NUL;
7092}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
7094#if defined(FEAT_STL_OPT) || defined(PROTO)
7095/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007096 * Redraw the status line or ruler of window "wp".
7097 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 */
7099 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007100win_redr_custom(
7101 win_T *wp,
7102 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007104 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 int attr;
7106 int curattr;
7107 int row;
7108 int col = 0;
7109 int maxwidth;
7110 int width;
7111 int n;
7112 int len;
7113 int fillchar;
7114 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007115 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007117 struct stl_hlrec hltab[STL_MAX_ITEM];
7118 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007119 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007120 win_T *ewp;
7121 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122
Bram Moolenaar1d633412013-12-11 15:52:01 +01007123 /* There is a tiny chance that this gets called recursively: When
7124 * redrawing a status line triggers redrawing the ruler or tabline.
7125 * Avoid trouble by not allowing recursion. */
7126 if (entered)
7127 return;
7128 entered = TRUE;
7129
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007131 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007133 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007134 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007135 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007136 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007137 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007138 maxwidth = Columns;
7139# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007140 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007143 else
7144 {
7145 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007146 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007147 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007148
7149 if (draw_ruler)
7150 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007151 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007152 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007153 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007154 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007155 if (*++stl == '-')
7156 stl++;
7157 if (atoi((char *)stl))
7158 while (VIM_ISDIGIT(*stl))
7159 stl++;
7160 if (*stl++ != '(')
7161 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007162 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007163 col = ru_col - (Columns - wp->w_width);
7164 if (col < (wp->w_width + 1) / 2)
7165 col = (wp->w_width + 1) / 2;
7166 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007167 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007168 {
7169 row = Rows - 1;
7170 --maxwidth; /* writing in last column may cause scrolling */
7171 fillchar = ' ';
7172 attr = 0;
7173 }
7174
7175# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007176 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007177# endif
7178 }
7179 else
7180 {
7181 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007182 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007183 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007184 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007185# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007186 use_sandbox = was_set_insecurely((char_u *)"statusline",
7187 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007188# endif
7189 }
7190
Bram Moolenaar53f81742017-09-22 14:35:51 +02007191 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007192 }
7193
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007195 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196
Bram Moolenaar61452852011-02-01 18:01:11 +01007197 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7198 * the cursor away and back. */
7199 ewp = wp == NULL ? curwin : wp;
7200 p_crb_save = ewp->w_p_crb;
7201 ewp->w_p_crb = FALSE;
7202
Bram Moolenaar362f3562009-11-03 16:20:34 +00007203 /* Make a copy, because the statusline may include a function call that
7204 * might change the option value and free the memory. */
7205 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007206 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007207 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007208 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007209 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007210 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007212 /* Make all characters printable. */
7213 p = transstr(buf);
7214 if (p != NULL)
7215 {
7216 vim_strncpy(buf, p, sizeof(buf) - 1);
7217 vim_free(p);
7218 }
7219
7220 /* fill up with "fillchar" */
7221 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007222 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 {
7224#ifdef FEAT_MBYTE
7225 len += (*mb_char2bytes)(fillchar, buf + len);
7226#else
7227 buf[len++] = fillchar;
7228#endif
7229 ++width;
7230 }
7231 buf[len] = NUL;
7232
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007233 /*
7234 * Draw each snippet with the specified highlighting.
7235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 curattr = attr;
7237 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007238 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007240 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 screen_puts_len(p, len, row, col, curattr);
7242 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007243 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007245 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007247 else if (hltab[n].userhl < 0)
7248 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007249#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007250 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7251 && wp->w_status_height != 0)
7252 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007253 else if (wp != NULL && bt_terminal(wp->w_buffer)
7254 && wp->w_status_height != 0)
7255 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007256#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007257 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007258 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007260 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 }
7262 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007263
7264 if (wp == NULL)
7265 {
7266 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7267 col = 0;
7268 len = 0;
7269 p = buf;
7270 fillchar = 0;
7271 for (n = 0; tabtab[n].start != NULL; n++)
7272 {
7273 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7274 while (col < len)
7275 TabPageIdxs[col++] = fillchar;
7276 p = tabtab[n].start;
7277 fillchar = tabtab[n].userhl;
7278 }
7279 while (col < Columns)
7280 TabPageIdxs[col++] = fillchar;
7281 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007282
7283theend:
7284 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285}
7286
7287#endif /* FEAT_STL_OPT */
7288
7289/*
7290 * Output a single character directly to the screen and update ScreenLines.
7291 */
7292 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007293screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 char_u buf[MB_MAXBYTES + 1];
7296
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007297#ifdef FEAT_MBYTE
7298 if (has_mbyte)
7299 buf[(*mb_char2bytes)(c, buf)] = NUL;
7300 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007302 {
7303 buf[0] = c;
7304 buf[1] = NUL;
7305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 screen_puts(buf, row, col, attr);
7307}
7308
7309/*
7310 * Get a single character directly from ScreenLines into "bytes[]".
7311 * Also return its attribute in *attrp;
7312 */
7313 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007314screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
7316 unsigned off;
7317
7318 /* safety check */
7319 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7320 {
7321 off = LineOffset[row] + col;
7322 *attrp = ScreenAttrs[off];
7323 bytes[0] = ScreenLines[off];
7324 bytes[1] = NUL;
7325
7326#ifdef FEAT_MBYTE
7327 if (enc_utf8 && ScreenLinesUC[off] != 0)
7328 bytes[utfc_char2bytes(off, bytes)] = NUL;
7329 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7330 {
7331 bytes[0] = ScreenLines[off];
7332 bytes[1] = ScreenLines2[off];
7333 bytes[2] = NUL;
7334 }
7335 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7336 {
7337 bytes[1] = ScreenLines[off + 1];
7338 bytes[2] = NUL;
7339 }
7340#endif
7341 }
7342}
7343
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007344#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007345static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007346
7347/*
7348 * Return TRUE if composing characters for screen posn "off" differs from
7349 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007350 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007351 */
7352 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007353screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007354{
7355 int i;
7356
7357 for (i = 0; i < Screen_mco; ++i)
7358 {
7359 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7360 return TRUE;
7361 if (u8cc[i] == 0)
7362 break;
7363 }
7364 return FALSE;
7365}
7366#endif
7367
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368/*
7369 * Put string '*text' on the screen at position 'row' and 'col', with
7370 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7371 * Note: only outputs within one row, message is truncated at screen boundary!
7372 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7373 */
7374 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007375screen_puts(
7376 char_u *text,
7377 int row,
7378 int col,
7379 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380{
7381 screen_puts_len(text, -1, row, col, attr);
7382}
7383
7384/*
7385 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7386 * a NUL.
7387 */
7388 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007389screen_puts_len(
7390 char_u *text,
7391 int textlen,
7392 int row,
7393 int col,
7394 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 unsigned off;
7397 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007398 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 int c;
7400#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007401 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 int mbyte_blen = 1;
7403 int mbyte_cells = 1;
7404 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007405 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 int clear_next_cell = FALSE;
7407# ifdef FEAT_ARABIC
7408 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007409 int pc, nc, nc1;
7410 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411# endif
7412#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007413#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7414 int force_redraw_this;
7415 int force_redraw_next = FALSE;
7416#endif
7417 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418
7419 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7420 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007421 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422
Bram Moolenaarc236c162008-07-13 17:41:49 +00007423#ifdef FEAT_MBYTE
7424 /* When drawing over the right halve of a double-wide char clear out the
7425 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007426 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007427# ifdef FEAT_GUI
7428 && !gui.in_use
7429# endif
7430 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007431 {
7432 ScreenLines[off - 1] = ' ';
7433 ScreenAttrs[off - 1] = 0;
7434 if (enc_utf8)
7435 {
7436 ScreenLinesUC[off - 1] = 0;
7437 ScreenLinesC[0][off - 1] = 0;
7438 }
7439 /* redraw the previous cell, make it empty */
7440 screen_char(off - 1, row, col - 1);
7441 /* force the cell at "col" to be redrawn */
7442 force_redraw_next = TRUE;
7443 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007444#endif
7445
Bram Moolenaar367329b2007-08-30 11:53:22 +00007446#ifdef FEAT_MBYTE
7447 max_off = LineOffset[row] + screen_Columns;
7448#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007449 while (col < screen_Columns
7450 && (len < 0 || (int)(ptr - text) < len)
7451 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 {
7453 c = *ptr;
7454#ifdef FEAT_MBYTE
7455 /* check if this is the first byte of a multibyte */
7456 if (has_mbyte)
7457 {
7458 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007459 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007461 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7463 mbyte_cells = 1;
7464 else if (enc_dbcs != 0)
7465 mbyte_cells = mbyte_blen;
7466 else /* enc_utf8 */
7467 {
7468 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007469 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 (int)((text + len) - ptr));
7471 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007472 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007474# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 /* Non-BMP character: display as ? or fullwidth ?. */
7476 if (u8c >= 0x10000)
7477 {
7478 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7479 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007480 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483# ifdef FEAT_ARABIC
7484 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7485 {
7486 /* Do Arabic shaping. */
7487 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7488 {
7489 /* Past end of string to be displayed. */
7490 nc = NUL;
7491 nc1 = NUL;
7492 }
7493 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007494 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007495 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7496 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007497 nc1 = pcc[0];
7498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 pc = prev_c;
7500 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007501 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
7503 else
7504 prev_c = u8c;
7505# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007506 if (col + mbyte_cells > screen_Columns)
7507 {
7508 /* Only 1 cell left, but character requires 2 cells:
7509 * display a '>' in the last column to avoid wrapping. */
7510 c = '>';
7511 mbyte_cells = 1;
7512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 }
7514 }
7515#endif
7516
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007517#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7518 force_redraw_this = force_redraw_next;
7519 force_redraw_next = FALSE;
7520#endif
7521
7522 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523#ifdef FEAT_MBYTE
7524 || (mbyte_cells == 2
7525 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7526 || (enc_dbcs == DBCS_JPNU
7527 && c == 0x8e
7528 && ScreenLines2[off] != ptr[1])
7529 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007530 && (ScreenLinesUC[off] !=
7531 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7532 || (ScreenLinesUC[off] != 0
7533 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534#endif
7535 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007536 || exmode_active;
7537
7538 if (need_redraw
7539#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7540 || force_redraw_this
7541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 )
7543 {
7544#if defined(FEAT_GUI) || defined(UNIX)
7545 /* The bold trick makes a single row of pixels appear in the next
7546 * character. When a bold character is removed, the next
7547 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007548 * and for some xterms. */
7549 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550# ifdef FEAT_GUI
7551 gui.in_use
7552# endif
7553# if defined(FEAT_GUI) && defined(UNIX)
7554 ||
7555# endif
7556# ifdef UNIX
7557 term_is_xterm
7558# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007559 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007561 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007563 if (n > HL_ALL)
7564 n = syn_attr2attr(n);
7565 if (n & HL_BOLD)
7566 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 }
7568#endif
7569#ifdef FEAT_MBYTE
7570 /* When at the end of the text and overwriting a two-cell
7571 * character with a one-cell character, need to clear the next
7572 * cell. Also when overwriting the left halve of a two-cell char
7573 * with the right halve of a two-cell char. Do this only once
7574 * (mb_off2cells() may return 2 on the right halve). */
7575 if (clear_next_cell)
7576 clear_next_cell = FALSE;
7577 else if (has_mbyte
7578 && (len < 0 ? ptr[mbyte_blen] == NUL
7579 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007580 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007582 && (*mb_off2cells)(off, max_off) == 1
7583 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 clear_next_cell = TRUE;
7585
7586 /* Make sure we never leave a second byte of a double-byte behind,
7587 * it confuses mb_off2cells(). */
7588 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007589 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007591 && (*mb_off2cells)(off, max_off) == 1
7592 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 ScreenLines[off + mbyte_blen] = 0;
7594#endif
7595 ScreenLines[off] = c;
7596 ScreenAttrs[off] = attr;
7597#ifdef FEAT_MBYTE
7598 if (enc_utf8)
7599 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007600 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 ScreenLinesUC[off] = 0;
7602 else
7603 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007604 int i;
7605
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007607 for (i = 0; i < Screen_mco; ++i)
7608 {
7609 ScreenLinesC[i][off] = u8cc[i];
7610 if (u8cc[i] == 0)
7611 break;
7612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 }
7614 if (mbyte_cells == 2)
7615 {
7616 ScreenLines[off + 1] = 0;
7617 ScreenAttrs[off + 1] = attr;
7618 }
7619 screen_char(off, row, col);
7620 }
7621 else if (mbyte_cells == 2)
7622 {
7623 ScreenLines[off + 1] = ptr[1];
7624 ScreenAttrs[off + 1] = attr;
7625 screen_char_2(off, row, col);
7626 }
7627 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7628 {
7629 ScreenLines2[off] = ptr[1];
7630 screen_char(off, row, col);
7631 }
7632 else
7633#endif
7634 screen_char(off, row, col);
7635 }
7636#ifdef FEAT_MBYTE
7637 if (has_mbyte)
7638 {
7639 off += mbyte_cells;
7640 col += mbyte_cells;
7641 ptr += mbyte_blen;
7642 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007643 {
7644 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007646 len = -1;
7647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
7649 else
7650#endif
7651 {
7652 ++off;
7653 ++col;
7654 ++ptr;
7655 }
7656 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007657
7658#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7659 /* If we detected the next character needs to be redrawn, but the text
7660 * doesn't extend up to there, update the character here. */
7661 if (force_redraw_next && col < screen_Columns)
7662 {
7663# ifdef FEAT_MBYTE
7664 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7665 screen_char_2(off, row, col);
7666 else
7667# endif
7668 screen_char(off, row, col);
7669 }
7670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671}
7672
7673#ifdef FEAT_SEARCH_EXTRA
7674/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007675 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 */
7677 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007678start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679{
7680 if (p_hls && !no_hlsearch)
7681 {
7682 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007683 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007684# ifdef FEAT_RELTIME
7685 /* Set the time limit to 'redrawtime'. */
7686 profile_setlimit(p_rdt, &search_hl.tm);
7687# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
7689}
7690
7691/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007692 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 */
7694 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007695end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
7697 if (search_hl.rm.regprog != NULL)
7698 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007699 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 search_hl.rm.regprog = NULL;
7701 }
7702}
7703
7704/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007705 * Init for calling prepare_search_hl().
7706 */
7707 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007708init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007709{
7710 matchitem_T *cur;
7711
7712 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7713 * match */
7714 cur = wp->w_match_head;
7715 while (cur != NULL)
7716 {
7717 cur->hl.rm = cur->match;
7718 if (cur->hlg_id == 0)
7719 cur->hl.attr = 0;
7720 else
7721 cur->hl.attr = syn_id2attr(cur->hlg_id);
7722 cur->hl.buf = wp->w_buffer;
7723 cur->hl.lnum = 0;
7724 cur->hl.first_lnum = 0;
7725# ifdef FEAT_RELTIME
7726 /* Set the time limit to 'redrawtime'. */
7727 profile_setlimit(p_rdt, &(cur->hl.tm));
7728# endif
7729 cur = cur->next;
7730 }
7731 search_hl.buf = wp->w_buffer;
7732 search_hl.lnum = 0;
7733 search_hl.first_lnum = 0;
7734 /* time limit is set at the toplevel, for all windows */
7735}
7736
7737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 * Advance to the match in window "wp" line "lnum" or past it.
7739 */
7740 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007741prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007743 matchitem_T *cur; /* points to the match list */
7744 match_T *shl; /* points to search_hl or a match */
7745 int shl_flag; /* flag to indicate whether search_hl
7746 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007747 int pos_inprogress; /* marks that position match search is
7748 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 int n;
7750
7751 /*
7752 * When using a multi-line pattern, start searching at the top
7753 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007754 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007756 cur = wp->w_match_head;
7757 shl_flag = FALSE;
7758 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007760 if (shl_flag == FALSE)
7761 {
7762 shl = &search_hl;
7763 shl_flag = TRUE;
7764 }
7765 else
7766 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 if (shl->rm.regprog != NULL
7768 && shl->lnum == 0
7769 && re_multiline(shl->rm.regprog))
7770 {
7771 if (shl->first_lnum == 0)
7772 {
7773# ifdef FEAT_FOLDING
7774 for (shl->first_lnum = lnum;
7775 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7776 if (hasFoldingWin(wp, shl->first_lnum - 1,
7777 NULL, NULL, TRUE, NULL))
7778 break;
7779# else
7780 shl->first_lnum = wp->w_topline;
7781# endif
7782 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007783 if (cur != NULL)
7784 cur->pos.cur = 0;
7785 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007787 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7788 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007790 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7791 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007792 pos_inprogress = cur == NULL || cur->pos.cur == 0
7793 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 if (shl->lnum != 0)
7795 {
7796 shl->first_lnum = shl->lnum
7797 + shl->rm.endpos[0].lnum
7798 - shl->rm.startpos[0].lnum;
7799 n = shl->rm.endpos[0].col;
7800 }
7801 else
7802 {
7803 ++shl->first_lnum;
7804 n = 0;
7805 }
7806 }
7807 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007808 if (shl != &search_hl && cur != NULL)
7809 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 }
7811}
7812
7813/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007814 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 * Uses shl->buf.
7816 * Sets shl->lnum and shl->rm contents.
7817 * Note: Assumes a previous match is always before "lnum", unless
7818 * shl->lnum is zero.
7819 * Careful: Any pointers for buffer lines will become invalid.
7820 */
7821 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007822next_search_hl(
7823 win_T *win,
7824 match_T *shl, /* points to search_hl or a match */
7825 linenr_T lnum,
7826 colnr_T mincol, /* minimal column for a match */
7827 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 linenr_T l;
7830 colnr_T matchcol;
7831 long nmatched;
7832
7833 if (shl->lnum != 0)
7834 {
7835 /* Check for three situations:
7836 * 1. If the "lnum" is below a previous match, start a new search.
7837 * 2. If the previous match includes "mincol", use it.
7838 * 3. Continue after the previous match.
7839 */
7840 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7841 if (lnum > l)
7842 shl->lnum = 0;
7843 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7844 return;
7845 }
7846
7847 /*
7848 * Repeat searching for a match until one is found that includes "mincol"
7849 * or none is found in this line.
7850 */
7851 called_emsg = FALSE;
7852 for (;;)
7853 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007854#ifdef FEAT_RELTIME
7855 /* Stop searching after passing the time limit. */
7856 if (profile_passed_limit(&(shl->tm)))
7857 {
7858 shl->lnum = 0; /* no match found in time */
7859 break;
7860 }
7861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 /* Three situations:
7863 * 1. No useful previous match: search from start of line.
7864 * 2. Not Vi compatible or empty match: continue at next character.
7865 * Break the loop if this is beyond the end of the line.
7866 * 3. Vi compatible searching: continue at end of previous match.
7867 */
7868 if (shl->lnum == 0)
7869 matchcol = 0;
7870 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7871 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007872 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007874 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007875
7876 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007877 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007878 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007880 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 shl->lnum = 0;
7882 break;
7883 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007884#ifdef FEAT_MBYTE
7885 if (has_mbyte)
7886 matchcol += mb_ptr2len(ml);
7887 else
7888#endif
7889 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 }
7891 else
7892 matchcol = shl->rm.endpos[0].col;
7893
7894 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007895 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007897 /* Remember whether shl->rm is using a copy of the regprog in
7898 * cur->match. */
7899 int regprog_is_copy = (shl != &search_hl && cur != NULL
7900 && shl == &cur->hl
7901 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007902 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007903
Bram Moolenaarb3414592014-06-17 17:48:32 +02007904 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7905 matchcol,
7906#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007907 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007908#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007909 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007910#endif
7911 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007912 /* Copy the regprog, in case it got freed and recompiled. */
7913 if (regprog_is_copy)
7914 cur->match.regprog = cur->hl.rm.regprog;
7915
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007916 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007917 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007918 /* Error while handling regexp: stop using this regexp. */
7919 if (shl == &search_hl)
7920 {
7921 /* don't free regprog in the match list, it's a copy */
7922 vim_regfree(shl->rm.regprog);
7923 SET_NO_HLSEARCH(TRUE);
7924 }
7925 shl->rm.regprog = NULL;
7926 shl->lnum = 0;
7927 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7928 message */
7929 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007930 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007931 }
7932 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007933 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007934 else
7935 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 if (nmatched == 0)
7937 {
7938 shl->lnum = 0; /* no match found */
7939 break;
7940 }
7941 if (shl->rm.startpos[0].lnum > 0
7942 || shl->rm.startpos[0].col >= mincol
7943 || nmatched > 1
7944 || shl->rm.endpos[0].col > mincol)
7945 {
7946 shl->lnum += shl->rm.startpos[0].lnum;
7947 break; /* useful match found */
7948 }
7949 }
7950}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951
Bram Moolenaar85077472016-10-16 14:35:48 +02007952/*
7953 * If there is a match fill "shl" and return one.
7954 * Return zero otherwise.
7955 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007956 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007957next_search_hl_pos(
7958 match_T *shl, /* points to a match */
7959 linenr_T lnum,
7960 posmatch_T *posmatch, /* match positions */
7961 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007962{
7963 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007964 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007965
Bram Moolenaarb3414592014-06-17 17:48:32 +02007966 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7967 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007968 llpos_T *pos = &posmatch->pos[i];
7969
7970 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007971 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007972 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007973 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007974 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007975 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007976 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007977 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007978 /* if this match comes before the one at "found" then swap
7979 * them */
7980 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007982 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007983
Bram Moolenaar85077472016-10-16 14:35:48 +02007984 *pos = posmatch->pos[found];
7985 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007986 }
7987 }
7988 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007989 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990 }
7991 }
7992 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007993 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007994 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007995 colnr_T start = posmatch->pos[found].col == 0
7996 ? 0 : posmatch->pos[found].col - 1;
7997 colnr_T end = posmatch->pos[found].col == 0
7998 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007999
Bram Moolenaar85077472016-10-16 14:35:48 +02008000 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008001 shl->rm.startpos[0].lnum = 0;
8002 shl->rm.startpos[0].col = start;
8003 shl->rm.endpos[0].lnum = 0;
8004 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008005 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008006 posmatch->cur = found + 1;
8007 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008008 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008009 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008010}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008011#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008012
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008014screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015{
8016 attrentry_T *aep = NULL;
8017
8018 screen_attr = attr;
8019 if (full_screen
8020#ifdef WIN3264
8021 && termcap_active
8022#endif
8023 )
8024 {
8025#ifdef FEAT_GUI
8026 if (gui.in_use)
8027 {
8028 char buf[20];
8029
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008030 /* The GUI handles this internally. */
8031 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 OUT_STR(buf);
8033 }
8034 else
8035#endif
8036 {
8037 if (attr > HL_ALL) /* special HL attr. */
8038 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008039 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 aep = syn_cterm_attr2entry(attr);
8041 else
8042 aep = syn_term_attr2entry(attr);
8043 if (aep == NULL) /* did ":syntax clear" */
8044 attr = 0;
8045 else
8046 attr = aep->ae_attr;
8047 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008048 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008050 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008051#ifdef FEAT_TERMGUICOLORS
8052 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008053 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008054#endif
8055 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008056#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008057 )
8058#endif
8059 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008060 /* If the Normal FG color has BOLD attribute and the new HL
8061 * has a FG color defined, clear BOLD. */
8062 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008063 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008065 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008066 out_str(T_UCS);
8067 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008068 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8069 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008071 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008073 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008075 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008076 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077
8078 /*
8079 * Output the color or start string after bold etc., in case the
8080 * bold etc. override the color setting.
8081 */
8082 if (aep != NULL)
8083 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008084#ifdef FEAT_TERMGUICOLORS
8085 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008087 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008088 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008089 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008090 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 }
8092 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008095 if (t_colors > 1)
8096 {
8097 if (aep->ae_u.cterm.fg_color)
8098 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8099 if (aep->ae_u.cterm.bg_color)
8100 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8101 }
8102 else
8103 {
8104 if (aep->ae_u.term.start != NULL)
8105 out_str(aep->ae_u.term.start);
8106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 }
8108 }
8109 }
8110 }
8111}
8112
8113 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008114screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115{
8116 int do_ME = FALSE; /* output T_ME code */
8117
8118 if (screen_attr != 0
8119#ifdef WIN3264
8120 && termcap_active
8121#endif
8122 )
8123 {
8124#ifdef FEAT_GUI
8125 if (gui.in_use)
8126 {
8127 char buf[20];
8128
8129 /* use internal GUI code */
8130 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8131 OUT_STR(buf);
8132 }
8133 else
8134#endif
8135 {
8136 if (screen_attr > HL_ALL) /* special HL attr. */
8137 {
8138 attrentry_T *aep;
8139
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008140 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {
8142 /*
8143 * Assume that t_me restores the original colors!
8144 */
8145 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008146 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008147#ifdef FEAT_TERMGUICOLORS
8148 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008149 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8150 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008151#endif
8152 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008153#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008154 )
8155#endif
8156 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 do_ME = TRUE;
8158 }
8159 else
8160 {
8161 aep = syn_term_attr2entry(screen_attr);
8162 if (aep != NULL && aep->ae_u.term.stop != NULL)
8163 {
8164 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8165 do_ME = TRUE;
8166 else
8167 out_str(aep->ae_u.term.stop);
8168 }
8169 }
8170 if (aep == NULL) /* did ":syntax clear" */
8171 screen_attr = 0;
8172 else
8173 screen_attr = aep->ae_attr;
8174 }
8175
8176 /*
8177 * Often all ending-codes are equal to T_ME. Avoid outputting the
8178 * same sequence several times.
8179 */
8180 if (screen_attr & HL_STANDOUT)
8181 {
8182 if (STRCMP(T_SE, T_ME) == 0)
8183 do_ME = TRUE;
8184 else
8185 out_str(T_SE);
8186 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008187 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008188 {
8189 if (STRCMP(T_UCE, T_ME) == 0)
8190 do_ME = TRUE;
8191 else
8192 out_str(T_UCE);
8193 }
8194 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008195 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {
8197 if (STRCMP(T_UE, T_ME) == 0)
8198 do_ME = TRUE;
8199 else
8200 out_str(T_UE);
8201 }
8202 if (screen_attr & HL_ITALIC)
8203 {
8204 if (STRCMP(T_CZR, T_ME) == 0)
8205 do_ME = TRUE;
8206 else
8207 out_str(T_CZR);
8208 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008209 if (screen_attr & HL_STRIKETHROUGH)
8210 {
8211 if (STRCMP(T_STE, T_ME) == 0)
8212 do_ME = TRUE;
8213 else
8214 out_str(T_STE);
8215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8217 out_str(T_ME);
8218
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008219#ifdef FEAT_TERMGUICOLORS
8220 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008222 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008223 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008224 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008225 term_bg_rgb_color(cterm_normal_bg_gui_color);
8226 }
8227 else
8228#endif
8229 {
8230 if (t_colors > 1)
8231 {
8232 /* set Normal cterm colors */
8233 if (cterm_normal_fg_color != 0)
8234 term_fg_color(cterm_normal_fg_color - 1);
8235 if (cterm_normal_bg_color != 0)
8236 term_bg_color(cterm_normal_bg_color - 1);
8237 if (cterm_normal_fg_bold)
8238 out_str(T_MD);
8239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 }
8241 }
8242 }
8243 screen_attr = 0;
8244}
8245
8246/*
8247 * Reset the colors for a cterm. Used when leaving Vim.
8248 * The machine specific code may override this again.
8249 */
8250 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008251reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008253 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {
8255 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008256#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008257 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8258 || cterm_normal_bg_gui_color != INVALCOLOR)
8259 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008260#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {
8264 out_str(T_OP);
8265 screen_attr = -1;
8266 }
8267 if (cterm_normal_fg_bold)
8268 {
8269 out_str(T_ME);
8270 screen_attr = -1;
8271 }
8272 }
8273}
8274
8275/*
8276 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8277 * using the attributes from ScreenAttrs["off"].
8278 */
8279 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008280screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281{
8282 int attr;
8283
8284 /* Check for illegal values, just in case (could happen just after
8285 * resizing). */
8286 if (row >= screen_Rows || col >= screen_Columns)
8287 return;
8288
Bram Moolenaar494838a2015-02-10 19:20:37 +01008289 /* Outputting a character in the last cell on the screen may scroll the
8290 * screen up. Only do it when the "xn" termcap property is set, otherwise
8291 * mark the character invalid (update it when scrolled up). */
8292 if (*T_XN == NUL
8293 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294#ifdef FEAT_RIGHTLEFT
8295 /* account for first command-line character in rightleft mode */
8296 && !cmdmsg_rl
8297#endif
8298 )
8299 {
8300 ScreenAttrs[off] = (sattr_T)-1;
8301 return;
8302 }
8303
8304 /*
8305 * Stop highlighting first, so it's easier to move the cursor.
8306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 if (screen_char_attr != 0)
8308 attr = screen_char_attr;
8309 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 attr = ScreenAttrs[off];
8311 if (screen_attr != attr)
8312 screen_stop_highlight();
8313
8314 windgoto(row, col);
8315
8316 if (screen_attr != attr)
8317 screen_start_highlight(attr);
8318
8319#ifdef FEAT_MBYTE
8320 if (enc_utf8 && ScreenLinesUC[off] != 0)
8321 {
8322 char_u buf[MB_MAXBYTES + 1];
8323
Bram Moolenaarcb070082016-04-02 22:14:51 +02008324 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008325 {
8326 if (*p_ambw == 'd'
8327# ifdef FEAT_GUI
8328 && !gui.in_use
8329# endif
8330 )
8331 {
8332 /* Clear the two screen cells. If the character is actually
8333 * single width it won't change the second cell. */
8334 out_str((char_u *)" ");
8335 term_windgoto(row, col);
8336 }
8337 /* not sure where the cursor is after drawing the ambiguous width
8338 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008339 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008340 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008341 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008343
8344 /* Convert the UTF-8 character to bytes and write it. */
8345 buf[utfc_char2bytes(off, buf)] = NUL;
8346 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 }
8348 else
8349#endif
8350 {
8351#ifdef FEAT_MBYTE
8352 out_flush_check();
8353#endif
8354 out_char(ScreenLines[off]);
8355#ifdef FEAT_MBYTE
8356 /* double-byte character in single-width cell */
8357 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8358 out_char(ScreenLines2[off]);
8359#endif
8360 }
8361
8362 screen_cur_col++;
8363}
8364
8365#ifdef FEAT_MBYTE
8366
8367/*
8368 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8369 * on the screen at position 'row' and 'col'.
8370 * The attributes of the first byte is used for all. This is required to
8371 * output the two bytes of a double-byte character with nothing in between.
8372 */
8373 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008374screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
8376 /* Check for illegal values (could be wrong when screen was resized). */
8377 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8378 return;
8379
8380 /* Outputting the last character on the screen may scrollup the screen.
8381 * Don't to it! Mark the character invalid (update it when scrolled up) */
8382 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8383 {
8384 ScreenAttrs[off] = (sattr_T)-1;
8385 return;
8386 }
8387
8388 /* Output the first byte normally (positions the cursor), then write the
8389 * second byte directly. */
8390 screen_char(off, row, col);
8391 out_char(ScreenLines[off + 1]);
8392 ++screen_cur_col;
8393}
8394#endif
8395
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396/*
8397 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8398 * This uses the contents of ScreenLines[] and doesn't change it.
8399 */
8400 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008401screen_draw_rectangle(
8402 int row,
8403 int col,
8404 int height,
8405 int width,
8406 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
8408 int r, c;
8409 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008410#ifdef FEAT_MBYTE
8411 int max_off;
8412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008414 /* Can't use ScreenLines unless initialized */
8415 if (ScreenLines == NULL)
8416 return;
8417
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 if (invert)
8419 screen_char_attr = HL_INVERSE;
8420 for (r = row; r < row + height; ++r)
8421 {
8422 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008423#ifdef FEAT_MBYTE
8424 max_off = off + screen_Columns;
8425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 for (c = col; c < col + width; ++c)
8427 {
8428#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008429 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {
8431 screen_char_2(off + c, r, c);
8432 ++c;
8433 }
8434 else
8435#endif
8436 {
8437 screen_char(off + c, r, c);
8438#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008439 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 ++c;
8441#endif
8442 }
8443 }
8444 }
8445 screen_char_attr = 0;
8446}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448/*
8449 * Redraw the characters for a vertically split window.
8450 */
8451 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008452redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453{
8454 int col;
8455 int width;
8456
8457# ifdef FEAT_CLIPBOARD
8458 clip_may_clear_selection(row, end - 1);
8459# endif
8460
8461 if (wp == NULL)
8462 {
8463 col = 0;
8464 width = Columns;
8465 }
8466 else
8467 {
8468 col = wp->w_wincol;
8469 width = wp->w_width;
8470 }
8471 screen_draw_rectangle(row, col, end - row, width, FALSE);
8472}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008474 static void
8475space_to_screenline(int off, int attr)
8476{
8477 ScreenLines[off] = ' ';
8478 ScreenAttrs[off] = attr;
8479# ifdef FEAT_MBYTE
8480 if (enc_utf8)
8481 ScreenLinesUC[off] = 0;
8482# endif
8483}
8484
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485/*
8486 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8487 * with character 'c1' in first column followed by 'c2' in the other columns.
8488 * Use attributes 'attr'.
8489 */
8490 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008491screen_fill(
8492 int start_row,
8493 int end_row,
8494 int start_col,
8495 int end_col,
8496 int c1,
8497 int c2,
8498 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499{
8500 int row;
8501 int col;
8502 int off;
8503 int end_off;
8504 int did_delete;
8505 int c;
8506 int norm_term;
8507#if defined(FEAT_GUI) || defined(UNIX)
8508 int force_next = FALSE;
8509#endif
8510
8511 if (end_row > screen_Rows) /* safety check */
8512 end_row = screen_Rows;
8513 if (end_col > screen_Columns) /* safety check */
8514 end_col = screen_Columns;
8515 if (ScreenLines == NULL
8516 || start_row >= end_row
8517 || start_col >= end_col) /* nothing to do */
8518 return;
8519
8520 /* it's a "normal" terminal when not in a GUI or cterm */
8521 norm_term = (
8522#ifdef FEAT_GUI
8523 !gui.in_use &&
8524#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008525 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 for (row = start_row; row < end_row; ++row)
8527 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008528#ifdef FEAT_MBYTE
8529 if (has_mbyte
8530# ifdef FEAT_GUI
8531 && !gui.in_use
8532# endif
8533 )
8534 {
8535 /* When drawing over the right halve of a double-wide char clear
8536 * out the left halve. When drawing over the left halve of a
8537 * double wide-char clear out the right halve. Only needed in a
8538 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008539 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008540 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008541 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008542 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008543 }
8544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 /*
8546 * Try to use delete-line termcap code, when no attributes or in a
8547 * "normal" terminal, where a bold/italic space is just a
8548 * space.
8549 */
8550 did_delete = FALSE;
8551 if (c2 == ' '
8552 && end_col == Columns
8553 && can_clear(T_CE)
8554 && (attr == 0
8555 || (norm_term
8556 && attr <= HL_ALL
8557 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8558 {
8559 /*
8560 * check if we really need to clear something
8561 */
8562 col = start_col;
8563 if (c1 != ' ') /* don't clear first char */
8564 ++col;
8565
8566 off = LineOffset[row] + col;
8567 end_off = LineOffset[row] + end_col;
8568
8569 /* skip blanks (used often, keep it fast!) */
8570#ifdef FEAT_MBYTE
8571 if (enc_utf8)
8572 while (off < end_off && ScreenLines[off] == ' '
8573 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8574 ++off;
8575 else
8576#endif
8577 while (off < end_off && ScreenLines[off] == ' '
8578 && ScreenAttrs[off] == 0)
8579 ++off;
8580 if (off < end_off) /* something to be cleared */
8581 {
8582 col = off - LineOffset[row];
8583 screen_stop_highlight();
8584 term_windgoto(row, col);/* clear rest of this screen line */
8585 out_str(T_CE);
8586 screen_start(); /* don't know where cursor is now */
8587 col = end_col - col;
8588 while (col--) /* clear chars in ScreenLines */
8589 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008590 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 ++off;
8592 }
8593 }
8594 did_delete = TRUE; /* the chars are cleared now */
8595 }
8596
8597 off = LineOffset[row] + start_col;
8598 c = c1;
8599 for (col = start_col; col < end_col; ++col)
8600 {
8601 if (ScreenLines[off] != c
8602#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008603 || (enc_utf8 && (int)ScreenLinesUC[off]
8604 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605#endif
8606 || ScreenAttrs[off] != attr
8607#if defined(FEAT_GUI) || defined(UNIX)
8608 || force_next
8609#endif
8610 )
8611 {
8612#if defined(FEAT_GUI) || defined(UNIX)
8613 /* The bold trick may make a single row of pixels appear in
8614 * the next character. When a bold character is removed, the
8615 * next character should be redrawn too. This happens for our
8616 * own GUI and for some xterms. */
8617 if (
8618# ifdef FEAT_GUI
8619 gui.in_use
8620# endif
8621# if defined(FEAT_GUI) && defined(UNIX)
8622 ||
8623# endif
8624# ifdef UNIX
8625 term_is_xterm
8626# endif
8627 )
8628 {
8629 if (ScreenLines[off] != ' '
8630 && (ScreenAttrs[off] > HL_ALL
8631 || ScreenAttrs[off] & HL_BOLD))
8632 force_next = TRUE;
8633 else
8634 force_next = FALSE;
8635 }
8636#endif
8637 ScreenLines[off] = c;
8638#ifdef FEAT_MBYTE
8639 if (enc_utf8)
8640 {
8641 if (c >= 0x80)
8642 {
8643 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008644 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 }
8646 else
8647 ScreenLinesUC[off] = 0;
8648 }
8649#endif
8650 ScreenAttrs[off] = attr;
8651 if (!did_delete || c != ' ')
8652 screen_char(off, row, col);
8653 }
8654 ++off;
8655 if (col == start_col)
8656 {
8657 if (did_delete)
8658 break;
8659 c = c2;
8660 }
8661 }
8662 if (end_col == Columns)
8663 LineWraps[row] = FALSE;
8664 if (row == Rows - 1) /* overwritten the command line */
8665 {
8666 redraw_cmdline = TRUE;
8667 if (c1 == ' ' && c2 == ' ')
8668 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008669 if (start_col == 0)
8670 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 }
8672 }
8673}
8674
8675/*
8676 * Check if there should be a delay. Used before clearing or redrawing the
8677 * screen or the command line.
8678 */
8679 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008680check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681{
8682 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8683 && !did_wait_return
8684 && emsg_silent == 0)
8685 {
8686 out_flush();
8687 ui_delay(1000L, TRUE);
8688 emsg_on_display = FALSE;
8689 if (check_msg_scroll)
8690 msg_scroll = FALSE;
8691 }
8692}
8693
8694/*
8695 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008696 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 * Returns TRUE if there is a valid screen to write to.
8698 * Returns FALSE when starting up and screen not initialized yet.
8699 */
8700 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008701screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008703 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 return (ScreenLines != NULL);
8705}
8706
8707/*
8708 * Resize the shell to Rows and Columns.
8709 * Allocate ScreenLines[] and associated items.
8710 *
8711 * There may be some time between setting Rows and Columns and (re)allocating
8712 * ScreenLines[]. This happens when starting up and when (manually) changing
8713 * the shell size. Always use screen_Rows and screen_Columns to access items
8714 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8715 * final size of the shell is needed.
8716 */
8717 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008718screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719{
8720 int new_row, old_row;
8721#ifdef FEAT_GUI
8722 int old_Rows;
8723#endif
8724 win_T *wp;
8725 int outofmem = FALSE;
8726 int len;
8727 schar_T *new_ScreenLines;
8728#ifdef FEAT_MBYTE
8729 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008730 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008732 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733#endif
8734 sattr_T *new_ScreenAttrs;
8735 unsigned *new_LineOffset;
8736 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008737 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008738 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008740 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008741#ifdef FEAT_AUTOCMD
8742 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008744retry:
8745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 /*
8747 * Allocation of the screen buffers is done only when the size changes and
8748 * when Rows and Columns have been set and we have started doing full
8749 * screen stuff.
8750 */
8751 if ((ScreenLines != NULL
8752 && Rows == screen_Rows
8753 && Columns == screen_Columns
8754#ifdef FEAT_MBYTE
8755 && enc_utf8 == (ScreenLinesUC != NULL)
8756 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008757 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758#endif
8759 )
8760 || Rows == 0
8761 || Columns == 0
8762 || (!full_screen && ScreenLines == NULL))
8763 return;
8764
8765 /*
8766 * It's possible that we produce an out-of-memory message below, which
8767 * will cause this function to be called again. To break the loop, just
8768 * return here.
8769 */
8770 if (entered)
8771 return;
8772 entered = TRUE;
8773
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008774 /*
8775 * Note that the window sizes are updated before reallocating the arrays,
8776 * thus we must not redraw here!
8777 */
8778 ++RedrawingDisabled;
8779
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 win_new_shellsize(); /* fit the windows in the new sized shell */
8781
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 comp_col(); /* recompute columns for shown command and ruler */
8783
8784 /*
8785 * We're changing the size of the screen.
8786 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8787 * - Move lines from the old arrays into the new arrays, clear extra
8788 * lines (unless the screen is going to be cleared).
8789 * - Free the old arrays.
8790 *
8791 * If anything fails, make ScreenLines NULL, so we don't do anything!
8792 * Continuing with the old ScreenLines may result in a crash, because the
8793 * size is wrong.
8794 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008795 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008797#ifdef FEAT_AUTOCMD
8798 if (aucmd_win != NULL)
8799 win_free_lsize(aucmd_win);
8800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801
8802 new_ScreenLines = (schar_T *)lalloc((long_u)(
8803 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8804#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008805 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 if (enc_utf8)
8807 {
8808 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8809 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008810 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008811 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8813 }
8814 if (enc_dbcs == DBCS_JPNU)
8815 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8816 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8817#endif
8818 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8819 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8820 new_LineOffset = (unsigned *)lalloc((long_u)(
8821 Rows * sizeof(unsigned)), FALSE);
8822 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008823 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008825 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 {
8827 if (win_alloc_lines(wp) == FAIL)
8828 {
8829 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008830 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 }
8832 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008833#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008834 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8835 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008836 outofmem = TRUE;
8837#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008838give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008840#ifdef FEAT_MBYTE
8841 for (i = 0; i < p_mco; ++i)
8842 if (new_ScreenLinesC[i] == NULL)
8843 break;
8844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 if (new_ScreenLines == NULL
8846#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008847 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8849#endif
8850 || new_ScreenAttrs == NULL
8851 || new_LineOffset == NULL
8852 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008853 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 || outofmem)
8855 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008856 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008857 {
8858 /* guess the size */
8859 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8860
8861 /* Remember we did this to avoid getting outofmem messages over
8862 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008863 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008864 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008865 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008867 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008868 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008869 VIM_CLEAR(new_ScreenLinesC[i]);
8870 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008872 VIM_CLEAR(new_ScreenAttrs);
8873 VIM_CLEAR(new_LineOffset);
8874 VIM_CLEAR(new_LineWraps);
8875 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 }
8877 else
8878 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008879 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008880
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 for (new_row = 0; new_row < Rows; ++new_row)
8882 {
8883 new_LineOffset[new_row] = new_row * Columns;
8884 new_LineWraps[new_row] = FALSE;
8885
8886 /*
8887 * If the screen is not going to be cleared, copy as much as
8888 * possible from the old screen to the new one and clear the rest
8889 * (used when resizing the window at the "--more--" prompt or when
8890 * executing an external command, for the GUI).
8891 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008892 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 {
8894 (void)vim_memset(new_ScreenLines + new_row * Columns,
8895 ' ', (size_t)Columns * sizeof(schar_T));
8896#ifdef FEAT_MBYTE
8897 if (enc_utf8)
8898 {
8899 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8900 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008901 for (i = 0; i < p_mco; ++i)
8902 (void)vim_memset(new_ScreenLinesC[i]
8903 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 0, (size_t)Columns * sizeof(u8char_T));
8905 }
8906 if (enc_dbcs == DBCS_JPNU)
8907 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8908 0, (size_t)Columns * sizeof(schar_T));
8909#endif
8910 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8911 0, (size_t)Columns * sizeof(sattr_T));
8912 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008913 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 {
8915 if (screen_Columns < Columns)
8916 len = screen_Columns;
8917 else
8918 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008919#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008920 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008921 * may be invalid now. Also when p_mco changes. */
8922 if (!(enc_utf8 && ScreenLinesUC == NULL)
8923 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008924#endif
8925 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8926 ScreenLines + LineOffset[old_row],
8927 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008929 if (enc_utf8 && ScreenLinesUC != NULL
8930 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 {
8932 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8933 ScreenLinesUC + LineOffset[old_row],
8934 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008935 for (i = 0; i < p_mco; ++i)
8936 mch_memmove(new_ScreenLinesC[i]
8937 + new_LineOffset[new_row],
8938 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 (size_t)len * sizeof(u8char_T));
8940 }
8941 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8942 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8943 ScreenLines2 + LineOffset[old_row],
8944 (size_t)len * sizeof(schar_T));
8945#endif
8946 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8947 ScreenAttrs + LineOffset[old_row],
8948 (size_t)len * sizeof(sattr_T));
8949 }
8950 }
8951 }
8952 /* Use the last line of the screen for the current line. */
8953 current_ScreenLine = new_ScreenLines + Rows * Columns;
8954 }
8955
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008956 free_screenlines();
8957
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 ScreenLines = new_ScreenLines;
8959#ifdef FEAT_MBYTE
8960 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008961 for (i = 0; i < p_mco; ++i)
8962 ScreenLinesC[i] = new_ScreenLinesC[i];
8963 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 ScreenLines2 = new_ScreenLines2;
8965#endif
8966 ScreenAttrs = new_ScreenAttrs;
8967 LineOffset = new_LineOffset;
8968 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008969 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970
8971 /* It's important that screen_Rows and screen_Columns reflect the actual
8972 * size of ScreenLines[]. Set them before calling anything. */
8973#ifdef FEAT_GUI
8974 old_Rows = screen_Rows;
8975#endif
8976 screen_Rows = Rows;
8977 screen_Columns = Columns;
8978
8979 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008980 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 screenclear2();
8982
8983#ifdef FEAT_GUI
8984 else if (gui.in_use
8985 && !gui.starting
8986 && ScreenLines != NULL
8987 && old_Rows != Rows)
8988 {
8989 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8990 /*
8991 * Adjust the position of the cursor, for when executing an external
8992 * command.
8993 */
8994 if (msg_row >= Rows) /* Rows got smaller */
8995 msg_row = Rows - 1; /* put cursor at last row */
8996 else if (Rows > old_Rows) /* Rows got bigger */
8997 msg_row += Rows - old_Rows; /* put cursor in same place */
8998 if (msg_col >= Columns) /* Columns got smaller */
8999 msg_col = Columns - 1; /* put cursor at last column */
9000 }
9001#endif
9002
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009004 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009005
9006#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009007 /*
9008 * Do not apply autocommands more than 3 times to avoid an endless loop
9009 * in case applying autocommands always changes Rows or Columns.
9010 */
9011 if (starting == 0 && ++retry_count <= 3)
9012 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009013 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009014 /* In rare cases, autocommands may have altered Rows or Columns,
9015 * jump back to check if we need to allocate the screen again. */
9016 goto retry;
9017 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019}
9020
9021 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009022free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009023{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009024#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009025 int i;
9026
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009027 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009028 for (i = 0; i < Screen_mco; ++i)
9029 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009030 vim_free(ScreenLines2);
9031#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009032 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009033 vim_free(ScreenAttrs);
9034 vim_free(LineOffset);
9035 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009036 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009037}
9038
9039 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009040screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041{
9042 check_for_delay(FALSE);
9043 screenalloc(FALSE); /* allocate screen buffers if size changed */
9044 screenclear2(); /* clear the screen */
9045}
9046
9047 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009048screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049{
9050 int i;
9051
9052 if (starting == NO_SCREEN || ScreenLines == NULL
9053#ifdef FEAT_GUI
9054 || (gui.in_use && gui.starting)
9055#endif
9056 )
9057 return;
9058
9059#ifdef FEAT_GUI
9060 if (!gui.in_use)
9061#endif
9062 screen_attr = -1; /* force setting the Normal colors */
9063 screen_stop_highlight(); /* don't want highlighting here */
9064
9065#ifdef FEAT_CLIPBOARD
9066 /* disable selection without redrawing it */
9067 clip_scroll_selection(9999);
9068#endif
9069
9070 /* blank out ScreenLines */
9071 for (i = 0; i < Rows; ++i)
9072 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009073 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 LineWraps[i] = FALSE;
9075 }
9076
9077 if (can_clear(T_CL))
9078 {
9079 out_str(T_CL); /* clear the display */
9080 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009081 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 }
9083 else
9084 {
9085 /* can't clear the screen, mark all chars with invalid attributes */
9086 for (i = 0; i < Rows; ++i)
9087 lineinvalid(LineOffset[i], (int)Columns);
9088 clear_cmdline = TRUE;
9089 }
9090
9091 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9092
9093 win_rest_invalid(firstwin);
9094 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009095 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 if (must_redraw == CLEAR) /* no need to clear again */
9097 must_redraw = NOT_VALID;
9098 compute_cmdrow();
9099 msg_row = cmdline_row; /* put cursor on last line for messages */
9100 msg_col = 0;
9101 screen_start(); /* don't know where cursor is now */
9102 msg_scrolled = 0; /* can't scroll back */
9103 msg_didany = FALSE;
9104 msg_didout = FALSE;
9105}
9106
9107/*
9108 * Clear one line in ScreenLines.
9109 */
9110 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009111lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112{
9113 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9114#ifdef FEAT_MBYTE
9115 if (enc_utf8)
9116 (void)vim_memset(ScreenLinesUC + off, 0,
9117 (size_t)width * sizeof(u8char_T));
9118#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009119 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120}
9121
9122/*
9123 * Mark one line in ScreenLines invalid by setting the attributes to an
9124 * invalid value.
9125 */
9126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009127lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128{
9129 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9130}
9131
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132/*
9133 * Copy part of a Screenline for vertically split window "wp".
9134 */
9135 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009136linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
9138 unsigned off_to = LineOffset[to] + wp->w_wincol;
9139 unsigned off_from = LineOffset[from] + wp->w_wincol;
9140
9141 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9142 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009143#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 if (enc_utf8)
9145 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009146 int i;
9147
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9149 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009150 for (i = 0; i < p_mco; ++i)
9151 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9152 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 }
9154 if (enc_dbcs == DBCS_JPNU)
9155 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9156 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9159 wp->w_width * sizeof(sattr_T));
9160}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161
9162/*
9163 * Return TRUE if clearing with term string "p" would work.
9164 * It can't work when the string is empty or it won't set the right background.
9165 */
9166 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009167can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
9169 return (*p != NUL && (t_colors <= 1
9170#ifdef FEAT_GUI
9171 || gui.in_use
9172#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009173#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009174 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009175 || (!p_tgc && cterm_normal_bg_color == 0)
9176#else
9177 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009178#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009179 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180}
9181
9182/*
9183 * Reset cursor position. Use whenever cursor was moved because of outputting
9184 * something directly to the screen (shell commands) or a terminal control
9185 * code.
9186 */
9187 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009188screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189{
9190 screen_cur_row = screen_cur_col = 9999;
9191}
9192
9193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 * Move the cursor to position "row","col" in the screen.
9195 * This tries to find the most efficient way to move, minimizing the number of
9196 * characters sent to the terminal.
9197 */
9198 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009199windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009201 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 int i;
9203 int plan;
9204 int cost;
9205 int wouldbe_col;
9206 int noinvcurs;
9207 char_u *bs;
9208 int goto_cost;
9209 int attr;
9210
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009211#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9213
9214#define PLAN_LE 1
9215#define PLAN_CR 2
9216#define PLAN_NL 3
9217#define PLAN_WRITE 4
9218 /* Can't use ScreenLines unless initialized */
9219 if (ScreenLines == NULL)
9220 return;
9221
9222 if (col != screen_cur_col || row != screen_cur_row)
9223 {
9224 /* Check for valid position. */
9225 if (row < 0) /* window without text lines? */
9226 row = 0;
9227 if (row >= screen_Rows)
9228 row = screen_Rows - 1;
9229 if (col >= screen_Columns)
9230 col = screen_Columns - 1;
9231
9232 /* check if no cursor movement is allowed in highlight mode */
9233 if (screen_attr && *T_MS == NUL)
9234 noinvcurs = HIGHL_COST;
9235 else
9236 noinvcurs = 0;
9237 goto_cost = GOTO_COST + noinvcurs;
9238
9239 /*
9240 * Plan how to do the positioning:
9241 * 1. Use CR to move it to column 0, same row.
9242 * 2. Use T_LE to move it a few columns to the left.
9243 * 3. Use NL to move a few lines down, column 0.
9244 * 4. Move a few columns to the right with T_ND or by writing chars.
9245 *
9246 * Don't do this if the cursor went beyond the last column, the cursor
9247 * position is unknown then (some terminals wrap, some don't )
9248 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009249 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 * characters to move the cursor to the right.
9251 */
9252 if (row >= screen_cur_row && screen_cur_col < Columns)
9253 {
9254 /*
9255 * If the cursor is in the same row, bigger col, we can use CR
9256 * or T_LE.
9257 */
9258 bs = NULL; /* init for GCC */
9259 attr = screen_attr;
9260 if (row == screen_cur_row && col < screen_cur_col)
9261 {
9262 /* "le" is preferred over "bc", because "bc" is obsolete */
9263 if (*T_LE)
9264 bs = T_LE; /* "cursor left" */
9265 else
9266 bs = T_BC; /* "backspace character (old) */
9267 if (*bs)
9268 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9269 else
9270 cost = 999;
9271 if (col + 1 < cost) /* using CR is less characters */
9272 {
9273 plan = PLAN_CR;
9274 wouldbe_col = 0;
9275 cost = 1; /* CR is just one character */
9276 }
9277 else
9278 {
9279 plan = PLAN_LE;
9280 wouldbe_col = col;
9281 }
9282 if (noinvcurs) /* will stop highlighting */
9283 {
9284 cost += noinvcurs;
9285 attr = 0;
9286 }
9287 }
9288
9289 /*
9290 * If the cursor is above where we want to be, we can use CR LF.
9291 */
9292 else if (row > screen_cur_row)
9293 {
9294 plan = PLAN_NL;
9295 wouldbe_col = 0;
9296 cost = (row - screen_cur_row) * 2; /* CR LF */
9297 if (noinvcurs) /* will stop highlighting */
9298 {
9299 cost += noinvcurs;
9300 attr = 0;
9301 }
9302 }
9303
9304 /*
9305 * If the cursor is in the same row, smaller col, just use write.
9306 */
9307 else
9308 {
9309 plan = PLAN_WRITE;
9310 wouldbe_col = screen_cur_col;
9311 cost = 0;
9312 }
9313
9314 /*
9315 * Check if any characters that need to be written have the
9316 * correct attributes. Also avoid UTF-8 characters.
9317 */
9318 i = col - wouldbe_col;
9319 if (i > 0)
9320 cost += i;
9321 if (cost < goto_cost && i > 0)
9322 {
9323 /*
9324 * Check if the attributes are correct without additionally
9325 * stopping highlighting.
9326 */
9327 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9328 while (i && *p++ == attr)
9329 --i;
9330 if (i != 0)
9331 {
9332 /*
9333 * Try if it works when highlighting is stopped here.
9334 */
9335 if (*--p == 0)
9336 {
9337 cost += noinvcurs;
9338 while (i && *p++ == 0)
9339 --i;
9340 }
9341 if (i != 0)
9342 cost = 999; /* different attributes, don't do it */
9343 }
9344#ifdef FEAT_MBYTE
9345 if (enc_utf8)
9346 {
9347 /* Don't use an UTF-8 char for positioning, it's slow. */
9348 for (i = wouldbe_col; i < col; ++i)
9349 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9350 {
9351 cost = 999;
9352 break;
9353 }
9354 }
9355#endif
9356 }
9357
9358 /*
9359 * We can do it without term_windgoto()!
9360 */
9361 if (cost < goto_cost)
9362 {
9363 if (plan == PLAN_LE)
9364 {
9365 if (noinvcurs)
9366 screen_stop_highlight();
9367 while (screen_cur_col > col)
9368 {
9369 out_str(bs);
9370 --screen_cur_col;
9371 }
9372 }
9373 else if (plan == PLAN_CR)
9374 {
9375 if (noinvcurs)
9376 screen_stop_highlight();
9377 out_char('\r');
9378 screen_cur_col = 0;
9379 }
9380 else if (plan == PLAN_NL)
9381 {
9382 if (noinvcurs)
9383 screen_stop_highlight();
9384 while (screen_cur_row < row)
9385 {
9386 out_char('\n');
9387 ++screen_cur_row;
9388 }
9389 screen_cur_col = 0;
9390 }
9391
9392 i = col - screen_cur_col;
9393 if (i > 0)
9394 {
9395 /*
9396 * Use cursor-right if it's one character only. Avoids
9397 * removing a line of pixels from the last bold char, when
9398 * using the bold trick in the GUI.
9399 */
9400 if (T_ND[0] != NUL && T_ND[1] == NUL)
9401 {
9402 while (i-- > 0)
9403 out_char(*T_ND);
9404 }
9405 else
9406 {
9407 int off;
9408
9409 off = LineOffset[row] + screen_cur_col;
9410 while (i-- > 0)
9411 {
9412 if (ScreenAttrs[off] != screen_attr)
9413 screen_stop_highlight();
9414#ifdef FEAT_MBYTE
9415 out_flush_check();
9416#endif
9417 out_char(ScreenLines[off]);
9418#ifdef FEAT_MBYTE
9419 if (enc_dbcs == DBCS_JPNU
9420 && ScreenLines[off] == 0x8e)
9421 out_char(ScreenLines2[off]);
9422#endif
9423 ++off;
9424 }
9425 }
9426 }
9427 }
9428 }
9429 else
9430 cost = 999;
9431
9432 if (cost >= goto_cost)
9433 {
9434 if (noinvcurs)
9435 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009436 if (row == screen_cur_row && (col > screen_cur_col)
9437 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 term_cursor_right(col - screen_cur_col);
9439 else
9440 term_windgoto(row, col);
9441 }
9442 screen_cur_row = row;
9443 screen_cur_col = col;
9444 }
9445}
9446
9447/*
9448 * Set cursor to its position in the current window.
9449 */
9450 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009451setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
9453 if (redrawing())
9454 {
9455 validate_cursor();
9456 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009457 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009459 /* With 'rightleft' set and the cursor on a double-wide
9460 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009461 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009463 (has_mbyte
9464 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9465 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466# endif
9467 1)) :
9468#endif
9469 curwin->w_wcol));
9470 }
9471}
9472
9473
9474/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009475 * Insert 'line_count' lines at 'row' in window 'wp'.
9476 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9477 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 * scrolling.
9479 * Returns FAIL if the lines are not inserted, OK for success.
9480 */
9481 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009482win_ins_lines(
9483 win_T *wp,
9484 int row,
9485 int line_count,
9486 int invalid,
9487 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
9489 int did_delete;
9490 int nextrow;
9491 int lastrow;
9492 int retval;
9493
9494 if (invalid)
9495 wp->w_lines_valid = 0;
9496
9497 if (wp->w_height < 5)
9498 return FAIL;
9499
9500 if (line_count > wp->w_height - row)
9501 line_count = wp->w_height - row;
9502
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009503 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 if (retval != MAYBE)
9505 return retval;
9506
9507 /*
9508 * If there is a next window or a status line, we first try to delete the
9509 * lines at the bottom to avoid messing what is after the window.
9510 * If this fails and there are following windows, don't do anything to avoid
9511 * messing up those windows, better just redraw.
9512 */
9513 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 if (wp->w_next != NULL || wp->w_status_height)
9515 {
9516 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009517 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 did_delete = TRUE;
9519 else if (wp->w_next)
9520 return FAIL;
9521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 /*
9523 * if no lines deleted, blank the lines that will end up below the window
9524 */
9525 if (!did_delete)
9526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009529 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 lastrow = nextrow + line_count;
9531 if (lastrow > Rows)
9532 lastrow = Rows;
9533 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009534 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 ' ', ' ', 0);
9536 }
9537
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009538 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 == FAIL)
9540 {
9541 /* deletion will have messed up other windows */
9542 if (did_delete)
9543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 win_rest_invalid(W_NEXT(wp));
9546 }
9547 return FAIL;
9548 }
9549
9550 return OK;
9551}
9552
9553/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009554 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9556 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9557 * scrolling
9558 * Return OK for success, FAIL if the lines are not deleted.
9559 */
9560 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009561win_del_lines(
9562 win_T *wp,
9563 int row,
9564 int line_count,
9565 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009566 int mayclear,
9567 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569 int retval;
9570
9571 if (invalid)
9572 wp->w_lines_valid = 0;
9573
9574 if (line_count > wp->w_height - row)
9575 line_count = wp->w_height - row;
9576
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009577 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 if (retval != MAYBE)
9579 return retval;
9580
9581 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009582 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 return FAIL;
9584
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 /*
9586 * If there are windows or status lines below, try to put them at the
9587 * correct place. If we can't do that, they have to be redrawn.
9588 */
9589 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9590 {
9591 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009592 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 {
9594 wp->w_redr_status = TRUE;
9595 win_rest_invalid(wp->w_next);
9596 }
9597 }
9598 /*
9599 * If this is the last window and there is no status line, redraw the
9600 * command line later.
9601 */
9602 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 redraw_cmdline = TRUE;
9604 return OK;
9605}
9606
9607/*
9608 * Common code for win_ins_lines() and win_del_lines().
9609 * Returns OK or FAIL when the work has been done.
9610 * Returns MAYBE when not finished yet.
9611 */
9612 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009613win_do_lines(
9614 win_T *wp,
9615 int row,
9616 int line_count,
9617 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009618 int del,
9619 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620{
9621 int retval;
9622
9623 if (!redrawing() || line_count <= 0)
9624 return FAIL;
9625
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009626 /* When inserting lines would result in loss of command output, just redraw
9627 * the lines. */
9628 if (no_win_do_lines_ins && !del)
9629 return FAIL;
9630
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009632 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009634 if (!no_win_do_lines_ins)
9635 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 return FAIL;
9637 }
9638
9639 /*
9640 * Delete all remaining lines
9641 */
9642 if (row + line_count >= wp->w_height)
9643 {
9644 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009645 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 ' ', ' ', 0);
9647 return OK;
9648 }
9649
9650 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009651 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009653 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009655 if (!no_win_do_lines_ins)
9656 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657
9658 /*
9659 * If the terminal can set a scroll region, use that.
9660 * Always do this in a vertically split window. This will redraw from
9661 * ScreenLines[] when t_CV isn't defined. That's faster than using
9662 * win_line().
9663 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009664 * a character in the lower right corner of the scroll region may cause a
9665 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009667 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 scroll_region_set(wp, row);
9671 if (del)
9672 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009673 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 else
9675 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009676 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 scroll_region_reset();
9679 return retval;
9680 }
9681
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9683 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684
9685 return MAYBE;
9686}
9687
9688/*
9689 * window 'wp' and everything after it is messed up, mark it for redraw
9690 */
9691 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009692win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 {
9696 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 wp->w_redr_status = TRUE;
9698 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 }
9700 redraw_cmdline = TRUE;
9701}
9702
9703/*
9704 * The rest of the routines in this file perform screen manipulations. The
9705 * given operation is performed physically on the screen. The corresponding
9706 * change is also made to the internal screen image. In this way, the editor
9707 * anticipates the effect of editing changes on the appearance of the screen.
9708 * That way, when we call screenupdate a complete redraw isn't usually
9709 * necessary. Another advantage is that we can keep adding code to anticipate
9710 * screen changes, and in the meantime, everything still works.
9711 */
9712
9713/*
9714 * types for inserting or deleting lines
9715 */
9716#define USE_T_CAL 1
9717#define USE_T_CDL 2
9718#define USE_T_AL 3
9719#define USE_T_CE 4
9720#define USE_T_DL 5
9721#define USE_T_SR 6
9722#define USE_NL 7
9723#define USE_T_CD 8
9724#define USE_REDRAW 9
9725
9726/*
9727 * insert lines on the screen and update ScreenLines[]
9728 * 'end' is the line after the scrolled part. Normally it is Rows.
9729 * When scrolling region used 'off' is the offset from the top for the region.
9730 * 'row' and 'end' are relative to the start of the region.
9731 *
9732 * return FAIL for failure, OK for success.
9733 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009734 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009735screen_ins_lines(
9736 int off,
9737 int row,
9738 int line_count,
9739 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009740 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009741 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742{
9743 int i;
9744 int j;
9745 unsigned temp;
9746 int cursor_row;
9747 int type;
9748 int result_empty;
9749 int can_ce = can_clear(T_CE);
9750
9751 /*
9752 * FAIL if
9753 * - there is no valid screen
9754 * - the screen has to be redrawn completely
9755 * - the line count is less than one
9756 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009757 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009759 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9760#ifdef FEAT_CLIPBOARD
9761 || (clip_star.state != SELECT_CLEARED
9762 && redrawing_for_callback > 0)
9763#endif
9764 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 return FAIL;
9766
9767 /*
9768 * There are seven ways to insert lines:
9769 * 0. When in a vertically split window and t_CV isn't set, redraw the
9770 * characters from ScreenLines[].
9771 * 1. Use T_CD (clear to end of display) if it exists and the result of
9772 * the insert is just empty lines
9773 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9774 * present or line_count > 1. It looks better if we do all the inserts
9775 * at once.
9776 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9777 * insert is just empty lines and T_CE is not present or line_count >
9778 * 1.
9779 * 4. Use T_AL (insert line) if it exists.
9780 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9781 * just empty lines.
9782 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9783 * just empty lines.
9784 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9785 * the 'da' flag is not set or we have clear line capability.
9786 * 8. redraw the characters from ScreenLines[].
9787 *
9788 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9789 * the scrollbar for the window. It does have insert line, use that if it
9790 * exists.
9791 */
9792 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9794 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009795 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 type = USE_T_CD;
9797 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9798 type = USE_T_CAL;
9799 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9800 type = USE_T_CDL;
9801 else if (*T_AL != NUL)
9802 type = USE_T_AL;
9803 else if (can_ce && result_empty)
9804 type = USE_T_CE;
9805 else if (*T_DL != NUL && result_empty)
9806 type = USE_T_DL;
9807 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9808 type = USE_T_SR;
9809 else
9810 return FAIL;
9811
9812 /*
9813 * For clearing the lines screen_del_lines() is used. This will also take
9814 * care of t_db if necessary.
9815 */
9816 if (type == USE_T_CD || type == USE_T_CDL ||
9817 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009818 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819
9820 /*
9821 * If text is retained below the screen, first clear or delete as many
9822 * lines at the bottom of the window as are about to be inserted so that
9823 * the deleted lines won't later surface during a screen_del_lines.
9824 */
9825 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009826 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827
9828#ifdef FEAT_CLIPBOARD
9829 /* Remove a modeless selection when inserting lines halfway the screen
9830 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009831 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009832 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 else
9834 clip_scroll_selection(-line_count);
9835#endif
9836
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837#ifdef FEAT_GUI
9838 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9839 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009840 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841#endif
9842
9843 if (*T_CCS != NUL) /* cursor relative to region */
9844 cursor_row = row;
9845 else
9846 cursor_row = row + off;
9847
9848 /*
9849 * Shift LineOffset[] line_count down to reflect the inserted lines.
9850 * Clear the inserted lines in ScreenLines[].
9851 */
9852 row += off;
9853 end += off;
9854 for (i = 0; i < line_count; ++i)
9855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 if (wp != NULL && wp->w_width != Columns)
9857 {
9858 /* need to copy part of a line */
9859 j = end - 1 - i;
9860 while ((j -= line_count) >= row)
9861 linecopy(j + line_count, j, wp);
9862 j += line_count;
9863 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009864 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9865 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 else
9867 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9868 LineWraps[j] = FALSE;
9869 }
9870 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 {
9872 j = end - 1 - i;
9873 temp = LineOffset[j];
9874 while ((j -= line_count) >= row)
9875 {
9876 LineOffset[j + line_count] = LineOffset[j];
9877 LineWraps[j + line_count] = LineWraps[j];
9878 }
9879 LineOffset[j + line_count] = temp;
9880 LineWraps[j + line_count] = FALSE;
9881 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009882 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 else
9884 lineinvalid(temp, (int)Columns);
9885 }
9886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887
9888 screen_stop_highlight();
9889 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009890 if (clear_attr != 0)
9891 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 /* redraw the characters */
9894 if (type == USE_REDRAW)
9895 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009896 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 {
9898 term_append_lines(line_count);
9899 screen_start(); /* don't know where cursor is now */
9900 }
9901 else
9902 {
9903 for (i = 0; i < line_count; i++)
9904 {
9905 if (type == USE_T_AL)
9906 {
9907 if (i && cursor_row != 0)
9908 windgoto(cursor_row, 0);
9909 out_str(T_AL);
9910 }
9911 else /* type == USE_T_SR */
9912 out_str(T_SR);
9913 screen_start(); /* don't know where cursor is now */
9914 }
9915 }
9916
9917 /*
9918 * With scroll-reverse and 'da' flag set we need to clear the lines that
9919 * have been scrolled down into the region.
9920 */
9921 if (type == USE_T_SR && *T_DA)
9922 {
9923 for (i = 0; i < line_count; ++i)
9924 {
9925 windgoto(off + i, 0);
9926 out_str(T_CE);
9927 screen_start(); /* don't know where cursor is now */
9928 }
9929 }
9930
9931#ifdef FEAT_GUI
9932 gui_can_update_cursor();
9933 if (gui.in_use)
9934 out_flush(); /* always flush after a scroll */
9935#endif
9936 return OK;
9937}
9938
9939/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009940 * Delete lines on the screen and update ScreenLines[].
9941 * "end" is the line after the scrolled part. Normally it is Rows.
9942 * When scrolling region used "off" is the offset from the top for the region.
9943 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 *
9945 * Return OK for success, FAIL if the lines are not deleted.
9946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009948screen_del_lines(
9949 int off,
9950 int row,
9951 int line_count,
9952 int end,
9953 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009954 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009955 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956{
9957 int j;
9958 int i;
9959 unsigned temp;
9960 int cursor_row;
9961 int cursor_end;
9962 int result_empty; /* result is empty until end of region */
9963 int can_delete; /* deleting line codes can be used */
9964 int type;
9965
9966 /*
9967 * FAIL if
9968 * - there is no valid screen
9969 * - the screen has to be redrawn completely
9970 * - the line count is less than one
9971 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009972 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009974 if (!screen_valid(TRUE) || line_count <= 0
9975 || (!force && line_count > p_ttyscroll)
9976#ifdef FEAT_CLIPBOARD
9977 || (clip_star.state != SELECT_CLEARED
9978 && redrawing_for_callback > 0)
9979#endif
9980 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 return FAIL;
9982
9983 /*
9984 * Check if the rest of the current region will become empty.
9985 */
9986 result_empty = row + line_count >= end;
9987
9988 /*
9989 * We can delete lines only when 'db' flag not set or when 'ce' option
9990 * available.
9991 */
9992 can_delete = (*T_DB == NUL || can_clear(T_CE));
9993
9994 /*
9995 * There are six ways to delete lines:
9996 * 0. When in a vertically split window and t_CV isn't set, redraw the
9997 * characters from ScreenLines[].
9998 * 1. Use T_CD if it exists and the result is empty.
9999 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10000 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10001 * none of the other ways work.
10002 * 4. Use T_CE (erase line) if the result is empty.
10003 * 5. Use T_DL (delete line) if it exists.
10004 * 6. redraw the characters from ScreenLines[].
10005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10007 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010008 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 type = USE_T_CD;
10010#if defined(__BEOS__) && defined(BEOS_DR8)
10011 /*
10012 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10013 * its internal termcap... this works okay for tests which test *T_DB !=
10014 * NUL. It has the disadvantage that the user cannot use any :set t_*
10015 * command to get T_DB (back) to empty_option, only :set term=... will do
10016 * the trick...
10017 * Anyway, this hack will hopefully go away with the next OS release.
10018 * (Olaf Seibert)
10019 */
10020 else if (row == 0 && T_DB == empty_option
10021 && (line_count == 1 || *T_CDL == NUL))
10022#else
10023 else if (row == 0 && (
10024#ifndef AMIGA
10025 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10026 * up, so use delete-line command */
10027 line_count == 1 ||
10028#endif
10029 *T_CDL == NUL))
10030#endif
10031 type = USE_NL;
10032 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10033 type = USE_T_CDL;
10034 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010035 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 type = USE_T_CE;
10037 else if (*T_DL != NUL && can_delete)
10038 type = USE_T_DL;
10039 else if (*T_CDL != NUL && can_delete)
10040 type = USE_T_CDL;
10041 else
10042 return FAIL;
10043
10044#ifdef FEAT_CLIPBOARD
10045 /* Remove a modeless selection when deleting lines halfway the screen or
10046 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010047 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010048 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 else
10050 clip_scroll_selection(line_count);
10051#endif
10052
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053#ifdef FEAT_GUI
10054 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10055 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010056 gui_dont_update_cursor(gui.cursor_row >= row + off
10057 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058#endif
10059
10060 if (*T_CCS != NUL) /* cursor relative to region */
10061 {
10062 cursor_row = row;
10063 cursor_end = end;
10064 }
10065 else
10066 {
10067 cursor_row = row + off;
10068 cursor_end = end + off;
10069 }
10070
10071 /*
10072 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10073 * Clear the inserted lines in ScreenLines[].
10074 */
10075 row += off;
10076 end += off;
10077 for (i = 0; i < line_count; ++i)
10078 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 if (wp != NULL && wp->w_width != Columns)
10080 {
10081 /* need to copy part of a line */
10082 j = row + i;
10083 while ((j += line_count) <= end - 1)
10084 linecopy(j - line_count, j, wp);
10085 j -= line_count;
10086 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010087 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10088 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 else
10090 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10091 LineWraps[j] = FALSE;
10092 }
10093 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 {
10095 /* whole width, moving the line pointers is faster */
10096 j = row + i;
10097 temp = LineOffset[j];
10098 while ((j += line_count) <= end - 1)
10099 {
10100 LineOffset[j - line_count] = LineOffset[j];
10101 LineWraps[j - line_count] = LineWraps[j];
10102 }
10103 LineOffset[j - line_count] = temp;
10104 LineWraps[j - line_count] = FALSE;
10105 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010106 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 else
10108 lineinvalid(temp, (int)Columns);
10109 }
10110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010112 if (screen_attr != clear_attr)
10113 screen_stop_highlight();
10114 if (clear_attr != 0)
10115 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 /* redraw the characters */
10118 if (type == USE_REDRAW)
10119 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010120 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 {
10122 windgoto(cursor_row, 0);
10123 out_str(T_CD);
10124 screen_start(); /* don't know where cursor is now */
10125 }
10126 else if (type == USE_T_CDL)
10127 {
10128 windgoto(cursor_row, 0);
10129 term_delete_lines(line_count);
10130 screen_start(); /* don't know where cursor is now */
10131 }
10132 /*
10133 * Deleting lines at top of the screen or scroll region: Just scroll
10134 * the whole screen (scroll region) up by outputting newlines on the
10135 * last line.
10136 */
10137 else if (type == USE_NL)
10138 {
10139 windgoto(cursor_end - 1, 0);
10140 for (i = line_count; --i >= 0; )
10141 out_char('\n'); /* cursor will remain on same line */
10142 }
10143 else
10144 {
10145 for (i = line_count; --i >= 0; )
10146 {
10147 if (type == USE_T_DL)
10148 {
10149 windgoto(cursor_row, 0);
10150 out_str(T_DL); /* delete a line */
10151 }
10152 else /* type == USE_T_CE */
10153 {
10154 windgoto(cursor_row + i, 0);
10155 out_str(T_CE); /* erase a line */
10156 }
10157 screen_start(); /* don't know where cursor is now */
10158 }
10159 }
10160
10161 /*
10162 * If the 'db' flag is set, we need to clear the lines that have been
10163 * scrolled up at the bottom of the region.
10164 */
10165 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10166 {
10167 for (i = line_count; i > 0; --i)
10168 {
10169 windgoto(cursor_end - i, 0);
10170 out_str(T_CE); /* erase a line */
10171 screen_start(); /* don't know where cursor is now */
10172 }
10173 }
10174
10175#ifdef FEAT_GUI
10176 gui_can_update_cursor();
10177 if (gui.in_use)
10178 out_flush(); /* always flush after a scroll */
10179#endif
10180
10181 return OK;
10182}
10183
10184/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010185 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 *
10187 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10188 * If clear_cmdline is FALSE there may be a message there that needs to be
10189 * cleared only if a mode is shown.
10190 * Return the length of the message (0 if no message).
10191 */
10192 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010193showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194{
10195 int need_clear;
10196 int length = 0;
10197 int do_mode;
10198 int attr;
10199 int nwr_save;
10200#ifdef FEAT_INS_EXPAND
10201 int sub_attr;
10202#endif
10203
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010204 do_mode = ((p_smd && msg_silent == 0)
10205 && ((State & INSERT)
10206 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010207 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 if (do_mode || Recording)
10209 {
10210 /*
10211 * Don't show mode right now, when not redrawing or inside a mapping.
10212 * Call char_avail() only when we are going to show something, because
10213 * it takes a bit of time.
10214 */
10215 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10216 {
10217 redraw_cmdline = TRUE; /* show mode later */
10218 return 0;
10219 }
10220
10221 nwr_save = need_wait_return;
10222
10223 /* wait a bit before overwriting an important message */
10224 check_for_delay(FALSE);
10225
10226 /* if the cmdline is more than one line high, erase top lines */
10227 need_clear = clear_cmdline;
10228 if (clear_cmdline && cmdline_row < Rows - 1)
10229 msg_clr_cmdline(); /* will reset clear_cmdline */
10230
10231 /* Position on the last line in the window, column 0 */
10232 msg_pos_mode();
10233 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010234 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 if (do_mode)
10236 {
10237 MSG_PUTS_ATTR("--", attr);
10238#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010239 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010240# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010241 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010242# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010243 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010244# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010245 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010246# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 MSG_PUTS_ATTR(" IM", attr);
10248# else
10249 MSG_PUTS_ATTR(" XIM", attr);
10250# endif
10251#endif
10252#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10253 if (gui.in_use)
10254 {
10255 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010256 {
10257 /* HANGUL */
10258 if (enc_utf8)
10259 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10260 else
10261 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 }
10264#endif
10265#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010266 /* CTRL-X in Insert mode */
10267 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 {
10269 /* These messages can get long, avoid a wrap in a narrow
10270 * window. Prefer showing edit_submode_extra. */
10271 length = (Rows - msg_row) * Columns - 3;
10272 if (edit_submode_extra != NULL)
10273 length -= vim_strsize(edit_submode_extra);
10274 if (length > 0)
10275 {
10276 if (edit_submode_pre != NULL)
10277 length -= vim_strsize(edit_submode_pre);
10278 if (length - vim_strsize(edit_submode) > 0)
10279 {
10280 if (edit_submode_pre != NULL)
10281 msg_puts_attr(edit_submode_pre, attr);
10282 msg_puts_attr(edit_submode, attr);
10283 }
10284 if (edit_submode_extra != NULL)
10285 {
10286 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10287 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010288 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 else
10290 sub_attr = attr;
10291 msg_puts_attr(edit_submode_extra, sub_attr);
10292 }
10293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 }
10295 else
10296#endif
10297 {
10298#ifdef FEAT_VREPLACE
10299 if (State & VREPLACE_FLAG)
10300 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10301 else
10302#endif
10303 if (State & REPLACE_FLAG)
10304 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10305 else if (State & INSERT)
10306 {
10307#ifdef FEAT_RIGHTLEFT
10308 if (p_ri)
10309 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10310#endif
10311 MSG_PUTS_ATTR(_(" INSERT"), attr);
10312 }
10313 else if (restart_edit == 'I')
10314 MSG_PUTS_ATTR(_(" (insert)"), attr);
10315 else if (restart_edit == 'R')
10316 MSG_PUTS_ATTR(_(" (replace)"), attr);
10317 else if (restart_edit == 'V')
10318 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10319#ifdef FEAT_RIGHTLEFT
10320 if (p_hkmap)
10321 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10322# ifdef FEAT_FKMAP
10323 if (p_fkmap)
10324 MSG_PUTS_ATTR(farsi_text_5, attr);
10325# endif
10326#endif
10327#ifdef FEAT_KEYMAP
10328 if (State & LANGMAP)
10329 {
10330# ifdef FEAT_ARABIC
10331 if (curwin->w_p_arab)
10332 MSG_PUTS_ATTR(_(" Arabic"), attr);
10333 else
10334# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010335 if (get_keymap_str(curwin, (char_u *)" (%s)",
10336 NameBuff, MAXPATHL))
10337 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 }
10339#endif
10340 if ((State & INSERT) && p_paste)
10341 MSG_PUTS_ATTR(_(" (paste)"), attr);
10342
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 if (VIsual_active)
10344 {
10345 char *p;
10346
10347 /* Don't concatenate separate words to avoid translation
10348 * problems. */
10349 switch ((VIsual_select ? 4 : 0)
10350 + (VIsual_mode == Ctrl_V) * 2
10351 + (VIsual_mode == 'V'))
10352 {
10353 case 0: p = N_(" VISUAL"); break;
10354 case 1: p = N_(" VISUAL LINE"); break;
10355 case 2: p = N_(" VISUAL BLOCK"); break;
10356 case 4: p = N_(" SELECT"); break;
10357 case 5: p = N_(" SELECT LINE"); break;
10358 default: p = N_(" SELECT BLOCK"); break;
10359 }
10360 MSG_PUTS_ATTR(_(p), attr);
10361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 MSG_PUTS_ATTR(" --", attr);
10363 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010364
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 need_clear = TRUE;
10366 }
10367 if (Recording
10368#ifdef FEAT_INS_EXPAND
10369 && edit_submode == NULL /* otherwise it gets too long */
10370#endif
10371 )
10372 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010373 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 need_clear = TRUE;
10375 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010376
10377 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 if (need_clear || clear_cmdline)
10379 msg_clr_eos();
10380 msg_didout = FALSE; /* overwrite this message */
10381 length = msg_col;
10382 msg_col = 0;
10383 need_wait_return = nwr_save; /* never ask for hit-return for this */
10384 }
10385 else if (clear_cmdline && msg_silent == 0)
10386 /* Clear the whole command line. Will reset "clear_cmdline". */
10387 msg_clr_cmdline();
10388
10389#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 /* In Visual mode the size of the selected area must be redrawn. */
10391 if (VIsual_active)
10392 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393
10394 /* If the last window has no status line, the ruler is after the mode
10395 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010396 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 win_redr_ruler(lastwin, TRUE);
10398#endif
10399 redraw_cmdline = FALSE;
10400 clear_cmdline = FALSE;
10401
10402 return length;
10403}
10404
10405/*
10406 * Position for a mode message.
10407 */
10408 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010409msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410{
10411 msg_col = 0;
10412 msg_row = Rows - 1;
10413}
10414
10415/*
10416 * Delete mode message. Used when ESC is typed which is expected to end
10417 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010418 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 */
10420 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010421unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422{
10423 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010424 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 */
10426 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10427 redraw_cmdline = TRUE; /* delete mode later */
10428 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010429 clearmode();
10430}
10431
10432/*
10433 * Clear the mode message.
10434 */
10435 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010436clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010437{
10438 msg_pos_mode();
10439 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010440 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010441 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442}
10443
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010444 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010445recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010446{
10447 MSG_PUTS_ATTR(_("recording"), attr);
10448 if (!shortmess(SHM_RECORDING))
10449 {
10450 char_u s[4];
10451 sprintf((char *)s, " @%c", Recording);
10452 MSG_PUTS_ATTR(s, attr);
10453 }
10454}
10455
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010456/*
10457 * Draw the tab pages line at the top of the Vim window.
10458 */
10459 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010460draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010461{
10462 int tabcount = 0;
10463 tabpage_T *tp;
10464 int tabwidth;
10465 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010466 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010467 int attr;
10468 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010469 win_T *cwp;
10470 int wincount;
10471 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010472 int c;
10473 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010474 int attr_sel = HL_ATTR(HLF_TPS);
10475 int attr_nosel = HL_ATTR(HLF_TP);
10476 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010477 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010478 int room;
10479 int use_sep_chars = (t_colors < 8
10480#ifdef FEAT_GUI
10481 && !gui.in_use
10482#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010483#ifdef FEAT_TERMGUICOLORS
10484 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010485#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010486 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010487
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010488 if (ScreenLines == NULL)
10489 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010490 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010491
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010492#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010493 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010494 if (gui_use_tabline())
10495 {
10496 gui_update_tabline();
10497 return;
10498 }
10499#endif
10500
10501 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010502 return;
10503
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010504#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010505
10506 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10507 for (scol = 0; scol < Columns; ++scol)
10508 TabPageIdxs[scol] = 0;
10509
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010510 /* Use the 'tabline' option if it's set. */
10511 if (*p_tal != NUL)
10512 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010513 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010514
10515 /* Check for an error. If there is one we would loop in redrawing the
10516 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010517 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010518 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010519 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010520 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010521 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010522 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010523 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010524 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010525#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010526 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010527 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010528 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010529
Bram Moolenaar238a5642006-02-21 22:12:05 +000010530 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10531 if (tabwidth < 6)
10532 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010533
Bram Moolenaar238a5642006-02-21 22:12:05 +000010534 attr = attr_nosel;
10535 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010536 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010537 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10538 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010539 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010540 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010541
Bram Moolenaar238a5642006-02-21 22:12:05 +000010542 if (tp->tp_topframe == topframe)
10543 attr = attr_sel;
10544 if (use_sep_chars && col > 0)
10545 screen_putchar('|', 0, col++, attr);
10546
10547 if (tp->tp_topframe != topframe)
10548 attr = attr_nosel;
10549
10550 screen_putchar(' ', 0, col++, attr);
10551
10552 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010553 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010554 cwp = curwin;
10555 wp = firstwin;
10556 }
10557 else
10558 {
10559 cwp = tp->tp_curwin;
10560 wp = tp->tp_firstwin;
10561 }
10562
10563 modified = FALSE;
10564 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10565 if (bufIsChanged(wp->w_buffer))
10566 modified = TRUE;
10567 if (modified || wincount > 1)
10568 {
10569 if (wincount > 1)
10570 {
10571 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010572 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010573 if (col + len >= Columns - 3)
10574 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010575 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010576#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010577 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010578#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010579 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010580#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010581 );
10582 col += len;
10583 }
10584 if (modified)
10585 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10586 screen_putchar(' ', 0, col++, attr);
10587 }
10588
10589 room = scol - col + tabwidth - 1;
10590 if (room > 0)
10591 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010592 /* Get buffer name in NameBuff[] */
10593 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010594 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010595 len = vim_strsize(NameBuff);
10596 p = NameBuff;
10597#ifdef FEAT_MBYTE
10598 if (has_mbyte)
10599 while (len > room)
10600 {
10601 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010602 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010603 }
10604 else
10605#endif
10606 if (len > room)
10607 {
10608 p += len - room;
10609 len = room;
10610 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010611 if (len > Columns - col - 1)
10612 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010614 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010615 col += len;
10616 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010617 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010618
10619 /* Store the tab page number in TabPageIdxs[], so that
10620 * jump_to_mouse() knows where each one is. */
10621 ++tabcount;
10622 while (scol < col)
10623 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010624 }
10625
Bram Moolenaar238a5642006-02-21 22:12:05 +000010626 if (use_sep_chars)
10627 c = '_';
10628 else
10629 c = ' ';
10630 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010631
10632 /* Put an "X" for closing the current tab if there are several. */
10633 if (first_tabpage->tp_next != NULL)
10634 {
10635 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10636 TabPageIdxs[Columns - 1] = -999;
10637 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010638 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010639
10640 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10641 * set. */
10642 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010643}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010644
10645/*
10646 * Get buffer name for "buf" into NameBuff[].
10647 * Takes care of special buffer names and translates special characters.
10648 */
10649 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010650get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010651{
10652 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010653 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010654 else
10655 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10656 trans_characters(NameBuff, MAXPATHL);
10657}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010658
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659/*
10660 * Get the character to use in a status line. Get its attributes in "*attr".
10661 */
10662 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010663fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664{
10665 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010666
10667#ifdef FEAT_TERMINAL
10668 if (bt_terminal(wp->w_buffer))
10669 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010670 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010671 {
10672 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010673 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010674 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010675 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010676 {
10677 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010678 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010679 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010680 }
10681 else
10682#endif
10683 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010685 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 fill = fill_stl;
10687 }
10688 else
10689 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010690 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 fill = fill_stlnc;
10692 }
10693 /* Use fill when there is highlighting, and highlighting of current
10694 * window differs, or the fillchars differ, or this is not the
10695 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010696 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010697 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 || (fill_stl != fill_stlnc)))
10699 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010700 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 return '^';
10702 return '=';
10703}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705/*
10706 * Get the character to use in a separator between vertically split windows.
10707 * Get its attributes in "*attr".
10708 */
10709 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010710fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010712 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 if (*attr == 0 && fill_vert == ' ')
10714 return '|';
10715 else
10716 return fill_vert;
10717}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718
10719/*
10720 * Return TRUE if redrawing should currently be done.
10721 */
10722 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010723redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010725#ifdef FEAT_EVAL
10726 if (disable_redraw_for_testing)
10727 return 0;
10728 else
10729#endif
10730 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10732}
10733
10734/*
10735 * Return TRUE if printing messages should currently be done.
10736 */
10737 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010738messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739{
10740 return (!(p_lz && char_avail() && !KeyTyped));
10741}
10742
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010743#ifdef FEAT_MENU
10744/*
10745 * Draw the window toolbar.
10746 */
10747 static void
10748redraw_win_toolbar(win_T *wp)
10749{
10750 vimmenu_T *menu;
10751 int item_idx = 0;
10752 int item_count = 0;
10753 int col = 0;
10754 int next_col;
10755 int off = (int)(current_ScreenLine - ScreenLines);
10756 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10757 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10758
10759 vim_free(wp->w_winbar_items);
10760 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10761 ++item_count;
10762 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10763 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10764
10765 /* TODO: use fewer spaces if there is not enough room */
10766 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010767 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010768 {
10769 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010770 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010771 break;
10772 if (col > 1)
10773 {
10774 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010775 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010776 break;
10777 }
10778
10779 wp->w_winbar_items[item_idx].wb_startcol = col;
10780 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010781 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010782 break;
10783
10784 next_col = text_to_screenline(wp, menu->name, col);
10785 while (col < next_col)
10786 {
10787 ScreenAttrs[off + col] = button_attr;
10788 ++col;
10789 }
10790 wp->w_winbar_items[item_idx].wb_endcol = col;
10791 wp->w_winbar_items[item_idx].wb_menu = menu;
10792 ++item_idx;
10793
Bram Moolenaar02631462017-09-22 15:20:32 +020010794 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010795 break;
10796 space_to_screenline(off + col, button_attr);
10797 ++col;
10798 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010799 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010800 {
10801 space_to_screenline(off + col, fill_attr);
10802 ++col;
10803 }
10804 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10805
Bram Moolenaar02631462017-09-22 15:20:32 +020010806 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10807 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010808}
10809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810/*
10811 * Show current status info in ruler and various other places
10812 * If always is FALSE, only show ruler if position has changed.
10813 */
10814 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010815showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816{
10817 if (!always && !redrawing())
10818 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010819#ifdef FEAT_INS_EXPAND
10820 if (pum_visible())
10821 {
10822 /* Don't redraw right now, do it later. */
10823 curwin->w_redr_status = TRUE;
10824 return;
10825 }
10826#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010827#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010828 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010829 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010830 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 else
10833#endif
10834#ifdef FEAT_CMDL_INFO
10835 win_redr_ruler(curwin, always);
10836#endif
10837
10838#ifdef FEAT_TITLE
10839 if (need_maketitle
10840# ifdef FEAT_STL_OPT
10841 || (p_icon && (stl_syntax & STL_IN_ICON))
10842 || (p_title && (stl_syntax & STL_IN_TITLE))
10843# endif
10844 )
10845 maketitle();
10846#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010847 /* Redraw the tab pages line if needed. */
10848 if (redraw_tabline)
10849 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850}
10851
10852#ifdef FEAT_CMDL_INFO
10853 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010854win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010856#define RULER_BUF_LEN 70
10857 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 int row;
10859 int fillchar;
10860 int attr;
10861 int empty_line = FALSE;
10862 colnr_T virtcol;
10863 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010864 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 int this_ru_col;
10867 int off = 0;
10868 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869
10870 /* If 'ruler' off or redrawing disabled, don't do anything */
10871 if (!p_ru)
10872 return;
10873
10874 /*
10875 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10876 * after deleting lines, before cursor.lnum is corrected.
10877 */
10878 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10879 return;
10880
10881#ifdef FEAT_INS_EXPAND
10882 /* Don't draw the ruler while doing insert-completion, it might overwrite
10883 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 if (edit_submode != NULL)
10886 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010887 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10888 if (pum_visible())
10889 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890#endif
10891
10892#ifdef FEAT_STL_OPT
10893 if (*p_ruf)
10894 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010895 int save_called_emsg = called_emsg;
10896
10897 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010899 if (called_emsg)
10900 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010901 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010902 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 return;
10904 }
10905#endif
10906
10907 /*
10908 * Check if not in Insert mode and the line is empty (will show "0-1").
10909 */
10910 if (!(State & INSERT)
10911 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10912 empty_line = TRUE;
10913
10914 /*
10915 * Only draw the ruler when something changed.
10916 */
10917 validate_virtcol_win(wp);
10918 if ( redraw_cmdline
10919 || always
10920 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10921 || wp->w_cursor.col != wp->w_ru_cursor.col
10922 || wp->w_virtcol != wp->w_ru_virtcol
10923#ifdef FEAT_VIRTUALEDIT
10924 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10925#endif
10926 || wp->w_topline != wp->w_ru_topline
10927 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10928#ifdef FEAT_DIFF
10929 || wp->w_topfill != wp->w_ru_topfill
10930#endif
10931 || empty_line != wp->w_ru_empty)
10932 {
10933 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 if (wp->w_status_height)
10935 {
10936 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010937 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010938 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010939 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 }
10941 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 {
10943 row = Rows - 1;
10944 fillchar = ' ';
10945 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 width = Columns;
10947 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 }
10949
10950 /* In list mode virtcol needs to be recomputed */
10951 virtcol = wp->w_virtcol;
10952 if (wp->w_p_list && lcs_tab1 == NUL)
10953 {
10954 wp->w_p_list = FALSE;
10955 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10956 wp->w_p_list = TRUE;
10957 }
10958
10959 /*
10960 * Some sprintfs return the length, some return a pointer.
10961 * To avoid portability problems we use strlen() here.
10962 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010963 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10965 ? 0L
10966 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010967 len = STRLEN(buffer);
10968 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10970 (int)virtcol + 1);
10971
10972 /*
10973 * Add a "50%" if there is room for it.
10974 * On the last line, don't print in the last column (scrolls the
10975 * screen up on some terminals).
10976 */
10977 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010978 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 this_ru_col = ru_col - (Columns - width);
10983 if (this_ru_col < 0)
10984 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 /* Never use more than half the window/screen width, leave the other
10986 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010987 if (this_ru_col < (width + 1) / 2)
10988 this_ru_col = (width + 1) / 2;
10989 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010991 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010992 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 {
10994#ifdef FEAT_MBYTE
10995 if (has_mbyte)
10996 i += (*mb_char2bytes)(fillchar, buffer + i);
10997 else
10998#endif
10999 buffer[i++] = fillchar;
11000 ++o;
11001 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011002 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 }
11004 /* Truncate at window boundary. */
11005#ifdef FEAT_MBYTE
11006 if (has_mbyte)
11007 {
11008 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011009 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 {
11011 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011012 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 {
11014 buffer[i] = NUL;
11015 break;
11016 }
11017 }
11018 }
11019 else
11020#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011021 if (this_ru_col + (int)STRLEN(buffer) > width)
11022 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023
Bram Moolenaar4033c552017-09-16 20:54:51 +020011024 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 i = redraw_cmdline;
11026 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011027 this_ru_col + off + (int)STRLEN(buffer),
11028 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 fillchar, fillchar, attr);
11030 /* don't redraw the cmdline because of showing the ruler */
11031 redraw_cmdline = i;
11032 wp->w_ru_cursor = wp->w_cursor;
11033 wp->w_ru_virtcol = wp->w_virtcol;
11034 wp->w_ru_empty = empty_line;
11035 wp->w_ru_topline = wp->w_topline;
11036 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11037#ifdef FEAT_DIFF
11038 wp->w_ru_topfill = wp->w_topfill;
11039#endif
11040 }
11041}
11042#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011043
11044#if defined(FEAT_LINEBREAK) || defined(PROTO)
11045/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011046 * Return the width of the 'number' and 'relativenumber' column.
11047 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011048 * Otherwise it depends on 'numberwidth' and the line count.
11049 */
11050 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011051number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011052{
11053 int n;
11054 linenr_T lnum;
11055
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011056 if (wp->w_p_rnu && !wp->w_p_nu)
11057 /* cursor line shows "0" */
11058 lnum = wp->w_height;
11059 else
11060 /* cursor line shows absolute line number */
11061 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011062
Bram Moolenaar6b314672015-03-20 15:42:10 +010011063 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011064 return wp->w_nrwidth_width;
11065 wp->w_nrwidth_line_count = lnum;
11066
11067 n = 0;
11068 do
11069 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011070 lnum /= 10;
11071 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011072 } while (lnum > 0);
11073
11074 /* 'numberwidth' gives the minimal width plus one */
11075 if (n < wp->w_p_nuw - 1)
11076 n = wp->w_p_nuw - 1;
11077
11078 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011079 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011080 return n;
11081}
11082#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011083
11084/*
11085 * Return the current cursor column. This is the actual position on the
11086 * screen. First column is 0.
11087 */
11088 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011089screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011090{
11091 return screen_cur_col;
11092}
11093
11094/*
11095 * Return the current cursor row. This is the actual position on the screen.
11096 * First row is 0.
11097 */
11098 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011099screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011100{
11101 return screen_cur_row;
11102}