blob: 9f39edf04f91d963959d03d9fed27c93bfa29861 [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 Moolenaar06f1ed22017-06-18 22:41:03 +0200134static int win_line(win_T *, linenr_T, int, int, int nochange, proftime_T *syntax_tm);
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{
206 if (wp->w_redr_type < type)
207 {
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();
471 out_flush();
472#ifdef FEAT_GUI
473 if (gui.in_use)
474 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200475 /* Don't update the cursor when it is blinking and off to avoid
476 * flicker. */
477 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200478 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100479 gui_mch_flush();
480 }
481#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200482
483 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100484}
485
486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 * Changed something in the current window, at buffer line "lnum", that
488 * requires that line and possibly other lines to be redrawn.
489 * Used when entering/leaving Insert mode with the cursor on a folded line.
490 * Used to remove the "$" from a change command.
491 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
492 * may become invalid and the whole window will have to be redrawn.
493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100495redrawWinline(
496 linenr_T lnum,
497 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499#ifdef FEAT_FOLDING
500 int i;
501#endif
502
503 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
504 curwin->w_redraw_top = lnum;
505 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
506 curwin->w_redraw_bot = lnum;
507 redraw_later(VALID);
508
509#ifdef FEAT_FOLDING
510 if (invalid)
511 {
512 /* A w_lines[] entry for this lnum has become invalid. */
513 i = find_wl_entry(curwin, lnum);
514 if (i >= 0)
515 curwin->w_lines[i].wl_valid = FALSE;
516 }
517#endif
518}
519
520/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200521 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 */
523 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100524update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
526 redraw_curbuf_later(type);
527 update_screen(type);
528}
529
530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 * Based on the current value of curwin->w_topline, transfer a screenfull
532 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200533 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200535 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200536update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200538 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 win_T *wp;
540 static int did_intro = FALSE;
541#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
542 int did_one;
543#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200544#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200545 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200546 int gui_cursor_col;
547 int gui_cursor_row;
548#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200549 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000551 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200553 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200555 if (type == VALID_NO_UPDATE)
556 {
557 no_update = TRUE;
558 type = 0;
559 }
560
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 if (must_redraw)
562 {
563 if (type < must_redraw) /* use maximal type */
564 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000565
566 /* must_redraw is reset here, so that when we run into some weird
567 * reason to redraw while busy redrawing (e.g., asynchronous
568 * scrolling), or update_topline() in win_update() will cause a
569 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 must_redraw = 0;
571 }
572
573 /* Need to update w_lines[]. */
574 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
575 type = NOT_VALID;
576
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000577 /* Postpone the redrawing when it's not needed and when being called
578 * recursively. */
579 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {
581 redraw_later(type); /* remember type for next time */
582 must_redraw = type;
583 if (type > INVERTED_ALL)
584 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200585 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587
588 updating_screen = TRUE;
589#ifdef FEAT_SYN_HL
590 ++display_tick; /* let syntax code know we're in a next round of
591 * display updating */
592#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200593 if (no_update)
594 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
596 /*
597 * if the screen was scrolled up when displaying a message, scroll it down
598 */
599 if (msg_scrolled)
600 {
601 clear_cmdline = TRUE;
602 if (msg_scrolled > Rows - 5) /* clearing is faster */
603 type = CLEAR;
604 else if (type != CLEAR)
605 {
606 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200607 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
608 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 type = CLEAR;
610 FOR_ALL_WINDOWS(wp)
611 {
612 if (W_WINROW(wp) < msg_scrolled)
613 {
614 if (W_WINROW(wp) + wp->w_height > msg_scrolled
615 && wp->w_redr_type < REDRAW_TOP
616 && wp->w_lines_valid > 0
617 && wp->w_topline == wp->w_lines[0].wl_lnum)
618 {
619 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
620 wp->w_redr_type = REDRAW_TOP;
621 }
622 else
623 {
624 wp->w_redr_type = NOT_VALID;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
626 <= msg_scrolled)
627 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629 }
630 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200631 if (!no_update)
632 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000633 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 }
635 msg_scrolled = 0;
636 need_wait_return = FALSE;
637 }
638
639 /* reset cmdline_row now (may have been changed temporarily) */
640 compute_cmdrow();
641
642 /* Check for changed highlighting */
643 if (need_highlight_changed)
644 highlight_changed();
645
646 if (type == CLEAR) /* first clear screen */
647 {
648 screenclear(); /* will reset clear_cmdline */
649 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200650 /* must_redraw may be set indirectly, avoid another redraw later */
651 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
653
654 if (clear_cmdline) /* going to clear cmdline (done below) */
655 check_for_delay(FALSE);
656
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000657#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200658 /* Force redraw when width of 'number' or 'relativenumber' column
659 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000660 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200661 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
662 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000663 curwin->w_redr_type = NOT_VALID;
664#endif
665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 /*
667 * Only start redrawing if there is really something to do.
668 */
669 if (type == INVERTED)
670 update_curswant();
671 if (curwin->w_redr_type < type
672 && !((type == VALID
673 && curwin->w_lines[0].wl_valid
674#ifdef FEAT_DIFF
675 && curwin->w_topfill == curwin->w_old_topfill
676 && curwin->w_botfill == curwin->w_old_botfill
677#endif
678 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000680 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
682 && curwin->w_old_visual_mode == VIsual_mode
683 && (curwin->w_valid & VALID_VIRTCOL)
684 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 ))
686 curwin->w_redr_type = type;
687
Bram Moolenaar5a305422006-04-28 22:38:25 +0000688 /* Redraw the tab pages line if needed. */
689 if (redraw_tabline || type >= NOT_VALID)
690 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000691
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692#ifdef FEAT_SYN_HL
693 /*
694 * Correct stored syntax highlighting info for changes in each displayed
695 * buffer. Each buffer must only be done once.
696 */
697 FOR_ALL_WINDOWS(wp)
698 {
699 if (wp->w_buffer->b_mod_set)
700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 win_T *wwp;
702
703 /* Check if we already did this buffer. */
704 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
705 if (wwp->w_buffer == wp->w_buffer)
706 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200707 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 syn_stack_apply_changes(wp->w_buffer);
709 }
710 }
711#endif
712
713 /*
714 * Go from top to bottom through the windows, redrawing the ones that need
715 * it.
716 */
717#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
718 did_one = FALSE;
719#endif
720#ifdef FEAT_SEARCH_EXTRA
721 search_hl.rm.regprog = NULL;
722#endif
723 FOR_ALL_WINDOWS(wp)
724 {
725 if (wp->w_redr_type != 0)
726 {
727 cursor_off();
728#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
729 if (!did_one)
730 {
731 did_one = TRUE;
732# ifdef FEAT_SEARCH_EXTRA
733 start_search_hl();
734# endif
735# ifdef FEAT_CLIPBOARD
736 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200737 if (clip_star.available && clip_isautosel_star())
738 clip_update_selection(&clip_star);
739 if (clip_plus.available && clip_isautosel_plus())
740 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741# endif
742#ifdef FEAT_GUI
743 /* Remove the cursor before starting to do anything, because
744 * scrolling may make it difficult to redraw the text under
745 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200746 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200747 {
748 gui_cursor_col = gui.cursor_col;
749 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200751 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753#endif
754 }
755#endif
756 win_update(wp);
757 }
758
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 /* redraw status line after the window to minimize cursor movement */
760 if (wp->w_redr_status)
761 {
762 cursor_off();
763 win_redr_status(wp);
764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 }
766#if defined(FEAT_SEARCH_EXTRA)
767 end_search_hl();
768#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100769#ifdef FEAT_INS_EXPAND
770 /* May need to redraw the popup menu. */
771 if (pum_visible())
772 pum_redraw();
773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 /* Reset b_mod_set flags. Going through all windows is probably faster
776 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200777 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 updating_screen = FALSE;
781#ifdef FEAT_GUI
782 gui_may_resize_shell();
783#endif
784
785 /* Clear or redraw the command line. Done last, because scrolling may
786 * mess up the command line. */
787 if (clear_cmdline || redraw_cmdline)
788 showmode();
789
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200790 if (no_update)
791 --no_win_do_lines_ins;
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200794 if (!did_intro)
795 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 did_intro = TRUE;
797
798#ifdef FEAT_GUI
799 /* Redraw the cursor and update the scrollbars when all screen updating is
800 * done. */
801 if (gui.in_use)
802 {
803 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200804 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200805 {
806 /* 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 Moolenaar65549bd2016-07-08 22:52:37 +0200814 screen_cur_col = gui.col;
815 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 gui_update_scrollbars(FALSE);
818 }
819#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200820 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821}
822
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100823#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
824/*
825 * Prepare for updating one or more windows.
826 * Caller must check for "updating_screen" already set to avoid recursiveness.
827 */
828 static void
829update_prepare(void)
830{
831 cursor_off();
832 updating_screen = TRUE;
833#ifdef FEAT_GUI
834 /* Remove the cursor before starting to do anything, because scrolling may
835 * make it difficult to redraw the text under it. */
836 if (gui.in_use)
837 gui_undraw_cursor();
838#endif
839#ifdef FEAT_SEARCH_EXTRA
840 start_search_hl();
841#endif
842}
843
844/*
845 * Finish updating one or more windows.
846 */
847 static void
848update_finish(void)
849{
850 if (redraw_cmdline)
851 showmode();
852
853# ifdef FEAT_SEARCH_EXTRA
854 end_search_hl();
855# endif
856
857 updating_screen = FALSE;
858
859# ifdef FEAT_GUI
860 gui_may_resize_shell();
861
862 /* Redraw the cursor and update the scrollbars when all screen updating is
863 * done. */
864 if (gui.in_use)
865 {
866 out_flush(); /* required before updating the cursor */
867 gui_update_cursor(FALSE, FALSE);
868 gui_update_scrollbars(FALSE);
869 }
870# endif
871}
872#endif
873
Bram Moolenaar860cae12010-06-05 23:22:07 +0200874#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200875/*
876 * Return TRUE if the cursor line in window "wp" may be concealed, according
877 * to the 'concealcursor' option.
878 */
879 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100880conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200881{
882 int c;
883
884 if (*wp->w_p_cocu == NUL)
885 return FALSE;
886 if (get_real_state() & VISUAL)
887 c = 'v';
888 else if (State & INSERT)
889 c = 'i';
890 else if (State & NORMAL)
891 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200892 else if (State & CMDLINE)
893 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200894 else
895 return FALSE;
896 return vim_strchr(wp->w_p_cocu, c) != NULL;
897}
898
899/*
900 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
901 */
902 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100903conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200904{
905 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
906 {
907 need_cursor_line_redraw = TRUE;
908 /* Need to recompute cursor column, e.g., when starting Visual mode
909 * without concealing. */
910 curs_columns(TRUE);
911 }
912}
913
Bram Moolenaar860cae12010-06-05 23:22:07 +0200914 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100915update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200916{
917 int row;
918 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200919#ifdef SYN_TIME_LIMIT
920 proftime_T syntax_tm;
921#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200922
Bram Moolenaar908be432016-05-24 10:51:30 +0200923 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100924 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200925 return;
926
Bram Moolenaar860cae12010-06-05 23:22:07 +0200927 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200928 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200929 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200930#ifdef SYN_TIME_LIMIT
931 /* Set the time limit to 'redrawtime'. */
932 profile_setlimit(p_rdt, &syntax_tm);
933#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100934 update_prepare();
935
Bram Moolenaar860cae12010-06-05 23:22:07 +0200936 row = 0;
937 for (j = 0; j < wp->w_lines_valid; ++j)
938 {
939 if (lnum == wp->w_lines[j].wl_lnum)
940 {
941 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200942# ifdef FEAT_SEARCH_EXTRA
943 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200944 start_search_hl();
945 prepare_search_hl(wp, lnum);
946# endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200947 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE,
948#ifdef SYN_TIME_LIMIT
949 &syntax_tm
950#else
951 NULL
952#endif
953 );
Bram Moolenaar860cae12010-06-05 23:22:07 +0200954# if defined(FEAT_SEARCH_EXTRA)
955 end_search_hl();
956# endif
957 break;
958 }
959 row += wp->w_lines[j].wl_size;
960 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100961
962 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200963 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200964 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965}
966#endif
967
968#if defined(FEAT_SIGNS) || defined(PROTO)
969 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100970update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971{
972 win_T *wp;
973 int doit = FALSE;
974
975# ifdef FEAT_FOLDING
976 win_foldinfo.fi_level = 0;
977# endif
978
979 /* update/delete a specific mark */
980 FOR_ALL_WINDOWS(wp)
981 {
982 if (buf != NULL && lnum > 0)
983 {
984 if (wp->w_buffer == buf && lnum >= wp->w_topline
985 && lnum < wp->w_botline)
986 {
987 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
988 wp->w_redraw_top = lnum;
989 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
990 wp->w_redraw_bot = lnum;
991 redraw_win_later(wp, VALID);
992 }
993 }
994 else
995 redraw_win_later(wp, VALID);
996 if (wp->w_redr_type != 0)
997 doit = TRUE;
998 }
999
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001000 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001001 * happening (recursive call), messages on the screen or still starting up.
1002 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001003 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001004 || State == ASKMORE || State == HITRETURN
1005 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001006#ifdef FEAT_GUI
1007 || gui.starting
1008#endif
1009 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 return;
1011
1012 /* update all windows that need updating */
1013 update_prepare();
1014
Bram Moolenaar29323592016-07-24 22:04:11 +02001015 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
1017 if (wp->w_redr_type != 0)
1018 win_update(wp);
1019 if (wp->w_redr_status)
1020 win_redr_status(wp);
1021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
1023 update_finish();
1024}
1025#endif
1026
1027
1028#if defined(FEAT_GUI) || defined(PROTO)
1029/*
1030 * Update a single window, its status line and maybe the command line msg.
1031 * Used for the GUI scrollbar.
1032 */
1033 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001034updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001036 /* return if already busy updating */
1037 if (updating_screen)
1038 return;
1039
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 update_prepare();
1041
1042#ifdef FEAT_CLIPBOARD
1043 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001044 if (clip_star.available && clip_isautosel_star())
1045 clip_update_selection(&clip_star);
1046 if (clip_plus.available && clip_isautosel_plus())
1047 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001051
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001052 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001053 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001054 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (wp->w_redr_status
1057# ifdef FEAT_CMDL_INFO
1058 || p_ru
1059# endif
1060# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001061 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062# endif
1063 )
1064 win_redr_status(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
1066 update_finish();
1067}
1068#endif
1069
1070/*
1071 * Update a single window.
1072 *
1073 * This may cause the windows below it also to be redrawn (when clearing the
1074 * screen or scrolling lines).
1075 *
1076 * How the window is redrawn depends on wp->w_redr_type. Each type also
1077 * implies the one below it.
1078 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001079 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1081 * INVERTED redraw the changed part of the Visual area
1082 * INVERTED_ALL redraw the whole Visual area
1083 * VALID 1. scroll up/down to adjust for a changed w_topline
1084 * 2. update lines at the top when scrolled down
1085 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001086 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 * b_mod_top and b_mod_bot.
1088 * - if wp->w_redraw_top non-zero, redraw lines between
1089 * wp->w_redraw_top and wp->w_redr_bot.
1090 * - continue redrawing when syntax status is invalid.
1091 * 4. if scrolled up, update lines at the bottom.
1092 * This results in three areas that may need updating:
1093 * top: from first row to top_end (when scrolled down)
1094 * mid: from mid_start to mid_end (update inversion or changed text)
1095 * bot: from bot_start to last row (when scrolled up)
1096 */
1097 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001098win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
1100 buf_T *buf = wp->w_buffer;
1101 int type;
1102 int top_end = 0; /* Below last row of the top area that needs
1103 updating. 0 when no top area updating. */
1104 int mid_start = 999;/* first row of the mid area that needs
1105 updating. 999 when no mid area updating. */
1106 int mid_end = 0; /* Below last row of the mid area that needs
1107 updating. 0 when no mid area updating. */
1108 int bot_start = 999;/* first row of the bot area that needs
1109 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 int scrolled_down = FALSE; /* TRUE when scrolled down when
1111 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001113 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 int top_to_mod = FALSE; /* redraw above mod_top */
1115#endif
1116
1117 int row; /* current window row to display */
1118 linenr_T lnum; /* current buffer lnum to display */
1119 int idx; /* current index in w_lines[] */
1120 int srow; /* starting row of the current line */
1121
1122 int eof = FALSE; /* if TRUE, we hit the end of the file */
1123 int didline = FALSE; /* if TRUE, we finished the last line */
1124 int i;
1125 long j;
1126 static int recursive = FALSE; /* being called recursively */
1127 int old_botline = wp->w_botline;
1128#ifdef FEAT_FOLDING
1129 long fold_count;
1130#endif
1131#ifdef FEAT_SYN_HL
1132 /* remember what happened to the previous line, to know if
1133 * check_visual_highlight() can be used */
1134#define DID_NONE 1 /* didn't update a line */
1135#define DID_LINE 2 /* updated a normal line */
1136#define DID_FOLD 3 /* updated a folded line */
1137 int did_update = DID_NONE;
1138 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1139#endif
1140 linenr_T mod_top = 0;
1141 linenr_T mod_bot = 0;
1142#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1143 int save_got_int;
1144#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001145#ifdef SYN_TIME_LIMIT
1146 proftime_T syntax_tm;
1147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148
1149 type = wp->w_redr_type;
1150
1151 if (type == NOT_VALID)
1152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 wp->w_lines_valid = 0;
1155 }
1156
1157 /* Window is zero-height: nothing to draw. */
1158 if (wp->w_height == 0)
1159 {
1160 wp->w_redr_type = 0;
1161 return;
1162 }
1163
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 /* Window is zero-width: Only need to draw the separator. */
1165 if (wp->w_width == 0)
1166 {
1167 /* draw the vertical separator right of this window */
1168 draw_vsep_win(wp, 0);
1169 wp->w_redr_type = 0;
1170 return;
1171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001173#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001174 /* If this window contains a terminal, redraw works completely differently.
1175 */
1176 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001177 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001178 wp->w_redr_type = 0;
1179 return;
1180 }
1181#endif
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001184 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#endif
1186
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001187#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001188 /* Force redraw when width of 'number' or 'relativenumber' column
1189 * changes. */
1190 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001191 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001192 {
1193 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001194 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001195 }
1196 else
1197#endif
1198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1200 {
1201 /*
1202 * When there are both inserted/deleted lines and specific lines to be
1203 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1204 * everything (only happens when redrawing is off for while).
1205 */
1206 type = NOT_VALID;
1207 }
1208 else
1209 {
1210 /*
1211 * Set mod_top to the first line that needs displaying because of
1212 * changes. Set mod_bot to the first line after the changes.
1213 */
1214 mod_top = wp->w_redraw_top;
1215 if (wp->w_redraw_bot != 0)
1216 mod_bot = wp->w_redraw_bot + 1;
1217 else
1218 mod_bot = 0;
1219 wp->w_redraw_top = 0; /* reset for next time */
1220 wp->w_redraw_bot = 0;
1221 if (buf->b_mod_set)
1222 {
1223 if (mod_top == 0 || mod_top > buf->b_mod_top)
1224 {
1225 mod_top = buf->b_mod_top;
1226#ifdef FEAT_SYN_HL
1227 /* Need to redraw lines above the change that may be included
1228 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001229 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001231 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (mod_top < 1)
1233 mod_top = 1;
1234 }
1235#endif
1236 }
1237 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1238 mod_bot = buf->b_mod_bot;
1239
1240#ifdef FEAT_SEARCH_EXTRA
1241 /* When 'hlsearch' is on and using a multi-line search pattern, a
1242 * change in one line may make the Search highlighting in a
1243 * previous line invalid. Simple solution: redraw all visible
1244 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001245 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001247 if (search_hl.rm.regprog != NULL
1248 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001250 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001251 {
1252 cur = wp->w_match_head;
1253 while (cur != NULL)
1254 {
1255 if (cur->match.regprog != NULL
1256 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001257 {
1258 top_to_mod = TRUE;
1259 break;
1260 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001261 cur = cur->next;
1262 }
1263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264#endif
1265 }
1266#ifdef FEAT_FOLDING
1267 if (mod_top != 0 && hasAnyFolding(wp))
1268 {
1269 linenr_T lnumt, lnumb;
1270
1271 /*
1272 * A change in a line can cause lines above it to become folded or
1273 * unfolded. Find the top most buffer line that may be affected.
1274 * If the line was previously folded and displayed, get the first
1275 * line of that fold. If the line is folded now, get the first
1276 * folded line. Use the minimum of these two.
1277 */
1278
1279 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1280 * the line below it. If there is no valid entry, use w_topline.
1281 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1282 * to this line. If there is no valid entry, use MAXLNUM. */
1283 lnumt = wp->w_topline;
1284 lnumb = MAXLNUM;
1285 for (i = 0; i < wp->w_lines_valid; ++i)
1286 if (wp->w_lines[i].wl_valid)
1287 {
1288 if (wp->w_lines[i].wl_lastlnum < mod_top)
1289 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1290 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1291 {
1292 lnumb = wp->w_lines[i].wl_lnum;
1293 /* When there is a fold column it might need updating
1294 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001295 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 ++lnumb;
1297 }
1298 }
1299
1300 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1301 if (mod_top > lnumt)
1302 mod_top = lnumt;
1303
1304 /* Now do the same for the bottom line (one above mod_bot). */
1305 --mod_bot;
1306 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1307 ++mod_bot;
1308 if (mod_bot < lnumb)
1309 mod_bot = lnumb;
1310 }
1311#endif
1312
1313 /* When a change starts above w_topline and the end is below
1314 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001315 * If the end of the change is above w_topline: do like no change was
1316 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 if (mod_top != 0 && mod_top < wp->w_topline)
1318 {
1319 if (mod_bot > wp->w_topline)
1320 mod_top = wp->w_topline;
1321#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001322 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 top_end = 1;
1324#endif
1325 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001326
1327 /* When line numbers are displayed need to redraw all lines below
1328 * inserted/deleted lines. */
1329 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1330 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332
1333 /*
1334 * When only displaying the lines at the top, set top_end. Used when
1335 * window has scrolled down for msg_scrolled.
1336 */
1337 if (type == REDRAW_TOP)
1338 {
1339 j = 0;
1340 for (i = 0; i < wp->w_lines_valid; ++i)
1341 {
1342 j += wp->w_lines[i].wl_size;
1343 if (j >= wp->w_upd_rows)
1344 {
1345 top_end = j;
1346 break;
1347 }
1348 }
1349 if (top_end == 0)
1350 /* not found (cannot happen?): redraw everything */
1351 type = NOT_VALID;
1352 else
1353 /* top area defined, the rest is VALID */
1354 type = VALID;
1355 }
1356
Bram Moolenaar367329b2007-08-30 11:53:22 +00001357 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001358 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1359 * non-zero and thus not FALSE) will indicate that screenclear() was not
1360 * called. */
1361 if (screen_cleared)
1362 screen_cleared = MAYBE;
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 /*
1365 * If there are no changes on the screen that require a complete redraw,
1366 * handle three cases:
1367 * 1: we are off the top of the screen by a few lines: scroll down
1368 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1369 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1370 * w_lines[] that needs updating.
1371 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001372 if ((type == VALID || type == SOME_VALID
1373 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374#ifdef FEAT_DIFF
1375 && !wp->w_botfill && !wp->w_old_botfill
1376#endif
1377 )
1378 {
1379 if (mod_top != 0 && wp->w_topline == mod_top)
1380 {
1381 /*
1382 * w_topline is the first changed line, the scrolling will be done
1383 * further down.
1384 */
1385 }
1386 else if (wp->w_lines[0].wl_valid
1387 && (wp->w_topline < wp->w_lines[0].wl_lnum
1388#ifdef FEAT_DIFF
1389 || (wp->w_topline == wp->w_lines[0].wl_lnum
1390 && wp->w_topfill > wp->w_old_topfill)
1391#endif
1392 ))
1393 {
1394 /*
1395 * New topline is above old topline: May scroll down.
1396 */
1397#ifdef FEAT_FOLDING
1398 if (hasAnyFolding(wp))
1399 {
1400 linenr_T ln;
1401
1402 /* count the number of lines we are off, counting a sequence
1403 * of folded lines as one */
1404 j = 0;
1405 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1406 {
1407 ++j;
1408 if (j >= wp->w_height - 2)
1409 break;
1410 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1411 }
1412 }
1413 else
1414#endif
1415 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1416 if (j < wp->w_height - 2) /* not too far off */
1417 {
1418 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1419#ifdef FEAT_DIFF
1420 /* insert extra lines for previously invisible filler lines */
1421 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1422 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1423 - wp->w_old_topfill;
1424#endif
1425 if (i < wp->w_height - 2) /* less than a screen off */
1426 {
1427 /*
1428 * Try to insert the correct number of lines.
1429 * If not the last window, delete the lines at the bottom.
1430 * win_ins_lines may fail when the terminal can't do it.
1431 */
1432 if (i > 0)
1433 check_for_delay(FALSE);
1434 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1435 {
1436 if (wp->w_lines_valid != 0)
1437 {
1438 /* Need to update rows that are new, stop at the
1439 * first one that scrolled down. */
1440 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
1443 /* Move the entries that were scrolled, disable
1444 * the entries for the lines to be redrawn. */
1445 if ((wp->w_lines_valid += j) > wp->w_height)
1446 wp->w_lines_valid = wp->w_height;
1447 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1448 wp->w_lines[idx] = wp->w_lines[idx - j];
1449 while (idx >= 0)
1450 wp->w_lines[idx--].wl_valid = FALSE;
1451 }
1452 }
1453 else
1454 mid_start = 0; /* redraw all lines */
1455 }
1456 else
1457 mid_start = 0; /* redraw all lines */
1458 }
1459 else
1460 mid_start = 0; /* redraw all lines */
1461 }
1462 else
1463 {
1464 /*
1465 * New topline is at or below old topline: May scroll up.
1466 * When topline didn't change, find first entry in w_lines[] that
1467 * needs updating.
1468 */
1469
1470 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1471 j = -1;
1472 row = 0;
1473 for (i = 0; i < wp->w_lines_valid; i++)
1474 {
1475 if (wp->w_lines[i].wl_valid
1476 && wp->w_lines[i].wl_lnum == wp->w_topline)
1477 {
1478 j = i;
1479 break;
1480 }
1481 row += wp->w_lines[i].wl_size;
1482 }
1483 if (j == -1)
1484 {
1485 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1486 * lines */
1487 mid_start = 0;
1488 }
1489 else
1490 {
1491 /*
1492 * Try to delete the correct number of lines.
1493 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1494 */
1495#ifdef FEAT_DIFF
1496 /* If the topline didn't change, delete old filler lines,
1497 * otherwise delete filler lines of the new topline... */
1498 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1499 row += wp->w_old_topfill;
1500 else
1501 row += diff_check_fill(wp, wp->w_topline);
1502 /* ... but don't delete new filler lines. */
1503 row -= wp->w_topfill;
1504#endif
1505 if (row > 0)
1506 {
1507 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001508 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1509 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 bot_start = wp->w_height - row;
1511 else
1512 mid_start = 0; /* redraw all lines */
1513 }
1514 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1515 {
1516 /*
1517 * Skip the lines (below the deleted lines) that are still
1518 * valid and don't need redrawing. Copy their info
1519 * upwards, to compensate for the deleted lines. Set
1520 * bot_start to the first row that needs redrawing.
1521 */
1522 bot_start = 0;
1523 idx = 0;
1524 for (;;)
1525 {
1526 wp->w_lines[idx] = wp->w_lines[j];
1527 /* stop at line that didn't fit, unless it is still
1528 * valid (no lines deleted) */
1529 if (row > 0 && bot_start + row
1530 + (int)wp->w_lines[j].wl_size > wp->w_height)
1531 {
1532 wp->w_lines_valid = idx + 1;
1533 break;
1534 }
1535 bot_start += wp->w_lines[idx++].wl_size;
1536
1537 /* stop at the last valid entry in w_lines[].wl_size */
1538 if (++j >= wp->w_lines_valid)
1539 {
1540 wp->w_lines_valid = idx;
1541 break;
1542 }
1543 }
1544#ifdef FEAT_DIFF
1545 /* Correct the first entry for filler lines at the top
1546 * when it won't get updated below. */
1547 if (wp->w_p_diff && bot_start > 0)
1548 wp->w_lines[0].wl_size =
1549 plines_win_nofill(wp, wp->w_topline, TRUE)
1550 + wp->w_topfill;
1551#endif
1552 }
1553 }
1554 }
1555
1556 /* When starting redraw in the first line, redraw all lines. When
1557 * there is only one window it's probably faster to clear the screen
1558 * first. */
1559 if (mid_start == 0)
1560 {
1561 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001562 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001563 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001564 /* Clear the screen when it was not done by win_del_lines() or
1565 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1566 * then. */
1567 if (screen_cleared != TRUE)
1568 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001569 /* The screen was cleared, redraw the tab pages line. */
1570 if (redraw_tabline)
1571 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001574
1575 /* When win_del_lines() or win_ins_lines() caused the screen to be
1576 * cleared (only happens for the first window) or when screenclear()
1577 * was called directly above, "must_redraw" will have been set to
1578 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1579 if (screen_cleared == TRUE)
1580 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 }
1582 else
1583 {
1584 /* Not VALID or INVERTED: redraw all lines. */
1585 mid_start = 0;
1586 mid_end = wp->w_height;
1587 }
1588
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001589 if (type == SOME_VALID)
1590 {
1591 /* SOME_VALID: redraw all lines. */
1592 mid_start = 0;
1593 mid_end = wp->w_height;
1594 type = NOT_VALID;
1595 }
1596
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 /* check if we are updating or removing the inverted part */
1598 if ((VIsual_active && buf == curwin->w_buffer)
1599 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1600 {
1601 linenr_T from, to;
1602
1603 if (VIsual_active)
1604 {
1605 if (VIsual_active
1606 && (VIsual_mode != wp->w_old_visual_mode
1607 || type == INVERTED_ALL))
1608 {
1609 /*
1610 * If the type of Visual selection changed, redraw the whole
1611 * selection. Also when the ownership of the X selection is
1612 * gained or lost.
1613 */
1614 if (curwin->w_cursor.lnum < VIsual.lnum)
1615 {
1616 from = curwin->w_cursor.lnum;
1617 to = VIsual.lnum;
1618 }
1619 else
1620 {
1621 from = VIsual.lnum;
1622 to = curwin->w_cursor.lnum;
1623 }
1624 /* redraw more when the cursor moved as well */
1625 if (wp->w_old_cursor_lnum < from)
1626 from = wp->w_old_cursor_lnum;
1627 if (wp->w_old_cursor_lnum > to)
1628 to = wp->w_old_cursor_lnum;
1629 if (wp->w_old_visual_lnum < from)
1630 from = wp->w_old_visual_lnum;
1631 if (wp->w_old_visual_lnum > to)
1632 to = wp->w_old_visual_lnum;
1633 }
1634 else
1635 {
1636 /*
1637 * Find the line numbers that need to be updated: The lines
1638 * between the old cursor position and the current cursor
1639 * position. Also check if the Visual position changed.
1640 */
1641 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1642 {
1643 from = curwin->w_cursor.lnum;
1644 to = wp->w_old_cursor_lnum;
1645 }
1646 else
1647 {
1648 from = wp->w_old_cursor_lnum;
1649 to = curwin->w_cursor.lnum;
1650 if (from == 0) /* Visual mode just started */
1651 from = to;
1652 }
1653
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001654 if (VIsual.lnum != wp->w_old_visual_lnum
1655 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 {
1657 if (wp->w_old_visual_lnum < from
1658 && wp->w_old_visual_lnum != 0)
1659 from = wp->w_old_visual_lnum;
1660 if (wp->w_old_visual_lnum > to)
1661 to = wp->w_old_visual_lnum;
1662 if (VIsual.lnum < from)
1663 from = VIsual.lnum;
1664 if (VIsual.lnum > to)
1665 to = VIsual.lnum;
1666 }
1667 }
1668
1669 /*
1670 * If in block mode and changed column or curwin->w_curswant:
1671 * update all lines.
1672 * First compute the actual start and end column.
1673 */
1674 if (VIsual_mode == Ctrl_V)
1675 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001676 colnr_T fromc, toc;
1677#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1678 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
Bram Moolenaar404406a2014-10-09 13:24:43 +02001680 if (curwin->w_p_lbr)
1681 ve_flags = VE_ALL;
1682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001684#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1685 ve_flags = save_ve_flags;
1686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 ++toc;
1688 if (curwin->w_curswant == MAXCOL)
1689 toc = MAXCOL;
1690
1691 if (fromc != wp->w_old_cursor_fcol
1692 || toc != wp->w_old_cursor_lcol)
1693 {
1694 if (from > VIsual.lnum)
1695 from = VIsual.lnum;
1696 if (to < VIsual.lnum)
1697 to = VIsual.lnum;
1698 }
1699 wp->w_old_cursor_fcol = fromc;
1700 wp->w_old_cursor_lcol = toc;
1701 }
1702 }
1703 else
1704 {
1705 /* Use the line numbers of the old Visual area. */
1706 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1707 {
1708 from = wp->w_old_cursor_lnum;
1709 to = wp->w_old_visual_lnum;
1710 }
1711 else
1712 {
1713 from = wp->w_old_visual_lnum;
1714 to = wp->w_old_cursor_lnum;
1715 }
1716 }
1717
1718 /*
1719 * There is no need to update lines above the top of the window.
1720 */
1721 if (from < wp->w_topline)
1722 from = wp->w_topline;
1723
1724 /*
1725 * If we know the value of w_botline, use it to restrict the update to
1726 * the lines that are visible in the window.
1727 */
1728 if (wp->w_valid & VALID_BOTLINE)
1729 {
1730 if (from >= wp->w_botline)
1731 from = wp->w_botline - 1;
1732 if (to >= wp->w_botline)
1733 to = wp->w_botline - 1;
1734 }
1735
1736 /*
1737 * Find the minimal part to be updated.
1738 * Watch out for scrolling that made entries in w_lines[] invalid.
1739 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1740 * top_end; need to redraw from top_end to the "to" line.
1741 * A middle mouse click with a Visual selection may change the text
1742 * above the Visual area and reset wl_valid, do count these for
1743 * mid_end (in srow).
1744 */
1745 if (mid_start > 0)
1746 {
1747 lnum = wp->w_topline;
1748 idx = 0;
1749 srow = 0;
1750 if (scrolled_down)
1751 mid_start = top_end;
1752 else
1753 mid_start = 0;
1754 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1755 {
1756 if (wp->w_lines[idx].wl_valid)
1757 mid_start += wp->w_lines[idx].wl_size;
1758 else if (!scrolled_down)
1759 srow += wp->w_lines[idx].wl_size;
1760 ++idx;
1761# ifdef FEAT_FOLDING
1762 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1763 lnum = wp->w_lines[idx].wl_lnum;
1764 else
1765# endif
1766 ++lnum;
1767 }
1768 srow += mid_start;
1769 mid_end = wp->w_height;
1770 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1771 {
1772 if (wp->w_lines[idx].wl_valid
1773 && wp->w_lines[idx].wl_lnum >= to + 1)
1774 {
1775 /* Only update until first row of this line */
1776 mid_end = srow;
1777 break;
1778 }
1779 srow += wp->w_lines[idx].wl_size;
1780 }
1781 }
1782 }
1783
1784 if (VIsual_active && buf == curwin->w_buffer)
1785 {
1786 wp->w_old_visual_mode = VIsual_mode;
1787 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1788 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001789 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 wp->w_old_curswant = curwin->w_curswant;
1791 }
1792 else
1793 {
1794 wp->w_old_visual_mode = 0;
1795 wp->w_old_cursor_lnum = 0;
1796 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001797 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1801 /* reset got_int, otherwise regexp won't work */
1802 save_got_int = got_int;
1803 got_int = 0;
1804#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001805#ifdef SYN_TIME_LIMIT
1806 /* Set the time limit to 'redrawtime'. */
1807 profile_setlimit(p_rdt, &syntax_tm);
1808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#ifdef FEAT_FOLDING
1810 win_foldinfo.fi_level = 0;
1811#endif
1812
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001813#ifdef FEAT_MENU
1814 /*
1815 * Draw the window toolbar, if there is one.
1816 * TODO: only when needed.
1817 */
1818 if (winbar_height(wp) > 0)
1819 redraw_win_toolbar(wp);
1820#endif
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 /*
1823 * Update all the window rows.
1824 */
1825 idx = 0; /* first entry in w_lines[].wl_size */
1826 row = 0;
1827 srow = 0;
1828 lnum = wp->w_topline; /* first line shown in window */
1829 for (;;)
1830 {
1831 /* stop updating when reached the end of the window (check for _past_
1832 * the end of the window is at the end of the loop) */
1833 if (row == wp->w_height)
1834 {
1835 didline = TRUE;
1836 break;
1837 }
1838
1839 /* stop updating when hit the end of the file */
1840 if (lnum > buf->b_ml.ml_line_count)
1841 {
1842 eof = TRUE;
1843 break;
1844 }
1845
1846 /* Remember the starting row of the line that is going to be dealt
1847 * with. It is used further down when the line doesn't fit. */
1848 srow = row;
1849
1850 /*
1851 * Update a line when it is in an area that needs updating, when it
1852 * has changes or w_lines[idx] is invalid.
1853 * bot_start may be halfway a wrapped line after using
1854 * win_del_lines(), check if the current line includes it.
1855 * When syntax folding is being used, the saved syntax states will
1856 * already have been updated, we can't see where the syntax state is
1857 * the same again, just update until the end of the window.
1858 */
1859 if (row < top_end
1860 || (row >= mid_start && row < mid_end)
1861#ifdef FEAT_SEARCH_EXTRA
1862 || top_to_mod
1863#endif
1864 || idx >= wp->w_lines_valid
1865 || (row + wp->w_lines[idx].wl_size > bot_start)
1866 || (mod_top != 0
1867 && (lnum == mod_top
1868 || (lnum >= mod_top
1869 && (lnum < mod_bot
1870#ifdef FEAT_SYN_HL
1871 || did_update == DID_FOLD
1872 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001873 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 && (
1875# ifdef FEAT_FOLDING
1876 (foldmethodIsSyntax(wp)
1877 && hasAnyFolding(wp)) ||
1878# endif
1879 syntax_check_changed(lnum)))
1880#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001881#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001882 /* match in fixed position might need redraw
1883 * if lines were inserted or deleted */
1884 || (wp->w_match_head != NULL
1885 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 )))))
1888 {
1889#ifdef FEAT_SEARCH_EXTRA
1890 if (lnum == mod_top)
1891 top_to_mod = FALSE;
1892#endif
1893
1894 /*
1895 * When at start of changed lines: May scroll following lines
1896 * up or down to minimize redrawing.
1897 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001898 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 */
1900 if (lnum == mod_top
1901 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001902 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {
1904 int old_rows = 0;
1905 int new_rows = 0;
1906 int xtra_rows;
1907 linenr_T l;
1908
1909 /* Count the old number of window rows, using w_lines[], which
1910 * should still contain the sizes for the lines as they are
1911 * currently displayed. */
1912 for (i = idx; i < wp->w_lines_valid; ++i)
1913 {
1914 /* Only valid lines have a meaningful wl_lnum. Invalid
1915 * lines are part of the changed area. */
1916 if (wp->w_lines[i].wl_valid
1917 && wp->w_lines[i].wl_lnum == mod_bot)
1918 break;
1919 old_rows += wp->w_lines[i].wl_size;
1920#ifdef FEAT_FOLDING
1921 if (wp->w_lines[i].wl_valid
1922 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1923 {
1924 /* Must have found the last valid entry above mod_bot.
1925 * Add following invalid entries. */
1926 ++i;
1927 while (i < wp->w_lines_valid
1928 && !wp->w_lines[i].wl_valid)
1929 old_rows += wp->w_lines[i++].wl_size;
1930 break;
1931 }
1932#endif
1933 }
1934
1935 if (i >= wp->w_lines_valid)
1936 {
1937 /* We can't find a valid line below the changed lines,
1938 * need to redraw until the end of the window.
1939 * Inserting/deleting lines has no use. */
1940 bot_start = 0;
1941 }
1942 else
1943 {
1944 /* Able to count old number of rows: Count new window
1945 * rows, and may insert/delete lines */
1946 j = idx;
1947 for (l = lnum; l < mod_bot; ++l)
1948 {
1949#ifdef FEAT_FOLDING
1950 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1951 ++new_rows;
1952 else
1953#endif
1954#ifdef FEAT_DIFF
1955 if (l == wp->w_topline)
1956 new_rows += plines_win_nofill(wp, l, TRUE)
1957 + wp->w_topfill;
1958 else
1959#endif
1960 new_rows += plines_win(wp, l, TRUE);
1961 ++j;
1962 if (new_rows > wp->w_height - row - 2)
1963 {
1964 /* it's getting too much, must redraw the rest */
1965 new_rows = 9999;
1966 break;
1967 }
1968 }
1969 xtra_rows = new_rows - old_rows;
1970 if (xtra_rows < 0)
1971 {
1972 /* May scroll text up. If there is not enough
1973 * remaining text or scrolling fails, must redraw the
1974 * rest. If scrolling works, must redraw the text
1975 * below the scrolled text. */
1976 if (row - xtra_rows >= wp->w_height - 2)
1977 mod_bot = MAXLNUM;
1978 else
1979 {
1980 check_for_delay(FALSE);
1981 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001982 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 mod_bot = MAXLNUM;
1984 else
1985 bot_start = wp->w_height + xtra_rows;
1986 }
1987 }
1988 else if (xtra_rows > 0)
1989 {
1990 /* May scroll text down. If there is not enough
1991 * remaining text of scrolling fails, must redraw the
1992 * rest. */
1993 if (row + xtra_rows >= wp->w_height - 2)
1994 mod_bot = MAXLNUM;
1995 else
1996 {
1997 check_for_delay(FALSE);
1998 if (win_ins_lines(wp, row + old_rows,
1999 xtra_rows, FALSE, FALSE) == FAIL)
2000 mod_bot = MAXLNUM;
2001 else if (top_end > row + old_rows)
2002 /* Scrolled the part at the top that requires
2003 * updating down. */
2004 top_end += xtra_rows;
2005 }
2006 }
2007
2008 /* When not updating the rest, may need to move w_lines[]
2009 * entries. */
2010 if (mod_bot != MAXLNUM && i != j)
2011 {
2012 if (j < i)
2013 {
2014 int x = row + new_rows;
2015
2016 /* move entries in w_lines[] upwards */
2017 for (;;)
2018 {
2019 /* stop at last valid entry in w_lines[] */
2020 if (i >= wp->w_lines_valid)
2021 {
2022 wp->w_lines_valid = j;
2023 break;
2024 }
2025 wp->w_lines[j] = wp->w_lines[i];
2026 /* stop at a line that won't fit */
2027 if (x + (int)wp->w_lines[j].wl_size
2028 > wp->w_height)
2029 {
2030 wp->w_lines_valid = j + 1;
2031 break;
2032 }
2033 x += wp->w_lines[j++].wl_size;
2034 ++i;
2035 }
2036 if (bot_start > x)
2037 bot_start = x;
2038 }
2039 else /* j > i */
2040 {
2041 /* move entries in w_lines[] downwards */
2042 j -= i;
2043 wp->w_lines_valid += j;
2044 if (wp->w_lines_valid > wp->w_height)
2045 wp->w_lines_valid = wp->w_height;
2046 for (i = wp->w_lines_valid; i - j >= idx; --i)
2047 wp->w_lines[i] = wp->w_lines[i - j];
2048
2049 /* The w_lines[] entries for inserted lines are
2050 * now invalid, but wl_size may be used above.
2051 * Reset to zero. */
2052 while (i >= idx)
2053 {
2054 wp->w_lines[i].wl_size = 0;
2055 wp->w_lines[i--].wl_valid = FALSE;
2056 }
2057 }
2058 }
2059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 }
2061
2062#ifdef FEAT_FOLDING
2063 /*
2064 * When lines are folded, display one line for all of them.
2065 * Otherwise, display normally (can be several display lines when
2066 * 'wrap' is on).
2067 */
2068 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2069 if (fold_count != 0)
2070 {
2071 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2072 ++row;
2073 --fold_count;
2074 wp->w_lines[idx].wl_folded = TRUE;
2075 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2076# ifdef FEAT_SYN_HL
2077 did_update = DID_FOLD;
2078# endif
2079 }
2080 else
2081#endif
2082 if (idx < wp->w_lines_valid
2083 && wp->w_lines[idx].wl_valid
2084 && wp->w_lines[idx].wl_lnum == lnum
2085 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002086 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 && srow + wp->w_lines[idx].wl_size > wp->w_height
2088#ifdef FEAT_DIFF
2089 && diff_check_fill(wp, lnum) == 0
2090#endif
2091 )
2092 {
2093 /* This line is not going to fit. Don't draw anything here,
2094 * will draw "@ " lines below. */
2095 row = wp->w_height + 1;
2096 }
2097 else
2098 {
2099#ifdef FEAT_SEARCH_EXTRA
2100 prepare_search_hl(wp, lnum);
2101#endif
2102#ifdef FEAT_SYN_HL
2103 /* Let the syntax stuff know we skipped a few lines. */
2104 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002105 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 syntax_end_parsing(syntax_last_parsed + 1);
2107#endif
2108
2109 /*
2110 * Display one line.
2111 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002112 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2113#ifdef SYN_TIME_LIMIT
2114 &syntax_tm
2115#else
2116 NULL
2117#endif
2118 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
2120#ifdef FEAT_FOLDING
2121 wp->w_lines[idx].wl_folded = FALSE;
2122 wp->w_lines[idx].wl_lastlnum = lnum;
2123#endif
2124#ifdef FEAT_SYN_HL
2125 did_update = DID_LINE;
2126 syntax_last_parsed = lnum;
2127#endif
2128 }
2129
2130 wp->w_lines[idx].wl_lnum = lnum;
2131 wp->w_lines[idx].wl_valid = TRUE;
2132 if (row > wp->w_height) /* past end of screen */
2133 {
2134 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002135 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2137 ++idx;
2138 break;
2139 }
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 = row - srow;
2142 ++idx;
2143#ifdef FEAT_FOLDING
2144 lnum += fold_count + 1;
2145#else
2146 ++lnum;
2147#endif
2148 }
2149 else
2150 {
2151 /* This line does not need updating, advance to the next one */
2152 row += wp->w_lines[idx++].wl_size;
2153 if (row > wp->w_height) /* past end of screen */
2154 break;
2155#ifdef FEAT_FOLDING
2156 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2157#else
2158 ++lnum;
2159#endif
2160#ifdef FEAT_SYN_HL
2161 did_update = DID_NONE;
2162#endif
2163 }
2164
2165 if (lnum > buf->b_ml.ml_line_count)
2166 {
2167 eof = TRUE;
2168 break;
2169 }
2170 }
2171 /*
2172 * End of loop over all window lines.
2173 */
2174
2175
2176 if (idx > wp->w_lines_valid)
2177 wp->w_lines_valid = idx;
2178
2179#ifdef FEAT_SYN_HL
2180 /*
2181 * Let the syntax stuff know we stop parsing here.
2182 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002183 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 syntax_end_parsing(syntax_last_parsed + 1);
2185#endif
2186
2187 /*
2188 * If we didn't hit the end of the file, and we didn't finish the last
2189 * line we were working on, then the line didn't fit.
2190 */
2191 wp->w_empty_rows = 0;
2192#ifdef FEAT_DIFF
2193 wp->w_filler_rows = 0;
2194#endif
2195 if (!eof && !didline)
2196 {
2197 if (lnum == wp->w_topline)
2198 {
2199 /*
2200 * Single line that does not fit!
2201 * Don't overwrite it, it can be edited.
2202 */
2203 wp->w_botline = lnum + 1;
2204 }
2205#ifdef FEAT_DIFF
2206 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2207 {
2208 /* Window ends in filler lines. */
2209 wp->w_botline = lnum;
2210 wp->w_filler_rows = wp->w_height - srow;
2211 }
2212#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002213 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2214 {
2215 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2216
2217 /*
2218 * Last line isn't finished: Display "@@@" in the last screen line.
2219 */
2220 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002221 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002222 screen_fill(scr_row, scr_row + 1,
2223 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002224 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002225 set_empty_rows(wp, srow);
2226 wp->w_botline = lnum;
2227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2229 {
2230 /*
2231 * Last line isn't finished: Display "@@@" at the end.
2232 */
2233 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2234 W_WINROW(wp) + wp->w_height,
2235 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002236 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 set_empty_rows(wp, srow);
2238 wp->w_botline = lnum;
2239 }
2240 else
2241 {
2242 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2243 wp->w_botline = lnum;
2244 }
2245 }
2246 else
2247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (eof) /* we hit the end of the file */
2250 {
2251 wp->w_botline = buf->b_ml.ml_line_count + 1;
2252#ifdef FEAT_DIFF
2253 j = diff_check_fill(wp, wp->w_botline);
2254 if (j > 0 && !wp->w_botfill)
2255 {
2256 /*
2257 * Display filler lines at the end of the file
2258 */
2259 if (char2cells(fill_diff) > 1)
2260 i = '-';
2261 else
2262 i = fill_diff;
2263 if (row + j > wp->w_height)
2264 j = wp->w_height - row;
2265 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2266 row += j;
2267 }
2268#endif
2269 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002270 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 wp->w_botline = lnum;
2272
2273 /* make sure the rest of the screen is blank */
2274 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002275 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
2277
2278 /* Reset the type of redrawing required, the window has been updated. */
2279 wp->w_redr_type = 0;
2280#ifdef FEAT_DIFF
2281 wp->w_old_topfill = wp->w_topfill;
2282 wp->w_old_botfill = wp->w_botfill;
2283#endif
2284
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002285 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
2287 /*
2288 * There is a trick with w_botline. If we invalidate it on each
2289 * change that might modify it, this will cause a lot of expensive
2290 * calls to plines() in update_topline() each time. Therefore the
2291 * value of w_botline is often approximated, and this value is used to
2292 * compute the value of w_topline. If the value of w_botline was
2293 * wrong, check that the value of w_topline is correct (cursor is on
2294 * the visible part of the text). If it's not, we need to redraw
2295 * again. Mostly this just means scrolling up a few lines, so it
2296 * doesn't look too bad. Only do this for the current window (where
2297 * changes are relevant).
2298 */
2299 wp->w_valid |= VALID_BOTLINE;
2300 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2301 {
2302 recursive = TRUE;
2303 curwin->w_valid &= ~VALID_TOPLINE;
2304 update_topline(); /* may invalidate w_botline again */
2305 if (must_redraw != 0)
2306 {
2307 /* Don't update for changes in buffer again. */
2308 i = curbuf->b_mod_set;
2309 curbuf->b_mod_set = FALSE;
2310 win_update(curwin);
2311 must_redraw = 0;
2312 curbuf->b_mod_set = i;
2313 }
2314 recursive = FALSE;
2315 }
2316 }
2317
2318#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2319 /* restore got_int, unless CTRL-C was hit while redrawing */
2320 if (!got_int)
2321 got_int = save_got_int;
2322#endif
2323}
2324
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325/*
2326 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2327 * as the filler character.
2328 */
2329 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002330win_draw_end(
2331 win_T *wp,
2332 int c1,
2333 int c2,
2334 int row,
2335 int endrow,
2336 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337{
2338#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2339 int n = 0;
2340# define FDC_OFF n
2341#else
2342# define FDC_OFF 0
2343#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002344#ifdef FEAT_FOLDING
2345 int fdc = compute_foldcolumn(wp, 0);
2346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
2348#ifdef FEAT_RIGHTLEFT
2349 if (wp->w_p_rl)
2350 {
2351 /* No check for cmdline window: should never be right-left. */
2352# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002353 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355 if (n > 0)
2356 {
2357 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002358 if (n > W_WIDTH(wp))
2359 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2361 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002362 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
2364# endif
2365# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002366 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
2368 int nn = n + 2;
2369
2370 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002371 if (nn > W_WIDTH(wp))
2372 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2374 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002375 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 n = nn;
2377 }
2378# endif
2379 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2380 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002381 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2383 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002384 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 }
2386 else
2387#endif
2388 {
2389#ifdef FEAT_CMDWIN
2390 if (cmdwin_type != 0 && wp == curwin)
2391 {
2392 /* draw the cmdline character in the leftmost column */
2393 n = 1;
2394 if (n > wp->w_width)
2395 n = wp->w_width;
2396 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2397 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002398 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400#endif
2401#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002402 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002404 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 /* draw the fold column at the left */
2407 if (nn > W_WIDTH(wp))
2408 nn = W_WIDTH(wp);
2409 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2410 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002411 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 n = nn;
2413 }
2414#endif
2415#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002416 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
2418 int nn = n + 2;
2419
2420 /* draw the sign column after the fold column */
2421 if (nn > W_WIDTH(wp))
2422 nn = W_WIDTH(wp);
2423 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2424 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002425 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 n = nn;
2427 }
2428#endif
2429 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2430 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002431 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433 set_empty_rows(wp, row);
2434}
2435
Bram Moolenaar1a384422010-07-14 19:53:30 +02002436#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002437static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002438
2439/*
2440 * Advance **color_cols and return TRUE when there are columns to draw.
2441 */
2442 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002443advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002444{
2445 while (**color_cols >= 0 && vcol > **color_cols)
2446 ++*color_cols;
2447 return (**color_cols >= 0);
2448}
2449#endif
2450
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002451#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2452/*
2453 * Copy "text" to ScreenLines using "attr".
2454 * Returns the next screen column.
2455 */
2456 static int
2457text_to_screenline(win_T *wp, char_u *text, int col)
2458{
2459 int off = (int)(current_ScreenLine - ScreenLines);
2460
2461#ifdef FEAT_MBYTE
2462 if (has_mbyte)
2463 {
2464 int cells;
2465 int u8c, u8cc[MAX_MCO];
2466 int i;
2467 int idx;
2468 int c_len;
2469 char_u *p;
2470# ifdef FEAT_ARABIC
2471 int prev_c = 0; /* previous Arabic character */
2472 int prev_c1 = 0; /* first composing char for prev_c */
2473# endif
2474
2475# ifdef FEAT_RIGHTLEFT
2476 if (wp->w_p_rl)
2477 idx = off;
2478 else
2479# endif
2480 idx = off + col;
2481
2482 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2483 for (p = text; *p != NUL; )
2484 {
2485 cells = (*mb_ptr2cells)(p);
2486 c_len = (*mb_ptr2len)(p);
2487 if (col + cells > W_WIDTH(wp)
2488# ifdef FEAT_RIGHTLEFT
2489 - (wp->w_p_rl ? col : 0)
2490# endif
2491 )
2492 break;
2493 ScreenLines[idx] = *p;
2494 if (enc_utf8)
2495 {
2496 u8c = utfc_ptr2char(p, u8cc);
2497 if (*p < 0x80 && u8cc[0] == 0)
2498 {
2499 ScreenLinesUC[idx] = 0;
2500#ifdef FEAT_ARABIC
2501 prev_c = u8c;
2502#endif
2503 }
2504 else
2505 {
2506#ifdef FEAT_ARABIC
2507 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2508 {
2509 /* Do Arabic shaping. */
2510 int pc, pc1, nc;
2511 int pcc[MAX_MCO];
2512 int firstbyte = *p;
2513
2514 /* The idea of what is the previous and next
2515 * character depends on 'rightleft'. */
2516 if (wp->w_p_rl)
2517 {
2518 pc = prev_c;
2519 pc1 = prev_c1;
2520 nc = utf_ptr2char(p + c_len);
2521 prev_c1 = u8cc[0];
2522 }
2523 else
2524 {
2525 pc = utfc_ptr2char(p + c_len, pcc);
2526 nc = prev_c;
2527 pc1 = pcc[0];
2528 }
2529 prev_c = u8c;
2530
2531 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2532 pc, pc1, nc);
2533 ScreenLines[idx] = firstbyte;
2534 }
2535 else
2536 prev_c = u8c;
2537#endif
2538 /* Non-BMP character: display as ? or fullwidth ?. */
2539#ifdef UNICODE16
2540 if (u8c >= 0x10000)
2541 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2542 else
2543#endif
2544 ScreenLinesUC[idx] = u8c;
2545 for (i = 0; i < Screen_mco; ++i)
2546 {
2547 ScreenLinesC[i][idx] = u8cc[i];
2548 if (u8cc[i] == 0)
2549 break;
2550 }
2551 }
2552 if (cells > 1)
2553 ScreenLines[idx + 1] = 0;
2554 }
2555 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2556 /* double-byte single width character */
2557 ScreenLines2[idx] = p[1];
2558 else if (cells > 1)
2559 /* double-width character */
2560 ScreenLines[idx + 1] = p[1];
2561 col += cells;
2562 idx += cells;
2563 p += c_len;
2564 }
2565 }
2566 else
2567#endif
2568 {
2569 int len = (int)STRLEN(text);
2570
2571 if (len > W_WIDTH(wp) - col)
2572 len = W_WIDTH(wp) - col;
2573 if (len > 0)
2574 {
2575#ifdef FEAT_RIGHTLEFT
2576 if (wp->w_p_rl)
2577 STRNCPY(current_ScreenLine, text, len);
2578 else
2579#endif
2580 STRNCPY(current_ScreenLine + col, text, len);
2581 col += len;
2582 }
2583 }
2584 return col;
2585}
2586#endif
2587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588#ifdef FEAT_FOLDING
2589/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002590 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2591 * space is available for window "wp", minus "col".
2592 */
2593 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002594compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002595{
2596 int fdc = wp->w_p_fdc;
2597 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2598 int wwidth = W_WIDTH(wp);
2599
2600 if (fdc > wwidth - (col + wmw))
2601 fdc = wwidth - (col + wmw);
2602 return fdc;
2603}
2604
2605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 * Display one folded line.
2607 */
2608 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002609fold_line(
2610 win_T *wp,
2611 long fold_count,
2612 foldinfo_T *foldinfo,
2613 linenr_T lnum,
2614 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002616 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 pos_T *top, *bot;
2618 linenr_T lnume = lnum + fold_count - 1;
2619 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002620 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 int col;
2623 int txtcol;
2624 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002625 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627 /* Build the fold line:
2628 * 1. Add the cmdwin_type for the command-line window
2629 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002630 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 * 4. Compose the text
2632 * 5. Add the text
2633 * 6. set highlighting for the Visual area an other text
2634 */
2635 col = 0;
2636
2637 /*
2638 * 1. Add the cmdwin_type for the command-line window
2639 * Ignores 'rightleft', this window is never right-left.
2640 */
2641#ifdef FEAT_CMDWIN
2642 if (cmdwin_type != 0 && wp == curwin)
2643 {
2644 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002645 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646#ifdef FEAT_MBYTE
2647 if (enc_utf8)
2648 ScreenLinesUC[off] = 0;
2649#endif
2650 ++col;
2651 }
2652#endif
2653
2654 /*
2655 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002656 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002658 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (fdc > 0)
2660 {
2661 fill_foldcolumn(buf, wp, TRUE, lnum);
2662#ifdef FEAT_RIGHTLEFT
2663 if (wp->w_p_rl)
2664 {
2665 int i;
2666
2667 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002668 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 /* reverse the fold column */
2670 for (i = 0; i < fdc; ++i)
2671 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2672 }
2673 else
2674#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002675 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 col += fdc;
2677 }
2678
2679#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002680# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2681 for (ri = 0; ri < l; ++ri) \
2682 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2683 else \
2684 for (ri = 0; ri < l; ++ri) \
2685 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002687# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2688 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689#endif
2690
Bram Moolenaar64486672010-05-16 15:46:46 +02002691 /* Set all attributes of the 'number' or 'relativenumber' column and the
2692 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002693 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
2695#ifdef FEAT_SIGNS
2696 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002697 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
2699 len = W_WIDTH(wp) - col;
2700 if (len > 0)
2701 {
2702 if (len > 2)
2703 len = 2;
2704# ifdef FEAT_RIGHTLEFT
2705 if (wp->w_p_rl)
2706 /* the line number isn't reversed */
2707 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002708 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 else
2710# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002711 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 col += len;
2713 }
2714 }
2715#endif
2716
2717 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002718 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002720 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
2722 len = W_WIDTH(wp) - col;
2723 if (len > 0)
2724 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002725 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002726 long num;
2727 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002728
2729 if (len > w + 1)
2730 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002731
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002732 if (wp->w_p_nu && !wp->w_p_rnu)
2733 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002734 num = (long)lnum;
2735 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002736 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002737 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002738 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002739 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002740 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002741 /* 'number' + 'relativenumber': cursor line shows absolute
2742 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002743 num = lnum;
2744 fmt = "%-*ld ";
2745 }
2746 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002747
Bram Moolenaar700e7342013-01-30 12:31:36 +01002748 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749#ifdef FEAT_RIGHTLEFT
2750 if (wp->w_p_rl)
2751 /* the line number isn't reversed */
2752 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002753 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 else
2755#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002756 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 col += len;
2758 }
2759 }
2760
2761 /*
2762 * 4. Compose the folded-line string with 'foldtext', if set.
2763 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002764 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766 txtcol = col; /* remember where text starts */
2767
2768 /*
2769 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2770 * Right-left text is put in columns 0 - number-col, normal text is put
2771 * in columns number-col - window-width.
2772 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002773 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774
2775 /* Fill the rest of the line with the fold filler */
2776#ifdef FEAT_RIGHTLEFT
2777 if (wp->w_p_rl)
2778 col -= txtcol;
2779#endif
2780 while (col < W_WIDTH(wp)
2781#ifdef FEAT_RIGHTLEFT
2782 - (wp->w_p_rl ? txtcol : 0)
2783#endif
2784 )
2785 {
2786#ifdef FEAT_MBYTE
2787 if (enc_utf8)
2788 {
2789 if (fill_fold >= 0x80)
2790 {
2791 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002792 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002793 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 }
2795 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002798 ScreenLines[off + col] = fill_fold;
2799 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002800 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002802 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002804 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
2806
2807 if (text != buf)
2808 vim_free(text);
2809
2810 /*
2811 * 6. set highlighting for the Visual area an other text.
2812 * If all folded lines are in the Visual area, highlight the line.
2813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2815 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002816 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
2818 /* Visual is after curwin->w_cursor */
2819 top = &curwin->w_cursor;
2820 bot = &VIsual;
2821 }
2822 else
2823 {
2824 /* Visual is before curwin->w_cursor */
2825 top = &VIsual;
2826 bot = &curwin->w_cursor;
2827 }
2828 if (lnum >= top->lnum
2829 && lnume <= bot->lnum
2830 && (VIsual_mode != 'v'
2831 || ((lnum > top->lnum
2832 || (lnum == top->lnum
2833 && top->col == 0))
2834 && (lnume < bot->lnum
2835 || (lnume == bot->lnum
2836 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002837 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 {
2839 if (VIsual_mode == Ctrl_V)
2840 {
2841 /* Visual block mode: highlight the chars part of the block */
2842 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2843 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002844 if (wp->w_old_cursor_lcol != MAXCOL
2845 && wp->w_old_cursor_lcol + txtcol
2846 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 len = wp->w_old_cursor_lcol;
2848 else
2849 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002850 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002851 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
2853 }
2854 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002857 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
2860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002862#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002863 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2864 if (wp->w_p_cc_cols)
2865 {
2866 int i = 0;
2867 int j = wp->w_p_cc_cols[i];
2868 int old_txtcol = txtcol;
2869
2870 while (j > -1)
2871 {
2872 txtcol += j;
2873 if (wp->w_p_wrap)
2874 txtcol -= wp->w_skipcol;
2875 else
2876 txtcol -= wp->w_leftcol;
2877 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2878 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002879 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002880 txtcol = old_txtcol;
2881 j = wp->w_p_cc_cols[++i];
2882 }
2883 }
2884
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002885 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002886 if (wp->w_p_cuc)
2887 {
2888 txtcol += wp->w_virtcol;
2889 if (wp->w_p_wrap)
2890 txtcol -= wp->w_skipcol;
2891 else
2892 txtcol -= wp->w_leftcol;
2893 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2894 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002895 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002896 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002899 screen_line(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 (int)W_WIDTH(wp), FALSE);
2901
2902 /*
2903 * Update w_cline_height and w_cline_folded if the cursor line was
2904 * updated (saves a call to plines() later).
2905 */
2906 if (wp == curwin
2907 && lnum <= curwin->w_cursor.lnum
2908 && lnume >= curwin->w_cursor.lnum)
2909 {
2910 curwin->w_cline_row = row;
2911 curwin->w_cline_height = 1;
2912 curwin->w_cline_folded = TRUE;
2913 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2914 }
2915}
2916
2917/*
2918 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2919 */
2920 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002921copy_text_attr(
2922 int off,
2923 char_u *buf,
2924 int len,
2925 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002927 int i;
2928
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 mch_memmove(ScreenLines + off, buf, (size_t)len);
2930# ifdef FEAT_MBYTE
2931 if (enc_utf8)
2932 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2933# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002934 for (i = 0; i < len; ++i)
2935 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936}
2937
2938/*
2939 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002940 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 */
2942 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002943fill_foldcolumn(
2944 char_u *p,
2945 win_T *wp,
2946 int closed, /* TRUE of FALSE */
2947 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948{
2949 int i = 0;
2950 int level;
2951 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002952 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002953 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
2955 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002956 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 level = win_foldinfo.fi_level;
2959 if (level > 0)
2960 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002961 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002962 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 /* If the column is too narrow, we start at the lowest level that
2965 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002966 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 if (first_level < 1)
2968 first_level = 1;
2969
Bram Moolenaar1c934292015-01-27 16:39:29 +01002970 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 {
2972 if (win_foldinfo.fi_lnum == lnum
2973 && first_level + i >= win_foldinfo.fi_low_level)
2974 p[i] = '-';
2975 else if (first_level == 1)
2976 p[i] = '|';
2977 else if (first_level + i <= 9)
2978 p[i] = '0' + first_level + i;
2979 else
2980 p[i] = '>';
2981 if (first_level + i == level)
2982 break;
2983 }
2984 }
2985 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002986 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987}
2988#endif /* FEAT_FOLDING */
2989
2990/*
2991 * Display line "lnum" of window 'wp' on the screen.
2992 * Start at row "startrow", stop when "endrow" is reached.
2993 * wp->w_virtcol needs to be valid.
2994 *
2995 * Return the number of last row the line occupies.
2996 */
2997 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002998win_line(
2999 win_T *wp,
3000 linenr_T lnum,
3001 int startrow,
3002 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003003 int nochange UNUSED, /* not updating for changed text */
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003004 proftime_T *syntax_tm UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003006 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3008 int c = 0; /* init for GCC */
3009 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003010#ifdef FEAT_LINEBREAK
3011 long vcol_sbr = -1; /* virtual column after showbreak */
3012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 long vcol_prev = -1; /* "vcol" of previous character */
3014 char_u *line; /* current line */
3015 char_u *ptr; /* current position in "line" */
3016 int row; /* row in the window, excl w_winrow */
3017 int screen_row; /* row on the screen, incl w_winrow */
3018
3019 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3020 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003021 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003022 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 int c_extra = NUL; /* extra chars, all the same */
3024 int extra_attr = 0; /* attributes when n_extra != 0 */
3025 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3026 displaying lcs_eol at end-of-line */
3027 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3028 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3029
3030 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3031 int saved_n_extra = 0;
3032 char_u *saved_p_extra = NULL;
3033 int saved_c_extra = 0;
3034 int saved_char_attr = 0;
3035
3036 int n_attr = 0; /* chars with special attr */
3037 int saved_attr2 = 0; /* char_attr saved for n_attr */
3038 int n_attr3 = 0; /* chars with overruling special attr */
3039 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3040
3041 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3042
3043 int fromcol, tocol; /* start/end of inverting */
3044 int fromcol_prev = -2; /* start of inverting after cursor */
3045 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003047 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 pos_T pos;
3049 long v;
3050
3051 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003052 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3054 in this line */
3055 int attr = 0; /* attributes for area highlighting */
3056 int area_attr = 0; /* attributes desired by highlighting */
3057 int search_attr = 0; /* attributes desired by 'hlsearch' */
3058#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003059 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 int syntax_attr = 0; /* attributes desired by syntax */
3061 int has_syntax = FALSE; /* this buffer has syntax highl. */
3062 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003063 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003064 int draw_color_col = FALSE; /* highlight colorcolumn */
3065 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003066#endif
3067#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003068 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003069# define SPWORDLEN 150
3070 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003071 int nextlinecol = 0; /* column where nextline[] starts */
3072 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003073 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003074 int spell_attr = 0; /* attributes desired by spelling */
3075 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003076 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3077 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003078 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003079 static int cap_col = -1; /* column to check for Cap word */
3080 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003081 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082#endif
3083 int extra_check; /* has syntax or linebreak */
3084#ifdef FEAT_MBYTE
3085 int multi_attr = 0; /* attributes desired by multibyte */
3086 int mb_l = 1; /* multi-byte byte length */
3087 int mb_c = 0; /* decoded multi-byte character */
3088 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003089 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090#endif
3091#ifdef FEAT_DIFF
3092 int filler_lines; /* nr of filler lines to be drawn */
3093 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003094 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 int change_start = MAXCOL; /* first col of changed area */
3096 int change_end = -1; /* last col of changed area */
3097#endif
3098 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3099#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003100 int need_showbreak = FALSE; /* overlong line, skipping first x
3101 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003103#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003104 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003106 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107#endif
3108#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003109 matchitem_T *cur; /* points to the match list */
3110 match_T *shl; /* points to search_hl or a match */
3111 int shl_flag; /* flag to indicate whether search_hl
3112 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003113 int pos_inprogress; /* marks that position match search is
3114 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003115 int prevcol_hl_flag; /* flag to indicate whether prevcol
3116 equals startcol of search_hl or one
3117 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118#endif
3119#ifdef FEAT_ARABIC
3120 int prev_c = 0; /* previous Arabic character */
3121 int prev_c1 = 0; /* first composing char for prev_c */
3122#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003123#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003124 int did_line_attr = 0;
3125#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003126#ifdef FEAT_TERMINAL
3127 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003128 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
3131 /* draw_state: items that are drawn in sequence: */
3132#define WL_START 0 /* nothing done yet */
3133#ifdef FEAT_CMDWIN
3134# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3135#else
3136# define WL_CMDLINE WL_START
3137#endif
3138#ifdef FEAT_FOLDING
3139# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3140#else
3141# define WL_FOLD WL_CMDLINE
3142#endif
3143#ifdef FEAT_SIGNS
3144# define WL_SIGN WL_FOLD + 1 /* column for signs */
3145#else
3146# define WL_SIGN WL_FOLD /* column for signs */
3147#endif
3148#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003149#ifdef FEAT_LINEBREAK
3150# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003152# define WL_BRI WL_NR
3153#endif
3154#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3155# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3156#else
3157# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158#endif
3159#define WL_LINE WL_SBR + 1 /* text in the line */
3160 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003161#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 int feedback_col = 0;
3163 int feedback_old_attr = -1;
3164#endif
3165
Bram Moolenaar860cae12010-06-05 23:22:07 +02003166#ifdef FEAT_CONCEAL
3167 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003168 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003169 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003170 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003171 int is_concealing = FALSE;
3172 int boguscols = 0; /* nonexistent columns added to force
3173 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003174 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003175 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003176 int match_conc = 0; /* cchar for match functions */
3177 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003178 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003179# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003180# define FIX_FOR_BOGUSCOLS \
3181 { \
3182 n_extra += vcol_off; \
3183 vcol -= vcol_off; \
3184 vcol_off = 0; \
3185 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003186 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003187 boguscols = 0; \
3188 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003189#else
3190# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
3193 if (startrow > endrow) /* past the end already! */
3194 return startrow;
3195
3196 row = startrow;
3197 screen_row = row + W_WINROW(wp);
3198
3199 /*
3200 * To speed up the loop below, set extra_check when there is linebreak,
3201 * trailing white space and/or syntax processing to be done.
3202 */
3203#ifdef FEAT_LINEBREAK
3204 extra_check = wp->w_p_lbr;
3205#else
3206 extra_check = 0;
3207#endif
3208#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003209 if (syntax_present(wp) && !wp->w_s->b_syn_error
3210# ifdef SYN_TIME_LIMIT
3211 && !wp->w_s->b_syn_slow
3212# endif
3213 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 {
3215 /* Prepare for syntax highlighting in this line. When there is an
3216 * error, stop syntax highlighting. */
3217 save_did_emsg = did_emsg;
3218 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003219 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003221 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 else
3223 {
3224 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003225#ifdef SYN_TIME_LIMIT
3226 if (!wp->w_s->b_syn_slow)
3227#endif
3228 {
3229 has_syntax = TRUE;
3230 extra_check = TRUE;
3231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
3233 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003234
3235 /* Check for columns to display for 'colorcolumn'. */
3236 color_cols = wp->w_p_cc_cols;
3237 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003238 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003239#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003240
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003241#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003242 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003243 {
3244 extra_check = TRUE;
3245 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003246 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003247 }
3248#endif
3249
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003250#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003251 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003252 && *wp->w_s->b_p_spl != NUL
3253 && wp->w_s->b_langp.ga_len > 0
3254 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003255 {
3256 /* Prepare for spell checking. */
3257 has_spell = TRUE;
3258 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003259
3260 /* Get the start of the next line, so that words that wrap to the next
3261 * line are found too: "et<line-break>al.".
3262 * Trick: skip a few chars for C/shell/Vim comments */
3263 nextline[SPWORDLEN] = NUL;
3264 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3265 {
3266 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3267 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3268 }
3269
3270 /* When a word wrapped from the previous line the start of the current
3271 * line is valid. */
3272 if (lnum == checked_lnum)
3273 cur_checked_col = checked_col;
3274 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003275
3276 /* When there was a sentence end in the previous line may require a
3277 * word starting with capital in this line. In line 1 always check
3278 * the first word. */
3279 if (lnum != capcol_lnum)
3280 cap_col = -1;
3281 if (lnum == 1)
3282 cap_col = 0;
3283 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285#endif
3286
3287 /*
3288 * handle visual active in this window
3289 */
3290 fromcol = -10;
3291 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3293 {
3294 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003295 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
3297 top = &curwin->w_cursor;
3298 bot = &VIsual;
3299 }
3300 else /* Visual is before curwin->w_cursor */
3301 {
3302 top = &VIsual;
3303 bot = &curwin->w_cursor;
3304 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003305 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (VIsual_mode == Ctrl_V) /* block mode */
3307 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003308 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
3310 fromcol = wp->w_old_cursor_fcol;
3311 tocol = wp->w_old_cursor_lcol;
3312 }
3313 }
3314 else /* non-block mode */
3315 {
3316 if (lnum > top->lnum && lnum <= bot->lnum)
3317 fromcol = 0;
3318 else if (lnum == top->lnum)
3319 {
3320 if (VIsual_mode == 'V') /* linewise */
3321 fromcol = 0;
3322 else
3323 {
3324 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3325 if (gchar_pos(top) == NUL)
3326 tocol = fromcol + 1;
3327 }
3328 }
3329 if (VIsual_mode != 'V' && lnum == bot->lnum)
3330 {
3331 if (*p_sel == 'e' && bot->col == 0
3332#ifdef FEAT_VIRTUALEDIT
3333 && bot->coladd == 0
3334#endif
3335 )
3336 {
3337 fromcol = -10;
3338 tocol = MAXCOL;
3339 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003340 else if (bot->col == MAXCOL)
3341 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 else
3343 {
3344 pos = *bot;
3345 if (*p_sel == 'e')
3346 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3347 else
3348 {
3349 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3350 ++tocol;
3351 }
3352 }
3353 }
3354 }
3355
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 /* Check if the character under the cursor should not be inverted */
3357 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003358#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 )
3362 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
3364 /* if inverting in this line set area_highlighting */
3365 if (fromcol >= 0)
3366 {
3367 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003368 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003370 if ((clip_star.available && !clip_star.owned
3371 && clip_isautosel_star())
3372 || (clip_plus.available && !clip_plus.owned
3373 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003374 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#endif
3376 }
3377 }
3378
3379 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003380 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003382 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 && wp == curwin
3384 && lnum >= curwin->w_cursor.lnum
3385 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3386 {
3387 if (lnum == curwin->w_cursor.lnum)
3388 getvcol(curwin, &(curwin->w_cursor),
3389 (colnr_T *)&fromcol, NULL, NULL);
3390 else
3391 fromcol = 0;
3392 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3393 {
3394 pos.lnum = lnum;
3395 pos.col = search_match_endcol;
3396 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3397 }
3398 else
3399 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003400 /* do at least one character; happens when past end of line */
3401 if (fromcol == tocol)
3402 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003404 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406
3407#ifdef FEAT_DIFF
3408 filler_lines = diff_check(wp, lnum);
3409 if (filler_lines < 0)
3410 {
3411 if (filler_lines == -1)
3412 {
3413 if (diff_find_change(wp, lnum, &change_start, &change_end))
3414 diff_hlf = HLF_ADD; /* added line */
3415 else if (change_start == 0)
3416 diff_hlf = HLF_TXD; /* changed text */
3417 else
3418 diff_hlf = HLF_CHD; /* changed line */
3419 }
3420 else
3421 diff_hlf = HLF_ADD; /* added line */
3422 filler_lines = 0;
3423 area_highlighting = TRUE;
3424 }
3425 if (lnum == wp->w_topline)
3426 filler_lines = wp->w_topfill;
3427 filler_todo = filler_lines;
3428#endif
3429
3430#ifdef LINE_ATTR
3431# ifdef FEAT_SIGNS
3432 /* If this line has a sign with line highlighting set line_attr. */
3433 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3434 if (v != 0)
3435 line_attr = sign_get_attr((int)v, TRUE);
3436# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003437# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003439 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003440 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441# endif
3442 if (line_attr != 0)
3443 area_highlighting = TRUE;
3444#endif
3445
3446 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3447 ptr = line;
3448
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003449#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003450 if (has_spell)
3451 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003452 /* For checking first word with a capital skip white space. */
3453 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003454 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003455
Bram Moolenaar30abd282005-06-22 22:35:10 +00003456 /* To be able to spell-check over line boundaries copy the end of the
3457 * current line into nextline[]. Above the start of the next line was
3458 * copied to nextline[SPWORDLEN]. */
3459 if (nextline[SPWORDLEN] == NUL)
3460 {
3461 /* No next line or it is empty. */
3462 nextlinecol = MAXCOL;
3463 nextline_idx = 0;
3464 }
3465 else
3466 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003467 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003468 if (v < SPWORDLEN)
3469 {
3470 /* Short line, use it completely and append the start of the
3471 * next line. */
3472 nextlinecol = 0;
3473 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003474 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003475 nextline_idx = v + 1;
3476 }
3477 else
3478 {
3479 /* Long line, use only the last SPWORDLEN bytes. */
3480 nextlinecol = v - SPWORDLEN;
3481 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3482 nextline_idx = SPWORDLEN + 1;
3483 }
3484 }
3485 }
3486#endif
3487
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003488 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003490 if (lcs_space || lcs_trail)
3491 extra_check = TRUE;
3492 /* find start of trailing whitespace */
3493 if (lcs_trail)
3494 {
3495 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003496 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003497 --trailcol;
3498 trailcol += (colnr_T) (ptr - line);
3499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501
3502 /*
3503 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3504 * first character to be displayed.
3505 */
3506 if (wp->w_p_wrap)
3507 v = wp->w_skipcol;
3508 else
3509 v = wp->w_leftcol;
3510 if (v > 0)
3511 {
3512#ifdef FEAT_MBYTE
3513 char_u *prev_ptr = ptr;
3514#endif
3515 while (vcol < v && *ptr != NUL)
3516 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003517 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 vcol += c;
3519#ifdef FEAT_MBYTE
3520 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003522 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003525 /* When:
3526 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003527 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003528 * - 'virtualedit' is set, or
3529 * - the visual mode is active,
3530 * the end of the line may be before the start of the displayed part.
3531 */
3532 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003533#ifdef FEAT_SYN_HL
3534 wp->w_p_cuc || draw_color_col ||
3535#endif
3536#ifdef FEAT_VIRTUALEDIT
3537 virtual_active() ||
3538#endif
3539 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
3544 /* Handle a character that's not completely on the screen: Put ptr at
3545 * that character but skip the first few screen characters. */
3546 if (vcol > v)
3547 {
3548 vcol -= c;
3549#ifdef FEAT_MBYTE
3550 ptr = prev_ptr;
3551#else
3552 --ptr;
3553#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003554 /* If the character fits on the screen, don't need to skip it.
3555 * Except for a TAB. */
3556 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003557#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003558 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003559#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003560 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003561 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
3563
3564 /*
3565 * Adjust for when the inverted text is before the screen,
3566 * and when the start of the inverted text is before the screen.
3567 */
3568 if (tocol <= vcol)
3569 fromcol = 0;
3570 else if (fromcol >= 0 && fromcol < vcol)
3571 fromcol = vcol;
3572
3573#ifdef FEAT_LINEBREAK
3574 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3575 if (wp->w_p_wrap)
3576 need_showbreak = TRUE;
3577#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003578#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003579 /* When spell checking a word we need to figure out the start of the
3580 * word and if it's badly spelled or not. */
3581 if (has_spell)
3582 {
3583 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003584 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003585 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003586
3587 pos = wp->w_cursor;
3588 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003589 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003590 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003591
3592 /* spell_move_to() may call ml_get() and make "line" invalid */
3593 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3594 ptr = line + linecol;
3595
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003596 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003597 {
3598 /* no bad word found at line start, don't check until end of a
3599 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003600 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003601 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003602 }
3603 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003604 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003605 /* bad word found, use attributes until end of word */
3606 word_end = wp->w_cursor.col + len + 1;
3607
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003608 /* Turn index into actual attributes. */
3609 if (spell_hlf != HLF_COUNT)
3610 spell_attr = highlight_attr[spell_hlf];
3611 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003612 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003613
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003614# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003615 /* Need to restart syntax highlighting for this line. */
3616 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003617 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003618# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003619 }
3620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622
3623 /*
3624 * Correct highlighting for cursor that can't be disabled.
3625 * Avoids having to check this for each character.
3626 */
3627 if (fromcol >= 0)
3628 {
3629 if (noinvcur)
3630 {
3631 if ((colnr_T)fromcol == wp->w_virtcol)
3632 {
3633 /* highlighting starts at cursor, let it start just after the
3634 * cursor */
3635 fromcol_prev = fromcol;
3636 fromcol = -1;
3637 }
3638 else if ((colnr_T)fromcol < wp->w_virtcol)
3639 /* restart highlighting after the cursor */
3640 fromcol_prev = wp->w_virtcol;
3641 }
3642 if (fromcol >= tocol)
3643 fromcol = -1;
3644 }
3645
3646#ifdef FEAT_SEARCH_EXTRA
3647 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003648 * Handle highlighting the last used search pattern and matches.
3649 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003651 cur = wp->w_match_head;
3652 shl_flag = FALSE;
3653 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003655 if (shl_flag == FALSE)
3656 {
3657 shl = &search_hl;
3658 shl_flag = TRUE;
3659 }
3660 else
3661 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003662 shl->startcol = MAXCOL;
3663 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003665 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003666 v = (long)(ptr - line);
3667 if (cur != NULL)
3668 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003669 next_search_hl(wp, shl, lnum, (colnr_T)v,
3670 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003671
3672 /* Need to get the line again, a multi-line regexp may have made it
3673 * invalid. */
3674 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3675 ptr = line + v;
3676
3677 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003679 if (shl->lnum == lnum)
3680 shl->startcol = shl->rm.startpos[0].col;
3681 else
3682 shl->startcol = 0;
3683 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3684 - shl->rm.startpos[0].lnum)
3685 shl->endcol = shl->rm.endpos[0].col;
3686 else
3687 shl->endcol = MAXCOL;
3688 /* Highlight one character for an empty match. */
3689 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003692 if (has_mbyte && line[shl->endcol] != NUL)
3693 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3694 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003696 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003698 if ((long)shl->startcol < v) /* match at leftcol */
3699 {
3700 shl->attr_cur = shl->attr;
3701 search_attr = shl->attr;
3702 }
3703 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003705 if (shl != &search_hl && cur != NULL)
3706 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
3708#endif
3709
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003710#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003711 /* Cursor line highlighting for 'cursorline' in the current window. Not
3712 * when Visual mode is active, because it's not clear what is selected
3713 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003714 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003715 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003716 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003717 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003718 area_highlighting = TRUE;
3719 }
3720#endif
3721
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003722 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 col = 0;
3724#ifdef FEAT_RIGHTLEFT
3725 if (wp->w_p_rl)
3726 {
3727 /* Rightleft window: process the text in the normal direction, but put
3728 * it in current_ScreenLine[] from right to left. Start at the
3729 * rightmost column of the window. */
3730 col = W_WIDTH(wp) - 1;
3731 off += col;
3732 }
3733#endif
3734
3735 /*
3736 * Repeat for the whole displayed line.
3737 */
3738 for (;;)
3739 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003740#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003741 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 /* Skip this quickly when working on the text. */
3744 if (draw_state != WL_LINE)
3745 {
3746#ifdef FEAT_CMDWIN
3747 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3748 {
3749 draw_state = WL_CMDLINE;
3750 if (cmdwin_type != 0 && wp == curwin)
3751 {
3752 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003754 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003755 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757 }
3758#endif
3759
3760#ifdef FEAT_FOLDING
3761 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3762 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003763 int fdc = compute_foldcolumn(wp, 0);
3764
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003766 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003768 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003769 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003770 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003771 p_extra_free = alloc(12 + 1);
3772
3773 if (p_extra_free != NULL)
3774 {
3775 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3776 n_extra = fdc;
3777 p_extra_free[n_extra] = NUL;
3778 p_extra = p_extra_free;
3779 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003780 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
3783 }
3784#endif
3785
3786#ifdef FEAT_SIGNS
3787 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3788 {
3789 draw_state = WL_SIGN;
3790 /* Show the sign column when there are any signs in this
3791 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003792 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003794 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003796 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797# endif
3798
3799 /* Draw two cells with the sign value or blank. */
3800 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003801 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 n_extra = 2;
3803
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003804 if (row == startrow
3805#ifdef FEAT_DIFF
3806 + filler_lines && filler_todo <= 0
3807#endif
3808 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
3810 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3811 SIGN_TEXT);
3812# ifdef FEAT_SIGN_ICONS
3813 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3814 SIGN_ICON);
3815 if (gui.in_use && icon_sign != 0)
3816 {
3817 /* Use the image in this position. */
3818 c_extra = SIGN_BYTE;
3819# ifdef FEAT_NETBEANS_INTG
3820 if (buf_signcount(wp->w_buffer, lnum) > 1)
3821 c_extra = MULTISIGN_BYTE;
3822# endif
3823 char_attr = icon_sign;
3824 }
3825 else
3826# endif
3827 if (text_sign != 0)
3828 {
3829 p_extra = sign_get_text(text_sign);
3830 if (p_extra != NULL)
3831 {
3832 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003833 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835 char_attr = sign_get_attr(text_sign, FALSE);
3836 }
3837 }
3838 }
3839 }
3840#endif
3841
3842 if (draw_state == WL_NR - 1 && n_extra == 0)
3843 {
3844 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003845 /* Display the absolute or relative line number. After the
3846 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3847 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 && (row == startrow
3849#ifdef FEAT_DIFF
3850 + filler_lines
3851#endif
3852 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3853 {
3854 /* Draw the line number (empty space after wrapping). */
3855 if (row == startrow
3856#ifdef FEAT_DIFF
3857 + filler_lines
3858#endif
3859 )
3860 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003861 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003862 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003863
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003864 if (wp->w_p_nu && !wp->w_p_rnu)
3865 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003866 num = (long)lnum;
3867 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003868 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003869 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003870 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003871 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003872 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003873 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003874 num = lnum;
3875 fmt = "%-*ld ";
3876 }
3877 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003878
Bram Moolenaar700e7342013-01-30 12:31:36 +01003879 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003880 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 if (wp->w_skipcol > 0)
3882 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3883 *p_extra = '-';
3884#ifdef FEAT_RIGHTLEFT
3885 if (wp->w_p_rl) /* reverse line numbers */
3886 rl_mirror(extra);
3887#endif
3888 p_extra = extra;
3889 c_extra = NUL;
3890 }
3891 else
3892 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003893 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003894 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003895#ifdef FEAT_SYN_HL
3896 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003897 * the current line differently.
3898 * TODO: Can we use CursorLine instead of CursorLineNr
3899 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003900 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003901 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003902 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905 }
3906
Bram Moolenaar597a4222014-06-25 14:39:50 +02003907#ifdef FEAT_LINEBREAK
3908 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3909 && n_extra == 0 && *p_sbr != NUL)
3910 /* draw indent after showbreak value */
3911 draw_state = WL_BRI;
3912 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3913 /* After the showbreak, draw the breakindent */
3914 draw_state = WL_BRI - 1;
3915
3916 /* draw 'breakindent': indent wrapped text accordingly */
3917 if (draw_state == WL_BRI - 1 && n_extra == 0)
3918 {
3919 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003920 /* if need_showbreak is set, breakindent also applies */
3921 if (wp->w_p_bri && n_extra == 0
3922 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003923# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003924 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003925# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003926 )
3927 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003928 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003929# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003930 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003931 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003932 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003933# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003934 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3935 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003936 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003937# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003938 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003939# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003940 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003941 c_extra = ' ';
3942 n_extra = get_breakindent_win(wp,
3943 ml_get_buf(wp->w_buffer, lnum, FALSE));
3944 /* Correct end of highlighted area for 'breakindent',
3945 * required when 'linebreak' is also set. */
3946 if (tocol == vcol)
3947 tocol += n_extra;
3948 }
3949 }
3950#endif
3951
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3953 if (draw_state == WL_SBR - 1 && n_extra == 0)
3954 {
3955 draw_state = WL_SBR;
3956# ifdef FEAT_DIFF
3957 if (filler_todo > 0)
3958 {
3959 /* Draw "deleted" diff line(s). */
3960 if (char2cells(fill_diff) > 1)
3961 c_extra = '-';
3962 else
3963 c_extra = fill_diff;
3964# ifdef FEAT_RIGHTLEFT
3965 if (wp->w_p_rl)
3966 n_extra = col + 1;
3967 else
3968# endif
3969 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003970 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
3972# endif
3973# ifdef FEAT_LINEBREAK
3974 if (*p_sbr != NUL && need_showbreak)
3975 {
3976 /* Draw 'showbreak' at the start of each broken line. */
3977 p_extra = p_sbr;
3978 c_extra = NUL;
3979 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003980 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003982 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 /* Correct end of highlighted area for 'showbreak',
3984 * required when 'linebreak' is also set. */
3985 if (tocol == vcol)
3986 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003987#ifdef FEAT_SYN_HL
3988 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003989 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003990 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003991 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994# endif
3995 }
3996#endif
3997
3998 if (draw_state == WL_LINE - 1 && n_extra == 0)
3999 {
4000 draw_state = WL_LINE;
4001 if (saved_n_extra)
4002 {
4003 /* Continue item from end of wrapped line. */
4004 n_extra = saved_n_extra;
4005 c_extra = saved_c_extra;
4006 p_extra = saved_p_extra;
4007 char_attr = saved_char_attr;
4008 }
4009 else
4010 char_attr = 0;
4011 }
4012 }
4013
4014 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004015 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004016 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017#ifdef FEAT_DIFF
4018 && filler_todo <= 0
4019#endif
4020 )
4021 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004022 screen_line(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004023 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004024 /* Pretend we have finished updating the window. Except when
4025 * 'cursorcolumn' is set. */
4026#ifdef FEAT_SYN_HL
4027 if (wp->w_p_cuc)
4028 row = wp->w_cline_row + wp->w_cline_height;
4029 else
4030#endif
4031 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 break;
4033 }
4034
4035 if (draw_state == WL_LINE && area_highlighting)
4036 {
4037 /* handle Visual or match highlighting in this line */
4038 if (vcol == fromcol
4039#ifdef FEAT_MBYTE
4040 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4041 && (*mb_ptr2cells)(ptr) > 1)
4042#endif
4043 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004044 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 && vcol < tocol))
4046 area_attr = attr; /* start highlighting */
4047 else if (area_attr != 0
4048 && (vcol == tocol
4049 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052#ifdef FEAT_SEARCH_EXTRA
4053 if (!n_extra)
4054 {
4055 /*
4056 * Check for start/end of search pattern match.
4057 * After end, check for start/end of next match.
4058 * When another match, have to check for start again.
4059 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004060 * Do this for 'search_hl' and the match list (ordered by
4061 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004063 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004064 cur = wp->w_match_head;
4065 shl_flag = FALSE;
4066 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004068 if (shl_flag == FALSE
4069 && ((cur != NULL
4070 && cur->priority > SEARCH_HL_PRIORITY)
4071 || cur == NULL))
4072 {
4073 shl = &search_hl;
4074 shl_flag = TRUE;
4075 }
4076 else
4077 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004078 if (cur != NULL)
4079 cur->pos.cur = 0;
4080 pos_inprogress = TRUE;
4081 while (shl->rm.regprog != NULL
4082 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004084 if (shl->startcol != MAXCOL
4085 && v >= (long)shl->startcol
4086 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004088#ifdef FEAT_MBYTE
4089 int tmp_col = v + MB_PTR2LEN(ptr);
4090
4091 if (shl->endcol < tmp_col)
4092 shl->endcol = tmp_col;
4093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004095#ifdef FEAT_CONCEAL
4096 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4097 == cur->hlg_id)
4098 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004099 has_match_conc =
4100 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004101 match_conc = cur->conceal_char;
4102 }
4103 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004104 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004107 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
4109 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004110 next_search_hl(wp, shl, lnum, (colnr_T)v,
4111 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004112 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004113 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115 /* Need to get the line again, a multi-line regexp
4116 * may have made it invalid. */
4117 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4118 ptr = line + v;
4119
4120 if (shl->lnum == lnum)
4121 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004122 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004124 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004126 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004128 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
4130 /* highlight empty match, try again after
4131 * it */
4132#ifdef FEAT_MBYTE
4133 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004134 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004135 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 else
4137#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004138 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140
4141 /* Loop to check if the match starts at the
4142 * current position */
4143 continue;
4144 }
4145 }
4146 break;
4147 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004148 if (shl != &search_hl && cur != NULL)
4149 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004151
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004152 /* Use attributes from match with highest priority among
4153 * 'search_hl' and the match list. */
4154 search_attr = search_hl.attr_cur;
4155 cur = wp->w_match_head;
4156 shl_flag = FALSE;
4157 while (cur != NULL || shl_flag == FALSE)
4158 {
4159 if (shl_flag == FALSE
4160 && ((cur != NULL
4161 && cur->priority > SEARCH_HL_PRIORITY)
4162 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004163 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004164 shl = &search_hl;
4165 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004166 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004167 else
4168 shl = &cur->hl;
4169 if (shl->attr_cur != 0)
4170 search_attr = shl->attr_cur;
4171 if (shl != &search_hl && cur != NULL)
4172 cur = cur->next;
4173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 }
4175#endif
4176
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004178 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004180 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4181 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004183 if (diff_hlf == HLF_TXD && ptr - line > change_end
4184 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004186 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004187 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004188 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004191
4192 /* Decide which of the highlight attributes to use. */
4193 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004194#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004195 if (area_attr != 0)
4196 char_attr = hl_combine_attr(line_attr, area_attr);
4197 else if (search_attr != 0)
4198 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004199 /* Use line_attr when not in the Visual or 'incsearch' area
4200 * (area_attr may be 0 when "noinvcur" is set). */
4201 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004202 || vcol < fromcol || vcol_prev < fromcol_prev
4203 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004204 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004205#else
4206 if (area_attr != 0)
4207 char_attr = area_attr;
4208 else if (search_attr != 0)
4209 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004210#endif
4211 else
4212 {
4213 attr_pri = FALSE;
4214#ifdef FEAT_SYN_HL
4215 if (has_syntax)
4216 char_attr = syntax_attr;
4217 else
4218#endif
4219 char_attr = 0;
4220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222
4223 /*
4224 * Get the next character to put on the screen.
4225 */
4226 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004227 * The "p_extra" points to the extra stuff that is inserted to
4228 * represent special characters (non-printable stuff) and other
4229 * things. When all characters are the same, c_extra is used.
4230 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4231 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4233 */
4234 if (n_extra > 0)
4235 {
4236 if (c_extra != NUL)
4237 {
4238 c = c_extra;
4239#ifdef FEAT_MBYTE
4240 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004241 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
4243 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004244 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004245 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 else
4248 mb_utf8 = FALSE;
4249#endif
4250 }
4251 else
4252 {
4253 c = *p_extra;
4254#ifdef FEAT_MBYTE
4255 if (has_mbyte)
4256 {
4257 mb_c = c;
4258 if (enc_utf8)
4259 {
4260 /* If the UTF-8 character is more than one byte:
4261 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004262 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 mb_utf8 = FALSE;
4264 if (mb_l > n_extra)
4265 mb_l = 1;
4266 else if (mb_l > 1)
4267 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004268 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004270 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272 }
4273 else
4274 {
4275 /* if this is a DBCS character, put it in "mb_c" */
4276 mb_l = MB_BYTE2LEN(c);
4277 if (mb_l >= n_extra)
4278 mb_l = 1;
4279 else if (mb_l > 1)
4280 mb_c = (c << 8) + p_extra[1];
4281 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004282 if (mb_l == 0) /* at the NUL at end-of-line */
4283 mb_l = 1;
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 /* If a double-width char doesn't fit display a '>' in the
4286 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004287 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288# ifdef FEAT_RIGHTLEFT
4289 wp->w_p_rl ? (col <= 0) :
4290# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004291 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 && (*mb_char2cells)(mb_c) == 2)
4293 {
4294 c = '>';
4295 mb_c = c;
4296 mb_l = 1;
4297 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004298 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 /* put the pointer back to output the double-width
4300 * character at the start of the next line. */
4301 ++n_extra;
4302 --p_extra;
4303 }
4304 else
4305 {
4306 n_extra -= mb_l - 1;
4307 p_extra += mb_l - 1;
4308 }
4309 }
4310#endif
4311 ++p_extra;
4312 }
4313 --n_extra;
4314 }
4315 else
4316 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004317#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004318 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004319#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004320
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004321 if (p_extra_free != NULL)
4322 {
4323 vim_free(p_extra_free);
4324 p_extra_free = NULL;
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 /*
4327 * Get a character from the line itself.
4328 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004329 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004330#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004331 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333#ifdef FEAT_MBYTE
4334 if (has_mbyte)
4335 {
4336 mb_c = c;
4337 if (enc_utf8)
4338 {
4339 /* If the UTF-8 character is more than one byte: Decode it
4340 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004341 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 mb_utf8 = FALSE;
4343 if (mb_l > 1)
4344 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004345 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 /* Overlong encoded ASCII or ASCII with composing char
4347 * is displayed normally, except a NUL. */
4348 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004349 {
4350 c = mb_c;
4351# ifdef FEAT_LINEBREAK
4352 c0 = mb_c;
4353# endif
4354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004356
4357 /* At start of the line we can have a composing char.
4358 * Draw it as a space with a composing char. */
4359 if (utf_iscomposing(mb_c))
4360 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004361 int i;
4362
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004363 for (i = Screen_mco - 1; i > 0; --i)
4364 u8cc[i] = u8cc[i - 1];
4365 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004366 mb_c = ' ';
4367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369
4370 if ((mb_l == 1 && c >= 0x80)
4371 || (mb_l >= 1 && mb_c == 0)
4372 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004373# ifdef UNICODE16
4374 || mb_c >= 0x10000
4375# endif
4376 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 {
4378 /*
4379 * Illegal UTF-8 byte: display as <xx>.
4380 * Non-BMP character : display as ? or fullwidth ?.
4381 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004382# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
4386 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004387# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 if (wp->w_p_rl) /* reverse */
4389 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004392# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 else if (utf_char2cells(mb_c) != 2)
4394 STRCPY(extra, "?");
4395 else
4396 /* 0xff1f in UTF-8: full-width '?' */
4397 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
4400 p_extra = extra;
4401 c = *p_extra;
4402 mb_c = mb_ptr2char_adv(&p_extra);
4403 mb_utf8 = (c >= 0x80);
4404 n_extra = (int)STRLEN(p_extra);
4405 c_extra = NUL;
4406 if (area_attr == 0 && search_attr == 0)
4407 {
4408 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004409 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 saved_attr2 = char_attr; /* save current attr */
4411 }
4412 }
4413 else if (mb_l == 0) /* at the NUL at end-of-line */
4414 mb_l = 1;
4415#ifdef FEAT_ARABIC
4416 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4417 {
4418 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004419 int pc, pc1, nc;
4420 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422 /* The idea of what is the previous and next
4423 * character depends on 'rightleft'. */
4424 if (wp->w_p_rl)
4425 {
4426 pc = prev_c;
4427 pc1 = prev_c1;
4428 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004429 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431 else
4432 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004433 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004435 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437 prev_c = mb_c;
4438
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004439 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 else
4442 prev_c = mb_c;
4443#endif
4444 }
4445 else /* enc_dbcs */
4446 {
4447 mb_l = MB_BYTE2LEN(c);
4448 if (mb_l == 0) /* at the NUL at end-of-line */
4449 mb_l = 1;
4450 else if (mb_l > 1)
4451 {
4452 /* We assume a second byte below 32 is illegal.
4453 * Hopefully this is OK for all double-byte encodings!
4454 */
4455 if (ptr[1] >= 32)
4456 mb_c = (c << 8) + ptr[1];
4457 else
4458 {
4459 if (ptr[1] == NUL)
4460 {
4461 /* head byte at end of line */
4462 mb_l = 1;
4463 transchar_nonprint(extra, c);
4464 }
4465 else
4466 {
4467 /* illegal tail byte */
4468 mb_l = 2;
4469 STRCPY(extra, "XX");
4470 }
4471 p_extra = extra;
4472 n_extra = (int)STRLEN(extra) - 1;
4473 c_extra = NUL;
4474 c = *p_extra++;
4475 if (area_attr == 0 && search_attr == 0)
4476 {
4477 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004478 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 saved_attr2 = char_attr; /* save current attr */
4480 }
4481 mb_c = c;
4482 }
4483 }
4484 }
4485 /* If a double-width char doesn't fit display a '>' in the
4486 * last column; the character is displayed at the start of the
4487 * next line. */
4488 if ((
4489# ifdef FEAT_RIGHTLEFT
4490 wp->w_p_rl ? (col <= 0) :
4491# endif
4492 (col >= W_WIDTH(wp) - 1))
4493 && (*mb_char2cells)(mb_c) == 2)
4494 {
4495 c = '>';
4496 mb_c = c;
4497 mb_utf8 = FALSE;
4498 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004499 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 /* Put pointer back so that the character will be
4501 * displayed at the start of the next line. */
4502 --ptr;
4503 }
4504 else if (*ptr != NUL)
4505 ptr += mb_l - 1;
4506
4507 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004508 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004509 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004510 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004513 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 c = ' ';
4515 if (area_attr == 0 && search_attr == 0)
4516 {
4517 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004518 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 saved_attr2 = char_attr; /* save current attr */
4520 }
4521 mb_c = c;
4522 mb_utf8 = FALSE;
4523 mb_l = 1;
4524 }
4525
4526 }
4527#endif
4528 ++ptr;
4529
4530 if (extra_check)
4531 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004532#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004533 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004534#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004535
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004536#ifdef FEAT_TERMINAL
4537 if (get_term_attr)
4538 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004539 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004540
4541 if (!attr_pri)
4542 char_attr = syntax_attr;
4543 else
4544 char_attr = hl_combine_attr(syntax_attr, char_attr);
4545 }
4546#endif
4547
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004548#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 /* Get syntax attribute, unless still at the start of the line
4550 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004551 v = (long)(ptr - line);
4552 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
4554 /* Get the syntax attribute for the character. If there
4555 * is an error, disable syntax highlighting. */
4556 save_did_emsg = did_emsg;
4557 did_emsg = FALSE;
4558
Bram Moolenaar217ad922005-03-20 22:37:15 +00004559 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004560# ifdef FEAT_SPELL
4561 has_spell ? &can_spell :
4562# endif
4563 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
4565 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004566 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004567 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004568 has_syntax = FALSE;
4569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 else
4571 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004572#ifdef SYN_TIME_LIMIT
4573 if (wp->w_s->b_syn_slow)
4574 has_syntax = FALSE;
4575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
4577 /* Need to get the line again, a multi-line regexp may
4578 * have made it invalid. */
4579 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4580 ptr = line + v;
4581
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004582 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004584 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004585 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004586# ifdef FEAT_CONCEAL
4587 /* no concealing past the end of the line, it interferes
4588 * with line highlighting */
4589 if (c == NUL)
4590 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004591 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004592 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004595#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004596
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004597#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004598 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004599 * Only do this when there is no syntax highlighting, the
4600 * @Spell cluster is not used or the current syntax item
4601 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004602 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004603 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004604 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004605# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004606 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004607 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004608# endif
4609 if (c != 0 && (
4610# ifdef FEAT_SYN_HL
4611 !has_syntax ||
4612# endif
4613 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004614 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004615 char_u *prev_ptr, *p;
4616 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004617 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004618# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004619 if (has_mbyte)
4620 {
4621 prev_ptr = ptr - mb_l;
4622 v -= mb_l - 1;
4623 }
4624 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004625# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004626 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004627
4628 /* Use nextline[] if possible, it has the start of the
4629 * next line concatenated. */
4630 if ((prev_ptr - line) - nextlinecol >= 0)
4631 p = nextline + (prev_ptr - line) - nextlinecol;
4632 else
4633 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004634 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004635 len = spell_check(wp, p, &spell_hlf, &cap_col,
4636 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004637 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004638
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004639 /* In Insert mode only highlight a word that
4640 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004641 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004642 && (State & INSERT) != 0
4643 && wp->w_cursor.lnum == lnum
4644 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004645 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004646 && wp->w_cursor.col < (colnr_T)word_end)
4647 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004648 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004649 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004650 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004651
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004652 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004653 && (p - nextline) + len > nextline_idx)
4654 {
4655 /* Remember that the good word continues at the
4656 * start of the next line. */
4657 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004658 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004659 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004660
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004661 /* Turn index into actual attributes. */
4662 if (spell_hlf != HLF_COUNT)
4663 spell_attr = highlight_attr[spell_hlf];
4664
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004665 if (cap_col > 0)
4666 {
4667 if (p != prev_ptr
4668 && (p - nextline) + cap_col >= nextline_idx)
4669 {
4670 /* Remember that the word in the next line
4671 * must start with a capital. */
4672 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004673 cap_col = (int)((p - nextline) + cap_col
4674 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004675 }
4676 else
4677 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004678 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004679 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004680 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004681 }
4682 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004683 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004684 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004685 char_attr = hl_combine_attr(char_attr, spell_attr);
4686 else
4687 char_attr = hl_combine_attr(spell_attr, char_attr);
4688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689#endif
4690#ifdef FEAT_LINEBREAK
4691 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004692 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004694 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004695 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004697# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004698 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004699# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004700 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004702 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004704 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004705
Bram Moolenaar597a4222014-06-25 14:39:50 +02004706 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004707 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004708 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004709 if (c == TAB && n_extra + col > W_WIDTH(wp))
4710 n_extra = (int)wp->w_buffer->b_p_ts
4711 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4712
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004713# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004714 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004715# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004717# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004718 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004719 {
4720#ifdef FEAT_CONCEAL
4721 if (c == TAB)
4722 /* See "Tab alignment" below. */
4723 FIX_FOR_BOGUSCOLS;
4724#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004725 if (!wp->w_p_list)
4726 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 }
4729#endif
4730
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004731 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4732 */
4733 if (wp->w_p_list
4734 && (((c == 160
4735#ifdef FEAT_MBYTE
4736 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4737#endif
4738 ) && lcs_nbsp)
4739 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4740 {
4741 c = (c == ' ') ? lcs_space : lcs_nbsp;
4742 if (area_attr == 0 && search_attr == 0)
4743 {
4744 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004745 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004746 saved_attr2 = char_attr; /* save current attr */
4747 }
4748#ifdef FEAT_MBYTE
4749 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004750 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004751 {
4752 mb_utf8 = TRUE;
4753 u8cc[0] = 0;
4754 c = 0xc0;
4755 }
4756 else
4757 mb_utf8 = FALSE;
4758#endif
4759 }
4760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4762 {
4763 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004764 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
4766 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004767 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 saved_attr2 = char_attr; /* save current attr */
4769 }
4770#ifdef FEAT_MBYTE
4771 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004772 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
4774 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004775 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004776 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
4778 else
4779 mb_utf8 = FALSE;
4780#endif
4781 }
4782 }
4783
4784 /*
4785 * Handling of non-printable characters.
4786 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004787 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 {
4789 /*
4790 * when getting a character from the file, we may have to
4791 * turn it into something else on the way to putting it
4792 * into "ScreenLines".
4793 */
4794 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4795 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004796 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004797 long vcol_adjusted = vcol; /* removed showbreak length */
4798#ifdef FEAT_LINEBREAK
4799 /* only adjust the tab_len, when at the first column
4800 * after the showbreak value was drawn */
4801 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4802 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004805 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004806 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4807
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004808#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004809 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004810#endif
4811 /* tab amount depends on current column */
4812 n_extra = tab_len;
4813#ifdef FEAT_LINEBREAK
4814 else
4815 {
4816 char_u *p;
4817 int len = n_extra;
4818 int i;
4819 int saved_nextra = n_extra;
4820
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004821#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004822 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004823 /* there are characters to conceal */
4824 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004825 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4826 */
4827 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4828 && n_extra > tab_len)
4829 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004830#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004831
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004832 /* if n_extra > 0, it gives the number of chars, to
4833 * use for a tab, else we need to calculate the width
4834 * for a tab */
4835#ifdef FEAT_MBYTE
4836 len = (tab_len * mb_char2len(lcs_tab2));
4837 if (n_extra > 0)
4838 len += n_extra - tab_len;
4839#endif
4840 c = lcs_tab1;
4841 p = alloc((unsigned)(len + 1));
4842 vim_memset(p, ' ', len);
4843 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004844 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004845 p_extra_free = p;
4846 for (i = 0; i < tab_len; i++)
4847 {
4848#ifdef FEAT_MBYTE
4849 mb_char2bytes(lcs_tab2, p);
4850 p += mb_char2len(lcs_tab2);
4851 n_extra += mb_char2len(lcs_tab2)
4852 - (saved_nextra > 0 ? 1 : 0);
4853#else
4854 p[i] = lcs_tab2;
4855#endif
4856 }
4857 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004858#ifdef FEAT_CONCEAL
4859 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4860 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004861 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004862 n_extra -= vcol_off;
4863#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004864 }
4865#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004866#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004867 {
4868 int vc_saved = vcol_off;
4869
4870 /* Tab alignment should be identical regardless of
4871 * 'conceallevel' value. So tab compensates of all
4872 * previous concealed characters, and thus resets
4873 * vcol_off and boguscols accumulated so far in the
4874 * line. Note that the tab can be longer than
4875 * 'tabstop' when there are concealed characters. */
4876 FIX_FOR_BOGUSCOLS;
4877
4878 /* Make sure, the highlighting for the tab char will be
4879 * correctly set further below (effectively reverts the
4880 * FIX_FOR_BOGSUCOLS macro */
4881 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004882 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004883 tab_len += vc_saved;
4884 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886#ifdef FEAT_MBYTE
4887 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4888#endif
4889 if (wp->w_p_list)
4890 {
4891 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004892#ifdef FEAT_LINEBREAK
4893 if (wp->w_p_lbr)
4894 c_extra = NUL; /* using p_extra from above */
4895 else
4896#endif
4897 c_extra = lcs_tab2;
4898 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004899 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 saved_attr2 = char_attr; /* save current attr */
4901#ifdef FEAT_MBYTE
4902 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004903 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
4905 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004906 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004907 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909#endif
4910 }
4911 else
4912 {
4913 c_extra = ' ';
4914 c = ' ';
4915 }
4916 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004917 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004918 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004919 || ((fromcol >= 0 || fromcol_prev >= 0)
4920 && tocol > vcol
4921 && VIsual_mode != Ctrl_V
4922 && (
4923# ifdef FEAT_RIGHTLEFT
4924 wp->w_p_rl ? (col >= 0) :
4925# endif
4926 (col < W_WIDTH(wp)))
4927 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004928 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004929 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004930 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004932 /* Display a '$' after the line or highlight an extra
4933 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4935 /* For a diff line the highlighting continues after the
4936 * "$". */
4937 if (
4938# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004939 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940# ifdef LINE_ATTR
4941 &&
4942# endif
4943# endif
4944# ifdef LINE_ATTR
4945 line_attr == 0
4946# endif
4947 )
4948#endif
4949 {
4950#ifdef FEAT_VIRTUALEDIT
4951 /* In virtualedit, visual selections may extend
4952 * beyond end of line. */
4953 if (area_highlighting && virtual_active()
4954 && tocol != MAXCOL && vcol < tocol)
4955 n_extra = 0;
4956 else
4957#endif
4958 {
4959 p_extra = at_end_str;
4960 n_extra = 1;
4961 c_extra = NUL;
4962 }
4963 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004964 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004965 c = lcs_eol;
4966 else
4967 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 lcs_eol_one = -1;
4969 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004970 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004972 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 n_attr = 1;
4974 }
4975#ifdef FEAT_MBYTE
4976 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004977 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004980 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004981 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
4983 else
4984 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4985#endif
4986 }
4987 else if (c != NUL)
4988 {
4989 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004990 if (n_extra == 0)
4991 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992#ifdef FEAT_RIGHTLEFT
4993 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4994 rl_mirror(p_extra); /* reverse "<12>" */
4995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004997#ifdef FEAT_LINEBREAK
4998 if (wp->w_p_lbr)
4999 {
5000 char_u *p;
5001
5002 c = *p_extra;
5003 p = alloc((unsigned)n_extra + 1);
5004 vim_memset(p, ' ', n_extra);
5005 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5006 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005007 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005008 p_extra_free = p_extra = p;
5009 }
5010 else
5011#endif
5012 {
5013 n_extra = byte2cells(c) - 1;
5014 c = *p_extra++;
5015 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005016 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
5018 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005019 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 saved_attr2 = char_attr; /* save current attr */
5021 }
5022#ifdef FEAT_MBYTE
5023 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5024#endif
5025 }
5026#ifdef FEAT_VIRTUALEDIT
5027 else if (VIsual_active
5028 && (VIsual_mode == Ctrl_V
5029 || VIsual_mode == 'v')
5030 && virtual_active()
5031 && tocol != MAXCOL
5032 && vcol < tocol
5033 && (
5034# ifdef FEAT_RIGHTLEFT
5035 wp->w_p_rl ? (col >= 0) :
5036# endif
5037 (col < W_WIDTH(wp))))
5038 {
5039 c = ' ';
5040 --ptr; /* put it back at the NUL */
5041 }
5042#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005043#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 else if ((
5045# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005046 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005048# ifdef FEAT_TERMINAL
5049 term_attr != 0 ||
5050# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 ) && (
5053# ifdef FEAT_RIGHTLEFT
5054 wp->w_p_rl ? (col >= 0) :
5055# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005056 (col
5057# ifdef FEAT_CONCEAL
5058 - boguscols
5059# endif
5060 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
5062 /* Highlight until the right side of the window */
5063 c = ' ';
5064 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005065
5066 /* Remember we do the char for line highlighting. */
5067 ++did_line_attr;
5068
5069 /* don't do search HL for the rest of the line */
5070 if (line_attr != 0 && char_attr == search_attr && col > 0)
5071 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072# ifdef FEAT_DIFF
5073 if (diff_hlf == HLF_TXD)
5074 {
5075 diff_hlf = HLF_CHD;
5076 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005077 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005078 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005079 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5080 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005081 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
5084# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005085# ifdef FEAT_TERMINAL
5086 if (term_attr != 0)
5087 {
5088 char_attr = term_attr;
5089 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5090 char_attr = hl_combine_attr(char_attr,
5091 HL_ATTR(HLF_CUL));
5092 }
5093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095#endif
5096 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005097
5098#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005099 if ( wp->w_p_cole > 0
5100 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005101 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005102 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005103 && !(lnum_in_visual_area
5104 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005105 {
5106 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005107 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005108 && (syn_get_sub_char() != NUL || match_conc
5109 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005110 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005111 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005112 /* First time at this concealed item: display one
5113 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005114 if (match_conc)
5115 c = match_conc;
5116 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005117 c = syn_get_sub_char();
5118 else if (lcs_conceal != NUL)
5119 c = lcs_conceal;
5120 else
5121 c = ' ';
5122
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005123 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005124
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005125 if (n_extra > 0)
5126 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005127 vcol += n_extra;
5128 if (wp->w_p_wrap && n_extra > 0)
5129 {
5130# ifdef FEAT_RIGHTLEFT
5131 if (wp->w_p_rl)
5132 {
5133 col -= n_extra;
5134 boguscols -= n_extra;
5135 }
5136 else
5137# endif
5138 {
5139 boguscols += n_extra;
5140 col += n_extra;
5141 }
5142 }
5143 n_extra = 0;
5144 n_attr = 0;
5145 }
5146 else if (n_skip == 0)
5147 {
5148 is_concealing = TRUE;
5149 n_skip = 1;
5150 }
5151# ifdef FEAT_MBYTE
5152 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005153 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005154 {
5155 mb_utf8 = TRUE;
5156 u8cc[0] = 0;
5157 c = 0xc0;
5158 }
5159 else
5160 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5161# endif
5162 }
5163 else
5164 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005165 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005166 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005167 }
5168#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005171#ifdef FEAT_CONCEAL
5172 /* In the cursor line and we may be concealing characters: correct
5173 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005174 if (!did_wcol && draw_state == WL_LINE
5175 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005176 && conceal_cursor_line(wp)
5177 && (int)wp->w_virtcol <= vcol + n_skip)
5178 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005179# ifdef FEAT_RIGHTLEFT
5180 if (wp->w_p_rl)
5181 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5182 else
5183# endif
5184 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005185 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005186 did_wcol = TRUE;
5187 }
5188#endif
5189
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 /* Don't override visual selection highlighting. */
5191 if (n_attr > 0
5192 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005193 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 char_attr = extra_attr;
5195
Bram Moolenaar81695252004-12-29 20:58:21 +00005196#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 /* XIM don't send preedit_start and preedit_end, but they send
5198 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5199 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005200 if (p_imst == IM_ON_THE_SPOT
5201 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005202 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 && (State & INSERT)
5204 && !p_imdisable
5205 && im_is_preediting()
5206 && draw_state == WL_LINE)
5207 {
5208 colnr_T tcol;
5209
5210 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005211 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 else
5213 tcol = preedit_end_col;
5214 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5215 {
5216 if (feedback_old_attr < 0)
5217 {
5218 feedback_col = 0;
5219 feedback_old_attr = char_attr;
5220 }
5221 char_attr = im_get_feedback_attr(feedback_col);
5222 if (char_attr < 0)
5223 char_attr = feedback_old_attr;
5224 feedback_col++;
5225 }
5226 else if (feedback_old_attr >= 0)
5227 {
5228 char_attr = feedback_old_attr;
5229 feedback_old_attr = -1;
5230 feedback_col = 0;
5231 }
5232 }
5233#endif
5234 /*
5235 * Handle the case where we are in column 0 but not on the first
5236 * character of the line and the user wants us to show us a
5237 * special character (via 'listchars' option "precedes:<char>".
5238 */
5239 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005240 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5242#ifdef FEAT_DIFF
5243 && filler_todo <= 0
5244#endif
5245 && draw_state > WL_NR
5246 && c != NUL)
5247 {
5248 c = lcs_prec;
5249 lcs_prec_todo = NUL;
5250#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005251 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5252 {
5253 /* Double-width character being overwritten by the "precedes"
5254 * character, need to fill up half the character. */
5255 c_extra = MB_FILLER_CHAR;
5256 n_extra = 1;
5257 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005258 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005261 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
5263 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005264 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005265 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
5267 else
5268 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5269#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005270 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 {
5272 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005273 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 n_attr3 = 1;
5275 }
5276 }
5277
5278 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005279 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005281 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005282#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005283 || did_line_attr == 1
5284#endif
5285 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005287#ifdef FEAT_SEARCH_EXTRA
5288 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005289
5290 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005291 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005292 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005293#endif
5294
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005295 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 * highlight match at end of line. If it's beyond the last
5297 * char on the screen, just overwrite that one (tricky!) Not
5298 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005299#ifdef FEAT_SEARCH_EXTRA
5300 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005301 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005302 prevcol_hl_flag = TRUE;
5303 else
5304 {
5305 cur = wp->w_match_head;
5306 while (cur != NULL)
5307 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005308 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005309 {
5310 prevcol_hl_flag = TRUE;
5311 break;
5312 }
5313 cur = cur->next;
5314 }
5315 }
5316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005318 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005319 && (VIsual_mode != Ctrl_V
5320 || lnum == VIsual.lnum
5321 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005322 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323#ifdef FEAT_SEARCH_EXTRA
5324 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005325 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005326# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005327 && did_line_attr <= 1
5328# endif
5329 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330#endif
5331 ))
5332 {
5333 int n = 0;
5334
5335#ifdef FEAT_RIGHTLEFT
5336 if (wp->w_p_rl)
5337 {
5338 if (col < 0)
5339 n = 1;
5340 }
5341 else
5342#endif
5343 {
5344 if (col >= W_WIDTH(wp))
5345 n = -1;
5346 }
5347 if (n != 0)
5348 {
5349 /* At the window boundary, highlight the last character
5350 * instead (better than nothing). */
5351 off += n;
5352 col += n;
5353 }
5354 else
5355 {
5356 /* Add a blank character to highlight. */
5357 ScreenLines[off] = ' ';
5358#ifdef FEAT_MBYTE
5359 if (enc_utf8)
5360 ScreenLinesUC[off] = 0;
5361#endif
5362 }
5363#ifdef FEAT_SEARCH_EXTRA
5364 if (area_attr == 0)
5365 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005366 /* Use attributes from match with highest priority among
5367 * 'search_hl' and the match list. */
5368 char_attr = search_hl.attr;
5369 cur = wp->w_match_head;
5370 shl_flag = FALSE;
5371 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005372 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005373 if (shl_flag == FALSE
5374 && ((cur != NULL
5375 && cur->priority > SEARCH_HL_PRIORITY)
5376 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005377 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005378 shl = &search_hl;
5379 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005380 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005381 else
5382 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005383 if ((ptr - line) - 1 == (long)shl->startcol
5384 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005385 char_attr = shl->attr;
5386 if (shl != &search_hl && cur != NULL)
5387 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 }
5390#endif
5391 ScreenAttrs[off] = char_attr;
5392#ifdef FEAT_RIGHTLEFT
5393 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005396 --off;
5397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 else
5399#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005402 ++off;
5403 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005404 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005405#ifdef FEAT_SYN_HL
5406 eol_hl_off = 1;
5407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
Bram Moolenaar91170f82006-05-05 21:15:17 +00005411 /*
5412 * At end of the text line.
5413 */
5414 if (c == NUL)
5415 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005416#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005417 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5418 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005419 {
5420 /* highlight last char after line */
5421 --col;
5422 --off;
5423 --vcol;
5424 }
5425
Bram Moolenaar1a384422010-07-14 19:53:30 +02005426 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005427 if (wp->w_p_wrap)
5428 v = wp->w_skipcol;
5429 else
5430 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005431
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005432 /* check if line ends before left margin */
5433 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005434 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005435#ifdef FEAT_CONCEAL
5436 /* Get rid of the boguscols now, we want to draw until the right
5437 * edge for 'cursorcolumn'. */
5438 col -= boguscols;
5439 boguscols = 0;
5440#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005441
5442 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005443 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005444
5445 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005446 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5447 && (int)wp->w_virtcol <
5448 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005449 && lnum != wp->w_cursor.lnum)
5450 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005451# ifdef FEAT_RIGHTLEFT
5452 && !wp->w_p_rl
5453# endif
5454 )
5455 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005456 int rightmost_vcol = 0;
5457 int i;
5458
5459 if (wp->w_p_cuc)
5460 rightmost_vcol = wp->w_virtcol;
5461 if (draw_color_col)
5462 /* determine rightmost colorcolumn to possibly draw */
5463 for (i = 0; color_cols[i] >= 0; ++i)
5464 if (rightmost_vcol < color_cols[i])
5465 rightmost_vcol = color_cols[i];
5466
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005467 while (col < W_WIDTH(wp))
5468 {
5469 ScreenLines[off] = ' ';
5470#ifdef FEAT_MBYTE
5471 if (enc_utf8)
5472 ScreenLinesUC[off] = 0;
5473#endif
5474 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005475 if (draw_color_col)
5476 draw_color_col = advance_color_col(VCOL_HLC,
5477 &color_cols);
5478
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005479 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005480 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005481 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005482 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005483 else
5484 ScreenAttrs[off++] = 0;
5485
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005486 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005487 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005488
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005489 ++vcol;
5490 }
5491 }
5492#endif
5493
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005494 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005495 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 row++;
5497
5498 /*
5499 * Update w_cline_height and w_cline_folded if the cursor line was
5500 * updated (saves a call to plines() later).
5501 */
5502 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5503 {
5504 curwin->w_cline_row = startrow;
5505 curwin->w_cline_height = row - startrow;
5506#ifdef FEAT_FOLDING
5507 curwin->w_cline_folded = FALSE;
5508#endif
5509 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5510 }
5511
5512 break;
5513 }
5514
5515 /* line continues beyond line end */
5516 if (lcs_ext
5517 && !wp->w_p_wrap
5518#ifdef FEAT_DIFF
5519 && filler_todo <= 0
5520#endif
5521 && (
5522#ifdef FEAT_RIGHTLEFT
5523 wp->w_p_rl ? col == 0 :
5524#endif
5525 col == W_WIDTH(wp) - 1)
5526 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005527 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5529 {
5530 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005531 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532#ifdef FEAT_MBYTE
5533 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005534 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
5536 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005537 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005538 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 }
5540 else
5541 mb_utf8 = FALSE;
5542#endif
5543 }
5544
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005545#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005546 /* advance to the next 'colorcolumn' */
5547 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005548 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005549
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005550 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005551 * highlight the cursor position itself.
5552 * Also highlight the 'colorcolumn' if it is different than
5553 * 'cursorcolumn' */
5554 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005555 if (draw_state == WL_LINE && !lnum_in_visual_area
5556 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005557 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005558 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005559 && lnum != wp->w_cursor.lnum)
5560 {
5561 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005562 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005563 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005564 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005565 {
5566 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005567 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005568 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005569 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005570#endif
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 /*
5573 * Store character to be displayed.
5574 * Skip characters that are left of the screen for 'nowrap'.
5575 */
5576 vcol_prev = vcol;
5577 if (draw_state < WL_LINE || n_skip <= 0)
5578 {
5579 /*
5580 * Store the character.
5581 */
5582#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5583 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5584 {
5585 /* A double-wide character is: put first halve in left cell. */
5586 --off;
5587 --col;
5588 }
5589#endif
5590 ScreenLines[off] = c;
5591#ifdef FEAT_MBYTE
5592 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005593 {
5594 if ((mb_c & 0xff00) == 0x8e00)
5595 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 else if (enc_utf8)
5599 {
5600 if (mb_utf8)
5601 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005602 int i;
5603
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005605 if ((c & 0xff) == 0)
5606 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005607 for (i = 0; i < Screen_mco; ++i)
5608 {
5609 ScreenLinesC[i][off] = u8cc[i];
5610 if (u8cc[i] == 0)
5611 break;
5612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614 else
5615 ScreenLinesUC[off] = 0;
5616 }
5617 if (multi_attr)
5618 {
5619 ScreenAttrs[off] = multi_attr;
5620 multi_attr = 0;
5621 }
5622 else
5623#endif
5624 ScreenAttrs[off] = char_attr;
5625
5626#ifdef FEAT_MBYTE
5627 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5628 {
5629 /* Need to fill two screen columns. */
5630 ++off;
5631 ++col;
5632 if (enc_utf8)
5633 /* UTF-8: Put a 0 in the second screen char. */
5634 ScreenLines[off] = 0;
5635 else
5636 /* DBCS: Put second byte in the second screen char. */
5637 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005638 if (draw_state > WL_NR
5639#ifdef FEAT_DIFF
5640 && filler_todo <= 0
5641#endif
5642 )
5643 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 /* When "tocol" is halfway a character, set it to the end of
5645 * the character, otherwise highlighting won't stop. */
5646 if (tocol == vcol)
5647 ++tocol;
5648#ifdef FEAT_RIGHTLEFT
5649 if (wp->w_p_rl)
5650 {
5651 /* now it's time to backup one cell */
5652 --off;
5653 --col;
5654 }
5655#endif
5656 }
5657#endif
5658#ifdef FEAT_RIGHTLEFT
5659 if (wp->w_p_rl)
5660 {
5661 --off;
5662 --col;
5663 }
5664 else
5665#endif
5666 {
5667 ++off;
5668 ++col;
5669 }
5670 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005671#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005672 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005673 {
5674 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005675 ++vcol_off;
5676 if (n_extra > 0)
5677 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005678 if (wp->w_p_wrap)
5679 {
5680 /*
5681 * Special voodoo required if 'wrap' is on.
5682 *
5683 * Advance the column indicator to force the line
5684 * drawing to wrap early. This will make the line
5685 * take up the same screen space when parts are concealed,
5686 * so that cursor line computations aren't messed up.
5687 *
5688 * To avoid the fictitious advance of 'col' causing
5689 * trailing junk to be written out of the screen line
5690 * we are building, 'boguscols' keeps track of the number
5691 * of bad columns we have advanced.
5692 */
5693 if (n_extra > 0)
5694 {
5695 vcol += n_extra;
5696# ifdef FEAT_RIGHTLEFT
5697 if (wp->w_p_rl)
5698 {
5699 col -= n_extra;
5700 boguscols -= n_extra;
5701 }
5702 else
5703# endif
5704 {
5705 col += n_extra;
5706 boguscols += n_extra;
5707 }
5708 n_extra = 0;
5709 n_attr = 0;
5710 }
5711
5712
5713# ifdef FEAT_MBYTE
5714 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5715 {
5716 /* Need to fill two screen columns. */
5717# ifdef FEAT_RIGHTLEFT
5718 if (wp->w_p_rl)
5719 {
5720 --boguscols;
5721 --col;
5722 }
5723 else
5724# endif
5725 {
5726 ++boguscols;
5727 ++col;
5728 }
5729 }
5730# endif
5731
5732# ifdef FEAT_RIGHTLEFT
5733 if (wp->w_p_rl)
5734 {
5735 --boguscols;
5736 --col;
5737 }
5738 else
5739# endif
5740 {
5741 ++boguscols;
5742 ++col;
5743 }
5744 }
5745 else
5746 {
5747 if (n_extra > 0)
5748 {
5749 vcol += n_extra;
5750 n_extra = 0;
5751 n_attr = 0;
5752 }
5753 }
5754
5755 }
5756#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 else
5758 --n_skip;
5759
Bram Moolenaar64486672010-05-16 15:46:46 +02005760 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5761 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005762 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763#ifdef FEAT_DIFF
5764 && filler_todo <= 0
5765#endif
5766 )
5767 ++vcol;
5768
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005769#ifdef FEAT_SYN_HL
5770 if (vcol_save_attr >= 0)
5771 char_attr = vcol_save_attr;
5772#endif
5773
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 /* restore attributes after "predeces" in 'listchars' */
5775 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5776 char_attr = saved_attr3;
5777
5778 /* restore attributes after last 'listchars' or 'number' char */
5779 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5780 char_attr = saved_attr2;
5781
5782 /*
5783 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005784 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 */
5786 if ((
5787#ifdef FEAT_RIGHTLEFT
5788 wp->w_p_rl ? (col < 0) :
5789#endif
5790 (col >= W_WIDTH(wp)))
5791 && (*ptr != NUL
5792#ifdef FEAT_DIFF
5793 || filler_todo > 0
5794#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005795 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5797 )
5798 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005799#ifdef FEAT_CONCEAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005800 screen_line(screen_row, W_WINCOL(wp), col - boguscols,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005801 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005802 boguscols = 0;
5803#else
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005804 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005805 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 ++row;
5808 ++screen_row;
5809
5810 /* When not wrapping and finished diff lines, or when displayed
5811 * '$' and highlighting until last column, break here. */
5812 if ((!wp->w_p_wrap
5813#ifdef FEAT_DIFF
5814 && filler_todo <= 0
5815#endif
5816 ) || lcs_eol_one == -1)
5817 break;
5818
5819 /* When the window is too narrow draw all "@" lines. */
5820 if (draw_state != WL_LINE
5821#ifdef FEAT_DIFF
5822 && filler_todo <= 0
5823#endif
5824 )
5825 {
5826 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 row = endrow;
5829 }
5830
5831 /* When line got too long for screen break here. */
5832 if (row == endrow)
5833 {
5834 ++row;
5835 break;
5836 }
5837
5838 if (screen_cur_row == screen_row - 1
5839#ifdef FEAT_DIFF
5840 && filler_todo <= 0
5841#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005842 && W_WIDTH(wp) == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 {
5844 /* Remember that the line wraps, used for modeless copy. */
5845 LineWraps[screen_row - 1] = TRUE;
5846
5847 /*
5848 * Special trick to make copy/paste of wrapped lines work with
5849 * xterm/screen: write an extra character beyond the end of
5850 * the line. This will work with all terminal types
5851 * (regardless of the xn,am settings).
5852 * Only do this on a fast tty.
5853 * Only do this if the cursor is on the current line
5854 * (something has been written in it).
5855 * Don't do this for the GUI.
5856 * Don't do this for double-width characters.
5857 * Don't do this for a window not at the right screen border.
5858 */
5859 if (p_tf
5860#ifdef FEAT_GUI
5861 && !gui.in_use
5862#endif
5863#ifdef FEAT_MBYTE
5864 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005865 && ((*mb_off2cells)(LineOffset[screen_row],
5866 LineOffset[screen_row] + screen_Columns)
5867 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005869 + (int)Columns - 2,
5870 LineOffset[screen_row] + screen_Columns)
5871 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872#endif
5873 )
5874 {
5875 /* First make sure we are at the end of the screen line,
5876 * then output the same character again to let the
5877 * terminal know about the wrap. If the terminal doesn't
5878 * auto-wrap, we overwrite the character. */
5879 if (screen_cur_col != W_WIDTH(wp))
5880 screen_char(LineOffset[screen_row - 1]
5881 + (unsigned)Columns - 1,
5882 screen_row - 1, (int)(Columns - 1));
5883
5884#ifdef FEAT_MBYTE
5885 /* When there is a multi-byte character, just output a
5886 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005887 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5888 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 out_char(' ');
5890 else
5891#endif
5892 out_char(ScreenLines[LineOffset[screen_row - 1]
5893 + (Columns - 1)]);
5894 /* force a redraw of the first char on the next line */
5895 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5896 screen_start(); /* don't know where cursor is now */
5897 }
5898 }
5899
5900 col = 0;
5901 off = (unsigned)(current_ScreenLine - ScreenLines);
5902#ifdef FEAT_RIGHTLEFT
5903 if (wp->w_p_rl)
5904 {
5905 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5906 off += col;
5907 }
5908#endif
5909
5910 /* reset the drawing state for the start of a wrapped line */
5911 draw_state = WL_START;
5912 saved_n_extra = n_extra;
5913 saved_p_extra = p_extra;
5914 saved_c_extra = c_extra;
5915 saved_char_attr = char_attr;
5916 n_extra = 0;
5917 lcs_prec_todo = lcs_prec;
5918#ifdef FEAT_LINEBREAK
5919# ifdef FEAT_DIFF
5920 if (filler_todo <= 0)
5921# endif
5922 need_showbreak = TRUE;
5923#endif
5924#ifdef FEAT_DIFF
5925 --filler_todo;
5926 /* When the filler lines are actually below the last line of the
5927 * file, don't draw the line itself, break here. */
5928 if (filler_todo == 0 && wp->w_botfill)
5929 break;
5930#endif
5931 }
5932
5933 } /* for every character in the line */
5934
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005935#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005936 /* After an empty line check first word for capital. */
5937 if (*skipwhite(line) == NUL)
5938 {
5939 capcol_lnum = lnum + 1;
5940 cap_col = 0;
5941 }
5942#endif
5943
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005944 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 return row;
5946}
5947
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005948#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005949static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005950
5951/*
5952 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005953 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005954 */
5955 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005956comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005957{
5958 int i;
5959
5960 for (i = 0; i < Screen_mco; ++i)
5961 {
5962 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5963 return TRUE;
5964 if (ScreenLinesC[i][off_from] == 0)
5965 break;
5966 }
5967 return FALSE;
5968}
5969#endif
5970
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971/*
5972 * Check whether the given character needs redrawing:
5973 * - the (first byte of the) character is different
5974 * - the attributes are different
5975 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005976 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 */
5978 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005979char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
5981 if (cols > 0
5982 && ((ScreenLines[off_from] != ScreenLines[off_to]
5983 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5984
5985#ifdef FEAT_MBYTE
5986 || (enc_dbcs != 0
5987 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5988 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5989 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5990 : (cols > 1 && ScreenLines[off_from + 1]
5991 != ScreenLines[off_to + 1])))
5992 || (enc_utf8
5993 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5994 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005995 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005996 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5997 && ScreenLines[off_from + 1]
5998 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999#endif
6000 ))
6001 return TRUE;
6002 return FALSE;
6003}
6004
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006005#if defined(FEAT_TERMINAL) || defined(PROTO)
6006/*
6007 * Return the index in ScreenLines[] for the current screen line.
6008 */
6009 int
6010screen_get_current_line_off()
6011{
6012 return (int)(current_ScreenLine - ScreenLines);
6013}
6014#endif
6015
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016/*
6017 * Move one "cooked" screen line to the screen, but only the characters that
6018 * have actually changed. Handle insert/delete character.
6019 * "coloff" gives the first column on the screen for this line.
6020 * "endcol" gives the columns where valid characters are.
6021 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6022 * needs to be cleared, negative otherwise.
6023 * "rlflag" is TRUE in a rightleft window:
6024 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6025 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6026 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006027 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006028screen_line(
6029 int row,
6030 int coloff,
6031 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006032 int clear_width,
6033 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034{
6035 unsigned off_from;
6036 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006037#ifdef FEAT_MBYTE
6038 unsigned max_off_from;
6039 unsigned max_off_to;
6040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 int force = FALSE; /* force update rest of the line */
6044 int redraw_this /* bool: does character need redraw? */
6045#ifdef FEAT_GUI
6046 = TRUE /* For GUI when while-loop empty */
6047#endif
6048 ;
6049 int redraw_next; /* redraw_this for next character */
6050#ifdef FEAT_MBYTE
6051 int clear_next = FALSE;
6052 int char_cells; /* 1: normal char */
6053 /* 2: occupies two display cells */
6054# define CHAR_CELLS char_cells
6055#else
6056# define CHAR_CELLS 1
6057#endif
6058
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006059 /* Check for illegal row and col, just in case. */
6060 if (row >= Rows)
6061 row = Rows - 1;
6062 if (endcol > Columns)
6063 endcol = Columns;
6064
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065# ifdef FEAT_CLIPBOARD
6066 clip_may_clear_selection(row, row);
6067# endif
6068
6069 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6070 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006071#ifdef FEAT_MBYTE
6072 max_off_from = off_from + screen_Columns;
6073 max_off_to = LineOffset[row] + screen_Columns;
6074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
6076#ifdef FEAT_RIGHTLEFT
6077 if (rlflag)
6078 {
6079 /* Clear rest first, because it's left of the text. */
6080 if (clear_width > 0)
6081 {
6082 while (col <= endcol && ScreenLines[off_to] == ' '
6083 && ScreenAttrs[off_to] == 0
6084# ifdef FEAT_MBYTE
6085 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6086# endif
6087 )
6088 {
6089 ++off_to;
6090 ++col;
6091 }
6092 if (col <= endcol)
6093 screen_fill(row, row + 1, col + coloff,
6094 endcol + coloff + 1, ' ', ' ', 0);
6095 }
6096 col = endcol + 1;
6097 off_to = LineOffset[row] + col + coloff;
6098 off_from += col;
6099 endcol = (clear_width > 0 ? clear_width : -clear_width);
6100 }
6101#endif /* FEAT_RIGHTLEFT */
6102
6103 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6104
6105 while (col < endcol)
6106 {
6107#ifdef FEAT_MBYTE
6108 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006109 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 else
6111 char_cells = 1;
6112#endif
6113
6114 redraw_this = redraw_next;
6115 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6116 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6117
6118#ifdef FEAT_GUI
6119 /* If the next character was bold, then redraw the current character to
6120 * remove any pixels that might have spilt over into us. This only
6121 * happens in the GUI.
6122 */
6123 if (redraw_next && gui.in_use)
6124 {
6125 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006126 if (hl > HL_ALL)
6127 hl = syn_attr2attr(hl);
6128 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 redraw_this = TRUE;
6130 }
6131#endif
6132
6133 if (redraw_this)
6134 {
6135 /*
6136 * Special handling when 'xs' termcap flag set (hpterm):
6137 * Attributes for characters are stored at the position where the
6138 * cursor is when writing the highlighting code. The
6139 * start-highlighting code must be written with the cursor on the
6140 * first highlighted character. The stop-highlighting code must
6141 * be written with the cursor just after the last highlighted
6142 * character.
6143 * Overwriting a character doesn't remove it's highlighting. Need
6144 * to clear the rest of the line, and force redrawing it
6145 * completely.
6146 */
6147 if ( p_wiv
6148 && !force
6149#ifdef FEAT_GUI
6150 && !gui.in_use
6151#endif
6152 && ScreenAttrs[off_to] != 0
6153 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6154 {
6155 /*
6156 * Need to remove highlighting attributes here.
6157 */
6158 windgoto(row, col + coloff);
6159 out_str(T_CE); /* clear rest of this screen line */
6160 screen_start(); /* don't know where cursor is now */
6161 force = TRUE; /* force redraw of rest of the line */
6162 redraw_next = TRUE; /* or else next char would miss out */
6163
6164 /*
6165 * If the previous character was highlighted, need to stop
6166 * highlighting at this character.
6167 */
6168 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6169 {
6170 screen_attr = ScreenAttrs[off_to - 1];
6171 term_windgoto(row, col + coloff);
6172 screen_stop_highlight();
6173 }
6174 else
6175 screen_attr = 0; /* highlighting has stopped */
6176 }
6177#ifdef FEAT_MBYTE
6178 if (enc_dbcs != 0)
6179 {
6180 /* Check if overwriting a double-byte with a single-byte or
6181 * the other way around requires another character to be
6182 * redrawn. For UTF-8 this isn't needed, because comparing
6183 * ScreenLinesUC[] is sufficient. */
6184 if (char_cells == 1
6185 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006186 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 {
6188 /* Writing a single-cell character over a double-cell
6189 * character: need to redraw the next cell. */
6190 ScreenLines[off_to + 1] = 0;
6191 redraw_next = TRUE;
6192 }
6193 else if (char_cells == 2
6194 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006195 && (*mb_off2cells)(off_to, max_off_to) == 1
6196 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 {
6198 /* Writing the second half of a double-cell character over
6199 * a double-cell character: need to redraw the second
6200 * cell. */
6201 ScreenLines[off_to + 2] = 0;
6202 redraw_next = TRUE;
6203 }
6204
6205 if (enc_dbcs == DBCS_JPNU)
6206 ScreenLines2[off_to] = ScreenLines2[off_from];
6207 }
6208 /* When writing a single-width character over a double-width
6209 * character and at the end of the redrawn text, need to clear out
6210 * the right halve of the old character.
6211 * Also required when writing the right halve of a double-width
6212 * char over the left halve of an existing one. */
6213 if (has_mbyte && col + char_cells == endcol
6214 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006215 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006217 && (*mb_off2cells)(off_to, max_off_to) == 1
6218 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 clear_next = TRUE;
6220#endif
6221
6222 ScreenLines[off_to] = ScreenLines[off_from];
6223#ifdef FEAT_MBYTE
6224 if (enc_utf8)
6225 {
6226 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6227 if (ScreenLinesUC[off_from] != 0)
6228 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006229 int i;
6230
6231 for (i = 0; i < Screen_mco; ++i)
6232 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 }
6234 }
6235 if (char_cells == 2)
6236 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6237#endif
6238
6239#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006240 /* The bold trick makes a single column of pixels appear in the
6241 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 * character should be redrawn too. This happens for our own GUI
6243 * and for some xterms. */
6244 if (
6245# ifdef FEAT_GUI
6246 gui.in_use
6247# endif
6248# if defined(FEAT_GUI) && defined(UNIX)
6249 ||
6250# endif
6251# ifdef UNIX
6252 term_is_xterm
6253# endif
6254 )
6255 {
6256 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006257 if (hl > HL_ALL)
6258 hl = syn_attr2attr(hl);
6259 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 redraw_next = TRUE;
6261 }
6262#endif
6263 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6264#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006265 /* For simplicity set the attributes of second half of a
6266 * double-wide character equal to the first half. */
6267 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006269
6270 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 else
6273#endif
6274 screen_char(off_to, row, col + coloff);
6275 }
6276 else if ( p_wiv
6277#ifdef FEAT_GUI
6278 && !gui.in_use
6279#endif
6280 && col + coloff > 0)
6281 {
6282 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6283 {
6284 /*
6285 * Don't output stop-highlight when moving the cursor, it will
6286 * stop the highlighting when it should continue.
6287 */
6288 screen_attr = 0;
6289 }
6290 else if (screen_attr != 0)
6291 screen_stop_highlight();
6292 }
6293
6294 off_to += CHAR_CELLS;
6295 off_from += CHAR_CELLS;
6296 col += CHAR_CELLS;
6297 }
6298
6299#ifdef FEAT_MBYTE
6300 if (clear_next)
6301 {
6302 /* Clear the second half of a double-wide character of which the left
6303 * half was overwritten with a single-wide character. */
6304 ScreenLines[off_to] = ' ';
6305 if (enc_utf8)
6306 ScreenLinesUC[off_to] = 0;
6307 screen_char(off_to, row, col + coloff);
6308 }
6309#endif
6310
6311 if (clear_width > 0
6312#ifdef FEAT_RIGHTLEFT
6313 && !rlflag
6314#endif
6315 )
6316 {
6317#ifdef FEAT_GUI
6318 int startCol = col;
6319#endif
6320
6321 /* blank out the rest of the line */
6322 while (col < clear_width && ScreenLines[off_to] == ' '
6323 && ScreenAttrs[off_to] == 0
6324#ifdef FEAT_MBYTE
6325 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6326#endif
6327 )
6328 {
6329 ++off_to;
6330 ++col;
6331 }
6332 if (col < clear_width)
6333 {
6334#ifdef FEAT_GUI
6335 /*
6336 * In the GUI, clearing the rest of the line may leave pixels
6337 * behind if the first character cleared was bold. Some bold
6338 * fonts spill over the left. In this case we redraw the previous
6339 * character too. If we didn't skip any blanks above, then we
6340 * only redraw if the character wasn't already redrawn anyway.
6341 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006342 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 {
6344 hl = ScreenAttrs[off_to];
6345 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006346 {
6347 int prev_cells = 1;
6348# ifdef FEAT_MBYTE
6349 if (enc_utf8)
6350 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6351 * that its width is 2. */
6352 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6353 else if (enc_dbcs != 0)
6354 {
6355 /* find previous character by counting from first
6356 * column and get its width. */
6357 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006358 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006359
6360 while (off < off_to)
6361 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006362 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006363 off += prev_cells;
6364 }
6365 }
6366
6367 if (enc_dbcs != 0 && prev_cells > 1)
6368 screen_char_2(off_to - prev_cells, row,
6369 col + coloff - prev_cells);
6370 else
6371# endif
6372 screen_char(off_to - prev_cells, row,
6373 col + coloff - prev_cells);
6374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 }
6376#endif
6377 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6378 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 off_to += clear_width - col;
6380 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 }
6382 }
6383
6384 if (clear_width > 0)
6385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 /* For a window that's left of another, draw the separator char. */
6387 if (col + coloff < Columns)
6388 {
6389 int c;
6390
6391 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006392 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006393#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006394 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6395 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 || ScreenAttrs[off_to] != hl)
6398 {
6399 ScreenLines[off_to] = c;
6400 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006401#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 if (enc_utf8)
6403 {
6404 if (c >= 0x80)
6405 {
6406 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006407 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 }
6409 else
6410 ScreenLinesUC[off_to] = 0;
6411 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 screen_char(off_to, row, col + coloff);
6414 }
6415 }
6416 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 LineWraps[row] = FALSE;
6418 }
6419}
6420
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006421#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006423 * Mirror text "str" for right-left displaying.
6424 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006426 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006427rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428{
6429 char_u *p1, *p2;
6430 int t;
6431
6432 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6433 {
6434 t = *p1;
6435 *p1 = *p2;
6436 *p2 = t;
6437 }
6438}
6439#endif
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441/*
6442 * mark all status lines for redraw; used after first :cd
6443 */
6444 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006445status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447 win_T *wp;
6448
Bram Moolenaar29323592016-07-24 22:04:11 +02006449 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 if (wp->w_status_height)
6451 {
6452 wp->w_redr_status = TRUE;
6453 redraw_later(VALID);
6454 }
6455}
6456
6457/*
6458 * mark all status lines of the current buffer for redraw
6459 */
6460 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006461status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
6463 win_T *wp;
6464
Bram Moolenaar29323592016-07-24 22:04:11 +02006465 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6467 {
6468 wp->w_redr_status = TRUE;
6469 redraw_later(VALID);
6470 }
6471}
6472
6473/*
6474 * Redraw all status lines that need to be redrawn.
6475 */
6476 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006477redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
6479 win_T *wp;
6480
Bram Moolenaar29323592016-07-24 22:04:11 +02006481 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if (wp->w_redr_status)
6483 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006484 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006485 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
Bram Moolenaar4033c552017-09-16 20:54:51 +02006488#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489/*
6490 * Redraw all status lines at the bottom of frame "frp".
6491 */
6492 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006493win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
6495 if (frp->fr_layout == FR_LEAF)
6496 frp->fr_win->w_redr_status = TRUE;
6497 else if (frp->fr_layout == FR_ROW)
6498 {
6499 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6500 win_redraw_last_status(frp);
6501 }
6502 else /* frp->fr_layout == FR_COL */
6503 {
6504 frp = frp->fr_child;
6505 while (frp->fr_next != NULL)
6506 frp = frp->fr_next;
6507 win_redraw_last_status(frp);
6508 }
6509}
6510#endif
6511
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512/*
6513 * Draw the verticap separator right of window "wp" starting with line "row".
6514 */
6515 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006516draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
6518 int hl;
6519 int c;
6520
6521 if (wp->w_vsep_width)
6522 {
6523 /* draw the vertical separator right of this window */
6524 c = fillchar_vsep(&hl);
6525 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6526 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6527 c, ' ', hl);
6528 }
6529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
6531#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006532static int status_match_len(expand_T *xp, char_u *s);
6533static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534
6535/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006536 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 */
6538 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006539status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540{
6541 int len = 0;
6542
6543#ifdef FEAT_MENU
6544 int emenu = (xp->xp_context == EXPAND_MENUS
6545 || xp->xp_context == EXPAND_MENUNAMES);
6546
6547 /* Check for menu separators - replace with '|'. */
6548 if (emenu && menu_is_separator(s))
6549 return 1;
6550#endif
6551
6552 while (*s != NUL)
6553 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006554 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006555 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006556 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 }
6558
6559 return len;
6560}
6561
6562/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006563 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006564 * These are backslashes used for escaping. Do show backslashes in help tags.
6565 */
6566 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006567skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006568{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006569 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006570#ifdef FEAT_MENU
6571 || ((xp->xp_context == EXPAND_MENUS
6572 || xp->xp_context == EXPAND_MENUNAMES)
6573 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6574#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006575 )
6576 {
6577#ifndef BACKSLASH_IN_FILENAME
6578 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6579 return 2;
6580#endif
6581 return 1;
6582 }
6583 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006584}
6585
6586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 * Show wildchar matches in the status line.
6588 * Show at least the "match" item.
6589 * We start at item 'first_match' in the list and show all matches that fit.
6590 *
6591 * If inversion is possible we use it. Else '=' characters are used.
6592 */
6593 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006594win_redr_status_matches(
6595 expand_T *xp,
6596 int num_matches,
6597 char_u **matches, /* list of matches */
6598 int match,
6599 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600{
6601#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6602 int row;
6603 char_u *buf;
6604 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006605 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 int fillchar;
6607 int attr;
6608 int i;
6609 int highlight = TRUE;
6610 char_u *selstart = NULL;
6611 int selstart_col = 0;
6612 char_u *selend = NULL;
6613 static int first_match = 0;
6614 int add_left = FALSE;
6615 char_u *s;
6616#ifdef FEAT_MENU
6617 int emenu;
6618#endif
6619#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6620 int l;
6621#endif
6622
6623 if (matches == NULL) /* interrupted completion? */
6624 return;
6625
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006626#ifdef FEAT_MBYTE
6627 if (has_mbyte)
6628 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6629 else
6630#endif
6631 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 if (buf == NULL)
6633 return;
6634
6635 if (match == -1) /* don't show match but original text */
6636 {
6637 match = 0;
6638 highlight = FALSE;
6639 }
6640 /* count 1 for the ending ">" */
6641 clen = status_match_len(xp, L_MATCH(match)) + 3;
6642 if (match == 0)
6643 first_match = 0;
6644 else if (match < first_match)
6645 {
6646 /* jumping left, as far as we can go */
6647 first_match = match;
6648 add_left = TRUE;
6649 }
6650 else
6651 {
6652 /* check if match fits on the screen */
6653 for (i = first_match; i < match; ++i)
6654 clen += status_match_len(xp, L_MATCH(i)) + 2;
6655 if (first_match > 0)
6656 clen += 2;
6657 /* jumping right, put match at the left */
6658 if ((long)clen > Columns)
6659 {
6660 first_match = match;
6661 /* if showing the last match, we can add some on the left */
6662 clen = 2;
6663 for (i = match; i < num_matches; ++i)
6664 {
6665 clen += status_match_len(xp, L_MATCH(i)) + 2;
6666 if ((long)clen >= Columns)
6667 break;
6668 }
6669 if (i == num_matches)
6670 add_left = TRUE;
6671 }
6672 }
6673 if (add_left)
6674 while (first_match > 0)
6675 {
6676 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6677 if ((long)clen >= Columns)
6678 break;
6679 --first_match;
6680 }
6681
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006682 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
6684 if (first_match == 0)
6685 {
6686 *buf = NUL;
6687 len = 0;
6688 }
6689 else
6690 {
6691 STRCPY(buf, "< ");
6692 len = 2;
6693 }
6694 clen = len;
6695
6696 i = first_match;
6697 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6698 {
6699 if (i == match)
6700 {
6701 selstart = buf + len;
6702 selstart_col = clen;
6703 }
6704
6705 s = L_MATCH(i);
6706 /* Check for menu separators - replace with '|' */
6707#ifdef FEAT_MENU
6708 emenu = (xp->xp_context == EXPAND_MENUS
6709 || xp->xp_context == EXPAND_MENUNAMES);
6710 if (emenu && menu_is_separator(s))
6711 {
6712 STRCPY(buf + len, transchar('|'));
6713 l = (int)STRLEN(buf + len);
6714 len += l;
6715 clen += l;
6716 }
6717 else
6718#endif
6719 for ( ; *s != NUL; ++s)
6720 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006721 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 clen += ptr2cells(s);
6723#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006724 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 {
6726 STRNCPY(buf + len, s, l);
6727 s += l - 1;
6728 len += l;
6729 }
6730 else
6731#endif
6732 {
6733 STRCPY(buf + len, transchar_byte(*s));
6734 len += (int)STRLEN(buf + len);
6735 }
6736 }
6737 if (i == match)
6738 selend = buf + len;
6739
6740 *(buf + len++) = ' ';
6741 *(buf + len++) = ' ';
6742 clen += 2;
6743 if (++i == num_matches)
6744 break;
6745 }
6746
6747 if (i != num_matches)
6748 {
6749 *(buf + len++) = '>';
6750 ++clen;
6751 }
6752
6753 buf[len] = NUL;
6754
6755 row = cmdline_row - 1;
6756 if (row >= 0)
6757 {
6758 if (wild_menu_showing == 0)
6759 {
6760 if (msg_scrolled > 0)
6761 {
6762 /* Put the wildmenu just above the command line. If there is
6763 * no room, scroll the screen one line up. */
6764 if (cmdline_row == Rows - 1)
6765 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006766 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 ++msg_scrolled;
6768 }
6769 else
6770 {
6771 ++cmdline_row;
6772 ++row;
6773 }
6774 wild_menu_showing = WM_SCROLLED;
6775 }
6776 else
6777 {
6778 /* Create status line if needed by setting 'laststatus' to 2.
6779 * Set 'winminheight' to zero to avoid that the window is
6780 * resized. */
6781 if (lastwin->w_status_height == 0)
6782 {
6783 save_p_ls = p_ls;
6784 save_p_wmh = p_wmh;
6785 p_ls = 2;
6786 p_wmh = 0;
6787 last_status(FALSE);
6788 }
6789 wild_menu_showing = WM_SHOWN;
6790 }
6791 }
6792
6793 screen_puts(buf, row, 0, attr);
6794 if (selstart != NULL && highlight)
6795 {
6796 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006797 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 }
6799
6800 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6801 }
6802
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 vim_free(buf);
6805}
6806#endif
6807
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808/*
6809 * Redraw the status line of window wp.
6810 *
6811 * If inversion is possible we use it. Else '=' characters are used.
6812 */
6813 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006814win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
6816 int row;
6817 char_u *p;
6818 int len;
6819 int fillchar;
6820 int attr;
6821 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006822 static int busy = FALSE;
6823
6824 /* It's possible to get here recursively when 'statusline' (indirectly)
6825 * invokes ":redrawstatus". Simply ignore the call then. */
6826 if (busy)
6827 return;
6828 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829
6830 wp->w_redr_status = FALSE;
6831 if (wp->w_status_height == 0)
6832 {
6833 /* no status line, can only be last window */
6834 redraw_cmdline = TRUE;
6835 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006836 else if (!redrawing()
6837#ifdef FEAT_INS_EXPAND
6838 /* don't update status line when popup menu is visible and may be
6839 * drawn over it */
6840 || pum_visible()
6841#endif
6842 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 {
6844 /* Don't redraw right now, do it later. */
6845 wp->w_redr_status = TRUE;
6846 }
6847#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006848 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {
6850 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006851 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 }
6853#endif
6854 else
6855 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006856 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006858 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 p = NameBuff;
6860 len = (int)STRLEN(p);
6861
Bram Moolenaard85f2712017-07-28 21:51:57 +02006862 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863#ifdef FEAT_QUICKFIX
6864 || wp->w_p_pvw
6865#endif
6866 || bufIsChanged(wp->w_buffer)
6867 || wp->w_buffer->b_p_ro)
6868 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006869 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006871 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 len += (int)STRLEN(p + len);
6873 }
6874#ifdef FEAT_QUICKFIX
6875 if (wp->w_p_pvw)
6876 {
6877 STRCPY(p + len, _("[Preview]"));
6878 len += (int)STRLEN(p + len);
6879 }
6880#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006881 if (bufIsChanged(wp->w_buffer)
6882#ifdef FEAT_TERMINAL
6883 && !bt_terminal(wp->w_buffer)
6884#endif
6885 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
6887 STRCPY(p + len, "[+]");
6888 len += 3;
6889 }
6890 if (wp->w_buffer->b_p_ro)
6891 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006892 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006893 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 }
6895
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6897 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6898 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6899 if (this_ru_col <= 1)
6900 {
6901 p = (char_u *)"<"; /* No room for file name! */
6902 len = 1;
6903 }
6904 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905#ifdef FEAT_MBYTE
6906 if (has_mbyte)
6907 {
6908 int clen = 0, i;
6909
6910 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006911 clen = mb_string2cells(p, -1);
6912
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 /* Find first character that will fit.
6914 * Going from start to end is much faster for DBCS. */
6915 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006916 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 clen -= (*mb_ptr2cells)(p + i);
6918 len = clen;
6919 if (i > 0)
6920 {
6921 p = p + i - 1;
6922 *p = '<';
6923 ++len;
6924 }
6925
6926 }
6927 else
6928#endif
6929 if (len > this_ru_col - 1)
6930 {
6931 p += len - (this_ru_col - 1);
6932 *p = '<';
6933 len = this_ru_col - 1;
6934 }
6935
6936 row = W_WINROW(wp) + wp->w_height;
6937 screen_puts(p, row, W_WINCOL(wp), attr);
6938 screen_fill(row, row + 1, len + W_WINCOL(wp),
6939 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6940
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006941 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6943 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6944 - 1 + W_WINCOL(wp)), attr);
6945
6946#ifdef FEAT_CMDL_INFO
6947 win_redr_ruler(wp, TRUE);
6948#endif
6949 }
6950
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 /*
6952 * May need to draw the character below the vertical separator.
6953 */
6954 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6955 {
6956 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006957 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 else
6959 fillchar = fillchar_vsep(&attr);
6960 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6961 attr);
6962 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006963 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964}
6965
Bram Moolenaar238a5642006-02-21 22:12:05 +00006966#ifdef FEAT_STL_OPT
6967/*
6968 * Redraw the status line according to 'statusline' and take care of any
6969 * errors encountered.
6970 */
6971 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006972redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006973{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006974 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006975 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006976
6977 /* When called recursively return. This can happen when the statusline
6978 * contains an expression that triggers a redraw. */
6979 if (entered)
6980 return;
6981 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006982
Bram Moolenaara742e082016-04-05 21:10:38 +02006983 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006984 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006985 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006986 {
6987 /* When there is an error disable the statusline, otherwise the
6988 * display is messed up with errors and a redraw triggers the problem
6989 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006990 set_string_option_direct((char_u *)"statusline", -1,
6991 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006992 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006993 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006994 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006995 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006996}
6997#endif
6998
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999/*
7000 * Return TRUE if the status line of window "wp" is connected to the status
7001 * line of the window right of it. If not, then it's a vertical separator.
7002 * Only call if (wp->w_vsep_width != 0).
7003 */
7004 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007005stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007 frame_T *fr;
7008
7009 fr = wp->w_frame;
7010 while (fr->fr_parent != NULL)
7011 {
7012 if (fr->fr_parent->fr_layout == FR_COL)
7013 {
7014 if (fr->fr_next != NULL)
7015 break;
7016 }
7017 else
7018 {
7019 if (fr->fr_next != NULL)
7020 return TRUE;
7021 }
7022 fr = fr->fr_parent;
7023 }
7024 return FALSE;
7025}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028/*
7029 * Get the value to show for the language mappings, active 'keymap'.
7030 */
7031 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007032get_keymap_str(
7033 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007034 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007035 char_u *buf, /* buffer for the result */
7036 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037{
7038 char_u *p;
7039
7040 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7041 return FALSE;
7042
7043 {
7044#ifdef FEAT_EVAL
7045 buf_T *old_curbuf = curbuf;
7046 win_T *old_curwin = curwin;
7047 char_u *s;
7048
7049 curbuf = wp->w_buffer;
7050 curwin = wp;
7051 STRCPY(buf, "b:keymap_name"); /* must be writable */
7052 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007053 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 --emsg_skip;
7055 curbuf = old_curbuf;
7056 curwin = old_curwin;
7057 if (p == NULL || *p == NUL)
7058#endif
7059 {
7060#ifdef FEAT_KEYMAP
7061 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7062 p = wp->w_buffer->b_p_keymap;
7063 else
7064#endif
7065 p = (char_u *)"lang";
7066 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007067 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 buf[0] = NUL;
7069#ifdef FEAT_EVAL
7070 vim_free(s);
7071#endif
7072 }
7073 return buf[0] != NUL;
7074}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
7076#if defined(FEAT_STL_OPT) || defined(PROTO)
7077/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007078 * Redraw the status line or ruler of window "wp".
7079 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 */
7081 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007082win_redr_custom(
7083 win_T *wp,
7084 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007086 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 int attr;
7088 int curattr;
7089 int row;
7090 int col = 0;
7091 int maxwidth;
7092 int width;
7093 int n;
7094 int len;
7095 int fillchar;
7096 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007097 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007099 struct stl_hlrec hltab[STL_MAX_ITEM];
7100 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007101 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007102 win_T *ewp;
7103 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
Bram Moolenaar1d633412013-12-11 15:52:01 +01007105 /* There is a tiny chance that this gets called recursively: When
7106 * redrawing a status line triggers redrawing the ruler or tabline.
7107 * Avoid trouble by not allowing recursion. */
7108 if (entered)
7109 return;
7110 entered = TRUE;
7111
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007113 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007115 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007116 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007117 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007118 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007119 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007120 maxwidth = Columns;
7121# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007122 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007125 else
7126 {
7127 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007128 fillchar = fillchar_status(&attr, wp);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007129 maxwidth = W_WIDTH(wp);
7130
7131 if (draw_ruler)
7132 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007133 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007134 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007135 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007136 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007137 if (*++stl == '-')
7138 stl++;
7139 if (atoi((char *)stl))
7140 while (VIM_ISDIGIT(*stl))
7141 stl++;
7142 if (*stl++ != '(')
7143 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007144 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007145 col = ru_col - (Columns - W_WIDTH(wp));
7146 if (col < (W_WIDTH(wp) + 1) / 2)
7147 col = (W_WIDTH(wp) + 1) / 2;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007148 maxwidth = W_WIDTH(wp) - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007149 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007150 {
7151 row = Rows - 1;
7152 --maxwidth; /* writing in last column may cause scrolling */
7153 fillchar = ' ';
7154 attr = 0;
7155 }
7156
7157# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007158 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007159# endif
7160 }
7161 else
7162 {
7163 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007164 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007165 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007166 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007167# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007168 use_sandbox = was_set_insecurely((char_u *)"statusline",
7169 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007170# endif
7171 }
7172
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007173 col += W_WINCOL(wp);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007174 }
7175
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007177 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
Bram Moolenaar61452852011-02-01 18:01:11 +01007179 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7180 * the cursor away and back. */
7181 ewp = wp == NULL ? curwin : wp;
7182 p_crb_save = ewp->w_p_crb;
7183 ewp->w_p_crb = FALSE;
7184
Bram Moolenaar362f3562009-11-03 16:20:34 +00007185 /* Make a copy, because the statusline may include a function call that
7186 * might change the option value and free the memory. */
7187 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007188 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007189 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007190 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007191 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007192 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007194 /* Make all characters printable. */
7195 p = transstr(buf);
7196 if (p != NULL)
7197 {
7198 vim_strncpy(buf, p, sizeof(buf) - 1);
7199 vim_free(p);
7200 }
7201
7202 /* fill up with "fillchar" */
7203 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007204 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 {
7206#ifdef FEAT_MBYTE
7207 len += (*mb_char2bytes)(fillchar, buf + len);
7208#else
7209 buf[len++] = fillchar;
7210#endif
7211 ++width;
7212 }
7213 buf[len] = NUL;
7214
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007215 /*
7216 * Draw each snippet with the specified highlighting.
7217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 curattr = attr;
7219 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007220 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007222 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 screen_puts_len(p, len, row, col, curattr);
7224 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007225 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007227 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007229 else if (hltab[n].userhl < 0)
7230 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007231#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007232 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7233 && wp->w_status_height != 0)
7234 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007235 else if (wp != NULL && bt_terminal(wp->w_buffer)
7236 && wp->w_status_height != 0)
7237 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007238#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007239 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007240 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007242 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 }
7244 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007245
7246 if (wp == NULL)
7247 {
7248 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7249 col = 0;
7250 len = 0;
7251 p = buf;
7252 fillchar = 0;
7253 for (n = 0; tabtab[n].start != NULL; n++)
7254 {
7255 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7256 while (col < len)
7257 TabPageIdxs[col++] = fillchar;
7258 p = tabtab[n].start;
7259 fillchar = tabtab[n].userhl;
7260 }
7261 while (col < Columns)
7262 TabPageIdxs[col++] = fillchar;
7263 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007264
7265theend:
7266 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267}
7268
7269#endif /* FEAT_STL_OPT */
7270
7271/*
7272 * Output a single character directly to the screen and update ScreenLines.
7273 */
7274 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007275screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 char_u buf[MB_MAXBYTES + 1];
7278
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007279#ifdef FEAT_MBYTE
7280 if (has_mbyte)
7281 buf[(*mb_char2bytes)(c, buf)] = NUL;
7282 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007284 {
7285 buf[0] = c;
7286 buf[1] = NUL;
7287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 screen_puts(buf, row, col, attr);
7289}
7290
7291/*
7292 * Get a single character directly from ScreenLines into "bytes[]".
7293 * Also return its attribute in *attrp;
7294 */
7295 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007296screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297{
7298 unsigned off;
7299
7300 /* safety check */
7301 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7302 {
7303 off = LineOffset[row] + col;
7304 *attrp = ScreenAttrs[off];
7305 bytes[0] = ScreenLines[off];
7306 bytes[1] = NUL;
7307
7308#ifdef FEAT_MBYTE
7309 if (enc_utf8 && ScreenLinesUC[off] != 0)
7310 bytes[utfc_char2bytes(off, bytes)] = NUL;
7311 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7312 {
7313 bytes[0] = ScreenLines[off];
7314 bytes[1] = ScreenLines2[off];
7315 bytes[2] = NUL;
7316 }
7317 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7318 {
7319 bytes[1] = ScreenLines[off + 1];
7320 bytes[2] = NUL;
7321 }
7322#endif
7323 }
7324}
7325
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007326#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007327static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007328
7329/*
7330 * Return TRUE if composing characters for screen posn "off" differs from
7331 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007332 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007333 */
7334 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007335screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007336{
7337 int i;
7338
7339 for (i = 0; i < Screen_mco; ++i)
7340 {
7341 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7342 return TRUE;
7343 if (u8cc[i] == 0)
7344 break;
7345 }
7346 return FALSE;
7347}
7348#endif
7349
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350/*
7351 * Put string '*text' on the screen at position 'row' and 'col', with
7352 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7353 * Note: only outputs within one row, message is truncated at screen boundary!
7354 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7355 */
7356 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007357screen_puts(
7358 char_u *text,
7359 int row,
7360 int col,
7361 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362{
7363 screen_puts_len(text, -1, row, col, attr);
7364}
7365
7366/*
7367 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7368 * a NUL.
7369 */
7370 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007371screen_puts_len(
7372 char_u *text,
7373 int textlen,
7374 int row,
7375 int col,
7376 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
7378 unsigned off;
7379 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007380 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 int c;
7382#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007383 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 int mbyte_blen = 1;
7385 int mbyte_cells = 1;
7386 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007387 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 int clear_next_cell = FALSE;
7389# ifdef FEAT_ARABIC
7390 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007391 int pc, nc, nc1;
7392 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393# endif
7394#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007395#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7396 int force_redraw_this;
7397 int force_redraw_next = FALSE;
7398#endif
7399 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
7401 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7402 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007403 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404
Bram Moolenaarc236c162008-07-13 17:41:49 +00007405#ifdef FEAT_MBYTE
7406 /* When drawing over the right halve of a double-wide char clear out the
7407 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007408 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007409# ifdef FEAT_GUI
7410 && !gui.in_use
7411# endif
7412 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007413 {
7414 ScreenLines[off - 1] = ' ';
7415 ScreenAttrs[off - 1] = 0;
7416 if (enc_utf8)
7417 {
7418 ScreenLinesUC[off - 1] = 0;
7419 ScreenLinesC[0][off - 1] = 0;
7420 }
7421 /* redraw the previous cell, make it empty */
7422 screen_char(off - 1, row, col - 1);
7423 /* force the cell at "col" to be redrawn */
7424 force_redraw_next = TRUE;
7425 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007426#endif
7427
Bram Moolenaar367329b2007-08-30 11:53:22 +00007428#ifdef FEAT_MBYTE
7429 max_off = LineOffset[row] + screen_Columns;
7430#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007431 while (col < screen_Columns
7432 && (len < 0 || (int)(ptr - text) < len)
7433 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 {
7435 c = *ptr;
7436#ifdef FEAT_MBYTE
7437 /* check if this is the first byte of a multibyte */
7438 if (has_mbyte)
7439 {
7440 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007441 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007443 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7445 mbyte_cells = 1;
7446 else if (enc_dbcs != 0)
7447 mbyte_cells = mbyte_blen;
7448 else /* enc_utf8 */
7449 {
7450 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007451 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 (int)((text + len) - ptr));
7453 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007454 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007456# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 /* Non-BMP character: display as ? or fullwidth ?. */
7458 if (u8c >= 0x10000)
7459 {
7460 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7461 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007462 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007464# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465# ifdef FEAT_ARABIC
7466 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7467 {
7468 /* Do Arabic shaping. */
7469 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7470 {
7471 /* Past end of string to be displayed. */
7472 nc = NUL;
7473 nc1 = NUL;
7474 }
7475 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007476 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007477 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7478 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007479 nc1 = pcc[0];
7480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 pc = prev_c;
7482 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007483 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 }
7485 else
7486 prev_c = u8c;
7487# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007488 if (col + mbyte_cells > screen_Columns)
7489 {
7490 /* Only 1 cell left, but character requires 2 cells:
7491 * display a '>' in the last column to avoid wrapping. */
7492 c = '>';
7493 mbyte_cells = 1;
7494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 }
7496 }
7497#endif
7498
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007499#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7500 force_redraw_this = force_redraw_next;
7501 force_redraw_next = FALSE;
7502#endif
7503
7504 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505#ifdef FEAT_MBYTE
7506 || (mbyte_cells == 2
7507 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7508 || (enc_dbcs == DBCS_JPNU
7509 && c == 0x8e
7510 && ScreenLines2[off] != ptr[1])
7511 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007512 && (ScreenLinesUC[off] !=
7513 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7514 || (ScreenLinesUC[off] != 0
7515 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516#endif
7517 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007518 || exmode_active;
7519
7520 if (need_redraw
7521#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7522 || force_redraw_this
7523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 )
7525 {
7526#if defined(FEAT_GUI) || defined(UNIX)
7527 /* The bold trick makes a single row of pixels appear in the next
7528 * character. When a bold character is removed, the next
7529 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007530 * and for some xterms. */
7531 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532# ifdef FEAT_GUI
7533 gui.in_use
7534# endif
7535# if defined(FEAT_GUI) && defined(UNIX)
7536 ||
7537# endif
7538# ifdef UNIX
7539 term_is_xterm
7540# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007541 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007543 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007545 if (n > HL_ALL)
7546 n = syn_attr2attr(n);
7547 if (n & HL_BOLD)
7548 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 }
7550#endif
7551#ifdef FEAT_MBYTE
7552 /* When at the end of the text and overwriting a two-cell
7553 * character with a one-cell character, need to clear the next
7554 * cell. Also when overwriting the left halve of a two-cell char
7555 * with the right halve of a two-cell char. Do this only once
7556 * (mb_off2cells() may return 2 on the right halve). */
7557 if (clear_next_cell)
7558 clear_next_cell = FALSE;
7559 else if (has_mbyte
7560 && (len < 0 ? ptr[mbyte_blen] == NUL
7561 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007562 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007564 && (*mb_off2cells)(off, max_off) == 1
7565 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 clear_next_cell = TRUE;
7567
7568 /* Make sure we never leave a second byte of a double-byte behind,
7569 * it confuses mb_off2cells(). */
7570 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007571 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007573 && (*mb_off2cells)(off, max_off) == 1
7574 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 ScreenLines[off + mbyte_blen] = 0;
7576#endif
7577 ScreenLines[off] = c;
7578 ScreenAttrs[off] = attr;
7579#ifdef FEAT_MBYTE
7580 if (enc_utf8)
7581 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007582 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 ScreenLinesUC[off] = 0;
7584 else
7585 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007586 int i;
7587
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007589 for (i = 0; i < Screen_mco; ++i)
7590 {
7591 ScreenLinesC[i][off] = u8cc[i];
7592 if (u8cc[i] == 0)
7593 break;
7594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596 if (mbyte_cells == 2)
7597 {
7598 ScreenLines[off + 1] = 0;
7599 ScreenAttrs[off + 1] = attr;
7600 }
7601 screen_char(off, row, col);
7602 }
7603 else if (mbyte_cells == 2)
7604 {
7605 ScreenLines[off + 1] = ptr[1];
7606 ScreenAttrs[off + 1] = attr;
7607 screen_char_2(off, row, col);
7608 }
7609 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7610 {
7611 ScreenLines2[off] = ptr[1];
7612 screen_char(off, row, col);
7613 }
7614 else
7615#endif
7616 screen_char(off, row, col);
7617 }
7618#ifdef FEAT_MBYTE
7619 if (has_mbyte)
7620 {
7621 off += mbyte_cells;
7622 col += mbyte_cells;
7623 ptr += mbyte_blen;
7624 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007625 {
7626 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007628 len = -1;
7629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 }
7631 else
7632#endif
7633 {
7634 ++off;
7635 ++col;
7636 ++ptr;
7637 }
7638 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007639
7640#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7641 /* If we detected the next character needs to be redrawn, but the text
7642 * doesn't extend up to there, update the character here. */
7643 if (force_redraw_next && col < screen_Columns)
7644 {
7645# ifdef FEAT_MBYTE
7646 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7647 screen_char_2(off, row, col);
7648 else
7649# endif
7650 screen_char(off, row, col);
7651 }
7652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653}
7654
7655#ifdef FEAT_SEARCH_EXTRA
7656/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007657 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 */
7659 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007660start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661{
7662 if (p_hls && !no_hlsearch)
7663 {
7664 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007665 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007666# ifdef FEAT_RELTIME
7667 /* Set the time limit to 'redrawtime'. */
7668 profile_setlimit(p_rdt, &search_hl.tm);
7669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 }
7671}
7672
7673/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007674 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 */
7676 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007677end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679 if (search_hl.rm.regprog != NULL)
7680 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007681 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 search_hl.rm.regprog = NULL;
7683 }
7684}
7685
7686/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007687 * Init for calling prepare_search_hl().
7688 */
7689 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007690init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007691{
7692 matchitem_T *cur;
7693
7694 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7695 * match */
7696 cur = wp->w_match_head;
7697 while (cur != NULL)
7698 {
7699 cur->hl.rm = cur->match;
7700 if (cur->hlg_id == 0)
7701 cur->hl.attr = 0;
7702 else
7703 cur->hl.attr = syn_id2attr(cur->hlg_id);
7704 cur->hl.buf = wp->w_buffer;
7705 cur->hl.lnum = 0;
7706 cur->hl.first_lnum = 0;
7707# ifdef FEAT_RELTIME
7708 /* Set the time limit to 'redrawtime'. */
7709 profile_setlimit(p_rdt, &(cur->hl.tm));
7710# endif
7711 cur = cur->next;
7712 }
7713 search_hl.buf = wp->w_buffer;
7714 search_hl.lnum = 0;
7715 search_hl.first_lnum = 0;
7716 /* time limit is set at the toplevel, for all windows */
7717}
7718
7719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 * Advance to the match in window "wp" line "lnum" or past it.
7721 */
7722 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007723prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007725 matchitem_T *cur; /* points to the match list */
7726 match_T *shl; /* points to search_hl or a match */
7727 int shl_flag; /* flag to indicate whether search_hl
7728 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007729 int pos_inprogress; /* marks that position match search is
7730 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 int n;
7732
7733 /*
7734 * When using a multi-line pattern, start searching at the top
7735 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007736 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007738 cur = wp->w_match_head;
7739 shl_flag = FALSE;
7740 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007742 if (shl_flag == FALSE)
7743 {
7744 shl = &search_hl;
7745 shl_flag = TRUE;
7746 }
7747 else
7748 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 if (shl->rm.regprog != NULL
7750 && shl->lnum == 0
7751 && re_multiline(shl->rm.regprog))
7752 {
7753 if (shl->first_lnum == 0)
7754 {
7755# ifdef FEAT_FOLDING
7756 for (shl->first_lnum = lnum;
7757 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7758 if (hasFoldingWin(wp, shl->first_lnum - 1,
7759 NULL, NULL, TRUE, NULL))
7760 break;
7761# else
7762 shl->first_lnum = wp->w_topline;
7763# endif
7764 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007765 if (cur != NULL)
7766 cur->pos.cur = 0;
7767 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007769 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7770 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007772 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7773 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007774 pos_inprogress = cur == NULL || cur->pos.cur == 0
7775 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 if (shl->lnum != 0)
7777 {
7778 shl->first_lnum = shl->lnum
7779 + shl->rm.endpos[0].lnum
7780 - shl->rm.startpos[0].lnum;
7781 n = shl->rm.endpos[0].col;
7782 }
7783 else
7784 {
7785 ++shl->first_lnum;
7786 n = 0;
7787 }
7788 }
7789 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007790 if (shl != &search_hl && cur != NULL)
7791 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
7793}
7794
7795/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007796 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 * Uses shl->buf.
7798 * Sets shl->lnum and shl->rm contents.
7799 * Note: Assumes a previous match is always before "lnum", unless
7800 * shl->lnum is zero.
7801 * Careful: Any pointers for buffer lines will become invalid.
7802 */
7803 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007804next_search_hl(
7805 win_T *win,
7806 match_T *shl, /* points to search_hl or a match */
7807 linenr_T lnum,
7808 colnr_T mincol, /* minimal column for a match */
7809 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811 linenr_T l;
7812 colnr_T matchcol;
7813 long nmatched;
7814
7815 if (shl->lnum != 0)
7816 {
7817 /* Check for three situations:
7818 * 1. If the "lnum" is below a previous match, start a new search.
7819 * 2. If the previous match includes "mincol", use it.
7820 * 3. Continue after the previous match.
7821 */
7822 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7823 if (lnum > l)
7824 shl->lnum = 0;
7825 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7826 return;
7827 }
7828
7829 /*
7830 * Repeat searching for a match until one is found that includes "mincol"
7831 * or none is found in this line.
7832 */
7833 called_emsg = FALSE;
7834 for (;;)
7835 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007836#ifdef FEAT_RELTIME
7837 /* Stop searching after passing the time limit. */
7838 if (profile_passed_limit(&(shl->tm)))
7839 {
7840 shl->lnum = 0; /* no match found in time */
7841 break;
7842 }
7843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 /* Three situations:
7845 * 1. No useful previous match: search from start of line.
7846 * 2. Not Vi compatible or empty match: continue at next character.
7847 * Break the loop if this is beyond the end of the line.
7848 * 3. Vi compatible searching: continue at end of previous match.
7849 */
7850 if (shl->lnum == 0)
7851 matchcol = 0;
7852 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7853 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007854 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007856 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007857
7858 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007859 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007860 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007862 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 shl->lnum = 0;
7864 break;
7865 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007866#ifdef FEAT_MBYTE
7867 if (has_mbyte)
7868 matchcol += mb_ptr2len(ml);
7869 else
7870#endif
7871 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 }
7873 else
7874 matchcol = shl->rm.endpos[0].col;
7875
7876 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007877 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007879 /* Remember whether shl->rm is using a copy of the regprog in
7880 * cur->match. */
7881 int regprog_is_copy = (shl != &search_hl && cur != NULL
7882 && shl == &cur->hl
7883 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007884 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007885
Bram Moolenaarb3414592014-06-17 17:48:32 +02007886 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7887 matchcol,
7888#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007889 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007890#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007891 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007892#endif
7893 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007894 /* Copy the regprog, in case it got freed and recompiled. */
7895 if (regprog_is_copy)
7896 cur->match.regprog = cur->hl.rm.regprog;
7897
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007898 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007899 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007900 /* Error while handling regexp: stop using this regexp. */
7901 if (shl == &search_hl)
7902 {
7903 /* don't free regprog in the match list, it's a copy */
7904 vim_regfree(shl->rm.regprog);
7905 SET_NO_HLSEARCH(TRUE);
7906 }
7907 shl->rm.regprog = NULL;
7908 shl->lnum = 0;
7909 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7910 message */
7911 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007912 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007913 }
7914 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007915 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007916 else
7917 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 if (nmatched == 0)
7919 {
7920 shl->lnum = 0; /* no match found */
7921 break;
7922 }
7923 if (shl->rm.startpos[0].lnum > 0
7924 || shl->rm.startpos[0].col >= mincol
7925 || nmatched > 1
7926 || shl->rm.endpos[0].col > mincol)
7927 {
7928 shl->lnum += shl->rm.startpos[0].lnum;
7929 break; /* useful match found */
7930 }
7931 }
7932}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933
Bram Moolenaar85077472016-10-16 14:35:48 +02007934/*
7935 * If there is a match fill "shl" and return one.
7936 * Return zero otherwise.
7937 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007938 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007939next_search_hl_pos(
7940 match_T *shl, /* points to a match */
7941 linenr_T lnum,
7942 posmatch_T *posmatch, /* match positions */
7943 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007944{
7945 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007946 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007947
Bram Moolenaarb3414592014-06-17 17:48:32 +02007948 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7949 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007950 llpos_T *pos = &posmatch->pos[i];
7951
7952 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007953 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007954 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007955 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007956 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007957 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007958 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007959 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007960 /* if this match comes before the one at "found" then swap
7961 * them */
7962 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007963 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007964 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007965
Bram Moolenaar85077472016-10-16 14:35:48 +02007966 *pos = posmatch->pos[found];
7967 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007968 }
7969 }
7970 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007971 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007972 }
7973 }
7974 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007975 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007976 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007977 colnr_T start = posmatch->pos[found].col == 0
7978 ? 0 : posmatch->pos[found].col - 1;
7979 colnr_T end = posmatch->pos[found].col == 0
7980 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981
Bram Moolenaar85077472016-10-16 14:35:48 +02007982 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007983 shl->rm.startpos[0].lnum = 0;
7984 shl->rm.startpos[0].col = start;
7985 shl->rm.endpos[0].lnum = 0;
7986 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007987 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007988 posmatch->cur = found + 1;
7989 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007991 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007992}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007993#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007994
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007996screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997{
7998 attrentry_T *aep = NULL;
7999
8000 screen_attr = attr;
8001 if (full_screen
8002#ifdef WIN3264
8003 && termcap_active
8004#endif
8005 )
8006 {
8007#ifdef FEAT_GUI
8008 if (gui.in_use)
8009 {
8010 char buf[20];
8011
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008012 /* The GUI handles this internally. */
8013 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 OUT_STR(buf);
8015 }
8016 else
8017#endif
8018 {
8019 if (attr > HL_ALL) /* special HL attr. */
8020 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008021 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 aep = syn_cterm_attr2entry(attr);
8023 else
8024 aep = syn_term_attr2entry(attr);
8025 if (aep == NULL) /* did ":syntax clear" */
8026 attr = 0;
8027 else
8028 attr = aep->ae_attr;
8029 }
8030 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8031 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008032 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008033#ifdef FEAT_TERMGUICOLORS
8034 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008035 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008036#endif
8037 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008038#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008039 )
8040#endif
8041 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008042 /* If the Normal FG color has BOLD attribute and the new HL
8043 * has a FG color defined, clear BOLD. */
8044 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8046 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008047 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8048 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 out_str(T_US);
8050 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8051 out_str(T_CZH);
8052 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8053 out_str(T_MR);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008054 if ((attr & HL_STRIKETHROUGH) && T_STS != NULL) /* strike */
8055 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056
8057 /*
8058 * Output the color or start string after bold etc., in case the
8059 * bold etc. override the color setting.
8060 */
8061 if (aep != NULL)
8062 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008063#ifdef FEAT_TERMGUICOLORS
8064 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008066 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008067 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008068 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008069 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 }
8071 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008074 if (t_colors > 1)
8075 {
8076 if (aep->ae_u.cterm.fg_color)
8077 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8078 if (aep->ae_u.cterm.bg_color)
8079 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8080 }
8081 else
8082 {
8083 if (aep->ae_u.term.start != NULL)
8084 out_str(aep->ae_u.term.start);
8085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 }
8087 }
8088 }
8089 }
8090}
8091
8092 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008093screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094{
8095 int do_ME = FALSE; /* output T_ME code */
8096
8097 if (screen_attr != 0
8098#ifdef WIN3264
8099 && termcap_active
8100#endif
8101 )
8102 {
8103#ifdef FEAT_GUI
8104 if (gui.in_use)
8105 {
8106 char buf[20];
8107
8108 /* use internal GUI code */
8109 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8110 OUT_STR(buf);
8111 }
8112 else
8113#endif
8114 {
8115 if (screen_attr > HL_ALL) /* special HL attr. */
8116 {
8117 attrentry_T *aep;
8118
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008119 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {
8121 /*
8122 * Assume that t_me restores the original colors!
8123 */
8124 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008125 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008126#ifdef FEAT_TERMGUICOLORS
8127 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008128 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8129 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008130#endif
8131 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008132#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008133 )
8134#endif
8135 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 do_ME = TRUE;
8137 }
8138 else
8139 {
8140 aep = syn_term_attr2entry(screen_attr);
8141 if (aep != NULL && aep->ae_u.term.stop != NULL)
8142 {
8143 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8144 do_ME = TRUE;
8145 else
8146 out_str(aep->ae_u.term.stop);
8147 }
8148 }
8149 if (aep == NULL) /* did ":syntax clear" */
8150 screen_attr = 0;
8151 else
8152 screen_attr = aep->ae_attr;
8153 }
8154
8155 /*
8156 * Often all ending-codes are equal to T_ME. Avoid outputting the
8157 * same sequence several times.
8158 */
8159 if (screen_attr & HL_STANDOUT)
8160 {
8161 if (STRCMP(T_SE, T_ME) == 0)
8162 do_ME = TRUE;
8163 else
8164 out_str(T_SE);
8165 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008166 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {
8168 if (STRCMP(T_UE, T_ME) == 0)
8169 do_ME = TRUE;
8170 else
8171 out_str(T_UE);
8172 }
8173 if (screen_attr & HL_ITALIC)
8174 {
8175 if (STRCMP(T_CZR, T_ME) == 0)
8176 do_ME = TRUE;
8177 else
8178 out_str(T_CZR);
8179 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008180 if (screen_attr & HL_STRIKETHROUGH)
8181 {
8182 if (STRCMP(T_STE, T_ME) == 0)
8183 do_ME = TRUE;
8184 else
8185 out_str(T_STE);
8186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8188 out_str(T_ME);
8189
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008190#ifdef FEAT_TERMGUICOLORS
8191 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008193 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008194 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008195 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008196 term_bg_rgb_color(cterm_normal_bg_gui_color);
8197 }
8198 else
8199#endif
8200 {
8201 if (t_colors > 1)
8202 {
8203 /* set Normal cterm colors */
8204 if (cterm_normal_fg_color != 0)
8205 term_fg_color(cterm_normal_fg_color - 1);
8206 if (cterm_normal_bg_color != 0)
8207 term_bg_color(cterm_normal_bg_color - 1);
8208 if (cterm_normal_fg_bold)
8209 out_str(T_MD);
8210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 }
8212 }
8213 }
8214 screen_attr = 0;
8215}
8216
8217/*
8218 * Reset the colors for a cterm. Used when leaving Vim.
8219 * The machine specific code may override this again.
8220 */
8221 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008222reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008224 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {
8226 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008227#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008228 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8229 || cterm_normal_bg_gui_color != INVALCOLOR)
8230 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008231#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {
8235 out_str(T_OP);
8236 screen_attr = -1;
8237 }
8238 if (cterm_normal_fg_bold)
8239 {
8240 out_str(T_ME);
8241 screen_attr = -1;
8242 }
8243 }
8244}
8245
8246/*
8247 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8248 * using the attributes from ScreenAttrs["off"].
8249 */
8250 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008251screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252{
8253 int attr;
8254
8255 /* Check for illegal values, just in case (could happen just after
8256 * resizing). */
8257 if (row >= screen_Rows || col >= screen_Columns)
8258 return;
8259
Bram Moolenaar494838a2015-02-10 19:20:37 +01008260 /* Outputting a character in the last cell on the screen may scroll the
8261 * screen up. Only do it when the "xn" termcap property is set, otherwise
8262 * mark the character invalid (update it when scrolled up). */
8263 if (*T_XN == NUL
8264 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265#ifdef FEAT_RIGHTLEFT
8266 /* account for first command-line character in rightleft mode */
8267 && !cmdmsg_rl
8268#endif
8269 )
8270 {
8271 ScreenAttrs[off] = (sattr_T)-1;
8272 return;
8273 }
8274
8275 /*
8276 * Stop highlighting first, so it's easier to move the cursor.
8277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 if (screen_char_attr != 0)
8279 attr = screen_char_attr;
8280 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 attr = ScreenAttrs[off];
8282 if (screen_attr != attr)
8283 screen_stop_highlight();
8284
8285 windgoto(row, col);
8286
8287 if (screen_attr != attr)
8288 screen_start_highlight(attr);
8289
8290#ifdef FEAT_MBYTE
8291 if (enc_utf8 && ScreenLinesUC[off] != 0)
8292 {
8293 char_u buf[MB_MAXBYTES + 1];
8294
8295 /* Convert UTF-8 character to bytes and write it. */
8296
8297 buf[utfc_char2bytes(off, buf)] = NUL;
8298
8299 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008300 if (utf_ambiguous_width(ScreenLinesUC[off]))
8301 screen_cur_col = 9999;
8302 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 ++screen_cur_col;
8304 }
8305 else
8306#endif
8307 {
8308#ifdef FEAT_MBYTE
8309 out_flush_check();
8310#endif
8311 out_char(ScreenLines[off]);
8312#ifdef FEAT_MBYTE
8313 /* double-byte character in single-width cell */
8314 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8315 out_char(ScreenLines2[off]);
8316#endif
8317 }
8318
8319 screen_cur_col++;
8320}
8321
8322#ifdef FEAT_MBYTE
8323
8324/*
8325 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8326 * on the screen at position 'row' and 'col'.
8327 * The attributes of the first byte is used for all. This is required to
8328 * output the two bytes of a double-byte character with nothing in between.
8329 */
8330 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008331screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332{
8333 /* Check for illegal values (could be wrong when screen was resized). */
8334 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8335 return;
8336
8337 /* Outputting the last character on the screen may scrollup the screen.
8338 * Don't to it! Mark the character invalid (update it when scrolled up) */
8339 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8340 {
8341 ScreenAttrs[off] = (sattr_T)-1;
8342 return;
8343 }
8344
8345 /* Output the first byte normally (positions the cursor), then write the
8346 * second byte directly. */
8347 screen_char(off, row, col);
8348 out_char(ScreenLines[off + 1]);
8349 ++screen_cur_col;
8350}
8351#endif
8352
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353/*
8354 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8355 * This uses the contents of ScreenLines[] and doesn't change it.
8356 */
8357 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008358screen_draw_rectangle(
8359 int row,
8360 int col,
8361 int height,
8362 int width,
8363 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364{
8365 int r, c;
8366 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008367#ifdef FEAT_MBYTE
8368 int max_off;
8369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008371 /* Can't use ScreenLines unless initialized */
8372 if (ScreenLines == NULL)
8373 return;
8374
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 if (invert)
8376 screen_char_attr = HL_INVERSE;
8377 for (r = row; r < row + height; ++r)
8378 {
8379 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008380#ifdef FEAT_MBYTE
8381 max_off = off + screen_Columns;
8382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 for (c = col; c < col + width; ++c)
8384 {
8385#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008386 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {
8388 screen_char_2(off + c, r, c);
8389 ++c;
8390 }
8391 else
8392#endif
8393 {
8394 screen_char(off + c, r, c);
8395#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008396 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 ++c;
8398#endif
8399 }
8400 }
8401 }
8402 screen_char_attr = 0;
8403}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405/*
8406 * Redraw the characters for a vertically split window.
8407 */
8408 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008409redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410{
8411 int col;
8412 int width;
8413
8414# ifdef FEAT_CLIPBOARD
8415 clip_may_clear_selection(row, end - 1);
8416# endif
8417
8418 if (wp == NULL)
8419 {
8420 col = 0;
8421 width = Columns;
8422 }
8423 else
8424 {
8425 col = wp->w_wincol;
8426 width = wp->w_width;
8427 }
8428 screen_draw_rectangle(row, col, end - row, width, FALSE);
8429}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008431 static void
8432space_to_screenline(int off, int attr)
8433{
8434 ScreenLines[off] = ' ';
8435 ScreenAttrs[off] = attr;
8436# ifdef FEAT_MBYTE
8437 if (enc_utf8)
8438 ScreenLinesUC[off] = 0;
8439# endif
8440}
8441
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442/*
8443 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8444 * with character 'c1' in first column followed by 'c2' in the other columns.
8445 * Use attributes 'attr'.
8446 */
8447 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008448screen_fill(
8449 int start_row,
8450 int end_row,
8451 int start_col,
8452 int end_col,
8453 int c1,
8454 int c2,
8455 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456{
8457 int row;
8458 int col;
8459 int off;
8460 int end_off;
8461 int did_delete;
8462 int c;
8463 int norm_term;
8464#if defined(FEAT_GUI) || defined(UNIX)
8465 int force_next = FALSE;
8466#endif
8467
8468 if (end_row > screen_Rows) /* safety check */
8469 end_row = screen_Rows;
8470 if (end_col > screen_Columns) /* safety check */
8471 end_col = screen_Columns;
8472 if (ScreenLines == NULL
8473 || start_row >= end_row
8474 || start_col >= end_col) /* nothing to do */
8475 return;
8476
8477 /* it's a "normal" terminal when not in a GUI or cterm */
8478 norm_term = (
8479#ifdef FEAT_GUI
8480 !gui.in_use &&
8481#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008482 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 for (row = start_row; row < end_row; ++row)
8484 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008485#ifdef FEAT_MBYTE
8486 if (has_mbyte
8487# ifdef FEAT_GUI
8488 && !gui.in_use
8489# endif
8490 )
8491 {
8492 /* When drawing over the right halve of a double-wide char clear
8493 * out the left halve. When drawing over the left halve of a
8494 * double wide-char clear out the right halve. Only needed in a
8495 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008496 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008497 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008498 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008499 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008500 }
8501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 /*
8503 * Try to use delete-line termcap code, when no attributes or in a
8504 * "normal" terminal, where a bold/italic space is just a
8505 * space.
8506 */
8507 did_delete = FALSE;
8508 if (c2 == ' '
8509 && end_col == Columns
8510 && can_clear(T_CE)
8511 && (attr == 0
8512 || (norm_term
8513 && attr <= HL_ALL
8514 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8515 {
8516 /*
8517 * check if we really need to clear something
8518 */
8519 col = start_col;
8520 if (c1 != ' ') /* don't clear first char */
8521 ++col;
8522
8523 off = LineOffset[row] + col;
8524 end_off = LineOffset[row] + end_col;
8525
8526 /* skip blanks (used often, keep it fast!) */
8527#ifdef FEAT_MBYTE
8528 if (enc_utf8)
8529 while (off < end_off && ScreenLines[off] == ' '
8530 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8531 ++off;
8532 else
8533#endif
8534 while (off < end_off && ScreenLines[off] == ' '
8535 && ScreenAttrs[off] == 0)
8536 ++off;
8537 if (off < end_off) /* something to be cleared */
8538 {
8539 col = off - LineOffset[row];
8540 screen_stop_highlight();
8541 term_windgoto(row, col);/* clear rest of this screen line */
8542 out_str(T_CE);
8543 screen_start(); /* don't know where cursor is now */
8544 col = end_col - col;
8545 while (col--) /* clear chars in ScreenLines */
8546 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008547 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 ++off;
8549 }
8550 }
8551 did_delete = TRUE; /* the chars are cleared now */
8552 }
8553
8554 off = LineOffset[row] + start_col;
8555 c = c1;
8556 for (col = start_col; col < end_col; ++col)
8557 {
8558 if (ScreenLines[off] != c
8559#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008560 || (enc_utf8 && (int)ScreenLinesUC[off]
8561 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562#endif
8563 || ScreenAttrs[off] != attr
8564#if defined(FEAT_GUI) || defined(UNIX)
8565 || force_next
8566#endif
8567 )
8568 {
8569#if defined(FEAT_GUI) || defined(UNIX)
8570 /* The bold trick may make a single row of pixels appear in
8571 * the next character. When a bold character is removed, the
8572 * next character should be redrawn too. This happens for our
8573 * own GUI and for some xterms. */
8574 if (
8575# ifdef FEAT_GUI
8576 gui.in_use
8577# endif
8578# if defined(FEAT_GUI) && defined(UNIX)
8579 ||
8580# endif
8581# ifdef UNIX
8582 term_is_xterm
8583# endif
8584 )
8585 {
8586 if (ScreenLines[off] != ' '
8587 && (ScreenAttrs[off] > HL_ALL
8588 || ScreenAttrs[off] & HL_BOLD))
8589 force_next = TRUE;
8590 else
8591 force_next = FALSE;
8592 }
8593#endif
8594 ScreenLines[off] = c;
8595#ifdef FEAT_MBYTE
8596 if (enc_utf8)
8597 {
8598 if (c >= 0x80)
8599 {
8600 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008601 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 }
8603 else
8604 ScreenLinesUC[off] = 0;
8605 }
8606#endif
8607 ScreenAttrs[off] = attr;
8608 if (!did_delete || c != ' ')
8609 screen_char(off, row, col);
8610 }
8611 ++off;
8612 if (col == start_col)
8613 {
8614 if (did_delete)
8615 break;
8616 c = c2;
8617 }
8618 }
8619 if (end_col == Columns)
8620 LineWraps[row] = FALSE;
8621 if (row == Rows - 1) /* overwritten the command line */
8622 {
8623 redraw_cmdline = TRUE;
8624 if (c1 == ' ' && c2 == ' ')
8625 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008626 if (start_col == 0)
8627 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 }
8629 }
8630}
8631
8632/*
8633 * Check if there should be a delay. Used before clearing or redrawing the
8634 * screen or the command line.
8635 */
8636 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008637check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
8639 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8640 && !did_wait_return
8641 && emsg_silent == 0)
8642 {
8643 out_flush();
8644 ui_delay(1000L, TRUE);
8645 emsg_on_display = FALSE;
8646 if (check_msg_scroll)
8647 msg_scroll = FALSE;
8648 }
8649}
8650
8651/*
8652 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008653 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 * Returns TRUE if there is a valid screen to write to.
8655 * Returns FALSE when starting up and screen not initialized yet.
8656 */
8657 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008658screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008660 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 return (ScreenLines != NULL);
8662}
8663
8664/*
8665 * Resize the shell to Rows and Columns.
8666 * Allocate ScreenLines[] and associated items.
8667 *
8668 * There may be some time between setting Rows and Columns and (re)allocating
8669 * ScreenLines[]. This happens when starting up and when (manually) changing
8670 * the shell size. Always use screen_Rows and screen_Columns to access items
8671 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8672 * final size of the shell is needed.
8673 */
8674 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008675screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676{
8677 int new_row, old_row;
8678#ifdef FEAT_GUI
8679 int old_Rows;
8680#endif
8681 win_T *wp;
8682 int outofmem = FALSE;
8683 int len;
8684 schar_T *new_ScreenLines;
8685#ifdef FEAT_MBYTE
8686 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008687 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008689 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690#endif
8691 sattr_T *new_ScreenAttrs;
8692 unsigned *new_LineOffset;
8693 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008694 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008695 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008697 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008698#ifdef FEAT_AUTOCMD
8699 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008701retry:
8702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 /*
8704 * Allocation of the screen buffers is done only when the size changes and
8705 * when Rows and Columns have been set and we have started doing full
8706 * screen stuff.
8707 */
8708 if ((ScreenLines != NULL
8709 && Rows == screen_Rows
8710 && Columns == screen_Columns
8711#ifdef FEAT_MBYTE
8712 && enc_utf8 == (ScreenLinesUC != NULL)
8713 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008714 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715#endif
8716 )
8717 || Rows == 0
8718 || Columns == 0
8719 || (!full_screen && ScreenLines == NULL))
8720 return;
8721
8722 /*
8723 * It's possible that we produce an out-of-memory message below, which
8724 * will cause this function to be called again. To break the loop, just
8725 * return here.
8726 */
8727 if (entered)
8728 return;
8729 entered = TRUE;
8730
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008731 /*
8732 * Note that the window sizes are updated before reallocating the arrays,
8733 * thus we must not redraw here!
8734 */
8735 ++RedrawingDisabled;
8736
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 win_new_shellsize(); /* fit the windows in the new sized shell */
8738
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 comp_col(); /* recompute columns for shown command and ruler */
8740
8741 /*
8742 * We're changing the size of the screen.
8743 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8744 * - Move lines from the old arrays into the new arrays, clear extra
8745 * lines (unless the screen is going to be cleared).
8746 * - Free the old arrays.
8747 *
8748 * If anything fails, make ScreenLines NULL, so we don't do anything!
8749 * Continuing with the old ScreenLines may result in a crash, because the
8750 * size is wrong.
8751 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008752 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008754#ifdef FEAT_AUTOCMD
8755 if (aucmd_win != NULL)
8756 win_free_lsize(aucmd_win);
8757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758
8759 new_ScreenLines = (schar_T *)lalloc((long_u)(
8760 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8761#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008762 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 if (enc_utf8)
8764 {
8765 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8766 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008767 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008768 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8770 }
8771 if (enc_dbcs == DBCS_JPNU)
8772 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8773 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8774#endif
8775 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8776 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8777 new_LineOffset = (unsigned *)lalloc((long_u)(
8778 Rows * sizeof(unsigned)), FALSE);
8779 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008780 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008782 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 {
8784 if (win_alloc_lines(wp) == FAIL)
8785 {
8786 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008787 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 }
8789 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008790#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008791 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8792 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008793 outofmem = TRUE;
8794#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008795give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008797#ifdef FEAT_MBYTE
8798 for (i = 0; i < p_mco; ++i)
8799 if (new_ScreenLinesC[i] == NULL)
8800 break;
8801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 if (new_ScreenLines == NULL
8803#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008804 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8806#endif
8807 || new_ScreenAttrs == NULL
8808 || new_LineOffset == NULL
8809 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008810 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 || outofmem)
8812 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008813 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008814 {
8815 /* guess the size */
8816 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8817
8818 /* Remember we did this to avoid getting outofmem messages over
8819 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008820 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 vim_free(new_ScreenLines);
8823 new_ScreenLines = NULL;
8824#ifdef FEAT_MBYTE
8825 vim_free(new_ScreenLinesUC);
8826 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008827 for (i = 0; i < p_mco; ++i)
8828 {
8829 vim_free(new_ScreenLinesC[i]);
8830 new_ScreenLinesC[i] = NULL;
8831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 vim_free(new_ScreenLines2);
8833 new_ScreenLines2 = NULL;
8834#endif
8835 vim_free(new_ScreenAttrs);
8836 new_ScreenAttrs = NULL;
8837 vim_free(new_LineOffset);
8838 new_LineOffset = NULL;
8839 vim_free(new_LineWraps);
8840 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008841 vim_free(new_TabPageIdxs);
8842 new_TabPageIdxs = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 }
8844 else
8845 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008846 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008847
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 for (new_row = 0; new_row < Rows; ++new_row)
8849 {
8850 new_LineOffset[new_row] = new_row * Columns;
8851 new_LineWraps[new_row] = FALSE;
8852
8853 /*
8854 * If the screen is not going to be cleared, copy as much as
8855 * possible from the old screen to the new one and clear the rest
8856 * (used when resizing the window at the "--more--" prompt or when
8857 * executing an external command, for the GUI).
8858 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008859 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 {
8861 (void)vim_memset(new_ScreenLines + new_row * Columns,
8862 ' ', (size_t)Columns * sizeof(schar_T));
8863#ifdef FEAT_MBYTE
8864 if (enc_utf8)
8865 {
8866 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8867 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008868 for (i = 0; i < p_mco; ++i)
8869 (void)vim_memset(new_ScreenLinesC[i]
8870 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 0, (size_t)Columns * sizeof(u8char_T));
8872 }
8873 if (enc_dbcs == DBCS_JPNU)
8874 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8875 0, (size_t)Columns * sizeof(schar_T));
8876#endif
8877 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8878 0, (size_t)Columns * sizeof(sattr_T));
8879 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008880 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 {
8882 if (screen_Columns < Columns)
8883 len = screen_Columns;
8884 else
8885 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008886#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008887 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008888 * may be invalid now. Also when p_mco changes. */
8889 if (!(enc_utf8 && ScreenLinesUC == NULL)
8890 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008891#endif
8892 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8893 ScreenLines + LineOffset[old_row],
8894 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008896 if (enc_utf8 && ScreenLinesUC != NULL
8897 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 {
8899 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8900 ScreenLinesUC + LineOffset[old_row],
8901 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008902 for (i = 0; i < p_mco; ++i)
8903 mch_memmove(new_ScreenLinesC[i]
8904 + new_LineOffset[new_row],
8905 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 (size_t)len * sizeof(u8char_T));
8907 }
8908 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8909 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8910 ScreenLines2 + LineOffset[old_row],
8911 (size_t)len * sizeof(schar_T));
8912#endif
8913 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8914 ScreenAttrs + LineOffset[old_row],
8915 (size_t)len * sizeof(sattr_T));
8916 }
8917 }
8918 }
8919 /* Use the last line of the screen for the current line. */
8920 current_ScreenLine = new_ScreenLines + Rows * Columns;
8921 }
8922
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008923 free_screenlines();
8924
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 ScreenLines = new_ScreenLines;
8926#ifdef FEAT_MBYTE
8927 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008928 for (i = 0; i < p_mco; ++i)
8929 ScreenLinesC[i] = new_ScreenLinesC[i];
8930 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 ScreenLines2 = new_ScreenLines2;
8932#endif
8933 ScreenAttrs = new_ScreenAttrs;
8934 LineOffset = new_LineOffset;
8935 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008936 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937
8938 /* It's important that screen_Rows and screen_Columns reflect the actual
8939 * size of ScreenLines[]. Set them before calling anything. */
8940#ifdef FEAT_GUI
8941 old_Rows = screen_Rows;
8942#endif
8943 screen_Rows = Rows;
8944 screen_Columns = Columns;
8945
8946 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008947 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 screenclear2();
8949
8950#ifdef FEAT_GUI
8951 else if (gui.in_use
8952 && !gui.starting
8953 && ScreenLines != NULL
8954 && old_Rows != Rows)
8955 {
8956 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8957 /*
8958 * Adjust the position of the cursor, for when executing an external
8959 * command.
8960 */
8961 if (msg_row >= Rows) /* Rows got smaller */
8962 msg_row = Rows - 1; /* put cursor at last row */
8963 else if (Rows > old_Rows) /* Rows got bigger */
8964 msg_row += Rows - old_Rows; /* put cursor in same place */
8965 if (msg_col >= Columns) /* Columns got smaller */
8966 msg_col = Columns - 1; /* put cursor at last column */
8967 }
8968#endif
8969
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008971 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008972
8973#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008974 /*
8975 * Do not apply autocommands more than 3 times to avoid an endless loop
8976 * in case applying autocommands always changes Rows or Columns.
8977 */
8978 if (starting == 0 && ++retry_count <= 3)
8979 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008980 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008981 /* In rare cases, autocommands may have altered Rows or Columns,
8982 * jump back to check if we need to allocate the screen again. */
8983 goto retry;
8984 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986}
8987
8988 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008989free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008990{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008991#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008992 int i;
8993
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008994 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008995 for (i = 0; i < Screen_mco; ++i)
8996 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008997 vim_free(ScreenLines2);
8998#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008999 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009000 vim_free(ScreenAttrs);
9001 vim_free(LineOffset);
9002 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009003 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009004}
9005
9006 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009007screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
9009 check_for_delay(FALSE);
9010 screenalloc(FALSE); /* allocate screen buffers if size changed */
9011 screenclear2(); /* clear the screen */
9012}
9013
9014 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009015screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016{
9017 int i;
9018
9019 if (starting == NO_SCREEN || ScreenLines == NULL
9020#ifdef FEAT_GUI
9021 || (gui.in_use && gui.starting)
9022#endif
9023 )
9024 return;
9025
9026#ifdef FEAT_GUI
9027 if (!gui.in_use)
9028#endif
9029 screen_attr = -1; /* force setting the Normal colors */
9030 screen_stop_highlight(); /* don't want highlighting here */
9031
9032#ifdef FEAT_CLIPBOARD
9033 /* disable selection without redrawing it */
9034 clip_scroll_selection(9999);
9035#endif
9036
9037 /* blank out ScreenLines */
9038 for (i = 0; i < Rows; ++i)
9039 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009040 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 LineWraps[i] = FALSE;
9042 }
9043
9044 if (can_clear(T_CL))
9045 {
9046 out_str(T_CL); /* clear the display */
9047 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009048 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 }
9050 else
9051 {
9052 /* can't clear the screen, mark all chars with invalid attributes */
9053 for (i = 0; i < Rows; ++i)
9054 lineinvalid(LineOffset[i], (int)Columns);
9055 clear_cmdline = TRUE;
9056 }
9057
9058 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9059
9060 win_rest_invalid(firstwin);
9061 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009062 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 if (must_redraw == CLEAR) /* no need to clear again */
9064 must_redraw = NOT_VALID;
9065 compute_cmdrow();
9066 msg_row = cmdline_row; /* put cursor on last line for messages */
9067 msg_col = 0;
9068 screen_start(); /* don't know where cursor is now */
9069 msg_scrolled = 0; /* can't scroll back */
9070 msg_didany = FALSE;
9071 msg_didout = FALSE;
9072}
9073
9074/*
9075 * Clear one line in ScreenLines.
9076 */
9077 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009078lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
9080 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9081#ifdef FEAT_MBYTE
9082 if (enc_utf8)
9083 (void)vim_memset(ScreenLinesUC + off, 0,
9084 (size_t)width * sizeof(u8char_T));
9085#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009086 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087}
9088
9089/*
9090 * Mark one line in ScreenLines invalid by setting the attributes to an
9091 * invalid value.
9092 */
9093 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009094lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095{
9096 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9097}
9098
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099/*
9100 * Copy part of a Screenline for vertically split window "wp".
9101 */
9102 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009103linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104{
9105 unsigned off_to = LineOffset[to] + wp->w_wincol;
9106 unsigned off_from = LineOffset[from] + wp->w_wincol;
9107
9108 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9109 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009110#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 if (enc_utf8)
9112 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009113 int i;
9114
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9116 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009117 for (i = 0; i < p_mco; ++i)
9118 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9119 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 }
9121 if (enc_dbcs == DBCS_JPNU)
9122 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9123 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9126 wp->w_width * sizeof(sattr_T));
9127}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128
9129/*
9130 * Return TRUE if clearing with term string "p" would work.
9131 * It can't work when the string is empty or it won't set the right background.
9132 */
9133 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009134can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135{
9136 return (*p != NUL && (t_colors <= 1
9137#ifdef FEAT_GUI
9138 || gui.in_use
9139#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009140#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009141 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009142 || (!p_tgc && cterm_normal_bg_color == 0)
9143#else
9144 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009145#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009146 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147}
9148
9149/*
9150 * Reset cursor position. Use whenever cursor was moved because of outputting
9151 * something directly to the screen (shell commands) or a terminal control
9152 * code.
9153 */
9154 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009155screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156{
9157 screen_cur_row = screen_cur_col = 9999;
9158}
9159
9160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 * Move the cursor to position "row","col" in the screen.
9162 * This tries to find the most efficient way to move, minimizing the number of
9163 * characters sent to the terminal.
9164 */
9165 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009166windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009168 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 int i;
9170 int plan;
9171 int cost;
9172 int wouldbe_col;
9173 int noinvcurs;
9174 char_u *bs;
9175 int goto_cost;
9176 int attr;
9177
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009178#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9180
9181#define PLAN_LE 1
9182#define PLAN_CR 2
9183#define PLAN_NL 3
9184#define PLAN_WRITE 4
9185 /* Can't use ScreenLines unless initialized */
9186 if (ScreenLines == NULL)
9187 return;
9188
9189 if (col != screen_cur_col || row != screen_cur_row)
9190 {
9191 /* Check for valid position. */
9192 if (row < 0) /* window without text lines? */
9193 row = 0;
9194 if (row >= screen_Rows)
9195 row = screen_Rows - 1;
9196 if (col >= screen_Columns)
9197 col = screen_Columns - 1;
9198
9199 /* check if no cursor movement is allowed in highlight mode */
9200 if (screen_attr && *T_MS == NUL)
9201 noinvcurs = HIGHL_COST;
9202 else
9203 noinvcurs = 0;
9204 goto_cost = GOTO_COST + noinvcurs;
9205
9206 /*
9207 * Plan how to do the positioning:
9208 * 1. Use CR to move it to column 0, same row.
9209 * 2. Use T_LE to move it a few columns to the left.
9210 * 3. Use NL to move a few lines down, column 0.
9211 * 4. Move a few columns to the right with T_ND or by writing chars.
9212 *
9213 * Don't do this if the cursor went beyond the last column, the cursor
9214 * position is unknown then (some terminals wrap, some don't )
9215 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009216 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 * characters to move the cursor to the right.
9218 */
9219 if (row >= screen_cur_row && screen_cur_col < Columns)
9220 {
9221 /*
9222 * If the cursor is in the same row, bigger col, we can use CR
9223 * or T_LE.
9224 */
9225 bs = NULL; /* init for GCC */
9226 attr = screen_attr;
9227 if (row == screen_cur_row && col < screen_cur_col)
9228 {
9229 /* "le" is preferred over "bc", because "bc" is obsolete */
9230 if (*T_LE)
9231 bs = T_LE; /* "cursor left" */
9232 else
9233 bs = T_BC; /* "backspace character (old) */
9234 if (*bs)
9235 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9236 else
9237 cost = 999;
9238 if (col + 1 < cost) /* using CR is less characters */
9239 {
9240 plan = PLAN_CR;
9241 wouldbe_col = 0;
9242 cost = 1; /* CR is just one character */
9243 }
9244 else
9245 {
9246 plan = PLAN_LE;
9247 wouldbe_col = col;
9248 }
9249 if (noinvcurs) /* will stop highlighting */
9250 {
9251 cost += noinvcurs;
9252 attr = 0;
9253 }
9254 }
9255
9256 /*
9257 * If the cursor is above where we want to be, we can use CR LF.
9258 */
9259 else if (row > screen_cur_row)
9260 {
9261 plan = PLAN_NL;
9262 wouldbe_col = 0;
9263 cost = (row - screen_cur_row) * 2; /* CR LF */
9264 if (noinvcurs) /* will stop highlighting */
9265 {
9266 cost += noinvcurs;
9267 attr = 0;
9268 }
9269 }
9270
9271 /*
9272 * If the cursor is in the same row, smaller col, just use write.
9273 */
9274 else
9275 {
9276 plan = PLAN_WRITE;
9277 wouldbe_col = screen_cur_col;
9278 cost = 0;
9279 }
9280
9281 /*
9282 * Check if any characters that need to be written have the
9283 * correct attributes. Also avoid UTF-8 characters.
9284 */
9285 i = col - wouldbe_col;
9286 if (i > 0)
9287 cost += i;
9288 if (cost < goto_cost && i > 0)
9289 {
9290 /*
9291 * Check if the attributes are correct without additionally
9292 * stopping highlighting.
9293 */
9294 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9295 while (i && *p++ == attr)
9296 --i;
9297 if (i != 0)
9298 {
9299 /*
9300 * Try if it works when highlighting is stopped here.
9301 */
9302 if (*--p == 0)
9303 {
9304 cost += noinvcurs;
9305 while (i && *p++ == 0)
9306 --i;
9307 }
9308 if (i != 0)
9309 cost = 999; /* different attributes, don't do it */
9310 }
9311#ifdef FEAT_MBYTE
9312 if (enc_utf8)
9313 {
9314 /* Don't use an UTF-8 char for positioning, it's slow. */
9315 for (i = wouldbe_col; i < col; ++i)
9316 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9317 {
9318 cost = 999;
9319 break;
9320 }
9321 }
9322#endif
9323 }
9324
9325 /*
9326 * We can do it without term_windgoto()!
9327 */
9328 if (cost < goto_cost)
9329 {
9330 if (plan == PLAN_LE)
9331 {
9332 if (noinvcurs)
9333 screen_stop_highlight();
9334 while (screen_cur_col > col)
9335 {
9336 out_str(bs);
9337 --screen_cur_col;
9338 }
9339 }
9340 else if (plan == PLAN_CR)
9341 {
9342 if (noinvcurs)
9343 screen_stop_highlight();
9344 out_char('\r');
9345 screen_cur_col = 0;
9346 }
9347 else if (plan == PLAN_NL)
9348 {
9349 if (noinvcurs)
9350 screen_stop_highlight();
9351 while (screen_cur_row < row)
9352 {
9353 out_char('\n');
9354 ++screen_cur_row;
9355 }
9356 screen_cur_col = 0;
9357 }
9358
9359 i = col - screen_cur_col;
9360 if (i > 0)
9361 {
9362 /*
9363 * Use cursor-right if it's one character only. Avoids
9364 * removing a line of pixels from the last bold char, when
9365 * using the bold trick in the GUI.
9366 */
9367 if (T_ND[0] != NUL && T_ND[1] == NUL)
9368 {
9369 while (i-- > 0)
9370 out_char(*T_ND);
9371 }
9372 else
9373 {
9374 int off;
9375
9376 off = LineOffset[row] + screen_cur_col;
9377 while (i-- > 0)
9378 {
9379 if (ScreenAttrs[off] != screen_attr)
9380 screen_stop_highlight();
9381#ifdef FEAT_MBYTE
9382 out_flush_check();
9383#endif
9384 out_char(ScreenLines[off]);
9385#ifdef FEAT_MBYTE
9386 if (enc_dbcs == DBCS_JPNU
9387 && ScreenLines[off] == 0x8e)
9388 out_char(ScreenLines2[off]);
9389#endif
9390 ++off;
9391 }
9392 }
9393 }
9394 }
9395 }
9396 else
9397 cost = 999;
9398
9399 if (cost >= goto_cost)
9400 {
9401 if (noinvcurs)
9402 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009403 if (row == screen_cur_row && (col > screen_cur_col)
9404 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 term_cursor_right(col - screen_cur_col);
9406 else
9407 term_windgoto(row, col);
9408 }
9409 screen_cur_row = row;
9410 screen_cur_col = col;
9411 }
9412}
9413
9414/*
9415 * Set cursor to its position in the current window.
9416 */
9417 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009418setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
9420 if (redrawing())
9421 {
9422 validate_cursor();
9423 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9424 W_WINCOL(curwin) + (
9425#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009426 /* With 'rightleft' set and the cursor on a double-wide
9427 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9429# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009430 (has_mbyte
9431 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9432 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433# endif
9434 1)) :
9435#endif
9436 curwin->w_wcol));
9437 }
9438}
9439
9440
9441/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009442 * Insert 'line_count' lines at 'row' in window 'wp'.
9443 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9444 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 * scrolling.
9446 * Returns FAIL if the lines are not inserted, OK for success.
9447 */
9448 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009449win_ins_lines(
9450 win_T *wp,
9451 int row,
9452 int line_count,
9453 int invalid,
9454 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
9456 int did_delete;
9457 int nextrow;
9458 int lastrow;
9459 int retval;
9460
9461 if (invalid)
9462 wp->w_lines_valid = 0;
9463
9464 if (wp->w_height < 5)
9465 return FAIL;
9466
9467 if (line_count > wp->w_height - row)
9468 line_count = wp->w_height - row;
9469
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009470 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 if (retval != MAYBE)
9472 return retval;
9473
9474 /*
9475 * If there is a next window or a status line, we first try to delete the
9476 * lines at the bottom to avoid messing what is after the window.
9477 * If this fails and there are following windows, don't do anything to avoid
9478 * messing up those windows, better just redraw.
9479 */
9480 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (wp->w_next != NULL || wp->w_status_height)
9482 {
9483 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009484 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 did_delete = TRUE;
9486 else if (wp->w_next)
9487 return FAIL;
9488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 /*
9490 * if no lines deleted, blank the lines that will end up below the window
9491 */
9492 if (!did_delete)
9493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 redraw_cmdline = TRUE;
9496 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9497 lastrow = nextrow + line_count;
9498 if (lastrow > Rows)
9499 lastrow = Rows;
9500 screen_fill(nextrow - line_count, lastrow - line_count,
9501 W_WINCOL(wp), (int)W_ENDCOL(wp),
9502 ' ', ' ', 0);
9503 }
9504
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009505 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 == FAIL)
9507 {
9508 /* deletion will have messed up other windows */
9509 if (did_delete)
9510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 win_rest_invalid(W_NEXT(wp));
9513 }
9514 return FAIL;
9515 }
9516
9517 return OK;
9518}
9519
9520/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009521 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9523 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9524 * scrolling
9525 * Return OK for success, FAIL if the lines are not deleted.
9526 */
9527 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009528win_del_lines(
9529 win_T *wp,
9530 int row,
9531 int line_count,
9532 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009533 int mayclear,
9534 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535{
9536 int retval;
9537
9538 if (invalid)
9539 wp->w_lines_valid = 0;
9540
9541 if (line_count > wp->w_height - row)
9542 line_count = wp->w_height - row;
9543
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009544 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 if (retval != MAYBE)
9546 return retval;
9547
9548 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009549 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 return FAIL;
9551
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 /*
9553 * If there are windows or status lines below, try to put them at the
9554 * correct place. If we can't do that, they have to be redrawn.
9555 */
9556 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9557 {
9558 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009559 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 {
9561 wp->w_redr_status = TRUE;
9562 win_rest_invalid(wp->w_next);
9563 }
9564 }
9565 /*
9566 * If this is the last window and there is no status line, redraw the
9567 * command line later.
9568 */
9569 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 redraw_cmdline = TRUE;
9571 return OK;
9572}
9573
9574/*
9575 * Common code for win_ins_lines() and win_del_lines().
9576 * Returns OK or FAIL when the work has been done.
9577 * Returns MAYBE when not finished yet.
9578 */
9579 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009580win_do_lines(
9581 win_T *wp,
9582 int row,
9583 int line_count,
9584 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009585 int del,
9586 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 int retval;
9589
9590 if (!redrawing() || line_count <= 0)
9591 return FAIL;
9592
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009593 /* When inserting lines would result in loss of command output, just redraw
9594 * the lines. */
9595 if (no_win_do_lines_ins && !del)
9596 return FAIL;
9597
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009599 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009601 if (!no_win_do_lines_ins)
9602 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 return FAIL;
9604 }
9605
9606 /*
9607 * Delete all remaining lines
9608 */
9609 if (row + line_count >= wp->w_height)
9610 {
9611 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9612 W_WINCOL(wp), (int)W_ENDCOL(wp),
9613 ' ', ' ', 0);
9614 return OK;
9615 }
9616
9617 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009618 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009620 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009622 if (!no_win_do_lines_ins)
9623 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624
9625 /*
9626 * If the terminal can set a scroll region, use that.
9627 * Always do this in a vertically split window. This will redraw from
9628 * ScreenLines[] when t_CV isn't defined. That's faster than using
9629 * win_line().
9630 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009631 * a character in the lower right corner of the scroll region may cause a
9632 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009634 if (scroll_region || W_WIDTH(wp) != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 scroll_region_set(wp, row);
9638 if (del)
9639 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009640 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 else
9642 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009643 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 scroll_region_reset();
9646 return retval;
9647 }
9648
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9650 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651
9652 return MAYBE;
9653}
9654
9655/*
9656 * window 'wp' and everything after it is messed up, mark it for redraw
9657 */
9658 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009659win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 {
9663 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 wp->w_redr_status = TRUE;
9665 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 }
9667 redraw_cmdline = TRUE;
9668}
9669
9670/*
9671 * The rest of the routines in this file perform screen manipulations. The
9672 * given operation is performed physically on the screen. The corresponding
9673 * change is also made to the internal screen image. In this way, the editor
9674 * anticipates the effect of editing changes on the appearance of the screen.
9675 * That way, when we call screenupdate a complete redraw isn't usually
9676 * necessary. Another advantage is that we can keep adding code to anticipate
9677 * screen changes, and in the meantime, everything still works.
9678 */
9679
9680/*
9681 * types for inserting or deleting lines
9682 */
9683#define USE_T_CAL 1
9684#define USE_T_CDL 2
9685#define USE_T_AL 3
9686#define USE_T_CE 4
9687#define USE_T_DL 5
9688#define USE_T_SR 6
9689#define USE_NL 7
9690#define USE_T_CD 8
9691#define USE_REDRAW 9
9692
9693/*
9694 * insert lines on the screen and update ScreenLines[]
9695 * 'end' is the line after the scrolled part. Normally it is Rows.
9696 * When scrolling region used 'off' is the offset from the top for the region.
9697 * 'row' and 'end' are relative to the start of the region.
9698 *
9699 * return FAIL for failure, OK for success.
9700 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009701 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009702screen_ins_lines(
9703 int off,
9704 int row,
9705 int line_count,
9706 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009707 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009708 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
9710 int i;
9711 int j;
9712 unsigned temp;
9713 int cursor_row;
9714 int type;
9715 int result_empty;
9716 int can_ce = can_clear(T_CE);
9717
9718 /*
9719 * FAIL if
9720 * - there is no valid screen
9721 * - the screen has to be redrawn completely
9722 * - the line count is less than one
9723 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009724 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009726 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9727#ifdef FEAT_CLIPBOARD
9728 || (clip_star.state != SELECT_CLEARED
9729 && redrawing_for_callback > 0)
9730#endif
9731 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 return FAIL;
9733
9734 /*
9735 * There are seven ways to insert lines:
9736 * 0. When in a vertically split window and t_CV isn't set, redraw the
9737 * characters from ScreenLines[].
9738 * 1. Use T_CD (clear to end of display) if it exists and the result of
9739 * the insert is just empty lines
9740 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9741 * present or line_count > 1. It looks better if we do all the inserts
9742 * at once.
9743 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9744 * insert is just empty lines and T_CE is not present or line_count >
9745 * 1.
9746 * 4. Use T_AL (insert line) if it exists.
9747 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9748 * just empty lines.
9749 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9750 * just empty lines.
9751 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9752 * the 'da' flag is not set or we have clear line capability.
9753 * 8. redraw the characters from ScreenLines[].
9754 *
9755 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9756 * the scrollbar for the window. It does have insert line, use that if it
9757 * exists.
9758 */
9759 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9761 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009762 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 type = USE_T_CD;
9764 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9765 type = USE_T_CAL;
9766 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9767 type = USE_T_CDL;
9768 else if (*T_AL != NUL)
9769 type = USE_T_AL;
9770 else if (can_ce && result_empty)
9771 type = USE_T_CE;
9772 else if (*T_DL != NUL && result_empty)
9773 type = USE_T_DL;
9774 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9775 type = USE_T_SR;
9776 else
9777 return FAIL;
9778
9779 /*
9780 * For clearing the lines screen_del_lines() is used. This will also take
9781 * care of t_db if necessary.
9782 */
9783 if (type == USE_T_CD || type == USE_T_CDL ||
9784 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009785 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786
9787 /*
9788 * If text is retained below the screen, first clear or delete as many
9789 * lines at the bottom of the window as are about to be inserted so that
9790 * the deleted lines won't later surface during a screen_del_lines.
9791 */
9792 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009793 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794
9795#ifdef FEAT_CLIPBOARD
9796 /* Remove a modeless selection when inserting lines halfway the screen
9797 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009798 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009799 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 else
9801 clip_scroll_selection(-line_count);
9802#endif
9803
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804#ifdef FEAT_GUI
9805 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9806 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009807 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808#endif
9809
9810 if (*T_CCS != NUL) /* cursor relative to region */
9811 cursor_row = row;
9812 else
9813 cursor_row = row + off;
9814
9815 /*
9816 * Shift LineOffset[] line_count down to reflect the inserted lines.
9817 * Clear the inserted lines in ScreenLines[].
9818 */
9819 row += off;
9820 end += off;
9821 for (i = 0; i < line_count; ++i)
9822 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 if (wp != NULL && wp->w_width != Columns)
9824 {
9825 /* need to copy part of a line */
9826 j = end - 1 - i;
9827 while ((j -= line_count) >= row)
9828 linecopy(j + line_count, j, wp);
9829 j += line_count;
9830 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009831 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9832 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 else
9834 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9835 LineWraps[j] = FALSE;
9836 }
9837 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 {
9839 j = end - 1 - i;
9840 temp = LineOffset[j];
9841 while ((j -= line_count) >= row)
9842 {
9843 LineOffset[j + line_count] = LineOffset[j];
9844 LineWraps[j + line_count] = LineWraps[j];
9845 }
9846 LineOffset[j + line_count] = temp;
9847 LineWraps[j + line_count] = FALSE;
9848 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009849 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 else
9851 lineinvalid(temp, (int)Columns);
9852 }
9853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854
9855 screen_stop_highlight();
9856 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009857 if (clear_attr != 0)
9858 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 /* redraw the characters */
9861 if (type == USE_REDRAW)
9862 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009863 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 {
9865 term_append_lines(line_count);
9866 screen_start(); /* don't know where cursor is now */
9867 }
9868 else
9869 {
9870 for (i = 0; i < line_count; i++)
9871 {
9872 if (type == USE_T_AL)
9873 {
9874 if (i && cursor_row != 0)
9875 windgoto(cursor_row, 0);
9876 out_str(T_AL);
9877 }
9878 else /* type == USE_T_SR */
9879 out_str(T_SR);
9880 screen_start(); /* don't know where cursor is now */
9881 }
9882 }
9883
9884 /*
9885 * With scroll-reverse and 'da' flag set we need to clear the lines that
9886 * have been scrolled down into the region.
9887 */
9888 if (type == USE_T_SR && *T_DA)
9889 {
9890 for (i = 0; i < line_count; ++i)
9891 {
9892 windgoto(off + i, 0);
9893 out_str(T_CE);
9894 screen_start(); /* don't know where cursor is now */
9895 }
9896 }
9897
9898#ifdef FEAT_GUI
9899 gui_can_update_cursor();
9900 if (gui.in_use)
9901 out_flush(); /* always flush after a scroll */
9902#endif
9903 return OK;
9904}
9905
9906/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009907 * Delete lines on the screen and update ScreenLines[].
9908 * "end" is the line after the scrolled part. Normally it is Rows.
9909 * When scrolling region used "off" is the offset from the top for the region.
9910 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 *
9912 * Return OK for success, FAIL if the lines are not deleted.
9913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009915screen_del_lines(
9916 int off,
9917 int row,
9918 int line_count,
9919 int end,
9920 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009921 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009922 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923{
9924 int j;
9925 int i;
9926 unsigned temp;
9927 int cursor_row;
9928 int cursor_end;
9929 int result_empty; /* result is empty until end of region */
9930 int can_delete; /* deleting line codes can be used */
9931 int type;
9932
9933 /*
9934 * FAIL if
9935 * - there is no valid screen
9936 * - the screen has to be redrawn completely
9937 * - the line count is less than one
9938 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009939 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009941 if (!screen_valid(TRUE) || line_count <= 0
9942 || (!force && line_count > p_ttyscroll)
9943#ifdef FEAT_CLIPBOARD
9944 || (clip_star.state != SELECT_CLEARED
9945 && redrawing_for_callback > 0)
9946#endif
9947 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 return FAIL;
9949
9950 /*
9951 * Check if the rest of the current region will become empty.
9952 */
9953 result_empty = row + line_count >= end;
9954
9955 /*
9956 * We can delete lines only when 'db' flag not set or when 'ce' option
9957 * available.
9958 */
9959 can_delete = (*T_DB == NUL || can_clear(T_CE));
9960
9961 /*
9962 * There are six ways to delete lines:
9963 * 0. When in a vertically split window and t_CV isn't set, redraw the
9964 * characters from ScreenLines[].
9965 * 1. Use T_CD if it exists and the result is empty.
9966 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9967 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9968 * none of the other ways work.
9969 * 4. Use T_CE (erase line) if the result is empty.
9970 * 5. Use T_DL (delete line) if it exists.
9971 * 6. redraw the characters from ScreenLines[].
9972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9974 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009975 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 type = USE_T_CD;
9977#if defined(__BEOS__) && defined(BEOS_DR8)
9978 /*
9979 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9980 * its internal termcap... this works okay for tests which test *T_DB !=
9981 * NUL. It has the disadvantage that the user cannot use any :set t_*
9982 * command to get T_DB (back) to empty_option, only :set term=... will do
9983 * the trick...
9984 * Anyway, this hack will hopefully go away with the next OS release.
9985 * (Olaf Seibert)
9986 */
9987 else if (row == 0 && T_DB == empty_option
9988 && (line_count == 1 || *T_CDL == NUL))
9989#else
9990 else if (row == 0 && (
9991#ifndef AMIGA
9992 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9993 * up, so use delete-line command */
9994 line_count == 1 ||
9995#endif
9996 *T_CDL == NUL))
9997#endif
9998 type = USE_NL;
9999 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10000 type = USE_T_CDL;
10001 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010002 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 type = USE_T_CE;
10004 else if (*T_DL != NUL && can_delete)
10005 type = USE_T_DL;
10006 else if (*T_CDL != NUL && can_delete)
10007 type = USE_T_CDL;
10008 else
10009 return FAIL;
10010
10011#ifdef FEAT_CLIPBOARD
10012 /* Remove a modeless selection when deleting lines halfway the screen or
10013 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010014 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010015 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 else
10017 clip_scroll_selection(line_count);
10018#endif
10019
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020#ifdef FEAT_GUI
10021 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10022 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010023 gui_dont_update_cursor(gui.cursor_row >= row + off
10024 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025#endif
10026
10027 if (*T_CCS != NUL) /* cursor relative to region */
10028 {
10029 cursor_row = row;
10030 cursor_end = end;
10031 }
10032 else
10033 {
10034 cursor_row = row + off;
10035 cursor_end = end + off;
10036 }
10037
10038 /*
10039 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10040 * Clear the inserted lines in ScreenLines[].
10041 */
10042 row += off;
10043 end += off;
10044 for (i = 0; i < line_count; ++i)
10045 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 if (wp != NULL && wp->w_width != Columns)
10047 {
10048 /* need to copy part of a line */
10049 j = row + i;
10050 while ((j += line_count) <= end - 1)
10051 linecopy(j - line_count, j, wp);
10052 j -= line_count;
10053 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010054 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10055 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 else
10057 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10058 LineWraps[j] = FALSE;
10059 }
10060 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 {
10062 /* whole width, moving the line pointers is faster */
10063 j = row + i;
10064 temp = LineOffset[j];
10065 while ((j += line_count) <= end - 1)
10066 {
10067 LineOffset[j - line_count] = LineOffset[j];
10068 LineWraps[j - line_count] = LineWraps[j];
10069 }
10070 LineOffset[j - line_count] = temp;
10071 LineWraps[j - line_count] = FALSE;
10072 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010073 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 else
10075 lineinvalid(temp, (int)Columns);
10076 }
10077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010079 if (screen_attr != clear_attr)
10080 screen_stop_highlight();
10081 if (clear_attr != 0)
10082 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 /* redraw the characters */
10085 if (type == USE_REDRAW)
10086 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010087 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 {
10089 windgoto(cursor_row, 0);
10090 out_str(T_CD);
10091 screen_start(); /* don't know where cursor is now */
10092 }
10093 else if (type == USE_T_CDL)
10094 {
10095 windgoto(cursor_row, 0);
10096 term_delete_lines(line_count);
10097 screen_start(); /* don't know where cursor is now */
10098 }
10099 /*
10100 * Deleting lines at top of the screen or scroll region: Just scroll
10101 * the whole screen (scroll region) up by outputting newlines on the
10102 * last line.
10103 */
10104 else if (type == USE_NL)
10105 {
10106 windgoto(cursor_end - 1, 0);
10107 for (i = line_count; --i >= 0; )
10108 out_char('\n'); /* cursor will remain on same line */
10109 }
10110 else
10111 {
10112 for (i = line_count; --i >= 0; )
10113 {
10114 if (type == USE_T_DL)
10115 {
10116 windgoto(cursor_row, 0);
10117 out_str(T_DL); /* delete a line */
10118 }
10119 else /* type == USE_T_CE */
10120 {
10121 windgoto(cursor_row + i, 0);
10122 out_str(T_CE); /* erase a line */
10123 }
10124 screen_start(); /* don't know where cursor is now */
10125 }
10126 }
10127
10128 /*
10129 * If the 'db' flag is set, we need to clear the lines that have been
10130 * scrolled up at the bottom of the region.
10131 */
10132 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10133 {
10134 for (i = line_count; i > 0; --i)
10135 {
10136 windgoto(cursor_end - i, 0);
10137 out_str(T_CE); /* erase a line */
10138 screen_start(); /* don't know where cursor is now */
10139 }
10140 }
10141
10142#ifdef FEAT_GUI
10143 gui_can_update_cursor();
10144 if (gui.in_use)
10145 out_flush(); /* always flush after a scroll */
10146#endif
10147
10148 return OK;
10149}
10150
10151/*
10152 * show the current mode and ruler
10153 *
10154 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10155 * If clear_cmdline is FALSE there may be a message there that needs to be
10156 * cleared only if a mode is shown.
10157 * Return the length of the message (0 if no message).
10158 */
10159 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010160showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161{
10162 int need_clear;
10163 int length = 0;
10164 int do_mode;
10165 int attr;
10166 int nwr_save;
10167#ifdef FEAT_INS_EXPAND
10168 int sub_attr;
10169#endif
10170
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010171 do_mode = ((p_smd && msg_silent == 0)
10172 && ((State & INSERT)
10173 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010174 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 if (do_mode || Recording)
10176 {
10177 /*
10178 * Don't show mode right now, when not redrawing or inside a mapping.
10179 * Call char_avail() only when we are going to show something, because
10180 * it takes a bit of time.
10181 */
10182 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10183 {
10184 redraw_cmdline = TRUE; /* show mode later */
10185 return 0;
10186 }
10187
10188 nwr_save = need_wait_return;
10189
10190 /* wait a bit before overwriting an important message */
10191 check_for_delay(FALSE);
10192
10193 /* if the cmdline is more than one line high, erase top lines */
10194 need_clear = clear_cmdline;
10195 if (clear_cmdline && cmdline_row < Rows - 1)
10196 msg_clr_cmdline(); /* will reset clear_cmdline */
10197
10198 /* Position on the last line in the window, column 0 */
10199 msg_pos_mode();
10200 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010201 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 if (do_mode)
10203 {
10204 MSG_PUTS_ATTR("--", attr);
10205#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010206 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010207# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010208 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010209# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010210 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010211# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010212 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010213# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 MSG_PUTS_ATTR(" IM", attr);
10215# else
10216 MSG_PUTS_ATTR(" XIM", attr);
10217# endif
10218#endif
10219#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10220 if (gui.in_use)
10221 {
10222 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010223 {
10224 /* HANGUL */
10225 if (enc_utf8)
10226 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10227 else
10228 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 }
10231#endif
10232#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010233 /* CTRL-X in Insert mode */
10234 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 {
10236 /* These messages can get long, avoid a wrap in a narrow
10237 * window. Prefer showing edit_submode_extra. */
10238 length = (Rows - msg_row) * Columns - 3;
10239 if (edit_submode_extra != NULL)
10240 length -= vim_strsize(edit_submode_extra);
10241 if (length > 0)
10242 {
10243 if (edit_submode_pre != NULL)
10244 length -= vim_strsize(edit_submode_pre);
10245 if (length - vim_strsize(edit_submode) > 0)
10246 {
10247 if (edit_submode_pre != NULL)
10248 msg_puts_attr(edit_submode_pre, attr);
10249 msg_puts_attr(edit_submode, attr);
10250 }
10251 if (edit_submode_extra != NULL)
10252 {
10253 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10254 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010255 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 else
10257 sub_attr = attr;
10258 msg_puts_attr(edit_submode_extra, sub_attr);
10259 }
10260 }
10261 length = 0;
10262 }
10263 else
10264#endif
10265 {
10266#ifdef FEAT_VREPLACE
10267 if (State & VREPLACE_FLAG)
10268 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10269 else
10270#endif
10271 if (State & REPLACE_FLAG)
10272 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10273 else if (State & INSERT)
10274 {
10275#ifdef FEAT_RIGHTLEFT
10276 if (p_ri)
10277 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10278#endif
10279 MSG_PUTS_ATTR(_(" INSERT"), attr);
10280 }
10281 else if (restart_edit == 'I')
10282 MSG_PUTS_ATTR(_(" (insert)"), attr);
10283 else if (restart_edit == 'R')
10284 MSG_PUTS_ATTR(_(" (replace)"), attr);
10285 else if (restart_edit == 'V')
10286 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10287#ifdef FEAT_RIGHTLEFT
10288 if (p_hkmap)
10289 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10290# ifdef FEAT_FKMAP
10291 if (p_fkmap)
10292 MSG_PUTS_ATTR(farsi_text_5, attr);
10293# endif
10294#endif
10295#ifdef FEAT_KEYMAP
10296 if (State & LANGMAP)
10297 {
10298# ifdef FEAT_ARABIC
10299 if (curwin->w_p_arab)
10300 MSG_PUTS_ATTR(_(" Arabic"), attr);
10301 else
10302# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010303 if (get_keymap_str(curwin, (char_u *)" (%s)",
10304 NameBuff, MAXPATHL))
10305 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 }
10307#endif
10308 if ((State & INSERT) && p_paste)
10309 MSG_PUTS_ATTR(_(" (paste)"), attr);
10310
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 if (VIsual_active)
10312 {
10313 char *p;
10314
10315 /* Don't concatenate separate words to avoid translation
10316 * problems. */
10317 switch ((VIsual_select ? 4 : 0)
10318 + (VIsual_mode == Ctrl_V) * 2
10319 + (VIsual_mode == 'V'))
10320 {
10321 case 0: p = N_(" VISUAL"); break;
10322 case 1: p = N_(" VISUAL LINE"); break;
10323 case 2: p = N_(" VISUAL BLOCK"); break;
10324 case 4: p = N_(" SELECT"); break;
10325 case 5: p = N_(" SELECT LINE"); break;
10326 default: p = N_(" SELECT BLOCK"); break;
10327 }
10328 MSG_PUTS_ATTR(_(p), attr);
10329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 MSG_PUTS_ATTR(" --", attr);
10331 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010332
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 need_clear = TRUE;
10334 }
10335 if (Recording
10336#ifdef FEAT_INS_EXPAND
10337 && edit_submode == NULL /* otherwise it gets too long */
10338#endif
10339 )
10340 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010341 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 need_clear = TRUE;
10343 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010344
10345 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 if (need_clear || clear_cmdline)
10347 msg_clr_eos();
10348 msg_didout = FALSE; /* overwrite this message */
10349 length = msg_col;
10350 msg_col = 0;
10351 need_wait_return = nwr_save; /* never ask for hit-return for this */
10352 }
10353 else if (clear_cmdline && msg_silent == 0)
10354 /* Clear the whole command line. Will reset "clear_cmdline". */
10355 msg_clr_cmdline();
10356
10357#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 /* In Visual mode the size of the selected area must be redrawn. */
10359 if (VIsual_active)
10360 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361
10362 /* If the last window has no status line, the ruler is after the mode
10363 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010364 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 win_redr_ruler(lastwin, TRUE);
10366#endif
10367 redraw_cmdline = FALSE;
10368 clear_cmdline = FALSE;
10369
10370 return length;
10371}
10372
10373/*
10374 * Position for a mode message.
10375 */
10376 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010377msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378{
10379 msg_col = 0;
10380 msg_row = Rows - 1;
10381}
10382
10383/*
10384 * Delete mode message. Used when ESC is typed which is expected to end
10385 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010386 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 */
10388 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010389unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390{
10391 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010392 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 */
10394 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10395 redraw_cmdline = TRUE; /* delete mode later */
10396 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010397 clearmode();
10398}
10399
10400/*
10401 * Clear the mode message.
10402 */
10403 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010404clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010405{
10406 msg_pos_mode();
10407 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010408 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010409 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410}
10411
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010412 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010413recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010414{
10415 MSG_PUTS_ATTR(_("recording"), attr);
10416 if (!shortmess(SHM_RECORDING))
10417 {
10418 char_u s[4];
10419 sprintf((char *)s, " @%c", Recording);
10420 MSG_PUTS_ATTR(s, attr);
10421 }
10422}
10423
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010424/*
10425 * Draw the tab pages line at the top of the Vim window.
10426 */
10427 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010428draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010429{
10430 int tabcount = 0;
10431 tabpage_T *tp;
10432 int tabwidth;
10433 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010434 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010435 int attr;
10436 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010437 win_T *cwp;
10438 int wincount;
10439 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010440 int c;
10441 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010442 int attr_sel = HL_ATTR(HLF_TPS);
10443 int attr_nosel = HL_ATTR(HLF_TP);
10444 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010445 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010446 int room;
10447 int use_sep_chars = (t_colors < 8
10448#ifdef FEAT_GUI
10449 && !gui.in_use
10450#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010451#ifdef FEAT_TERMGUICOLORS
10452 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010453#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010454 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010455
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010456 if (ScreenLines == NULL)
10457 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010458 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010459
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010460#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010461 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010462 if (gui_use_tabline())
10463 {
10464 gui_update_tabline();
10465 return;
10466 }
10467#endif
10468
10469 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010470 return;
10471
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010472#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010473
10474 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10475 for (scol = 0; scol < Columns; ++scol)
10476 TabPageIdxs[scol] = 0;
10477
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010478 /* Use the 'tabline' option if it's set. */
10479 if (*p_tal != NUL)
10480 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010481 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010482
10483 /* Check for an error. If there is one we would loop in redrawing the
10484 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010485 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010486 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010487 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010488 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010489 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010490 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010491 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010492 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010493#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010494 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010495 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010496 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010497
Bram Moolenaar238a5642006-02-21 22:12:05 +000010498 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10499 if (tabwidth < 6)
10500 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010501
Bram Moolenaar238a5642006-02-21 22:12:05 +000010502 attr = attr_nosel;
10503 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010504 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010505 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10506 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010507 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010508 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010509
Bram Moolenaar238a5642006-02-21 22:12:05 +000010510 if (tp->tp_topframe == topframe)
10511 attr = attr_sel;
10512 if (use_sep_chars && col > 0)
10513 screen_putchar('|', 0, col++, attr);
10514
10515 if (tp->tp_topframe != topframe)
10516 attr = attr_nosel;
10517
10518 screen_putchar(' ', 0, col++, attr);
10519
10520 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010521 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010522 cwp = curwin;
10523 wp = firstwin;
10524 }
10525 else
10526 {
10527 cwp = tp->tp_curwin;
10528 wp = tp->tp_firstwin;
10529 }
10530
10531 modified = FALSE;
10532 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10533 if (bufIsChanged(wp->w_buffer))
10534 modified = TRUE;
10535 if (modified || wincount > 1)
10536 {
10537 if (wincount > 1)
10538 {
10539 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010540 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010541 if (col + len >= Columns - 3)
10542 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010543 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010544#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010545 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010546#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010547 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010548#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010549 );
10550 col += len;
10551 }
10552 if (modified)
10553 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10554 screen_putchar(' ', 0, col++, attr);
10555 }
10556
10557 room = scol - col + tabwidth - 1;
10558 if (room > 0)
10559 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010560 /* Get buffer name in NameBuff[] */
10561 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010562 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010563 len = vim_strsize(NameBuff);
10564 p = NameBuff;
10565#ifdef FEAT_MBYTE
10566 if (has_mbyte)
10567 while (len > room)
10568 {
10569 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010570 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010571 }
10572 else
10573#endif
10574 if (len > room)
10575 {
10576 p += len - room;
10577 len = room;
10578 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010579 if (len > Columns - col - 1)
10580 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010581
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010582 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010583 col += len;
10584 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010585 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010586
10587 /* Store the tab page number in TabPageIdxs[], so that
10588 * jump_to_mouse() knows where each one is. */
10589 ++tabcount;
10590 while (scol < col)
10591 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010592 }
10593
Bram Moolenaar238a5642006-02-21 22:12:05 +000010594 if (use_sep_chars)
10595 c = '_';
10596 else
10597 c = ' ';
10598 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010599
10600 /* Put an "X" for closing the current tab if there are several. */
10601 if (first_tabpage->tp_next != NULL)
10602 {
10603 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10604 TabPageIdxs[Columns - 1] = -999;
10605 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010606 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010607
10608 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10609 * set. */
10610 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010611}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010612
10613/*
10614 * Get buffer name for "buf" into NameBuff[].
10615 * Takes care of special buffer names and translates special characters.
10616 */
10617 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010618get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010619{
10620 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010621 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010622 else
10623 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10624 trans_characters(NameBuff, MAXPATHL);
10625}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010626
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627/*
10628 * Get the character to use in a status line. Get its attributes in "*attr".
10629 */
10630 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010631fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632{
10633 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010634
10635#ifdef FEAT_TERMINAL
10636 if (bt_terminal(wp->w_buffer))
10637 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010638 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010639 {
10640 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010641 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010642 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010643 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010644 {
10645 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010646 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010647 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010648 }
10649 else
10650#endif
10651 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010653 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 fill = fill_stl;
10655 }
10656 else
10657 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010658 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 fill = fill_stlnc;
10660 }
10661 /* Use fill when there is highlighting, and highlighting of current
10662 * window differs, or the fillchars differ, or this is not the
10663 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010664 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010665 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 || (fill_stl != fill_stlnc)))
10667 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010668 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 return '^';
10670 return '=';
10671}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673/*
10674 * Get the character to use in a separator between vertically split windows.
10675 * Get its attributes in "*attr".
10676 */
10677 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010678fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010680 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 if (*attr == 0 && fill_vert == ' ')
10682 return '|';
10683 else
10684 return fill_vert;
10685}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686
10687/*
10688 * Return TRUE if redrawing should currently be done.
10689 */
10690 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010691redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010693#ifdef FEAT_EVAL
10694 if (disable_redraw_for_testing)
10695 return 0;
10696 else
10697#endif
10698 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10700}
10701
10702/*
10703 * Return TRUE if printing messages should currently be done.
10704 */
10705 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010706messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707{
10708 return (!(p_lz && char_avail() && !KeyTyped));
10709}
10710
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010711#ifdef FEAT_MENU
10712/*
10713 * Draw the window toolbar.
10714 */
10715 static void
10716redraw_win_toolbar(win_T *wp)
10717{
10718 vimmenu_T *menu;
10719 int item_idx = 0;
10720 int item_count = 0;
10721 int col = 0;
10722 int next_col;
10723 int off = (int)(current_ScreenLine - ScreenLines);
10724 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10725 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10726
10727 vim_free(wp->w_winbar_items);
10728 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10729 ++item_count;
10730 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10731 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10732
10733 /* TODO: use fewer spaces if there is not enough room */
10734 for (menu = wp->w_winbar->children;
10735 menu != NULL && col < W_WIDTH(wp); menu = menu->next)
10736 {
10737 space_to_screenline(off + col, fill_attr);
10738 if (++col >= W_WIDTH(wp))
10739 break;
10740 if (col > 1)
10741 {
10742 space_to_screenline(off + col, fill_attr);
10743 if (++col >= W_WIDTH(wp))
10744 break;
10745 }
10746
10747 wp->w_winbar_items[item_idx].wb_startcol = col;
10748 space_to_screenline(off + col, button_attr);
10749 if (++col >= W_WIDTH(wp))
10750 break;
10751
10752 next_col = text_to_screenline(wp, menu->name, col);
10753 while (col < next_col)
10754 {
10755 ScreenAttrs[off + col] = button_attr;
10756 ++col;
10757 }
10758 wp->w_winbar_items[item_idx].wb_endcol = col;
10759 wp->w_winbar_items[item_idx].wb_menu = menu;
10760 ++item_idx;
10761
10762 if (col >= W_WIDTH(wp))
10763 break;
10764 space_to_screenline(off + col, button_attr);
10765 ++col;
10766 }
10767 while (col < W_WIDTH(wp))
10768 {
10769 space_to_screenline(off + col, fill_attr);
10770 ++col;
10771 }
10772 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10773
10774 screen_line(wp->w_winrow, W_WINCOL(wp), (int)W_WIDTH(wp),
10775 (int)W_WIDTH(wp), FALSE);
10776}
10777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778/*
10779 * Show current status info in ruler and various other places
10780 * If always is FALSE, only show ruler if position has changed.
10781 */
10782 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010783showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784{
10785 if (!always && !redrawing())
10786 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010787#ifdef FEAT_INS_EXPAND
10788 if (pum_visible())
10789 {
10790 /* Don't redraw right now, do it later. */
10791 curwin->w_redr_status = TRUE;
10792 return;
10793 }
10794#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010795#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010796 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010797 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010798 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 else
10801#endif
10802#ifdef FEAT_CMDL_INFO
10803 win_redr_ruler(curwin, always);
10804#endif
10805
10806#ifdef FEAT_TITLE
10807 if (need_maketitle
10808# ifdef FEAT_STL_OPT
10809 || (p_icon && (stl_syntax & STL_IN_ICON))
10810 || (p_title && (stl_syntax & STL_IN_TITLE))
10811# endif
10812 )
10813 maketitle();
10814#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010815 /* Redraw the tab pages line if needed. */
10816 if (redraw_tabline)
10817 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818}
10819
10820#ifdef FEAT_CMDL_INFO
10821 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010822win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010824#define RULER_BUF_LEN 70
10825 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 int row;
10827 int fillchar;
10828 int attr;
10829 int empty_line = FALSE;
10830 colnr_T virtcol;
10831 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010832 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 int this_ru_col;
10835 int off = 0;
10836 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837
10838 /* If 'ruler' off or redrawing disabled, don't do anything */
10839 if (!p_ru)
10840 return;
10841
10842 /*
10843 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10844 * after deleting lines, before cursor.lnum is corrected.
10845 */
10846 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10847 return;
10848
10849#ifdef FEAT_INS_EXPAND
10850 /* Don't draw the ruler while doing insert-completion, it might overwrite
10851 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 if (edit_submode != NULL)
10854 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010855 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10856 if (pum_visible())
10857 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858#endif
10859
10860#ifdef FEAT_STL_OPT
10861 if (*p_ruf)
10862 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010863 int save_called_emsg = called_emsg;
10864
10865 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010867 if (called_emsg)
10868 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010869 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010870 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 return;
10872 }
10873#endif
10874
10875 /*
10876 * Check if not in Insert mode and the line is empty (will show "0-1").
10877 */
10878 if (!(State & INSERT)
10879 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10880 empty_line = TRUE;
10881
10882 /*
10883 * Only draw the ruler when something changed.
10884 */
10885 validate_virtcol_win(wp);
10886 if ( redraw_cmdline
10887 || always
10888 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10889 || wp->w_cursor.col != wp->w_ru_cursor.col
10890 || wp->w_virtcol != wp->w_ru_virtcol
10891#ifdef FEAT_VIRTUALEDIT
10892 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10893#endif
10894 || wp->w_topline != wp->w_ru_topline
10895 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10896#ifdef FEAT_DIFF
10897 || wp->w_topfill != wp->w_ru_topfill
10898#endif
10899 || empty_line != wp->w_ru_empty)
10900 {
10901 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 if (wp->w_status_height)
10903 {
10904 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010905 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 off = W_WINCOL(wp);
10907 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 }
10909 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 {
10911 row = Rows - 1;
10912 fillchar = ' ';
10913 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 width = Columns;
10915 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 }
10917
10918 /* In list mode virtcol needs to be recomputed */
10919 virtcol = wp->w_virtcol;
10920 if (wp->w_p_list && lcs_tab1 == NUL)
10921 {
10922 wp->w_p_list = FALSE;
10923 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10924 wp->w_p_list = TRUE;
10925 }
10926
10927 /*
10928 * Some sprintfs return the length, some return a pointer.
10929 * To avoid portability problems we use strlen() here.
10930 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010931 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10933 ? 0L
10934 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010935 len = STRLEN(buffer);
10936 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10938 (int)virtcol + 1);
10939
10940 /*
10941 * Add a "50%" if there is room for it.
10942 * On the last line, don't print in the last column (scrolls the
10943 * screen up on some terminals).
10944 */
10945 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010946 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 this_ru_col = ru_col - (Columns - width);
10951 if (this_ru_col < 0)
10952 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 /* Never use more than half the window/screen width, leave the other
10954 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010955 if (this_ru_col < (width + 1) / 2)
10956 this_ru_col = (width + 1) / 2;
10957 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010959 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010960 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 {
10962#ifdef FEAT_MBYTE
10963 if (has_mbyte)
10964 i += (*mb_char2bytes)(fillchar, buffer + i);
10965 else
10966#endif
10967 buffer[i++] = fillchar;
10968 ++o;
10969 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010970 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 }
10972 /* Truncate at window boundary. */
10973#ifdef FEAT_MBYTE
10974 if (has_mbyte)
10975 {
10976 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010977 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 {
10979 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010980 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 {
10982 buffer[i] = NUL;
10983 break;
10984 }
10985 }
10986 }
10987 else
10988#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010989 if (this_ru_col + (int)STRLEN(buffer) > width)
10990 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991
Bram Moolenaar4033c552017-09-16 20:54:51 +020010992 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 i = redraw_cmdline;
10994 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010995 this_ru_col + off + (int)STRLEN(buffer),
10996 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 fillchar, fillchar, attr);
10998 /* don't redraw the cmdline because of showing the ruler */
10999 redraw_cmdline = i;
11000 wp->w_ru_cursor = wp->w_cursor;
11001 wp->w_ru_virtcol = wp->w_virtcol;
11002 wp->w_ru_empty = empty_line;
11003 wp->w_ru_topline = wp->w_topline;
11004 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11005#ifdef FEAT_DIFF
11006 wp->w_ru_topfill = wp->w_topfill;
11007#endif
11008 }
11009}
11010#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011011
11012#if defined(FEAT_LINEBREAK) || defined(PROTO)
11013/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011014 * Return the width of the 'number' and 'relativenumber' column.
11015 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011016 * Otherwise it depends on 'numberwidth' and the line count.
11017 */
11018 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011019number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011020{
11021 int n;
11022 linenr_T lnum;
11023
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011024 if (wp->w_p_rnu && !wp->w_p_nu)
11025 /* cursor line shows "0" */
11026 lnum = wp->w_height;
11027 else
11028 /* cursor line shows absolute line number */
11029 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011030
Bram Moolenaar6b314672015-03-20 15:42:10 +010011031 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011032 return wp->w_nrwidth_width;
11033 wp->w_nrwidth_line_count = lnum;
11034
11035 n = 0;
11036 do
11037 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011038 lnum /= 10;
11039 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011040 } while (lnum > 0);
11041
11042 /* 'numberwidth' gives the minimal width plus one */
11043 if (n < wp->w_p_nuw - 1)
11044 n = wp->w_p_nuw - 1;
11045
11046 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011047 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011048 return n;
11049}
11050#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011051
11052/*
11053 * Return the current cursor column. This is the actual position on the
11054 * screen. First column is 0.
11055 */
11056 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011057screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011058{
11059 return screen_cur_col;
11060}
11061
11062/*
11063 * Return the current cursor row. This is the actual position on the screen.
11064 * First row is 0.
11065 */
11066 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011067screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011068{
11069 return screen_cur_row;
11070}