blob: c92b17ebb5c08e9d50dda61855a41bd878bac0b5 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200110#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
111static int text_to_screenline(win_T *wp, char_u *text, int col);
112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_FOLDING
114static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
117
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200118/* Flag that is set when drawing for a callback, not from the main command
119 * loop. */
120static int redrawing_for_callback = 0;
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122/*
123 * Buffer for one screen line (characters and attributes).
124 */
125static schar_T *current_ScreenLine;
126
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void win_update(win_T *wp);
128static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
131static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
132static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200134static int win_line(win_T *, linenr_T, int, int, int nochange);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200141# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void start_search_hl(void);
143static void end_search_hl(void);
144static void init_search_hl(win_T *wp);
145static void prepare_search_hl(win_T *wp, linenr_T lnum);
146static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
147static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screen_start_highlight(int attr);
150static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200155static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void lineinvalid(unsigned off, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void linecopy(int to, int from, win_T *wp);
158static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200159static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void win_rest_invalid(win_T *wp);
161static void msg_pos_mode(void);
162static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200164static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200166#ifdef FEAT_MENU
167static void redraw_win_toolbar(win_T *wp);
168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#endif
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176/* Ugly global: overrule attribute used by screen_char() */
177static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200179#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
180/* Can limit syntax highlight time to 'redrawtime'. */
181# define SYN_TIME_LIMIT 1
182#endif
183
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200184#ifdef FEAT_RIGHTLEFT
185# define HAS_RIGHTLEFT(x) x
186#else
187# define HAS_RIGHTLEFT(x) FALSE
188#endif
189
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190/*
191 * Redraw the current window later, with update_screen(type).
192 * Set must_redraw only if not already set to a higher value.
193 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
194 */
195 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100196redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197{
198 redraw_win_later(curwin, type);
199}
200
201 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100202redraw_win_later(
203 win_T *wp,
204 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200206 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
208 wp->w_redr_type = type;
209 if (type >= NOT_VALID)
210 wp->w_lines_valid = 0;
211 if (must_redraw < type) /* must_redraw is the maximum of all windows */
212 must_redraw = type;
213 }
214}
215
216/*
217 * Force a complete redraw later. Also resets the highlighting. To be used
218 * after executing a shell command that messes up the screen.
219 */
220 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100221redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000224#ifdef FEAT_GUI
225 if (gui.in_use)
226 /* Use a code that will reset gui.highlight_mask in
227 * gui_stop_highlight(). */
228 screen_attr = HL_ALL + 1;
229 else
230#endif
231 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200232 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233}
234
235/*
236 * Mark all windows to be redrawn later.
237 */
238 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100239redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240{
241 win_T *wp;
242
243 FOR_ALL_WINDOWS(wp)
244 {
245 redraw_win_later(wp, type);
246 }
247}
248
249/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000250 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 */
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 redraw_buf_later(curbuf, type);
256}
257
258 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100259redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 win_T *wp;
262
263 FOR_ALL_WINDOWS(wp)
264 {
265 if (wp->w_buffer == buf)
266 redraw_win_later(wp, type);
267 }
268}
269
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200270 void
271redraw_buf_and_status_later(buf_T *buf, int type)
272{
273 win_T *wp;
274
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200275#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200276 if (wild_menu_showing != 0)
277 /* Don't redraw while the command line completion is displayed, it
278 * would disappear. */
279 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200280#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281 FOR_ALL_WINDOWS(wp)
282 {
283 if (wp->w_buffer == buf)
284 {
285 redraw_win_later(wp, type);
286 wp->w_redr_status = TRUE;
287 }
288 }
289}
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200292 * Redraw as soon as possible. When the command line is not scrolled redraw
293 * right away and restore what was on the command line.
294 * Return a code indicating what happened.
295 */
296 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100297redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200298{
299 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200300 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200301 int r;
302 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 schar_T *screenline; /* copy from ScreenLines[] */
304 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200305#ifdef FEAT_MBYTE
306 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200307 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200309 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310#endif
311
312 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 return ret;
315
316 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 if (screenline == NULL || screenattr == NULL)
323 ret = 2;
324#ifdef FEAT_MBYTE
325 if (enc_utf8)
326 {
327 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenlineUC == NULL)
330 ret = 2;
331 for (i = 0; i < p_mco; ++i)
332 {
333 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200334 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (screenlineC[i] == NULL)
336 ret = 2;
337 }
338 }
339 if (enc_dbcs == DBCS_JPNU)
340 {
341 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200342 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenline2 == NULL)
344 ret = 2;
345 }
346#endif
347
348 if (ret != 2)
349 {
350 /* Save the text displayed in the command line area. */
351 for (r = 0; r < rows; ++r)
352 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200353 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200354 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200355 (size_t)cols * sizeof(schar_T));
356 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359#ifdef FEAT_MBYTE
360 if (enc_utf8)
361 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 mch_memmove(screenlineC[i] + r * cols,
367 ScreenLinesC[i] + LineOffset[cmdline_row + r],
368 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200369 }
370 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200373 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374#endif
375 }
376
377 update_screen(0);
378 ret = 3;
379
380 if (must_redraw == 0)
381 {
382 int off = (int)(current_ScreenLine - ScreenLines);
383
384 /* Restore the text displayed in the command line area. */
385 for (r = 0; r < rows; ++r)
386 {
387 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200388 screenline + r * cols,
389 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenattr + r * cols,
392 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393#ifdef FEAT_MBYTE
394 if (enc_utf8)
395 {
396 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenlineUC + r * cols,
398 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 for (i = 0; i < p_mco; ++i)
400 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200401 screenlineC[i] + r * cols,
402 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200403 }
404 if (enc_dbcs == DBCS_JPNU)
405 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200406 screenline2 + r * cols,
407 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200408#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200409 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 }
411 ret = 4;
412 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413 }
414
415 vim_free(screenline);
416 vim_free(screenattr);
417#ifdef FEAT_MBYTE
418 if (enc_utf8)
419 {
420 vim_free(screenlineUC);
421 for (i = 0; i < p_mco; ++i)
422 vim_free(screenlineC[i]);
423 }
424 if (enc_dbcs == DBCS_JPNU)
425 vim_free(screenline2);
426#endif
427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
435
436/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100437 * Invoked after an asynchronous callback is called.
438 * If an echo command was used the cursor needs to be put back where
439 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200440 * If "call_update_screen" is FALSE don't call update_screen() when at the
441 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100442 */
443 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200444redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200446 ++redrawing_for_callback;
447
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100448 if (State == HITRETURN || State == ASKMORE)
449 ; /* do nothing */
450 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200451 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200452 /* Redrawing only works when the screen didn't scroll. Don't clear
453 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454 if (msg_scrolled == 0
455#ifdef FEAT_WILDMENU
456 && wild_menu_showing == 0
457#endif
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200458 && call_update_screen)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200459 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200460 /* Redraw in the same position, so that the user can continue
461 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200462 redrawcmdline_ex(FALSE);
463 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200464 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100465 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 /* keep the command line if possible */
467 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100468 setcursor();
469 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470 cursor_on();
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 Moolenaare0de17d2017-09-24 16:24:34 +0200625 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
626 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 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);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200933 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200934#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100935 update_prepare();
936
Bram Moolenaar860cae12010-06-05 23:22:07 +0200937 row = 0;
938 for (j = 0; j < wp->w_lines_valid; ++j)
939 {
940 if (lnum == wp->w_lines[j].wl_lnum)
941 {
942 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200943# ifdef FEAT_SEARCH_EXTRA
944 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200945 start_search_hl();
946 prepare_search_hl(wp, lnum);
947# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200948 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200949# if defined(FEAT_SEARCH_EXTRA)
950 end_search_hl();
951# endif
952 break;
953 }
954 row += wp->w_lines[j].wl_size;
955 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100956
957 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200958
959#ifdef SYN_TIME_LIMIT
960 syn_set_timeout(NULL);
961#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200962 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200963 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964}
965#endif
966
967#if defined(FEAT_SIGNS) || defined(PROTO)
968 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100969update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
971 win_T *wp;
972 int doit = FALSE;
973
974# ifdef FEAT_FOLDING
975 win_foldinfo.fi_level = 0;
976# endif
977
978 /* update/delete a specific mark */
979 FOR_ALL_WINDOWS(wp)
980 {
981 if (buf != NULL && lnum > 0)
982 {
983 if (wp->w_buffer == buf && lnum >= wp->w_topline
984 && lnum < wp->w_botline)
985 {
986 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
987 wp->w_redraw_top = lnum;
988 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
989 wp->w_redraw_bot = lnum;
990 redraw_win_later(wp, VALID);
991 }
992 }
993 else
994 redraw_win_later(wp, VALID);
995 if (wp->w_redr_type != 0)
996 doit = TRUE;
997 }
998
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100999 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001000 * happening (recursive call), messages on the screen or still starting up.
1001 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001002 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001003 || State == ASKMORE || State == HITRETURN
1004 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001005#ifdef FEAT_GUI
1006 || gui.starting
1007#endif
1008 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 return;
1010
1011 /* update all windows that need updating */
1012 update_prepare();
1013
Bram Moolenaar29323592016-07-24 22:04:11 +02001014 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 {
1016 if (wp->w_redr_type != 0)
1017 win_update(wp);
1018 if (wp->w_redr_status)
1019 win_redr_status(wp);
1020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
1022 update_finish();
1023}
1024#endif
1025
1026
1027#if defined(FEAT_GUI) || defined(PROTO)
1028/*
1029 * Update a single window, its status line and maybe the command line msg.
1030 * Used for the GUI scrollbar.
1031 */
1032 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001033updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001035 /* return if already busy updating */
1036 if (updating_screen)
1037 return;
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 update_prepare();
1040
1041#ifdef FEAT_CLIPBOARD
1042 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001043 if (clip_star.available && clip_isautosel_star())
1044 clip_update_selection(&clip_star);
1045 if (clip_plus.available && clip_isautosel_plus())
1046 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001050
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001051 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001052 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001053 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 if (wp->w_redr_status
1056# ifdef FEAT_CMDL_INFO
1057 || p_ru
1058# endif
1059# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001060 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061# endif
1062 )
1063 win_redr_status(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
1065 update_finish();
1066}
1067#endif
1068
1069/*
1070 * Update a single window.
1071 *
1072 * This may cause the windows below it also to be redrawn (when clearing the
1073 * screen or scrolling lines).
1074 *
1075 * How the window is redrawn depends on wp->w_redr_type. Each type also
1076 * implies the one below it.
1077 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001078 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1080 * INVERTED redraw the changed part of the Visual area
1081 * INVERTED_ALL redraw the whole Visual area
1082 * VALID 1. scroll up/down to adjust for a changed w_topline
1083 * 2. update lines at the top when scrolled down
1084 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001085 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 * b_mod_top and b_mod_bot.
1087 * - if wp->w_redraw_top non-zero, redraw lines between
1088 * wp->w_redraw_top and wp->w_redr_bot.
1089 * - continue redrawing when syntax status is invalid.
1090 * 4. if scrolled up, update lines at the bottom.
1091 * This results in three areas that may need updating:
1092 * top: from first row to top_end (when scrolled down)
1093 * mid: from mid_start to mid_end (update inversion or changed text)
1094 * bot: from bot_start to last row (when scrolled up)
1095 */
1096 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001097win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098{
1099 buf_T *buf = wp->w_buffer;
1100 int type;
1101 int top_end = 0; /* Below last row of the top area that needs
1102 updating. 0 when no top area updating. */
1103 int mid_start = 999;/* first row of the mid area that needs
1104 updating. 999 when no mid area updating. */
1105 int mid_end = 0; /* Below last row of the mid area that needs
1106 updating. 0 when no mid area updating. */
1107 int bot_start = 999;/* first row of the bot area that needs
1108 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 int scrolled_down = FALSE; /* TRUE when scrolled down when
1110 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001112 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 int top_to_mod = FALSE; /* redraw above mod_top */
1114#endif
1115
1116 int row; /* current window row to display */
1117 linenr_T lnum; /* current buffer lnum to display */
1118 int idx; /* current index in w_lines[] */
1119 int srow; /* starting row of the current line */
1120
1121 int eof = FALSE; /* if TRUE, we hit the end of the file */
1122 int didline = FALSE; /* if TRUE, we finished the last line */
1123 int i;
1124 long j;
1125 static int recursive = FALSE; /* being called recursively */
1126 int old_botline = wp->w_botline;
1127#ifdef FEAT_FOLDING
1128 long fold_count;
1129#endif
1130#ifdef FEAT_SYN_HL
1131 /* remember what happened to the previous line, to know if
1132 * check_visual_highlight() can be used */
1133#define DID_NONE 1 /* didn't update a line */
1134#define DID_LINE 2 /* updated a normal line */
1135#define DID_FOLD 3 /* updated a folded line */
1136 int did_update = DID_NONE;
1137 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1138#endif
1139 linenr_T mod_top = 0;
1140 linenr_T mod_bot = 0;
1141#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1142 int save_got_int;
1143#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001144#ifdef SYN_TIME_LIMIT
1145 proftime_T syntax_tm;
1146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
1148 type = wp->w_redr_type;
1149
1150 if (type == NOT_VALID)
1151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 wp->w_lines_valid = 0;
1154 }
1155
1156 /* Window is zero-height: nothing to draw. */
1157 if (wp->w_height == 0)
1158 {
1159 wp->w_redr_type = 0;
1160 return;
1161 }
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 /* Window is zero-width: Only need to draw the separator. */
1164 if (wp->w_width == 0)
1165 {
1166 /* draw the vertical separator right of this window */
1167 draw_vsep_win(wp, 0);
1168 wp->w_redr_type = 0;
1169 return;
1170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001172#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001173 /* If this window contains a terminal, redraw works completely differently.
1174 */
1175 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001176 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001177 wp->w_redr_type = 0;
1178 return;
1179 }
1180#endif
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001183 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#endif
1185
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001186#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001187 /* Force redraw when width of 'number' or 'relativenumber' column
1188 * changes. */
1189 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001190 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001191 {
1192 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001193 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001194 }
1195 else
1196#endif
1197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1199 {
1200 /*
1201 * When there are both inserted/deleted lines and specific lines to be
1202 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1203 * everything (only happens when redrawing is off for while).
1204 */
1205 type = NOT_VALID;
1206 }
1207 else
1208 {
1209 /*
1210 * Set mod_top to the first line that needs displaying because of
1211 * changes. Set mod_bot to the first line after the changes.
1212 */
1213 mod_top = wp->w_redraw_top;
1214 if (wp->w_redraw_bot != 0)
1215 mod_bot = wp->w_redraw_bot + 1;
1216 else
1217 mod_bot = 0;
1218 wp->w_redraw_top = 0; /* reset for next time */
1219 wp->w_redraw_bot = 0;
1220 if (buf->b_mod_set)
1221 {
1222 if (mod_top == 0 || mod_top > buf->b_mod_top)
1223 {
1224 mod_top = buf->b_mod_top;
1225#ifdef FEAT_SYN_HL
1226 /* Need to redraw lines above the change that may be included
1227 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001228 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001230 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 if (mod_top < 1)
1232 mod_top = 1;
1233 }
1234#endif
1235 }
1236 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1237 mod_bot = buf->b_mod_bot;
1238
1239#ifdef FEAT_SEARCH_EXTRA
1240 /* When 'hlsearch' is on and using a multi-line search pattern, a
1241 * change in one line may make the Search highlighting in a
1242 * previous line invalid. Simple solution: redraw all visible
1243 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001244 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001246 if (search_hl.rm.regprog != NULL
1247 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001249 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001250 {
1251 cur = wp->w_match_head;
1252 while (cur != NULL)
1253 {
1254 if (cur->match.regprog != NULL
1255 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001256 {
1257 top_to_mod = TRUE;
1258 break;
1259 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001260 cur = cur->next;
1261 }
1262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263#endif
1264 }
1265#ifdef FEAT_FOLDING
1266 if (mod_top != 0 && hasAnyFolding(wp))
1267 {
1268 linenr_T lnumt, lnumb;
1269
1270 /*
1271 * A change in a line can cause lines above it to become folded or
1272 * unfolded. Find the top most buffer line that may be affected.
1273 * If the line was previously folded and displayed, get the first
1274 * line of that fold. If the line is folded now, get the first
1275 * folded line. Use the minimum of these two.
1276 */
1277
1278 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1279 * the line below it. If there is no valid entry, use w_topline.
1280 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1281 * to this line. If there is no valid entry, use MAXLNUM. */
1282 lnumt = wp->w_topline;
1283 lnumb = MAXLNUM;
1284 for (i = 0; i < wp->w_lines_valid; ++i)
1285 if (wp->w_lines[i].wl_valid)
1286 {
1287 if (wp->w_lines[i].wl_lastlnum < mod_top)
1288 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1289 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1290 {
1291 lnumb = wp->w_lines[i].wl_lnum;
1292 /* When there is a fold column it might need updating
1293 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001294 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 ++lnumb;
1296 }
1297 }
1298
1299 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1300 if (mod_top > lnumt)
1301 mod_top = lnumt;
1302
1303 /* Now do the same for the bottom line (one above mod_bot). */
1304 --mod_bot;
1305 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1306 ++mod_bot;
1307 if (mod_bot < lnumb)
1308 mod_bot = lnumb;
1309 }
1310#endif
1311
1312 /* When a change starts above w_topline and the end is below
1313 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001314 * If the end of the change is above w_topline: do like no change was
1315 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (mod_top != 0 && mod_top < wp->w_topline)
1317 {
1318 if (mod_bot > wp->w_topline)
1319 mod_top = wp->w_topline;
1320#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001321 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 top_end = 1;
1323#endif
1324 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001325
1326 /* When line numbers are displayed need to redraw all lines below
1327 * inserted/deleted lines. */
1328 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1329 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 }
1331
1332 /*
1333 * When only displaying the lines at the top, set top_end. Used when
1334 * window has scrolled down for msg_scrolled.
1335 */
1336 if (type == REDRAW_TOP)
1337 {
1338 j = 0;
1339 for (i = 0; i < wp->w_lines_valid; ++i)
1340 {
1341 j += wp->w_lines[i].wl_size;
1342 if (j >= wp->w_upd_rows)
1343 {
1344 top_end = j;
1345 break;
1346 }
1347 }
1348 if (top_end == 0)
1349 /* not found (cannot happen?): redraw everything */
1350 type = NOT_VALID;
1351 else
1352 /* top area defined, the rest is VALID */
1353 type = VALID;
1354 }
1355
Bram Moolenaar367329b2007-08-30 11:53:22 +00001356 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001357 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1358 * non-zero and thus not FALSE) will indicate that screenclear() was not
1359 * called. */
1360 if (screen_cleared)
1361 screen_cleared = MAYBE;
1362
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 /*
1364 * If there are no changes on the screen that require a complete redraw,
1365 * handle three cases:
1366 * 1: we are off the top of the screen by a few lines: scroll down
1367 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1368 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1369 * w_lines[] that needs updating.
1370 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001371 if ((type == VALID || type == SOME_VALID
1372 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373#ifdef FEAT_DIFF
1374 && !wp->w_botfill && !wp->w_old_botfill
1375#endif
1376 )
1377 {
1378 if (mod_top != 0 && wp->w_topline == mod_top)
1379 {
1380 /*
1381 * w_topline is the first changed line, the scrolling will be done
1382 * further down.
1383 */
1384 }
1385 else if (wp->w_lines[0].wl_valid
1386 && (wp->w_topline < wp->w_lines[0].wl_lnum
1387#ifdef FEAT_DIFF
1388 || (wp->w_topline == wp->w_lines[0].wl_lnum
1389 && wp->w_topfill > wp->w_old_topfill)
1390#endif
1391 ))
1392 {
1393 /*
1394 * New topline is above old topline: May scroll down.
1395 */
1396#ifdef FEAT_FOLDING
1397 if (hasAnyFolding(wp))
1398 {
1399 linenr_T ln;
1400
1401 /* count the number of lines we are off, counting a sequence
1402 * of folded lines as one */
1403 j = 0;
1404 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1405 {
1406 ++j;
1407 if (j >= wp->w_height - 2)
1408 break;
1409 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1410 }
1411 }
1412 else
1413#endif
1414 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1415 if (j < wp->w_height - 2) /* not too far off */
1416 {
1417 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1418#ifdef FEAT_DIFF
1419 /* insert extra lines for previously invisible filler lines */
1420 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1421 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1422 - wp->w_old_topfill;
1423#endif
1424 if (i < wp->w_height - 2) /* less than a screen off */
1425 {
1426 /*
1427 * Try to insert the correct number of lines.
1428 * If not the last window, delete the lines at the bottom.
1429 * win_ins_lines may fail when the terminal can't do it.
1430 */
1431 if (i > 0)
1432 check_for_delay(FALSE);
1433 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1434 {
1435 if (wp->w_lines_valid != 0)
1436 {
1437 /* Need to update rows that are new, stop at the
1438 * first one that scrolled down. */
1439 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
1442 /* Move the entries that were scrolled, disable
1443 * the entries for the lines to be redrawn. */
1444 if ((wp->w_lines_valid += j) > wp->w_height)
1445 wp->w_lines_valid = wp->w_height;
1446 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1447 wp->w_lines[idx] = wp->w_lines[idx - j];
1448 while (idx >= 0)
1449 wp->w_lines[idx--].wl_valid = FALSE;
1450 }
1451 }
1452 else
1453 mid_start = 0; /* redraw all lines */
1454 }
1455 else
1456 mid_start = 0; /* redraw all lines */
1457 }
1458 else
1459 mid_start = 0; /* redraw all lines */
1460 }
1461 else
1462 {
1463 /*
1464 * New topline is at or below old topline: May scroll up.
1465 * When topline didn't change, find first entry in w_lines[] that
1466 * needs updating.
1467 */
1468
1469 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1470 j = -1;
1471 row = 0;
1472 for (i = 0; i < wp->w_lines_valid; i++)
1473 {
1474 if (wp->w_lines[i].wl_valid
1475 && wp->w_lines[i].wl_lnum == wp->w_topline)
1476 {
1477 j = i;
1478 break;
1479 }
1480 row += wp->w_lines[i].wl_size;
1481 }
1482 if (j == -1)
1483 {
1484 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1485 * lines */
1486 mid_start = 0;
1487 }
1488 else
1489 {
1490 /*
1491 * Try to delete the correct number of lines.
1492 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1493 */
1494#ifdef FEAT_DIFF
1495 /* If the topline didn't change, delete old filler lines,
1496 * otherwise delete filler lines of the new topline... */
1497 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1498 row += wp->w_old_topfill;
1499 else
1500 row += diff_check_fill(wp, wp->w_topline);
1501 /* ... but don't delete new filler lines. */
1502 row -= wp->w_topfill;
1503#endif
1504 if (row > 0)
1505 {
1506 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001507 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1508 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 bot_start = wp->w_height - row;
1510 else
1511 mid_start = 0; /* redraw all lines */
1512 }
1513 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1514 {
1515 /*
1516 * Skip the lines (below the deleted lines) that are still
1517 * valid and don't need redrawing. Copy their info
1518 * upwards, to compensate for the deleted lines. Set
1519 * bot_start to the first row that needs redrawing.
1520 */
1521 bot_start = 0;
1522 idx = 0;
1523 for (;;)
1524 {
1525 wp->w_lines[idx] = wp->w_lines[j];
1526 /* stop at line that didn't fit, unless it is still
1527 * valid (no lines deleted) */
1528 if (row > 0 && bot_start + row
1529 + (int)wp->w_lines[j].wl_size > wp->w_height)
1530 {
1531 wp->w_lines_valid = idx + 1;
1532 break;
1533 }
1534 bot_start += wp->w_lines[idx++].wl_size;
1535
1536 /* stop at the last valid entry in w_lines[].wl_size */
1537 if (++j >= wp->w_lines_valid)
1538 {
1539 wp->w_lines_valid = idx;
1540 break;
1541 }
1542 }
1543#ifdef FEAT_DIFF
1544 /* Correct the first entry for filler lines at the top
1545 * when it won't get updated below. */
1546 if (wp->w_p_diff && bot_start > 0)
1547 wp->w_lines[0].wl_size =
1548 plines_win_nofill(wp, wp->w_topline, TRUE)
1549 + wp->w_topfill;
1550#endif
1551 }
1552 }
1553 }
1554
1555 /* When starting redraw in the first line, redraw all lines. When
1556 * there is only one window it's probably faster to clear the screen
1557 * first. */
1558 if (mid_start == 0)
1559 {
1560 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001561 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001562 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001563 /* Clear the screen when it was not done by win_del_lines() or
1564 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1565 * then. */
1566 if (screen_cleared != TRUE)
1567 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001568 /* The screen was cleared, redraw the tab pages line. */
1569 if (redraw_tabline)
1570 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001573
1574 /* When win_del_lines() or win_ins_lines() caused the screen to be
1575 * cleared (only happens for the first window) or when screenclear()
1576 * was called directly above, "must_redraw" will have been set to
1577 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1578 if (screen_cleared == TRUE)
1579 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
1581 else
1582 {
1583 /* Not VALID or INVERTED: redraw all lines. */
1584 mid_start = 0;
1585 mid_end = wp->w_height;
1586 }
1587
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001588 if (type == SOME_VALID)
1589 {
1590 /* SOME_VALID: redraw all lines. */
1591 mid_start = 0;
1592 mid_end = wp->w_height;
1593 type = NOT_VALID;
1594 }
1595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 /* check if we are updating or removing the inverted part */
1597 if ((VIsual_active && buf == curwin->w_buffer)
1598 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1599 {
1600 linenr_T from, to;
1601
1602 if (VIsual_active)
1603 {
1604 if (VIsual_active
1605 && (VIsual_mode != wp->w_old_visual_mode
1606 || type == INVERTED_ALL))
1607 {
1608 /*
1609 * If the type of Visual selection changed, redraw the whole
1610 * selection. Also when the ownership of the X selection is
1611 * gained or lost.
1612 */
1613 if (curwin->w_cursor.lnum < VIsual.lnum)
1614 {
1615 from = curwin->w_cursor.lnum;
1616 to = VIsual.lnum;
1617 }
1618 else
1619 {
1620 from = VIsual.lnum;
1621 to = curwin->w_cursor.lnum;
1622 }
1623 /* redraw more when the cursor moved as well */
1624 if (wp->w_old_cursor_lnum < from)
1625 from = wp->w_old_cursor_lnum;
1626 if (wp->w_old_cursor_lnum > to)
1627 to = wp->w_old_cursor_lnum;
1628 if (wp->w_old_visual_lnum < from)
1629 from = wp->w_old_visual_lnum;
1630 if (wp->w_old_visual_lnum > to)
1631 to = wp->w_old_visual_lnum;
1632 }
1633 else
1634 {
1635 /*
1636 * Find the line numbers that need to be updated: The lines
1637 * between the old cursor position and the current cursor
1638 * position. Also check if the Visual position changed.
1639 */
1640 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1641 {
1642 from = curwin->w_cursor.lnum;
1643 to = wp->w_old_cursor_lnum;
1644 }
1645 else
1646 {
1647 from = wp->w_old_cursor_lnum;
1648 to = curwin->w_cursor.lnum;
1649 if (from == 0) /* Visual mode just started */
1650 from = to;
1651 }
1652
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001653 if (VIsual.lnum != wp->w_old_visual_lnum
1654 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 {
1656 if (wp->w_old_visual_lnum < from
1657 && wp->w_old_visual_lnum != 0)
1658 from = wp->w_old_visual_lnum;
1659 if (wp->w_old_visual_lnum > to)
1660 to = wp->w_old_visual_lnum;
1661 if (VIsual.lnum < from)
1662 from = VIsual.lnum;
1663 if (VIsual.lnum > to)
1664 to = VIsual.lnum;
1665 }
1666 }
1667
1668 /*
1669 * If in block mode and changed column or curwin->w_curswant:
1670 * update all lines.
1671 * First compute the actual start and end column.
1672 */
1673 if (VIsual_mode == Ctrl_V)
1674 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001675 colnr_T fromc, toc;
1676#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1677 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaar404406a2014-10-09 13:24:43 +02001679 if (curwin->w_p_lbr)
1680 ve_flags = VE_ALL;
1681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001683#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1684 ve_flags = save_ve_flags;
1685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 ++toc;
1687 if (curwin->w_curswant == MAXCOL)
1688 toc = MAXCOL;
1689
1690 if (fromc != wp->w_old_cursor_fcol
1691 || toc != wp->w_old_cursor_lcol)
1692 {
1693 if (from > VIsual.lnum)
1694 from = VIsual.lnum;
1695 if (to < VIsual.lnum)
1696 to = VIsual.lnum;
1697 }
1698 wp->w_old_cursor_fcol = fromc;
1699 wp->w_old_cursor_lcol = toc;
1700 }
1701 }
1702 else
1703 {
1704 /* Use the line numbers of the old Visual area. */
1705 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1706 {
1707 from = wp->w_old_cursor_lnum;
1708 to = wp->w_old_visual_lnum;
1709 }
1710 else
1711 {
1712 from = wp->w_old_visual_lnum;
1713 to = wp->w_old_cursor_lnum;
1714 }
1715 }
1716
1717 /*
1718 * There is no need to update lines above the top of the window.
1719 */
1720 if (from < wp->w_topline)
1721 from = wp->w_topline;
1722
1723 /*
1724 * If we know the value of w_botline, use it to restrict the update to
1725 * the lines that are visible in the window.
1726 */
1727 if (wp->w_valid & VALID_BOTLINE)
1728 {
1729 if (from >= wp->w_botline)
1730 from = wp->w_botline - 1;
1731 if (to >= wp->w_botline)
1732 to = wp->w_botline - 1;
1733 }
1734
1735 /*
1736 * Find the minimal part to be updated.
1737 * Watch out for scrolling that made entries in w_lines[] invalid.
1738 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1739 * top_end; need to redraw from top_end to the "to" line.
1740 * A middle mouse click with a Visual selection may change the text
1741 * above the Visual area and reset wl_valid, do count these for
1742 * mid_end (in srow).
1743 */
1744 if (mid_start > 0)
1745 {
1746 lnum = wp->w_topline;
1747 idx = 0;
1748 srow = 0;
1749 if (scrolled_down)
1750 mid_start = top_end;
1751 else
1752 mid_start = 0;
1753 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1754 {
1755 if (wp->w_lines[idx].wl_valid)
1756 mid_start += wp->w_lines[idx].wl_size;
1757 else if (!scrolled_down)
1758 srow += wp->w_lines[idx].wl_size;
1759 ++idx;
1760# ifdef FEAT_FOLDING
1761 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1762 lnum = wp->w_lines[idx].wl_lnum;
1763 else
1764# endif
1765 ++lnum;
1766 }
1767 srow += mid_start;
1768 mid_end = wp->w_height;
1769 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1770 {
1771 if (wp->w_lines[idx].wl_valid
1772 && wp->w_lines[idx].wl_lnum >= to + 1)
1773 {
1774 /* Only update until first row of this line */
1775 mid_end = srow;
1776 break;
1777 }
1778 srow += wp->w_lines[idx].wl_size;
1779 }
1780 }
1781 }
1782
1783 if (VIsual_active && buf == curwin->w_buffer)
1784 {
1785 wp->w_old_visual_mode = VIsual_mode;
1786 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1787 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001788 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 wp->w_old_curswant = curwin->w_curswant;
1790 }
1791 else
1792 {
1793 wp->w_old_visual_mode = 0;
1794 wp->w_old_cursor_lnum = 0;
1795 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001796 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
1799#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1800 /* reset got_int, otherwise regexp won't work */
1801 save_got_int = got_int;
1802 got_int = 0;
1803#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001804#ifdef SYN_TIME_LIMIT
1805 /* Set the time limit to 'redrawtime'. */
1806 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001807 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001808#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 Moolenaarf3d769a2017-09-22 13:44:56 +02002112 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
2114#ifdef FEAT_FOLDING
2115 wp->w_lines[idx].wl_folded = FALSE;
2116 wp->w_lines[idx].wl_lastlnum = lnum;
2117#endif
2118#ifdef FEAT_SYN_HL
2119 did_update = DID_LINE;
2120 syntax_last_parsed = lnum;
2121#endif
2122 }
2123
2124 wp->w_lines[idx].wl_lnum = lnum;
2125 wp->w_lines[idx].wl_valid = TRUE;
2126 if (row > wp->w_height) /* past end of screen */
2127 {
2128 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002129 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2131 ++idx;
2132 break;
2133 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002134 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 wp->w_lines[idx].wl_size = row - srow;
2136 ++idx;
2137#ifdef FEAT_FOLDING
2138 lnum += fold_count + 1;
2139#else
2140 ++lnum;
2141#endif
2142 }
2143 else
2144 {
2145 /* This line does not need updating, advance to the next one */
2146 row += wp->w_lines[idx++].wl_size;
2147 if (row > wp->w_height) /* past end of screen */
2148 break;
2149#ifdef FEAT_FOLDING
2150 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2151#else
2152 ++lnum;
2153#endif
2154#ifdef FEAT_SYN_HL
2155 did_update = DID_NONE;
2156#endif
2157 }
2158
2159 if (lnum > buf->b_ml.ml_line_count)
2160 {
2161 eof = TRUE;
2162 break;
2163 }
2164 }
2165 /*
2166 * End of loop over all window lines.
2167 */
2168
2169
2170 if (idx > wp->w_lines_valid)
2171 wp->w_lines_valid = idx;
2172
2173#ifdef FEAT_SYN_HL
2174 /*
2175 * Let the syntax stuff know we stop parsing here.
2176 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002177 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 syntax_end_parsing(syntax_last_parsed + 1);
2179#endif
2180
2181 /*
2182 * If we didn't hit the end of the file, and we didn't finish the last
2183 * line we were working on, then the line didn't fit.
2184 */
2185 wp->w_empty_rows = 0;
2186#ifdef FEAT_DIFF
2187 wp->w_filler_rows = 0;
2188#endif
2189 if (!eof && !didline)
2190 {
2191 if (lnum == wp->w_topline)
2192 {
2193 /*
2194 * Single line that does not fit!
2195 * Don't overwrite it, it can be edited.
2196 */
2197 wp->w_botline = lnum + 1;
2198 }
2199#ifdef FEAT_DIFF
2200 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2201 {
2202 /* Window ends in filler lines. */
2203 wp->w_botline = lnum;
2204 wp->w_filler_rows = wp->w_height - srow;
2205 }
2206#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002207 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2208 {
2209 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2210
2211 /*
2212 * Last line isn't finished: Display "@@@" in the last screen line.
2213 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002214 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002215 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002216 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002217 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002218 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002219 set_empty_rows(wp, srow);
2220 wp->w_botline = lnum;
2221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2223 {
2224 /*
2225 * Last line isn't finished: Display "@@@" at the end.
2226 */
2227 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2228 W_WINROW(wp) + wp->w_height,
2229 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002230 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 set_empty_rows(wp, srow);
2232 wp->w_botline = lnum;
2233 }
2234 else
2235 {
2236 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2237 wp->w_botline = lnum;
2238 }
2239 }
2240 else
2241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (eof) /* we hit the end of the file */
2244 {
2245 wp->w_botline = buf->b_ml.ml_line_count + 1;
2246#ifdef FEAT_DIFF
2247 j = diff_check_fill(wp, wp->w_botline);
2248 if (j > 0 && !wp->w_botfill)
2249 {
2250 /*
2251 * Display filler lines at the end of the file
2252 */
2253 if (char2cells(fill_diff) > 1)
2254 i = '-';
2255 else
2256 i = fill_diff;
2257 if (row + j > wp->w_height)
2258 j = wp->w_height - row;
2259 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2260 row += j;
2261 }
2262#endif
2263 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002264 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 wp->w_botline = lnum;
2266
2267 /* make sure the rest of the screen is blank */
2268 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002269 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002272#ifdef SYN_TIME_LIMIT
2273 syn_set_timeout(NULL);
2274#endif
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 /* Reset the type of redrawing required, the window has been updated. */
2277 wp->w_redr_type = 0;
2278#ifdef FEAT_DIFF
2279 wp->w_old_topfill = wp->w_topfill;
2280 wp->w_old_botfill = wp->w_botfill;
2281#endif
2282
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002283 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 /*
2286 * There is a trick with w_botline. If we invalidate it on each
2287 * change that might modify it, this will cause a lot of expensive
2288 * calls to plines() in update_topline() each time. Therefore the
2289 * value of w_botline is often approximated, and this value is used to
2290 * compute the value of w_topline. If the value of w_botline was
2291 * wrong, check that the value of w_topline is correct (cursor is on
2292 * the visible part of the text). If it's not, we need to redraw
2293 * again. Mostly this just means scrolling up a few lines, so it
2294 * doesn't look too bad. Only do this for the current window (where
2295 * changes are relevant).
2296 */
2297 wp->w_valid |= VALID_BOTLINE;
2298 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2299 {
2300 recursive = TRUE;
2301 curwin->w_valid &= ~VALID_TOPLINE;
2302 update_topline(); /* may invalidate w_botline again */
2303 if (must_redraw != 0)
2304 {
2305 /* Don't update for changes in buffer again. */
2306 i = curbuf->b_mod_set;
2307 curbuf->b_mod_set = FALSE;
2308 win_update(curwin);
2309 must_redraw = 0;
2310 curbuf->b_mod_set = i;
2311 }
2312 recursive = FALSE;
2313 }
2314 }
2315
2316#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2317 /* restore got_int, unless CTRL-C was hit while redrawing */
2318 if (!got_int)
2319 got_int = save_got_int;
2320#endif
2321}
2322
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323/*
2324 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2325 * as the filler character.
2326 */
2327 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002328win_draw_end(
2329 win_T *wp,
2330 int c1,
2331 int c2,
2332 int row,
2333 int endrow,
2334 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2337 int n = 0;
2338# define FDC_OFF n
2339#else
2340# define FDC_OFF 0
2341#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002342#ifdef FEAT_FOLDING
2343 int fdc = compute_foldcolumn(wp, 0);
2344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
2346#ifdef FEAT_RIGHTLEFT
2347 if (wp->w_p_rl)
2348 {
2349 /* No check for cmdline window: should never be right-left. */
2350# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002351 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353 if (n > 0)
2354 {
2355 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002356 if (n > wp->w_width)
2357 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2359 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002360 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
2362# endif
2363# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002364 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
2366 int nn = n + 2;
2367
2368 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002369 if (nn > wp->w_width)
2370 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2372 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002373 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 n = nn;
2375 }
2376# endif
2377 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002378 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002379 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2381 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002382 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 }
2384 else
2385#endif
2386 {
2387#ifdef FEAT_CMDWIN
2388 if (cmdwin_type != 0 && wp == curwin)
2389 {
2390 /* draw the cmdline character in the leftmost column */
2391 n = 1;
2392 if (n > wp->w_width)
2393 n = wp->w_width;
2394 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002395 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002396 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
2398#endif
2399#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002400 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002402 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002405 if (nn > wp->w_width)
2406 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002408 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002409 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 n = nn;
2411 }
2412#endif
2413#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002414 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
2416 int nn = n + 2;
2417
2418 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002419 if (nn > wp->w_width)
2420 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002422 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002423 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 n = nn;
2425 }
2426#endif
2427 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002428 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002429 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 }
2431 set_empty_rows(wp, row);
2432}
2433
Bram Moolenaar1a384422010-07-14 19:53:30 +02002434#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002435static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002436
2437/*
2438 * Advance **color_cols and return TRUE when there are columns to draw.
2439 */
2440 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002441advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002442{
2443 while (**color_cols >= 0 && vcol > **color_cols)
2444 ++*color_cols;
2445 return (**color_cols >= 0);
2446}
2447#endif
2448
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002449#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2450/*
2451 * Copy "text" to ScreenLines using "attr".
2452 * Returns the next screen column.
2453 */
2454 static int
2455text_to_screenline(win_T *wp, char_u *text, int col)
2456{
2457 int off = (int)(current_ScreenLine - ScreenLines);
2458
2459#ifdef FEAT_MBYTE
2460 if (has_mbyte)
2461 {
2462 int cells;
2463 int u8c, u8cc[MAX_MCO];
2464 int i;
2465 int idx;
2466 int c_len;
2467 char_u *p;
2468# ifdef FEAT_ARABIC
2469 int prev_c = 0; /* previous Arabic character */
2470 int prev_c1 = 0; /* first composing char for prev_c */
2471# endif
2472
2473# ifdef FEAT_RIGHTLEFT
2474 if (wp->w_p_rl)
2475 idx = off;
2476 else
2477# endif
2478 idx = off + col;
2479
2480 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2481 for (p = text; *p != NUL; )
2482 {
2483 cells = (*mb_ptr2cells)(p);
2484 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002485 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002486# ifdef FEAT_RIGHTLEFT
2487 - (wp->w_p_rl ? col : 0)
2488# endif
2489 )
2490 break;
2491 ScreenLines[idx] = *p;
2492 if (enc_utf8)
2493 {
2494 u8c = utfc_ptr2char(p, u8cc);
2495 if (*p < 0x80 && u8cc[0] == 0)
2496 {
2497 ScreenLinesUC[idx] = 0;
2498#ifdef FEAT_ARABIC
2499 prev_c = u8c;
2500#endif
2501 }
2502 else
2503 {
2504#ifdef FEAT_ARABIC
2505 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2506 {
2507 /* Do Arabic shaping. */
2508 int pc, pc1, nc;
2509 int pcc[MAX_MCO];
2510 int firstbyte = *p;
2511
2512 /* The idea of what is the previous and next
2513 * character depends on 'rightleft'. */
2514 if (wp->w_p_rl)
2515 {
2516 pc = prev_c;
2517 pc1 = prev_c1;
2518 nc = utf_ptr2char(p + c_len);
2519 prev_c1 = u8cc[0];
2520 }
2521 else
2522 {
2523 pc = utfc_ptr2char(p + c_len, pcc);
2524 nc = prev_c;
2525 pc1 = pcc[0];
2526 }
2527 prev_c = u8c;
2528
2529 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2530 pc, pc1, nc);
2531 ScreenLines[idx] = firstbyte;
2532 }
2533 else
2534 prev_c = u8c;
2535#endif
2536 /* Non-BMP character: display as ? or fullwidth ?. */
2537#ifdef UNICODE16
2538 if (u8c >= 0x10000)
2539 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2540 else
2541#endif
2542 ScreenLinesUC[idx] = u8c;
2543 for (i = 0; i < Screen_mco; ++i)
2544 {
2545 ScreenLinesC[i][idx] = u8cc[i];
2546 if (u8cc[i] == 0)
2547 break;
2548 }
2549 }
2550 if (cells > 1)
2551 ScreenLines[idx + 1] = 0;
2552 }
2553 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2554 /* double-byte single width character */
2555 ScreenLines2[idx] = p[1];
2556 else if (cells > 1)
2557 /* double-width character */
2558 ScreenLines[idx + 1] = p[1];
2559 col += cells;
2560 idx += cells;
2561 p += c_len;
2562 }
2563 }
2564 else
2565#endif
2566 {
2567 int len = (int)STRLEN(text);
2568
Bram Moolenaar02631462017-09-22 15:20:32 +02002569 if (len > wp->w_width - col)
2570 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002571 if (len > 0)
2572 {
2573#ifdef FEAT_RIGHTLEFT
2574 if (wp->w_p_rl)
2575 STRNCPY(current_ScreenLine, text, len);
2576 else
2577#endif
2578 STRNCPY(current_ScreenLine + col, text, len);
2579 col += len;
2580 }
2581 }
2582 return col;
2583}
2584#endif
2585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586#ifdef FEAT_FOLDING
2587/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002588 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2589 * space is available for window "wp", minus "col".
2590 */
2591 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002592compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002593{
2594 int fdc = wp->w_p_fdc;
2595 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002596 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002597
2598 if (fdc > wwidth - (col + wmw))
2599 fdc = wwidth - (col + wmw);
2600 return fdc;
2601}
2602
2603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 * Display one folded line.
2605 */
2606 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002607fold_line(
2608 win_T *wp,
2609 long fold_count,
2610 foldinfo_T *foldinfo,
2611 linenr_T lnum,
2612 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002614 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 pos_T *top, *bot;
2616 linenr_T lnume = lnum + fold_count - 1;
2617 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002618 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 int col;
2621 int txtcol;
2622 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002623 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
2625 /* Build the fold line:
2626 * 1. Add the cmdwin_type for the command-line window
2627 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002628 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 * 4. Compose the text
2630 * 5. Add the text
2631 * 6. set highlighting for the Visual area an other text
2632 */
2633 col = 0;
2634
2635 /*
2636 * 1. Add the cmdwin_type for the command-line window
2637 * Ignores 'rightleft', this window is never right-left.
2638 */
2639#ifdef FEAT_CMDWIN
2640 if (cmdwin_type != 0 && wp == curwin)
2641 {
2642 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002643 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644#ifdef FEAT_MBYTE
2645 if (enc_utf8)
2646 ScreenLinesUC[off] = 0;
2647#endif
2648 ++col;
2649 }
2650#endif
2651
2652 /*
2653 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002654 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002656 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (fdc > 0)
2658 {
2659 fill_foldcolumn(buf, wp, TRUE, lnum);
2660#ifdef FEAT_RIGHTLEFT
2661 if (wp->w_p_rl)
2662 {
2663 int i;
2664
Bram Moolenaar02631462017-09-22 15:20:32 +02002665 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002666 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 /* reverse the fold column */
2668 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002669 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671 else
2672#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002673 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 col += fdc;
2675 }
2676
2677#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002678# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2679 for (ri = 0; ri < l; ++ri) \
Bram Moolenaar02631462017-09-22 15:20:32 +02002680 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002681 else \
2682 for (ri = 0; ri < l; ++ri) \
2683 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002685# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2686 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687#endif
2688
Bram Moolenaar64486672010-05-16 15:46:46 +02002689 /* Set all attributes of the 'number' or 'relativenumber' column and the
2690 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002691 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693#ifdef FEAT_SIGNS
2694 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002695 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002697 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (len > 0)
2699 {
2700 if (len > 2)
2701 len = 2;
2702# ifdef FEAT_RIGHTLEFT
2703 if (wp->w_p_rl)
2704 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002705 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002706 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 else
2708# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002709 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 col += len;
2711 }
2712 }
2713#endif
2714
2715 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002716 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002718 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002720 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 if (len > 0)
2722 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002723 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002724 long num;
2725 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002726
2727 if (len > w + 1)
2728 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002729
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002730 if (wp->w_p_nu && !wp->w_p_rnu)
2731 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002732 num = (long)lnum;
2733 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002734 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002735 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002736 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002737 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002738 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002739 /* 'number' + 'relativenumber': cursor line shows absolute
2740 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002741 num = lnum;
2742 fmt = "%-*ld ";
2743 }
2744 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002745
Bram Moolenaar700e7342013-01-30 12:31:36 +01002746 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#ifdef FEAT_RIGHTLEFT
2748 if (wp->w_p_rl)
2749 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002750 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002751 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 else
2753#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002754 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 col += len;
2756 }
2757 }
2758
2759 /*
2760 * 4. Compose the folded-line string with 'foldtext', if set.
2761 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002762 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
2764 txtcol = col; /* remember where text starts */
2765
2766 /*
2767 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2768 * Right-left text is put in columns 0 - number-col, normal text is put
2769 * in columns number-col - window-width.
2770 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002771 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
2773 /* Fill the rest of the line with the fold filler */
2774#ifdef FEAT_RIGHTLEFT
2775 if (wp->w_p_rl)
2776 col -= txtcol;
2777#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002778 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779#ifdef FEAT_RIGHTLEFT
2780 - (wp->w_p_rl ? txtcol : 0)
2781#endif
2782 )
2783 {
2784#ifdef FEAT_MBYTE
2785 if (enc_utf8)
2786 {
2787 if (fill_fold >= 0x80)
2788 {
2789 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002790 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002791 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
2793 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002794 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002796 ScreenLines[off + col] = fill_fold;
2797 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002798 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002800 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002802 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804
2805 if (text != buf)
2806 vim_free(text);
2807
2808 /*
2809 * 6. set highlighting for the Visual area an other text.
2810 * If all folded lines are in the Visual area, highlight the line.
2811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2813 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002814 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
2816 /* Visual is after curwin->w_cursor */
2817 top = &curwin->w_cursor;
2818 bot = &VIsual;
2819 }
2820 else
2821 {
2822 /* Visual is before curwin->w_cursor */
2823 top = &VIsual;
2824 bot = &curwin->w_cursor;
2825 }
2826 if (lnum >= top->lnum
2827 && lnume <= bot->lnum
2828 && (VIsual_mode != 'v'
2829 || ((lnum > top->lnum
2830 || (lnum == top->lnum
2831 && top->col == 0))
2832 && (lnume < bot->lnum
2833 || (lnume == bot->lnum
2834 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002835 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 {
2837 if (VIsual_mode == Ctrl_V)
2838 {
2839 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002840 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002842 if (wp->w_old_cursor_lcol != MAXCOL
2843 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002844 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 len = wp->w_old_cursor_lcol;
2846 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002847 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002848 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002849 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
2851 }
2852 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002855 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
2858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002860#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002861 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2862 if (wp->w_p_cc_cols)
2863 {
2864 int i = 0;
2865 int j = wp->w_p_cc_cols[i];
2866 int old_txtcol = txtcol;
2867
2868 while (j > -1)
2869 {
2870 txtcol += j;
2871 if (wp->w_p_wrap)
2872 txtcol -= wp->w_skipcol;
2873 else
2874 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002875 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002876 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002877 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002878 txtcol = old_txtcol;
2879 j = wp->w_p_cc_cols[++i];
2880 }
2881 }
2882
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002883 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002884 if (wp->w_p_cuc)
2885 {
2886 txtcol += wp->w_virtcol;
2887 if (wp->w_p_wrap)
2888 txtcol -= wp->w_skipcol;
2889 else
2890 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002891 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002892 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002893 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002894 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896
Bram Moolenaar02631462017-09-22 15:20:32 +02002897 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2898 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
2900 /*
2901 * Update w_cline_height and w_cline_folded if the cursor line was
2902 * updated (saves a call to plines() later).
2903 */
2904 if (wp == curwin
2905 && lnum <= curwin->w_cursor.lnum
2906 && lnume >= curwin->w_cursor.lnum)
2907 {
2908 curwin->w_cline_row = row;
2909 curwin->w_cline_height = 1;
2910 curwin->w_cline_folded = TRUE;
2911 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2912 }
2913}
2914
2915/*
2916 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2917 */
2918 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002919copy_text_attr(
2920 int off,
2921 char_u *buf,
2922 int len,
2923 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002925 int i;
2926
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 mch_memmove(ScreenLines + off, buf, (size_t)len);
2928# ifdef FEAT_MBYTE
2929 if (enc_utf8)
2930 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2931# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002932 for (i = 0; i < len; ++i)
2933 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934}
2935
2936/*
2937 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002938 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 */
2940 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002941fill_foldcolumn(
2942 char_u *p,
2943 win_T *wp,
2944 int closed, /* TRUE of FALSE */
2945 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946{
2947 int i = 0;
2948 int level;
2949 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002950 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002951 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952
2953 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002954 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
2956 level = win_foldinfo.fi_level;
2957 if (level > 0)
2958 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002959 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002960 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002961
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 /* If the column is too narrow, we start at the lowest level that
2963 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002964 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 if (first_level < 1)
2966 first_level = 1;
2967
Bram Moolenaar1c934292015-01-27 16:39:29 +01002968 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 {
2970 if (win_foldinfo.fi_lnum == lnum
2971 && first_level + i >= win_foldinfo.fi_low_level)
2972 p[i] = '-';
2973 else if (first_level == 1)
2974 p[i] = '|';
2975 else if (first_level + i <= 9)
2976 p[i] = '0' + first_level + i;
2977 else
2978 p[i] = '>';
2979 if (first_level + i == level)
2980 break;
2981 }
2982 }
2983 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002984 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985}
2986#endif /* FEAT_FOLDING */
2987
2988/*
2989 * Display line "lnum" of window 'wp' on the screen.
2990 * Start at row "startrow", stop when "endrow" is reached.
2991 * wp->w_virtcol needs to be valid.
2992 *
2993 * Return the number of last row the line occupies.
2994 */
2995 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002996win_line(
2997 win_T *wp,
2998 linenr_T lnum,
2999 int startrow,
3000 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003001 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003003 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3005 int c = 0; /* init for GCC */
3006 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003007#ifdef FEAT_LINEBREAK
3008 long vcol_sbr = -1; /* virtual column after showbreak */
3009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 long vcol_prev = -1; /* "vcol" of previous character */
3011 char_u *line; /* current line */
3012 char_u *ptr; /* current position in "line" */
3013 int row; /* row in the window, excl w_winrow */
3014 int screen_row; /* row on the screen, incl w_winrow */
3015
3016 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3017 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003018 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003019 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 int c_extra = NUL; /* extra chars, all the same */
3021 int extra_attr = 0; /* attributes when n_extra != 0 */
3022 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3023 displaying lcs_eol at end-of-line */
3024 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3025 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3026
3027 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3028 int saved_n_extra = 0;
3029 char_u *saved_p_extra = NULL;
3030 int saved_c_extra = 0;
3031 int saved_char_attr = 0;
3032
3033 int n_attr = 0; /* chars with special attr */
3034 int saved_attr2 = 0; /* char_attr saved for n_attr */
3035 int n_attr3 = 0; /* chars with overruling special attr */
3036 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3037
3038 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3039
3040 int fromcol, tocol; /* start/end of inverting */
3041 int fromcol_prev = -2; /* start of inverting after cursor */
3042 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003044 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 pos_T pos;
3046 long v;
3047
3048 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003049 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3051 in this line */
3052 int attr = 0; /* attributes for area highlighting */
3053 int area_attr = 0; /* attributes desired by highlighting */
3054 int search_attr = 0; /* attributes desired by 'hlsearch' */
3055#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003056 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 int syntax_attr = 0; /* attributes desired by syntax */
3058 int has_syntax = FALSE; /* this buffer has syntax highl. */
3059 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003060 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003061 int draw_color_col = FALSE; /* highlight colorcolumn */
3062 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003063#endif
3064#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003065 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003066# define SPWORDLEN 150
3067 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003068 int nextlinecol = 0; /* column where nextline[] starts */
3069 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003070 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003071 int spell_attr = 0; /* attributes desired by spelling */
3072 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003073 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3074 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003075 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003076 static int cap_col = -1; /* column to check for Cap word */
3077 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003078 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079#endif
3080 int extra_check; /* has syntax or linebreak */
3081#ifdef FEAT_MBYTE
3082 int multi_attr = 0; /* attributes desired by multibyte */
3083 int mb_l = 1; /* multi-byte byte length */
3084 int mb_c = 0; /* decoded multi-byte character */
3085 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003086 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087#endif
3088#ifdef FEAT_DIFF
3089 int filler_lines; /* nr of filler lines to be drawn */
3090 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003091 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 int change_start = MAXCOL; /* first col of changed area */
3093 int change_end = -1; /* last col of changed area */
3094#endif
3095 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3096#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003097 int need_showbreak = FALSE; /* overlong line, skipping first x
3098 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003100#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003101 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003103 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104#endif
3105#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003106 matchitem_T *cur; /* points to the match list */
3107 match_T *shl; /* points to search_hl or a match */
3108 int shl_flag; /* flag to indicate whether search_hl
3109 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003110 int pos_inprogress; /* marks that position match search is
3111 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003112 int prevcol_hl_flag; /* flag to indicate whether prevcol
3113 equals startcol of search_hl or one
3114 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#endif
3116#ifdef FEAT_ARABIC
3117 int prev_c = 0; /* previous Arabic character */
3118 int prev_c1 = 0; /* first composing char for prev_c */
3119#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003120#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003121 int did_line_attr = 0;
3122#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003123#ifdef FEAT_TERMINAL
3124 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003125 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
3128 /* draw_state: items that are drawn in sequence: */
3129#define WL_START 0 /* nothing done yet */
3130#ifdef FEAT_CMDWIN
3131# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3132#else
3133# define WL_CMDLINE WL_START
3134#endif
3135#ifdef FEAT_FOLDING
3136# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3137#else
3138# define WL_FOLD WL_CMDLINE
3139#endif
3140#ifdef FEAT_SIGNS
3141# define WL_SIGN WL_FOLD + 1 /* column for signs */
3142#else
3143# define WL_SIGN WL_FOLD /* column for signs */
3144#endif
3145#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003146#ifdef FEAT_LINEBREAK
3147# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003149# define WL_BRI WL_NR
3150#endif
3151#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3152# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3153#else
3154# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#endif
3156#define WL_LINE WL_SBR + 1 /* text in the line */
3157 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003158#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 int feedback_col = 0;
3160 int feedback_old_attr = -1;
3161#endif
3162
Bram Moolenaar860cae12010-06-05 23:22:07 +02003163#ifdef FEAT_CONCEAL
3164 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003165 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003166 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003167 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003168 int is_concealing = FALSE;
3169 int boguscols = 0; /* nonexistent columns added to force
3170 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003171 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003172 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003173 int match_conc = 0; /* cchar for match functions */
3174 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003175 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003176# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003177# define FIX_FOR_BOGUSCOLS \
3178 { \
3179 n_extra += vcol_off; \
3180 vcol -= vcol_off; \
3181 vcol_off = 0; \
3182 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003183 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003184 boguscols = 0; \
3185 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003186#else
3187# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 if (startrow > endrow) /* past the end already! */
3191 return startrow;
3192
3193 row = startrow;
3194 screen_row = row + W_WINROW(wp);
3195
3196 /*
3197 * To speed up the loop below, set extra_check when there is linebreak,
3198 * trailing white space and/or syntax processing to be done.
3199 */
3200#ifdef FEAT_LINEBREAK
3201 extra_check = wp->w_p_lbr;
3202#else
3203 extra_check = 0;
3204#endif
3205#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003206 if (syntax_present(wp) && !wp->w_s->b_syn_error
3207# ifdef SYN_TIME_LIMIT
3208 && !wp->w_s->b_syn_slow
3209# endif
3210 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
3212 /* Prepare for syntax highlighting in this line. When there is an
3213 * error, stop syntax highlighting. */
3214 save_did_emsg = did_emsg;
3215 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003216 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003218 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 else
3220 {
3221 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003222#ifdef SYN_TIME_LIMIT
3223 if (!wp->w_s->b_syn_slow)
3224#endif
3225 {
3226 has_syntax = TRUE;
3227 extra_check = TRUE;
3228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 }
3230 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003231
3232 /* Check for columns to display for 'colorcolumn'. */
3233 color_cols = wp->w_p_cc_cols;
3234 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003235 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003236#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003237
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003238#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003239 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003240 {
3241 extra_check = TRUE;
3242 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003243 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003244 }
3245#endif
3246
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003247#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003248 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003249 && *wp->w_s->b_p_spl != NUL
3250 && wp->w_s->b_langp.ga_len > 0
3251 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003252 {
3253 /* Prepare for spell checking. */
3254 has_spell = TRUE;
3255 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003256
3257 /* Get the start of the next line, so that words that wrap to the next
3258 * line are found too: "et<line-break>al.".
3259 * Trick: skip a few chars for C/shell/Vim comments */
3260 nextline[SPWORDLEN] = NUL;
3261 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3262 {
3263 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3264 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3265 }
3266
3267 /* When a word wrapped from the previous line the start of the current
3268 * line is valid. */
3269 if (lnum == checked_lnum)
3270 cur_checked_col = checked_col;
3271 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003272
3273 /* When there was a sentence end in the previous line may require a
3274 * word starting with capital in this line. In line 1 always check
3275 * the first word. */
3276 if (lnum != capcol_lnum)
3277 cap_col = -1;
3278 if (lnum == 1)
3279 cap_col = 0;
3280 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282#endif
3283
3284 /*
3285 * handle visual active in this window
3286 */
3287 fromcol = -10;
3288 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3290 {
3291 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003292 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
3294 top = &curwin->w_cursor;
3295 bot = &VIsual;
3296 }
3297 else /* Visual is before curwin->w_cursor */
3298 {
3299 top = &VIsual;
3300 bot = &curwin->w_cursor;
3301 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003302 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 if (VIsual_mode == Ctrl_V) /* block mode */
3304 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003305 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
3307 fromcol = wp->w_old_cursor_fcol;
3308 tocol = wp->w_old_cursor_lcol;
3309 }
3310 }
3311 else /* non-block mode */
3312 {
3313 if (lnum > top->lnum && lnum <= bot->lnum)
3314 fromcol = 0;
3315 else if (lnum == top->lnum)
3316 {
3317 if (VIsual_mode == 'V') /* linewise */
3318 fromcol = 0;
3319 else
3320 {
3321 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3322 if (gchar_pos(top) == NUL)
3323 tocol = fromcol + 1;
3324 }
3325 }
3326 if (VIsual_mode != 'V' && lnum == bot->lnum)
3327 {
3328 if (*p_sel == 'e' && bot->col == 0
3329#ifdef FEAT_VIRTUALEDIT
3330 && bot->coladd == 0
3331#endif
3332 )
3333 {
3334 fromcol = -10;
3335 tocol = MAXCOL;
3336 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003337 else if (bot->col == MAXCOL)
3338 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 else
3340 {
3341 pos = *bot;
3342 if (*p_sel == 'e')
3343 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3344 else
3345 {
3346 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3347 ++tocol;
3348 }
3349 }
3350 }
3351 }
3352
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 /* Check if the character under the cursor should not be inverted */
3354 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003355#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 )
3359 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
3361 /* if inverting in this line set area_highlighting */
3362 if (fromcol >= 0)
3363 {
3364 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003365 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003367 if ((clip_star.available && !clip_star.owned
3368 && clip_isautosel_star())
3369 || (clip_plus.available && !clip_plus.owned
3370 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003371 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#endif
3373 }
3374 }
3375
3376 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003377 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003379 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 && wp == curwin
3381 && lnum >= curwin->w_cursor.lnum
3382 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3383 {
3384 if (lnum == curwin->w_cursor.lnum)
3385 getvcol(curwin, &(curwin->w_cursor),
3386 (colnr_T *)&fromcol, NULL, NULL);
3387 else
3388 fromcol = 0;
3389 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3390 {
3391 pos.lnum = lnum;
3392 pos.col = search_match_endcol;
3393 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3394 }
3395 else
3396 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003397 /* do at least one character; happens when past end of line */
3398 if (fromcol == tocol)
3399 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003401 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
3403
3404#ifdef FEAT_DIFF
3405 filler_lines = diff_check(wp, lnum);
3406 if (filler_lines < 0)
3407 {
3408 if (filler_lines == -1)
3409 {
3410 if (diff_find_change(wp, lnum, &change_start, &change_end))
3411 diff_hlf = HLF_ADD; /* added line */
3412 else if (change_start == 0)
3413 diff_hlf = HLF_TXD; /* changed text */
3414 else
3415 diff_hlf = HLF_CHD; /* changed line */
3416 }
3417 else
3418 diff_hlf = HLF_ADD; /* added line */
3419 filler_lines = 0;
3420 area_highlighting = TRUE;
3421 }
3422 if (lnum == wp->w_topline)
3423 filler_lines = wp->w_topfill;
3424 filler_todo = filler_lines;
3425#endif
3426
3427#ifdef LINE_ATTR
3428# ifdef FEAT_SIGNS
3429 /* If this line has a sign with line highlighting set line_attr. */
3430 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3431 if (v != 0)
3432 line_attr = sign_get_attr((int)v, TRUE);
3433# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003434# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003436 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003437 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438# endif
3439 if (line_attr != 0)
3440 area_highlighting = TRUE;
3441#endif
3442
3443 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3444 ptr = line;
3445
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003446#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003447 if (has_spell)
3448 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003449 /* For checking first word with a capital skip white space. */
3450 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003451 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003452
Bram Moolenaar30abd282005-06-22 22:35:10 +00003453 /* To be able to spell-check over line boundaries copy the end of the
3454 * current line into nextline[]. Above the start of the next line was
3455 * copied to nextline[SPWORDLEN]. */
3456 if (nextline[SPWORDLEN] == NUL)
3457 {
3458 /* No next line or it is empty. */
3459 nextlinecol = MAXCOL;
3460 nextline_idx = 0;
3461 }
3462 else
3463 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003464 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003465 if (v < SPWORDLEN)
3466 {
3467 /* Short line, use it completely and append the start of the
3468 * next line. */
3469 nextlinecol = 0;
3470 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003471 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003472 nextline_idx = v + 1;
3473 }
3474 else
3475 {
3476 /* Long line, use only the last SPWORDLEN bytes. */
3477 nextlinecol = v - SPWORDLEN;
3478 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3479 nextline_idx = SPWORDLEN + 1;
3480 }
3481 }
3482 }
3483#endif
3484
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003485 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003487 if (lcs_space || lcs_trail)
3488 extra_check = TRUE;
3489 /* find start of trailing whitespace */
3490 if (lcs_trail)
3491 {
3492 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003493 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003494 --trailcol;
3495 trailcol += (colnr_T) (ptr - line);
3496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 }
3498
3499 /*
3500 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3501 * first character to be displayed.
3502 */
3503 if (wp->w_p_wrap)
3504 v = wp->w_skipcol;
3505 else
3506 v = wp->w_leftcol;
3507 if (v > 0)
3508 {
3509#ifdef FEAT_MBYTE
3510 char_u *prev_ptr = ptr;
3511#endif
3512 while (vcol < v && *ptr != NUL)
3513 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003514 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 vcol += c;
3516#ifdef FEAT_MBYTE
3517 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003519 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003522 /* When:
3523 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003524 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003525 * - 'virtualedit' is set, or
3526 * - the visual mode is active,
3527 * the end of the line may be before the start of the displayed part.
3528 */
3529 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003530#ifdef FEAT_SYN_HL
3531 wp->w_p_cuc || draw_color_col ||
3532#endif
3533#ifdef FEAT_VIRTUALEDIT
3534 virtual_active() ||
3535#endif
3536 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
3541 /* Handle a character that's not completely on the screen: Put ptr at
3542 * that character but skip the first few screen characters. */
3543 if (vcol > v)
3544 {
3545 vcol -= c;
3546#ifdef FEAT_MBYTE
3547 ptr = prev_ptr;
3548#else
3549 --ptr;
3550#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003551 /* If the character fits on the screen, don't need to skip it.
3552 * Except for a TAB. */
3553 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003554#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003555 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003556#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003557 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003558 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560
3561 /*
3562 * Adjust for when the inverted text is before the screen,
3563 * and when the start of the inverted text is before the screen.
3564 */
3565 if (tocol <= vcol)
3566 fromcol = 0;
3567 else if (fromcol >= 0 && fromcol < vcol)
3568 fromcol = vcol;
3569
3570#ifdef FEAT_LINEBREAK
3571 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3572 if (wp->w_p_wrap)
3573 need_showbreak = TRUE;
3574#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003575#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003576 /* When spell checking a word we need to figure out the start of the
3577 * word and if it's badly spelled or not. */
3578 if (has_spell)
3579 {
3580 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003581 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003582 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003583
3584 pos = wp->w_cursor;
3585 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003586 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003587 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003588
3589 /* spell_move_to() may call ml_get() and make "line" invalid */
3590 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3591 ptr = line + linecol;
3592
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003593 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003594 {
3595 /* no bad word found at line start, don't check until end of a
3596 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003597 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003598 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003599 }
3600 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003601 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003602 /* bad word found, use attributes until end of word */
3603 word_end = wp->w_cursor.col + len + 1;
3604
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003605 /* Turn index into actual attributes. */
3606 if (spell_hlf != HLF_COUNT)
3607 spell_attr = highlight_attr[spell_hlf];
3608 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003609 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003610
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003611# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003612 /* Need to restart syntax highlighting for this line. */
3613 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003614 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003615# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003616 }
3617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619
3620 /*
3621 * Correct highlighting for cursor that can't be disabled.
3622 * Avoids having to check this for each character.
3623 */
3624 if (fromcol >= 0)
3625 {
3626 if (noinvcur)
3627 {
3628 if ((colnr_T)fromcol == wp->w_virtcol)
3629 {
3630 /* highlighting starts at cursor, let it start just after the
3631 * cursor */
3632 fromcol_prev = fromcol;
3633 fromcol = -1;
3634 }
3635 else if ((colnr_T)fromcol < wp->w_virtcol)
3636 /* restart highlighting after the cursor */
3637 fromcol_prev = wp->w_virtcol;
3638 }
3639 if (fromcol >= tocol)
3640 fromcol = -1;
3641 }
3642
3643#ifdef FEAT_SEARCH_EXTRA
3644 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003645 * Handle highlighting the last used search pattern and matches.
3646 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003648 cur = wp->w_match_head;
3649 shl_flag = FALSE;
3650 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003652 if (shl_flag == FALSE)
3653 {
3654 shl = &search_hl;
3655 shl_flag = TRUE;
3656 }
3657 else
3658 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003659 shl->startcol = MAXCOL;
3660 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003662 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003663 v = (long)(ptr - line);
3664 if (cur != NULL)
3665 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003666 next_search_hl(wp, shl, lnum, (colnr_T)v,
3667 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003668
3669 /* Need to get the line again, a multi-line regexp may have made it
3670 * invalid. */
3671 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3672 ptr = line + v;
3673
3674 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003676 if (shl->lnum == lnum)
3677 shl->startcol = shl->rm.startpos[0].col;
3678 else
3679 shl->startcol = 0;
3680 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3681 - shl->rm.startpos[0].lnum)
3682 shl->endcol = shl->rm.endpos[0].col;
3683 else
3684 shl->endcol = MAXCOL;
3685 /* Highlight one character for an empty match. */
3686 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003689 if (has_mbyte && line[shl->endcol] != NUL)
3690 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003693 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003695 if ((long)shl->startcol < v) /* match at leftcol */
3696 {
3697 shl->attr_cur = shl->attr;
3698 search_attr = shl->attr;
3699 }
3700 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003702 if (shl != &search_hl && cur != NULL)
3703 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705#endif
3706
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003707#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003708 /* Cursor line highlighting for 'cursorline' in the current window. Not
3709 * when Visual mode is active, because it's not clear what is selected
3710 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003711 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003712 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003713 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003714 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003715 area_highlighting = TRUE;
3716 }
3717#endif
3718
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003719 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 col = 0;
3721#ifdef FEAT_RIGHTLEFT
3722 if (wp->w_p_rl)
3723 {
3724 /* Rightleft window: process the text in the normal direction, but put
3725 * it in current_ScreenLine[] from right to left. Start at the
3726 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003727 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 off += col;
3729 }
3730#endif
3731
3732 /*
3733 * Repeat for the whole displayed line.
3734 */
3735 for (;;)
3736 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003737#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003738 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 /* Skip this quickly when working on the text. */
3741 if (draw_state != WL_LINE)
3742 {
3743#ifdef FEAT_CMDWIN
3744 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3745 {
3746 draw_state = WL_CMDLINE;
3747 if (cmdwin_type != 0 && wp == curwin)
3748 {
3749 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003751 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003752 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754 }
3755#endif
3756
3757#ifdef FEAT_FOLDING
3758 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3759 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003760 int fdc = compute_foldcolumn(wp, 0);
3761
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003763 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003765 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003766 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003767 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003768 p_extra_free = alloc(12 + 1);
3769
3770 if (p_extra_free != NULL)
3771 {
3772 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3773 n_extra = fdc;
3774 p_extra_free[n_extra] = NUL;
3775 p_extra = p_extra_free;
3776 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003777 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 }
3781#endif
3782
3783#ifdef FEAT_SIGNS
3784 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3785 {
3786 draw_state = WL_SIGN;
3787 /* Show the sign column when there are any signs in this
3788 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003789 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003791 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003793 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794# endif
3795
3796 /* Draw two cells with the sign value or blank. */
3797 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003798 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 n_extra = 2;
3800
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003801 if (row == startrow
3802#ifdef FEAT_DIFF
3803 + filler_lines && filler_todo <= 0
3804#endif
3805 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
3807 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3808 SIGN_TEXT);
3809# ifdef FEAT_SIGN_ICONS
3810 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3811 SIGN_ICON);
3812 if (gui.in_use && icon_sign != 0)
3813 {
3814 /* Use the image in this position. */
3815 c_extra = SIGN_BYTE;
3816# ifdef FEAT_NETBEANS_INTG
3817 if (buf_signcount(wp->w_buffer, lnum) > 1)
3818 c_extra = MULTISIGN_BYTE;
3819# endif
3820 char_attr = icon_sign;
3821 }
3822 else
3823# endif
3824 if (text_sign != 0)
3825 {
3826 p_extra = sign_get_text(text_sign);
3827 if (p_extra != NULL)
3828 {
3829 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003830 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832 char_attr = sign_get_attr(text_sign, FALSE);
3833 }
3834 }
3835 }
3836 }
3837#endif
3838
3839 if (draw_state == WL_NR - 1 && n_extra == 0)
3840 {
3841 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003842 /* Display the absolute or relative line number. After the
3843 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3844 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 && (row == startrow
3846#ifdef FEAT_DIFF
3847 + filler_lines
3848#endif
3849 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3850 {
3851 /* Draw the line number (empty space after wrapping). */
3852 if (row == startrow
3853#ifdef FEAT_DIFF
3854 + filler_lines
3855#endif
3856 )
3857 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003858 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003859 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003860
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003861 if (wp->w_p_nu && !wp->w_p_rnu)
3862 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003863 num = (long)lnum;
3864 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003865 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003866 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003867 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003868 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003869 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003870 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003871 num = lnum;
3872 fmt = "%-*ld ";
3873 }
3874 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003875
Bram Moolenaar700e7342013-01-30 12:31:36 +01003876 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003877 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (wp->w_skipcol > 0)
3879 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3880 *p_extra = '-';
3881#ifdef FEAT_RIGHTLEFT
3882 if (wp->w_p_rl) /* reverse line numbers */
3883 rl_mirror(extra);
3884#endif
3885 p_extra = extra;
3886 c_extra = NUL;
3887 }
3888 else
3889 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003890 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003891 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003892#ifdef FEAT_SYN_HL
3893 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003894 * the current line differently.
3895 * TODO: Can we use CursorLine instead of CursorLineNr
3896 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003897 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003898 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003899 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902 }
3903
Bram Moolenaar597a4222014-06-25 14:39:50 +02003904#ifdef FEAT_LINEBREAK
3905 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3906 && n_extra == 0 && *p_sbr != NUL)
3907 /* draw indent after showbreak value */
3908 draw_state = WL_BRI;
3909 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3910 /* After the showbreak, draw the breakindent */
3911 draw_state = WL_BRI - 1;
3912
3913 /* draw 'breakindent': indent wrapped text accordingly */
3914 if (draw_state == WL_BRI - 1 && n_extra == 0)
3915 {
3916 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003917 /* if need_showbreak is set, breakindent also applies */
3918 if (wp->w_p_bri && n_extra == 0
3919 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003920# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003921 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003922# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003923 )
3924 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003925 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003926# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003927 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003928 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003929 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003930# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003931 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3932 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003933 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003934# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003935 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003936# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003937 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003938 c_extra = ' ';
3939 n_extra = get_breakindent_win(wp,
3940 ml_get_buf(wp->w_buffer, lnum, FALSE));
3941 /* Correct end of highlighted area for 'breakindent',
3942 * required when 'linebreak' is also set. */
3943 if (tocol == vcol)
3944 tocol += n_extra;
3945 }
3946 }
3947#endif
3948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3950 if (draw_state == WL_SBR - 1 && n_extra == 0)
3951 {
3952 draw_state = WL_SBR;
3953# ifdef FEAT_DIFF
3954 if (filler_todo > 0)
3955 {
3956 /* Draw "deleted" diff line(s). */
3957 if (char2cells(fill_diff) > 1)
3958 c_extra = '-';
3959 else
3960 c_extra = fill_diff;
3961# ifdef FEAT_RIGHTLEFT
3962 if (wp->w_p_rl)
3963 n_extra = col + 1;
3964 else
3965# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02003966 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003967 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969# endif
3970# ifdef FEAT_LINEBREAK
3971 if (*p_sbr != NUL && need_showbreak)
3972 {
3973 /* Draw 'showbreak' at the start of each broken line. */
3974 p_extra = p_sbr;
3975 c_extra = NUL;
3976 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003977 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003979 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 /* Correct end of highlighted area for 'showbreak',
3981 * required when 'linebreak' is also set. */
3982 if (tocol == vcol)
3983 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003984#ifdef FEAT_SYN_HL
3985 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003986 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003987 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003988 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
3991# endif
3992 }
3993#endif
3994
3995 if (draw_state == WL_LINE - 1 && n_extra == 0)
3996 {
3997 draw_state = WL_LINE;
3998 if (saved_n_extra)
3999 {
4000 /* Continue item from end of wrapped line. */
4001 n_extra = saved_n_extra;
4002 c_extra = saved_c_extra;
4003 p_extra = saved_p_extra;
4004 char_attr = saved_char_attr;
4005 }
4006 else
4007 char_attr = 0;
4008 }
4009 }
4010
4011 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004012 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004013 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014#ifdef FEAT_DIFF
4015 && filler_todo <= 0
4016#endif
4017 )
4018 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004019 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004020 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004021 /* Pretend we have finished updating the window. Except when
4022 * 'cursorcolumn' is set. */
4023#ifdef FEAT_SYN_HL
4024 if (wp->w_p_cuc)
4025 row = wp->w_cline_row + wp->w_cline_height;
4026 else
4027#endif
4028 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 break;
4030 }
4031
4032 if (draw_state == WL_LINE && area_highlighting)
4033 {
4034 /* handle Visual or match highlighting in this line */
4035 if (vcol == fromcol
4036#ifdef FEAT_MBYTE
4037 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4038 && (*mb_ptr2cells)(ptr) > 1)
4039#endif
4040 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004041 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 && vcol < tocol))
4043 area_attr = attr; /* start highlighting */
4044 else if (area_attr != 0
4045 && (vcol == tocol
4046 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
4049#ifdef FEAT_SEARCH_EXTRA
4050 if (!n_extra)
4051 {
4052 /*
4053 * Check for start/end of search pattern match.
4054 * After end, check for start/end of next match.
4055 * When another match, have to check for start again.
4056 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004057 * Do this for 'search_hl' and the match list (ordered by
4058 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004060 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004061 cur = wp->w_match_head;
4062 shl_flag = FALSE;
4063 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004065 if (shl_flag == FALSE
4066 && ((cur != NULL
4067 && cur->priority > SEARCH_HL_PRIORITY)
4068 || cur == NULL))
4069 {
4070 shl = &search_hl;
4071 shl_flag = TRUE;
4072 }
4073 else
4074 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004075 if (cur != NULL)
4076 cur->pos.cur = 0;
4077 pos_inprogress = TRUE;
4078 while (shl->rm.regprog != NULL
4079 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004081 if (shl->startcol != MAXCOL
4082 && v >= (long)shl->startcol
4083 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004085#ifdef FEAT_MBYTE
4086 int tmp_col = v + MB_PTR2LEN(ptr);
4087
4088 if (shl->endcol < tmp_col)
4089 shl->endcol = tmp_col;
4090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004092#ifdef FEAT_CONCEAL
4093 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4094 == cur->hlg_id)
4095 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004096 has_match_conc =
4097 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004098 match_conc = cur->conceal_char;
4099 }
4100 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004101 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004104 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
4106 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004107 next_search_hl(wp, shl, lnum, (colnr_T)v,
4108 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004109 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004110 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
4112 /* Need to get the line again, a multi-line regexp
4113 * may have made it invalid. */
4114 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4115 ptr = line + v;
4116
4117 if (shl->lnum == lnum)
4118 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004119 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004121 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004123 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004125 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
4127 /* highlight empty match, try again after
4128 * it */
4129#ifdef FEAT_MBYTE
4130 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004131 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004132 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 else
4134#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004135 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137
4138 /* Loop to check if the match starts at the
4139 * current position */
4140 continue;
4141 }
4142 }
4143 break;
4144 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004145 if (shl != &search_hl && cur != NULL)
4146 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004148
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004149 /* Use attributes from match with highest priority among
4150 * 'search_hl' and the match list. */
4151 search_attr = search_hl.attr_cur;
4152 cur = wp->w_match_head;
4153 shl_flag = FALSE;
4154 while (cur != NULL || shl_flag == FALSE)
4155 {
4156 if (shl_flag == FALSE
4157 && ((cur != NULL
4158 && cur->priority > SEARCH_HL_PRIORITY)
4159 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004160 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004161 shl = &search_hl;
4162 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004163 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004164 else
4165 shl = &cur->hl;
4166 if (shl->attr_cur != 0)
4167 search_attr = shl->attr_cur;
4168 if (shl != &search_hl && cur != NULL)
4169 cur = cur->next;
4170 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004171 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004172 if (*ptr == NUL && (did_line_attr >= 1
4173 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004174 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
4176#endif
4177
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004179 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004181 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4182 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004184 if (diff_hlf == HLF_TXD && ptr - line > change_end
4185 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004187 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004188 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004189 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004192
4193 /* Decide which of the highlight attributes to use. */
4194 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004195#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004196 if (area_attr != 0)
4197 char_attr = hl_combine_attr(line_attr, area_attr);
4198 else if (search_attr != 0)
4199 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004200 /* Use line_attr when not in the Visual or 'incsearch' area
4201 * (area_attr may be 0 when "noinvcur" is set). */
4202 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004203 || vcol < fromcol || vcol_prev < fromcol_prev
4204 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004205 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004206#else
4207 if (area_attr != 0)
4208 char_attr = area_attr;
4209 else if (search_attr != 0)
4210 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004211#endif
4212 else
4213 {
4214 attr_pri = FALSE;
4215#ifdef FEAT_SYN_HL
4216 if (has_syntax)
4217 char_attr = syntax_attr;
4218 else
4219#endif
4220 char_attr = 0;
4221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
4223
4224 /*
4225 * Get the next character to put on the screen.
4226 */
4227 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004228 * The "p_extra" points to the extra stuff that is inserted to
4229 * represent special characters (non-printable stuff) and other
4230 * things. When all characters are the same, c_extra is used.
4231 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4232 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4234 */
4235 if (n_extra > 0)
4236 {
4237 if (c_extra != NUL)
4238 {
4239 c = c_extra;
4240#ifdef FEAT_MBYTE
4241 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004242 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
4244 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004245 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004246 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248 else
4249 mb_utf8 = FALSE;
4250#endif
4251 }
4252 else
4253 {
4254 c = *p_extra;
4255#ifdef FEAT_MBYTE
4256 if (has_mbyte)
4257 {
4258 mb_c = c;
4259 if (enc_utf8)
4260 {
4261 /* If the UTF-8 character is more than one byte:
4262 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004263 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 mb_utf8 = FALSE;
4265 if (mb_l > n_extra)
4266 mb_l = 1;
4267 else if (mb_l > 1)
4268 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004269 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004271 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
4273 }
4274 else
4275 {
4276 /* if this is a DBCS character, put it in "mb_c" */
4277 mb_l = MB_BYTE2LEN(c);
4278 if (mb_l >= n_extra)
4279 mb_l = 1;
4280 else if (mb_l > 1)
4281 mb_c = (c << 8) + p_extra[1];
4282 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004283 if (mb_l == 0) /* at the NUL at end-of-line */
4284 mb_l = 1;
4285
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 /* If a double-width char doesn't fit display a '>' in the
4287 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004288 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289# ifdef FEAT_RIGHTLEFT
4290 wp->w_p_rl ? (col <= 0) :
4291# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004292 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 && (*mb_char2cells)(mb_c) == 2)
4294 {
4295 c = '>';
4296 mb_c = c;
4297 mb_l = 1;
4298 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004299 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 /* put the pointer back to output the double-width
4301 * character at the start of the next line. */
4302 ++n_extra;
4303 --p_extra;
4304 }
4305 else
4306 {
4307 n_extra -= mb_l - 1;
4308 p_extra += mb_l - 1;
4309 }
4310 }
4311#endif
4312 ++p_extra;
4313 }
4314 --n_extra;
4315 }
4316 else
4317 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004318#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004319 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004320#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004321
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004322 if (p_extra_free != NULL)
4323 {
4324 vim_free(p_extra_free);
4325 p_extra_free = NULL;
4326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 /*
4328 * Get a character from the line itself.
4329 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004330 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004331#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004332 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334#ifdef FEAT_MBYTE
4335 if (has_mbyte)
4336 {
4337 mb_c = c;
4338 if (enc_utf8)
4339 {
4340 /* If the UTF-8 character is more than one byte: Decode it
4341 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004342 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 mb_utf8 = FALSE;
4344 if (mb_l > 1)
4345 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004346 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 /* Overlong encoded ASCII or ASCII with composing char
4348 * is displayed normally, except a NUL. */
4349 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004350 {
4351 c = mb_c;
4352# ifdef FEAT_LINEBREAK
4353 c0 = mb_c;
4354# endif
4355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004357
4358 /* At start of the line we can have a composing char.
4359 * Draw it as a space with a composing char. */
4360 if (utf_iscomposing(mb_c))
4361 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004362 int i;
4363
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004364 for (i = Screen_mco - 1; i > 0; --i)
4365 u8cc[i] = u8cc[i - 1];
4366 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004367 mb_c = ' ';
4368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370
4371 if ((mb_l == 1 && c >= 0x80)
4372 || (mb_l >= 1 && mb_c == 0)
4373 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004374# ifdef UNICODE16
4375 || mb_c >= 0x10000
4376# endif
4377 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 {
4379 /*
4380 * Illegal UTF-8 byte: display as <xx>.
4381 * Non-BMP character : display as ? or fullwidth ?.
4382 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004383# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
4387 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004388# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 if (wp->w_p_rl) /* reverse */
4390 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004393# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 else if (utf_char2cells(mb_c) != 2)
4395 STRCPY(extra, "?");
4396 else
4397 /* 0xff1f in UTF-8: full-width '?' */
4398 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
4401 p_extra = extra;
4402 c = *p_extra;
4403 mb_c = mb_ptr2char_adv(&p_extra);
4404 mb_utf8 = (c >= 0x80);
4405 n_extra = (int)STRLEN(p_extra);
4406 c_extra = NUL;
4407 if (area_attr == 0 && search_attr == 0)
4408 {
4409 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004410 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 saved_attr2 = char_attr; /* save current attr */
4412 }
4413 }
4414 else if (mb_l == 0) /* at the NUL at end-of-line */
4415 mb_l = 1;
4416#ifdef FEAT_ARABIC
4417 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4418 {
4419 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004420 int pc, pc1, nc;
4421 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422
4423 /* The idea of what is the previous and next
4424 * character depends on 'rightleft'. */
4425 if (wp->w_p_rl)
4426 {
4427 pc = prev_c;
4428 pc1 = prev_c1;
4429 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004430 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432 else
4433 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004434 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004436 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 prev_c = mb_c;
4439
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004440 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
4442 else
4443 prev_c = mb_c;
4444#endif
4445 }
4446 else /* enc_dbcs */
4447 {
4448 mb_l = MB_BYTE2LEN(c);
4449 if (mb_l == 0) /* at the NUL at end-of-line */
4450 mb_l = 1;
4451 else if (mb_l > 1)
4452 {
4453 /* We assume a second byte below 32 is illegal.
4454 * Hopefully this is OK for all double-byte encodings!
4455 */
4456 if (ptr[1] >= 32)
4457 mb_c = (c << 8) + ptr[1];
4458 else
4459 {
4460 if (ptr[1] == NUL)
4461 {
4462 /* head byte at end of line */
4463 mb_l = 1;
4464 transchar_nonprint(extra, c);
4465 }
4466 else
4467 {
4468 /* illegal tail byte */
4469 mb_l = 2;
4470 STRCPY(extra, "XX");
4471 }
4472 p_extra = extra;
4473 n_extra = (int)STRLEN(extra) - 1;
4474 c_extra = NUL;
4475 c = *p_extra++;
4476 if (area_attr == 0 && search_attr == 0)
4477 {
4478 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004479 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 saved_attr2 = char_attr; /* save current attr */
4481 }
4482 mb_c = c;
4483 }
4484 }
4485 }
4486 /* If a double-width char doesn't fit display a '>' in the
4487 * last column; the character is displayed at the start of the
4488 * next line. */
4489 if ((
4490# ifdef FEAT_RIGHTLEFT
4491 wp->w_p_rl ? (col <= 0) :
4492# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004493 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 && (*mb_char2cells)(mb_c) == 2)
4495 {
4496 c = '>';
4497 mb_c = c;
4498 mb_utf8 = FALSE;
4499 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004500 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 /* Put pointer back so that the character will be
4502 * displayed at the start of the next line. */
4503 --ptr;
4504 }
4505 else if (*ptr != NUL)
4506 ptr += mb_l - 1;
4507
4508 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004509 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004510 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004511 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004514 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 c = ' ';
4516 if (area_attr == 0 && search_attr == 0)
4517 {
4518 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004519 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 saved_attr2 = char_attr; /* save current attr */
4521 }
4522 mb_c = c;
4523 mb_utf8 = FALSE;
4524 mb_l = 1;
4525 }
4526
4527 }
4528#endif
4529 ++ptr;
4530
4531 if (extra_check)
4532 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004533#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004534 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004535#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004536
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004537#ifdef FEAT_TERMINAL
4538 if (get_term_attr)
4539 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004540 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004541
4542 if (!attr_pri)
4543 char_attr = syntax_attr;
4544 else
4545 char_attr = hl_combine_attr(syntax_attr, char_attr);
4546 }
4547#endif
4548
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004549#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 /* Get syntax attribute, unless still at the start of the line
4551 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004552 v = (long)(ptr - line);
4553 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
4555 /* Get the syntax attribute for the character. If there
4556 * is an error, disable syntax highlighting. */
4557 save_did_emsg = did_emsg;
4558 did_emsg = FALSE;
4559
Bram Moolenaar217ad922005-03-20 22:37:15 +00004560 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004561# ifdef FEAT_SPELL
4562 has_spell ? &can_spell :
4563# endif
4564 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565
4566 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004567 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004568 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004569 has_syntax = FALSE;
4570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 else
4572 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004573#ifdef SYN_TIME_LIMIT
4574 if (wp->w_s->b_syn_slow)
4575 has_syntax = FALSE;
4576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
4578 /* Need to get the line again, a multi-line regexp may
4579 * have made it invalid. */
4580 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4581 ptr = line + v;
4582
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004583 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004585 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004586 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004587# ifdef FEAT_CONCEAL
4588 /* no concealing past the end of the line, it interferes
4589 * with line highlighting */
4590 if (c == NUL)
4591 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004592 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004593 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004596#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004597
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004598#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004599 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004600 * Only do this when there is no syntax highlighting, the
4601 * @Spell cluster is not used or the current syntax item
4602 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004603 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004604 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004605 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004606# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004607 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004608 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004609# endif
4610 if (c != 0 && (
4611# ifdef FEAT_SYN_HL
4612 !has_syntax ||
4613# endif
4614 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004615 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004616 char_u *prev_ptr, *p;
4617 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004618 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004619# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004620 if (has_mbyte)
4621 {
4622 prev_ptr = ptr - mb_l;
4623 v -= mb_l - 1;
4624 }
4625 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004626# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004627 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004628
4629 /* Use nextline[] if possible, it has the start of the
4630 * next line concatenated. */
4631 if ((prev_ptr - line) - nextlinecol >= 0)
4632 p = nextline + (prev_ptr - line) - nextlinecol;
4633 else
4634 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004635 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004636 len = spell_check(wp, p, &spell_hlf, &cap_col,
4637 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004638 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004639
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004640 /* In Insert mode only highlight a word that
4641 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004642 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004643 && (State & INSERT) != 0
4644 && wp->w_cursor.lnum == lnum
4645 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004646 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004647 && wp->w_cursor.col < (colnr_T)word_end)
4648 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004649 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004650 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004651 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004652
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004653 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004654 && (p - nextline) + len > nextline_idx)
4655 {
4656 /* Remember that the good word continues at the
4657 * start of the next line. */
4658 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004659 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004660 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004661
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004662 /* Turn index into actual attributes. */
4663 if (spell_hlf != HLF_COUNT)
4664 spell_attr = highlight_attr[spell_hlf];
4665
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004666 if (cap_col > 0)
4667 {
4668 if (p != prev_ptr
4669 && (p - nextline) + cap_col >= nextline_idx)
4670 {
4671 /* Remember that the word in the next line
4672 * must start with a capital. */
4673 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004674 cap_col = (int)((p - nextline) + cap_col
4675 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004676 }
4677 else
4678 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004679 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004680 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004681 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004682 }
4683 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004684 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004685 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004686 char_attr = hl_combine_attr(char_attr, spell_attr);
4687 else
4688 char_attr = hl_combine_attr(spell_attr, char_attr);
4689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690#endif
4691#ifdef FEAT_LINEBREAK
4692 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004693 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004695 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004696 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004698# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004699 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004700# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004701 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004703 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004705 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004706
Bram Moolenaar597a4222014-06-25 14:39:50 +02004707 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004708 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004709 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004710 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaara3650912014-11-19 13:21:57 +01004711 n_extra = (int)wp->w_buffer->b_p_ts
4712 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4713
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004714# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004715 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004716# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004718# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004719 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004720 {
4721#ifdef FEAT_CONCEAL
4722 if (c == TAB)
4723 /* See "Tab alignment" below. */
4724 FIX_FOR_BOGUSCOLS;
4725#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004726 if (!wp->w_p_list)
4727 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 }
4730#endif
4731
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004732 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4733 */
4734 if (wp->w_p_list
4735 && (((c == 160
4736#ifdef FEAT_MBYTE
4737 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4738#endif
4739 ) && lcs_nbsp)
4740 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4741 {
4742 c = (c == ' ') ? lcs_space : lcs_nbsp;
4743 if (area_attr == 0 && search_attr == 0)
4744 {
4745 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004746 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004747 saved_attr2 = char_attr; /* save current attr */
4748 }
4749#ifdef FEAT_MBYTE
4750 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004751 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004752 {
4753 mb_utf8 = TRUE;
4754 u8cc[0] = 0;
4755 c = 0xc0;
4756 }
4757 else
4758 mb_utf8 = FALSE;
4759#endif
4760 }
4761
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4763 {
4764 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004765 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
4767 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004768 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 saved_attr2 = char_attr; /* save current attr */
4770 }
4771#ifdef FEAT_MBYTE
4772 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004773 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004776 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004777 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 }
4779 else
4780 mb_utf8 = FALSE;
4781#endif
4782 }
4783 }
4784
4785 /*
4786 * Handling of non-printable characters.
4787 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004788 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
4790 /*
4791 * when getting a character from the file, we may have to
4792 * turn it into something else on the way to putting it
4793 * into "ScreenLines".
4794 */
4795 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4796 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004797 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004798 long vcol_adjusted = vcol; /* removed showbreak length */
4799#ifdef FEAT_LINEBREAK
4800 /* only adjust the tab_len, when at the first column
4801 * after the showbreak value was drawn */
4802 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4803 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004806 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004807 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4808
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004809#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004810 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004811#endif
4812 /* tab amount depends on current column */
4813 n_extra = tab_len;
4814#ifdef FEAT_LINEBREAK
4815 else
4816 {
4817 char_u *p;
4818 int len = n_extra;
4819 int i;
4820 int saved_nextra = n_extra;
4821
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004822#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004823 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004824 /* there are characters to conceal */
4825 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004826 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4827 */
4828 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4829 && n_extra > tab_len)
4830 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004831#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004832
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004833 /* if n_extra > 0, it gives the number of chars, to
4834 * use for a tab, else we need to calculate the width
4835 * for a tab */
4836#ifdef FEAT_MBYTE
4837 len = (tab_len * mb_char2len(lcs_tab2));
4838 if (n_extra > 0)
4839 len += n_extra - tab_len;
4840#endif
4841 c = lcs_tab1;
4842 p = alloc((unsigned)(len + 1));
4843 vim_memset(p, ' ', len);
4844 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004845 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004846 p_extra_free = p;
4847 for (i = 0; i < tab_len; i++)
4848 {
4849#ifdef FEAT_MBYTE
4850 mb_char2bytes(lcs_tab2, p);
4851 p += mb_char2len(lcs_tab2);
4852 n_extra += mb_char2len(lcs_tab2)
4853 - (saved_nextra > 0 ? 1 : 0);
4854#else
4855 p[i] = lcs_tab2;
4856#endif
4857 }
4858 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004859#ifdef FEAT_CONCEAL
4860 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4861 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004862 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004863 n_extra -= vcol_off;
4864#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004865 }
4866#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004867#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004868 {
4869 int vc_saved = vcol_off;
4870
4871 /* Tab alignment should be identical regardless of
4872 * 'conceallevel' value. So tab compensates of all
4873 * previous concealed characters, and thus resets
4874 * vcol_off and boguscols accumulated so far in the
4875 * line. Note that the tab can be longer than
4876 * 'tabstop' when there are concealed characters. */
4877 FIX_FOR_BOGUSCOLS;
4878
4879 /* Make sure, the highlighting for the tab char will be
4880 * correctly set further below (effectively reverts the
4881 * FIX_FOR_BOGSUCOLS macro */
4882 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004883 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004884 tab_len += vc_saved;
4885 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887#ifdef FEAT_MBYTE
4888 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4889#endif
4890 if (wp->w_p_list)
4891 {
4892 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004893#ifdef FEAT_LINEBREAK
4894 if (wp->w_p_lbr)
4895 c_extra = NUL; /* using p_extra from above */
4896 else
4897#endif
4898 c_extra = lcs_tab2;
4899 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004900 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 saved_attr2 = char_attr; /* save current attr */
4902#ifdef FEAT_MBYTE
4903 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004904 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
4906 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004907 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004908 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 }
4910#endif
4911 }
4912 else
4913 {
4914 c_extra = ' ';
4915 c = ' ';
4916 }
4917 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004918 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004919 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004920 || ((fromcol >= 0 || fromcol_prev >= 0)
4921 && tocol > vcol
4922 && VIsual_mode != Ctrl_V
4923 && (
4924# ifdef FEAT_RIGHTLEFT
4925 wp->w_p_rl ? (col >= 0) :
4926# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004927 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004928 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004929 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004930 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004931 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004933 /* Display a '$' after the line or highlight an extra
4934 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4936 /* For a diff line the highlighting continues after the
4937 * "$". */
4938 if (
4939# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004940 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941# ifdef LINE_ATTR
4942 &&
4943# endif
4944# endif
4945# ifdef LINE_ATTR
4946 line_attr == 0
4947# endif
4948 )
4949#endif
4950 {
4951#ifdef FEAT_VIRTUALEDIT
4952 /* In virtualedit, visual selections may extend
4953 * beyond end of line. */
4954 if (area_highlighting && virtual_active()
4955 && tocol != MAXCOL && vcol < tocol)
4956 n_extra = 0;
4957 else
4958#endif
4959 {
4960 p_extra = at_end_str;
4961 n_extra = 1;
4962 c_extra = NUL;
4963 }
4964 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004965 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004966 c = lcs_eol;
4967 else
4968 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 lcs_eol_one = -1;
4970 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004971 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004973 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 n_attr = 1;
4975 }
4976#ifdef FEAT_MBYTE
4977 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004978 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
4980 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004981 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004982 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 }
4984 else
4985 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4986#endif
4987 }
4988 else if (c != NUL)
4989 {
4990 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004991 if (n_extra == 0)
4992 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993#ifdef FEAT_RIGHTLEFT
4994 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4995 rl_mirror(p_extra); /* reverse "<12>" */
4996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004998#ifdef FEAT_LINEBREAK
4999 if (wp->w_p_lbr)
5000 {
5001 char_u *p;
5002
5003 c = *p_extra;
5004 p = alloc((unsigned)n_extra + 1);
5005 vim_memset(p, ' ', n_extra);
5006 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5007 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005008 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005009 p_extra_free = p_extra = p;
5010 }
5011 else
5012#endif
5013 {
5014 n_extra = byte2cells(c) - 1;
5015 c = *p_extra++;
5016 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005017 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 {
5019 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005020 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 saved_attr2 = char_attr; /* save current attr */
5022 }
5023#ifdef FEAT_MBYTE
5024 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5025#endif
5026 }
5027#ifdef FEAT_VIRTUALEDIT
5028 else if (VIsual_active
5029 && (VIsual_mode == Ctrl_V
5030 || VIsual_mode == 'v')
5031 && virtual_active()
5032 && tocol != MAXCOL
5033 && vcol < tocol
5034 && (
5035# ifdef FEAT_RIGHTLEFT
5036 wp->w_p_rl ? (col >= 0) :
5037# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005038 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
5040 c = ' ';
5041 --ptr; /* put it back at the NUL */
5042 }
5043#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005044#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 else if ((
5046# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005047 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005049# ifdef FEAT_TERMINAL
5050 term_attr != 0 ||
5051# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 ) && (
5054# ifdef FEAT_RIGHTLEFT
5055 wp->w_p_rl ? (col >= 0) :
5056# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005057 (col
5058# ifdef FEAT_CONCEAL
5059 - boguscols
5060# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005061 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
5063 /* Highlight until the right side of the window */
5064 c = ' ';
5065 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005066
5067 /* Remember we do the char for line highlighting. */
5068 ++did_line_attr;
5069
5070 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005071 if (line_attr != 0 && char_attr == search_attr
5072 && (did_line_attr > 1
5073 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005074 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075# ifdef FEAT_DIFF
5076 if (diff_hlf == HLF_TXD)
5077 {
5078 diff_hlf = HLF_CHD;
5079 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005080 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005081 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005082 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5083 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005084 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005088# ifdef FEAT_TERMINAL
5089 if (term_attr != 0)
5090 {
5091 char_attr = term_attr;
5092 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5093 char_attr = hl_combine_attr(char_attr,
5094 HL_ATTR(HLF_CUL));
5095 }
5096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098#endif
5099 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005100
5101#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005102 if ( wp->w_p_cole > 0
5103 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005104 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005105 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005106 && !(lnum_in_visual_area
5107 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005108 {
5109 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005110 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005111 && (syn_get_sub_char() != NUL || match_conc
5112 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005113 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005114 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005115 /* First time at this concealed item: display one
5116 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005117 if (match_conc)
5118 c = match_conc;
5119 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005120 c = syn_get_sub_char();
5121 else if (lcs_conceal != NUL)
5122 c = lcs_conceal;
5123 else
5124 c = ' ';
5125
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005126 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005127
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005128 if (n_extra > 0)
5129 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005130 vcol += n_extra;
5131 if (wp->w_p_wrap && n_extra > 0)
5132 {
5133# ifdef FEAT_RIGHTLEFT
5134 if (wp->w_p_rl)
5135 {
5136 col -= n_extra;
5137 boguscols -= n_extra;
5138 }
5139 else
5140# endif
5141 {
5142 boguscols += n_extra;
5143 col += n_extra;
5144 }
5145 }
5146 n_extra = 0;
5147 n_attr = 0;
5148 }
5149 else if (n_skip == 0)
5150 {
5151 is_concealing = TRUE;
5152 n_skip = 1;
5153 }
5154# ifdef FEAT_MBYTE
5155 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005156 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005157 {
5158 mb_utf8 = TRUE;
5159 u8cc[0] = 0;
5160 c = 0xc0;
5161 }
5162 else
5163 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5164# endif
5165 }
5166 else
5167 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005168 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005169 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005170 }
5171#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005174#ifdef FEAT_CONCEAL
5175 /* In the cursor line and we may be concealing characters: correct
5176 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005177 if (!did_wcol && draw_state == WL_LINE
5178 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005179 && conceal_cursor_line(wp)
5180 && (int)wp->w_virtcol <= vcol + n_skip)
5181 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005182# ifdef FEAT_RIGHTLEFT
5183 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005184 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005185 else
5186# endif
5187 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005188 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005189 did_wcol = TRUE;
5190 }
5191#endif
5192
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 /* Don't override visual selection highlighting. */
5194 if (n_attr > 0
5195 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005196 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 char_attr = extra_attr;
5198
Bram Moolenaar81695252004-12-29 20:58:21 +00005199#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 /* XIM don't send preedit_start and preedit_end, but they send
5201 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5202 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005203 if (p_imst == IM_ON_THE_SPOT
5204 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005205 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 && (State & INSERT)
5207 && !p_imdisable
5208 && im_is_preediting()
5209 && draw_state == WL_LINE)
5210 {
5211 colnr_T tcol;
5212
5213 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005214 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 else
5216 tcol = preedit_end_col;
5217 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5218 {
5219 if (feedback_old_attr < 0)
5220 {
5221 feedback_col = 0;
5222 feedback_old_attr = char_attr;
5223 }
5224 char_attr = im_get_feedback_attr(feedback_col);
5225 if (char_attr < 0)
5226 char_attr = feedback_old_attr;
5227 feedback_col++;
5228 }
5229 else if (feedback_old_attr >= 0)
5230 {
5231 char_attr = feedback_old_attr;
5232 feedback_old_attr = -1;
5233 feedback_col = 0;
5234 }
5235 }
5236#endif
5237 /*
5238 * Handle the case where we are in column 0 but not on the first
5239 * character of the line and the user wants us to show us a
5240 * special character (via 'listchars' option "precedes:<char>".
5241 */
5242 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005243 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5245#ifdef FEAT_DIFF
5246 && filler_todo <= 0
5247#endif
5248 && draw_state > WL_NR
5249 && c != NUL)
5250 {
5251 c = lcs_prec;
5252 lcs_prec_todo = NUL;
5253#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005254 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5255 {
5256 /* Double-width character being overwritten by the "precedes"
5257 * character, need to fill up half the character. */
5258 c_extra = MB_FILLER_CHAR;
5259 n_extra = 1;
5260 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005261 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005264 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
5266 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005267 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005268 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 }
5270 else
5271 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5272#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005273 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
5275 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005276 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 n_attr3 = 1;
5278 }
5279 }
5280
5281 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005282 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005284 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005285#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005286 || did_line_attr == 1
5287#endif
5288 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005290#ifdef FEAT_SEARCH_EXTRA
5291 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005292
5293 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005294 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005295 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005296#endif
5297
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005298 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 * highlight match at end of line. If it's beyond the last
5300 * char on the screen, just overwrite that one (tricky!) Not
5301 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005302#ifdef FEAT_SEARCH_EXTRA
5303 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005304 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005305 prevcol_hl_flag = TRUE;
5306 else
5307 {
5308 cur = wp->w_match_head;
5309 while (cur != NULL)
5310 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005311 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005312 {
5313 prevcol_hl_flag = TRUE;
5314 break;
5315 }
5316 cur = cur->next;
5317 }
5318 }
5319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005321 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005322 && (VIsual_mode != Ctrl_V
5323 || lnum == VIsual.lnum
5324 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005325 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326#ifdef FEAT_SEARCH_EXTRA
5327 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005328 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005329# ifdef FEAT_SYN_HL
5330 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5331 && !(wp == curwin && VIsual_active))
5332# endif
5333# ifdef FEAT_DIFF
5334 && diff_hlf == (hlf_T)0
5335# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005336# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005337 && did_line_attr <= 1
5338# endif
5339 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340#endif
5341 ))
5342 {
5343 int n = 0;
5344
5345#ifdef FEAT_RIGHTLEFT
5346 if (wp->w_p_rl)
5347 {
5348 if (col < 0)
5349 n = 1;
5350 }
5351 else
5352#endif
5353 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005354 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 n = -1;
5356 }
5357 if (n != 0)
5358 {
5359 /* At the window boundary, highlight the last character
5360 * instead (better than nothing). */
5361 off += n;
5362 col += n;
5363 }
5364 else
5365 {
5366 /* Add a blank character to highlight. */
5367 ScreenLines[off] = ' ';
5368#ifdef FEAT_MBYTE
5369 if (enc_utf8)
5370 ScreenLinesUC[off] = 0;
5371#endif
5372 }
5373#ifdef FEAT_SEARCH_EXTRA
5374 if (area_attr == 0)
5375 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005376 /* Use attributes from match with highest priority among
5377 * 'search_hl' and the match list. */
5378 char_attr = search_hl.attr;
5379 cur = wp->w_match_head;
5380 shl_flag = FALSE;
5381 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005382 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005383 if (shl_flag == FALSE
5384 && ((cur != NULL
5385 && cur->priority > SEARCH_HL_PRIORITY)
5386 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005387 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005388 shl = &search_hl;
5389 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005390 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005391 else
5392 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005393 if ((ptr - line) - 1 == (long)shl->startcol
5394 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005395 char_attr = shl->attr;
5396 if (shl != &search_hl && cur != NULL)
5397 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 }
5400#endif
5401 ScreenAttrs[off] = char_attr;
5402#ifdef FEAT_RIGHTLEFT
5403 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005404 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005406 --off;
5407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 else
5409#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005412 ++off;
5413 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005414 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005415#ifdef FEAT_SYN_HL
5416 eol_hl_off = 1;
5417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420
Bram Moolenaar91170f82006-05-05 21:15:17 +00005421 /*
5422 * At end of the text line.
5423 */
5424 if (c == NUL)
5425 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005426#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005427 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5428 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005429 {
5430 /* highlight last char after line */
5431 --col;
5432 --off;
5433 --vcol;
5434 }
5435
Bram Moolenaar1a384422010-07-14 19:53:30 +02005436 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005437 if (wp->w_p_wrap)
5438 v = wp->w_skipcol;
5439 else
5440 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005441
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005442 /* check if line ends before left margin */
5443 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005444 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005445#ifdef FEAT_CONCEAL
5446 /* Get rid of the boguscols now, we want to draw until the right
5447 * edge for 'cursorcolumn'. */
5448 col -= boguscols;
5449 boguscols = 0;
5450#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005451
5452 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005453 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005454
5455 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005456 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5457 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005458 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005459 && lnum != wp->w_cursor.lnum)
5460 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005461# ifdef FEAT_RIGHTLEFT
5462 && !wp->w_p_rl
5463# endif
5464 )
5465 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005466 int rightmost_vcol = 0;
5467 int i;
5468
5469 if (wp->w_p_cuc)
5470 rightmost_vcol = wp->w_virtcol;
5471 if (draw_color_col)
5472 /* determine rightmost colorcolumn to possibly draw */
5473 for (i = 0; color_cols[i] >= 0; ++i)
5474 if (rightmost_vcol < color_cols[i])
5475 rightmost_vcol = color_cols[i];
5476
Bram Moolenaar02631462017-09-22 15:20:32 +02005477 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005478 {
5479 ScreenLines[off] = ' ';
5480#ifdef FEAT_MBYTE
5481 if (enc_utf8)
5482 ScreenLinesUC[off] = 0;
5483#endif
5484 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005485 if (draw_color_col)
5486 draw_color_col = advance_color_col(VCOL_HLC,
5487 &color_cols);
5488
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005489 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005490 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005491 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005492 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005493 else
5494 ScreenAttrs[off++] = 0;
5495
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005496 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005497 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005498
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005499 ++vcol;
5500 }
5501 }
5502#endif
5503
Bram Moolenaar53f81742017-09-22 14:35:51 +02005504 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005505 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 row++;
5507
5508 /*
5509 * Update w_cline_height and w_cline_folded if the cursor line was
5510 * updated (saves a call to plines() later).
5511 */
5512 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5513 {
5514 curwin->w_cline_row = startrow;
5515 curwin->w_cline_height = row - startrow;
5516#ifdef FEAT_FOLDING
5517 curwin->w_cline_folded = FALSE;
5518#endif
5519 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5520 }
5521
5522 break;
5523 }
5524
5525 /* line continues beyond line end */
5526 if (lcs_ext
5527 && !wp->w_p_wrap
5528#ifdef FEAT_DIFF
5529 && filler_todo <= 0
5530#endif
5531 && (
5532#ifdef FEAT_RIGHTLEFT
5533 wp->w_p_rl ? col == 0 :
5534#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005535 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005537 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5539 {
5540 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005541 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542#ifdef FEAT_MBYTE
5543 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005544 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 {
5546 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005547 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005548 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
5550 else
5551 mb_utf8 = FALSE;
5552#endif
5553 }
5554
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005555#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005556 /* advance to the next 'colorcolumn' */
5557 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005558 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005559
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005560 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005561 * highlight the cursor position itself.
5562 * Also highlight the 'colorcolumn' if it is different than
5563 * 'cursorcolumn' */
5564 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005565 if (draw_state == WL_LINE && !lnum_in_visual_area
5566 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005567 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005568 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005569 && lnum != wp->w_cursor.lnum)
5570 {
5571 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005572 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005573 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005574 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005575 {
5576 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005577 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005578 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005579 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005580#endif
5581
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 /*
5583 * Store character to be displayed.
5584 * Skip characters that are left of the screen for 'nowrap'.
5585 */
5586 vcol_prev = vcol;
5587 if (draw_state < WL_LINE || n_skip <= 0)
5588 {
5589 /*
5590 * Store the character.
5591 */
5592#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5593 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5594 {
5595 /* A double-wide character is: put first halve in left cell. */
5596 --off;
5597 --col;
5598 }
5599#endif
5600 ScreenLines[off] = c;
5601#ifdef FEAT_MBYTE
5602 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005603 {
5604 if ((mb_c & 0xff00) == 0x8e00)
5605 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 else if (enc_utf8)
5609 {
5610 if (mb_utf8)
5611 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005612 int i;
5613
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005615 if ((c & 0xff) == 0)
5616 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005617 for (i = 0; i < Screen_mco; ++i)
5618 {
5619 ScreenLinesC[i][off] = u8cc[i];
5620 if (u8cc[i] == 0)
5621 break;
5622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
5624 else
5625 ScreenLinesUC[off] = 0;
5626 }
5627 if (multi_attr)
5628 {
5629 ScreenAttrs[off] = multi_attr;
5630 multi_attr = 0;
5631 }
5632 else
5633#endif
5634 ScreenAttrs[off] = char_attr;
5635
5636#ifdef FEAT_MBYTE
5637 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5638 {
5639 /* Need to fill two screen columns. */
5640 ++off;
5641 ++col;
5642 if (enc_utf8)
5643 /* UTF-8: Put a 0 in the second screen char. */
5644 ScreenLines[off] = 0;
5645 else
5646 /* DBCS: Put second byte in the second screen char. */
5647 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005648 if (draw_state > WL_NR
5649#ifdef FEAT_DIFF
5650 && filler_todo <= 0
5651#endif
5652 )
5653 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 /* When "tocol" is halfway a character, set it to the end of
5655 * the character, otherwise highlighting won't stop. */
5656 if (tocol == vcol)
5657 ++tocol;
5658#ifdef FEAT_RIGHTLEFT
5659 if (wp->w_p_rl)
5660 {
5661 /* now it's time to backup one cell */
5662 --off;
5663 --col;
5664 }
5665#endif
5666 }
5667#endif
5668#ifdef FEAT_RIGHTLEFT
5669 if (wp->w_p_rl)
5670 {
5671 --off;
5672 --col;
5673 }
5674 else
5675#endif
5676 {
5677 ++off;
5678 ++col;
5679 }
5680 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005681#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005682 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005683 {
5684 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005685 ++vcol_off;
5686 if (n_extra > 0)
5687 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005688 if (wp->w_p_wrap)
5689 {
5690 /*
5691 * Special voodoo required if 'wrap' is on.
5692 *
5693 * Advance the column indicator to force the line
5694 * drawing to wrap early. This will make the line
5695 * take up the same screen space when parts are concealed,
5696 * so that cursor line computations aren't messed up.
5697 *
5698 * To avoid the fictitious advance of 'col' causing
5699 * trailing junk to be written out of the screen line
5700 * we are building, 'boguscols' keeps track of the number
5701 * of bad columns we have advanced.
5702 */
5703 if (n_extra > 0)
5704 {
5705 vcol += n_extra;
5706# ifdef FEAT_RIGHTLEFT
5707 if (wp->w_p_rl)
5708 {
5709 col -= n_extra;
5710 boguscols -= n_extra;
5711 }
5712 else
5713# endif
5714 {
5715 col += n_extra;
5716 boguscols += n_extra;
5717 }
5718 n_extra = 0;
5719 n_attr = 0;
5720 }
5721
5722
5723# ifdef FEAT_MBYTE
5724 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5725 {
5726 /* Need to fill two screen columns. */
5727# ifdef FEAT_RIGHTLEFT
5728 if (wp->w_p_rl)
5729 {
5730 --boguscols;
5731 --col;
5732 }
5733 else
5734# endif
5735 {
5736 ++boguscols;
5737 ++col;
5738 }
5739 }
5740# endif
5741
5742# ifdef FEAT_RIGHTLEFT
5743 if (wp->w_p_rl)
5744 {
5745 --boguscols;
5746 --col;
5747 }
5748 else
5749# endif
5750 {
5751 ++boguscols;
5752 ++col;
5753 }
5754 }
5755 else
5756 {
5757 if (n_extra > 0)
5758 {
5759 vcol += n_extra;
5760 n_extra = 0;
5761 n_attr = 0;
5762 }
5763 }
5764
5765 }
5766#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 else
5768 --n_skip;
5769
Bram Moolenaar64486672010-05-16 15:46:46 +02005770 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5771 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005772 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773#ifdef FEAT_DIFF
5774 && filler_todo <= 0
5775#endif
5776 )
5777 ++vcol;
5778
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005779#ifdef FEAT_SYN_HL
5780 if (vcol_save_attr >= 0)
5781 char_attr = vcol_save_attr;
5782#endif
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 /* restore attributes after "predeces" in 'listchars' */
5785 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5786 char_attr = saved_attr3;
5787
5788 /* restore attributes after last 'listchars' or 'number' char */
5789 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5790 char_attr = saved_attr2;
5791
5792 /*
5793 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005794 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 */
5796 if ((
5797#ifdef FEAT_RIGHTLEFT
5798 wp->w_p_rl ? (col < 0) :
5799#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005800 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 && (*ptr != NUL
5802#ifdef FEAT_DIFF
5803 || filler_todo > 0
5804#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005805 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5807 )
5808 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005809#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005810 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005811 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005812 boguscols = 0;
5813#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005814 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005815 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 ++row;
5818 ++screen_row;
5819
5820 /* When not wrapping and finished diff lines, or when displayed
5821 * '$' and highlighting until last column, break here. */
5822 if ((!wp->w_p_wrap
5823#ifdef FEAT_DIFF
5824 && filler_todo <= 0
5825#endif
5826 ) || lcs_eol_one == -1)
5827 break;
5828
5829 /* When the window is too narrow draw all "@" lines. */
5830 if (draw_state != WL_LINE
5831#ifdef FEAT_DIFF
5832 && filler_todo <= 0
5833#endif
5834 )
5835 {
5836 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 row = endrow;
5839 }
5840
5841 /* When line got too long for screen break here. */
5842 if (row == endrow)
5843 {
5844 ++row;
5845 break;
5846 }
5847
5848 if (screen_cur_row == screen_row - 1
5849#ifdef FEAT_DIFF
5850 && filler_todo <= 0
5851#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005852 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 {
5854 /* Remember that the line wraps, used for modeless copy. */
5855 LineWraps[screen_row - 1] = TRUE;
5856
5857 /*
5858 * Special trick to make copy/paste of wrapped lines work with
5859 * xterm/screen: write an extra character beyond the end of
5860 * the line. This will work with all terminal types
5861 * (regardless of the xn,am settings).
5862 * Only do this on a fast tty.
5863 * Only do this if the cursor is on the current line
5864 * (something has been written in it).
5865 * Don't do this for the GUI.
5866 * Don't do this for double-width characters.
5867 * Don't do this for a window not at the right screen border.
5868 */
5869 if (p_tf
5870#ifdef FEAT_GUI
5871 && !gui.in_use
5872#endif
5873#ifdef FEAT_MBYTE
5874 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005875 && ((*mb_off2cells)(LineOffset[screen_row],
5876 LineOffset[screen_row] + screen_Columns)
5877 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005879 + (int)Columns - 2,
5880 LineOffset[screen_row] + screen_Columns)
5881 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882#endif
5883 )
5884 {
5885 /* First make sure we are at the end of the screen line,
5886 * then output the same character again to let the
5887 * terminal know about the wrap. If the terminal doesn't
5888 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005889 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 screen_char(LineOffset[screen_row - 1]
5891 + (unsigned)Columns - 1,
5892 screen_row - 1, (int)(Columns - 1));
5893
5894#ifdef FEAT_MBYTE
5895 /* When there is a multi-byte character, just output a
5896 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005897 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5898 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 out_char(' ');
5900 else
5901#endif
5902 out_char(ScreenLines[LineOffset[screen_row - 1]
5903 + (Columns - 1)]);
5904 /* force a redraw of the first char on the next line */
5905 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5906 screen_start(); /* don't know where cursor is now */
5907 }
5908 }
5909
5910 col = 0;
5911 off = (unsigned)(current_ScreenLine - ScreenLines);
5912#ifdef FEAT_RIGHTLEFT
5913 if (wp->w_p_rl)
5914 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005915 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 off += col;
5917 }
5918#endif
5919
5920 /* reset the drawing state for the start of a wrapped line */
5921 draw_state = WL_START;
5922 saved_n_extra = n_extra;
5923 saved_p_extra = p_extra;
5924 saved_c_extra = c_extra;
5925 saved_char_attr = char_attr;
5926 n_extra = 0;
5927 lcs_prec_todo = lcs_prec;
5928#ifdef FEAT_LINEBREAK
5929# ifdef FEAT_DIFF
5930 if (filler_todo <= 0)
5931# endif
5932 need_showbreak = TRUE;
5933#endif
5934#ifdef FEAT_DIFF
5935 --filler_todo;
5936 /* When the filler lines are actually below the last line of the
5937 * file, don't draw the line itself, break here. */
5938 if (filler_todo == 0 && wp->w_botfill)
5939 break;
5940#endif
5941 }
5942
5943 } /* for every character in the line */
5944
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005945#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005946 /* After an empty line check first word for capital. */
5947 if (*skipwhite(line) == NUL)
5948 {
5949 capcol_lnum = lnum + 1;
5950 cap_col = 0;
5951 }
5952#endif
5953
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005954 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 return row;
5956}
5957
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005958#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005959static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005960
5961/*
5962 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005963 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005964 */
5965 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005966comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005967{
5968 int i;
5969
5970 for (i = 0; i < Screen_mco; ++i)
5971 {
5972 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5973 return TRUE;
5974 if (ScreenLinesC[i][off_from] == 0)
5975 break;
5976 }
5977 return FALSE;
5978}
5979#endif
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981/*
5982 * Check whether the given character needs redrawing:
5983 * - the (first byte of the) character is different
5984 * - the attributes are different
5985 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005986 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 */
5988 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005989char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
5991 if (cols > 0
5992 && ((ScreenLines[off_from] != ScreenLines[off_to]
5993 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5994
5995#ifdef FEAT_MBYTE
5996 || (enc_dbcs != 0
5997 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5998 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5999 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6000 : (cols > 1 && ScreenLines[off_from + 1]
6001 != ScreenLines[off_to + 1])))
6002 || (enc_utf8
6003 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6004 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006005 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006006 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6007 && ScreenLines[off_from + 1]
6008 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009#endif
6010 ))
6011 return TRUE;
6012 return FALSE;
6013}
6014
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006015#if defined(FEAT_TERMINAL) || defined(PROTO)
6016/*
6017 * Return the index in ScreenLines[] for the current screen line.
6018 */
6019 int
6020screen_get_current_line_off()
6021{
6022 return (int)(current_ScreenLine - ScreenLines);
6023}
6024#endif
6025
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026/*
6027 * Move one "cooked" screen line to the screen, but only the characters that
6028 * have actually changed. Handle insert/delete character.
6029 * "coloff" gives the first column on the screen for this line.
6030 * "endcol" gives the columns where valid characters are.
6031 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6032 * needs to be cleared, negative otherwise.
6033 * "rlflag" is TRUE in a rightleft window:
6034 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6035 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6036 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006037 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006038screen_line(
6039 int row,
6040 int coloff,
6041 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006042 int clear_width,
6043 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044{
6045 unsigned off_from;
6046 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006047#ifdef FEAT_MBYTE
6048 unsigned max_off_from;
6049 unsigned max_off_to;
6050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 int force = FALSE; /* force update rest of the line */
6054 int redraw_this /* bool: does character need redraw? */
6055#ifdef FEAT_GUI
6056 = TRUE /* For GUI when while-loop empty */
6057#endif
6058 ;
6059 int redraw_next; /* redraw_this for next character */
6060#ifdef FEAT_MBYTE
6061 int clear_next = FALSE;
6062 int char_cells; /* 1: normal char */
6063 /* 2: occupies two display cells */
6064# define CHAR_CELLS char_cells
6065#else
6066# define CHAR_CELLS 1
6067#endif
6068
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006069 /* Check for illegal row and col, just in case. */
6070 if (row >= Rows)
6071 row = Rows - 1;
6072 if (endcol > Columns)
6073 endcol = Columns;
6074
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075# ifdef FEAT_CLIPBOARD
6076 clip_may_clear_selection(row, row);
6077# endif
6078
6079 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6080 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006081#ifdef FEAT_MBYTE
6082 max_off_from = off_from + screen_Columns;
6083 max_off_to = LineOffset[row] + screen_Columns;
6084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085
6086#ifdef FEAT_RIGHTLEFT
6087 if (rlflag)
6088 {
6089 /* Clear rest first, because it's left of the text. */
6090 if (clear_width > 0)
6091 {
6092 while (col <= endcol && ScreenLines[off_to] == ' '
6093 && ScreenAttrs[off_to] == 0
6094# ifdef FEAT_MBYTE
6095 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6096# endif
6097 )
6098 {
6099 ++off_to;
6100 ++col;
6101 }
6102 if (col <= endcol)
6103 screen_fill(row, row + 1, col + coloff,
6104 endcol + coloff + 1, ' ', ' ', 0);
6105 }
6106 col = endcol + 1;
6107 off_to = LineOffset[row] + col + coloff;
6108 off_from += col;
6109 endcol = (clear_width > 0 ? clear_width : -clear_width);
6110 }
6111#endif /* FEAT_RIGHTLEFT */
6112
6113 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6114
6115 while (col < endcol)
6116 {
6117#ifdef FEAT_MBYTE
6118 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006119 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 else
6121 char_cells = 1;
6122#endif
6123
6124 redraw_this = redraw_next;
6125 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6126 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6127
6128#ifdef FEAT_GUI
6129 /* If the next character was bold, then redraw the current character to
6130 * remove any pixels that might have spilt over into us. This only
6131 * happens in the GUI.
6132 */
6133 if (redraw_next && gui.in_use)
6134 {
6135 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006136 if (hl > HL_ALL)
6137 hl = syn_attr2attr(hl);
6138 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 redraw_this = TRUE;
6140 }
6141#endif
6142
6143 if (redraw_this)
6144 {
6145 /*
6146 * Special handling when 'xs' termcap flag set (hpterm):
6147 * Attributes for characters are stored at the position where the
6148 * cursor is when writing the highlighting code. The
6149 * start-highlighting code must be written with the cursor on the
6150 * first highlighted character. The stop-highlighting code must
6151 * be written with the cursor just after the last highlighted
6152 * character.
6153 * Overwriting a character doesn't remove it's highlighting. Need
6154 * to clear the rest of the line, and force redrawing it
6155 * completely.
6156 */
6157 if ( p_wiv
6158 && !force
6159#ifdef FEAT_GUI
6160 && !gui.in_use
6161#endif
6162 && ScreenAttrs[off_to] != 0
6163 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6164 {
6165 /*
6166 * Need to remove highlighting attributes here.
6167 */
6168 windgoto(row, col + coloff);
6169 out_str(T_CE); /* clear rest of this screen line */
6170 screen_start(); /* don't know where cursor is now */
6171 force = TRUE; /* force redraw of rest of the line */
6172 redraw_next = TRUE; /* or else next char would miss out */
6173
6174 /*
6175 * If the previous character was highlighted, need to stop
6176 * highlighting at this character.
6177 */
6178 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6179 {
6180 screen_attr = ScreenAttrs[off_to - 1];
6181 term_windgoto(row, col + coloff);
6182 screen_stop_highlight();
6183 }
6184 else
6185 screen_attr = 0; /* highlighting has stopped */
6186 }
6187#ifdef FEAT_MBYTE
6188 if (enc_dbcs != 0)
6189 {
6190 /* Check if overwriting a double-byte with a single-byte or
6191 * the other way around requires another character to be
6192 * redrawn. For UTF-8 this isn't needed, because comparing
6193 * ScreenLinesUC[] is sufficient. */
6194 if (char_cells == 1
6195 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006196 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 {
6198 /* Writing a single-cell character over a double-cell
6199 * character: need to redraw the next cell. */
6200 ScreenLines[off_to + 1] = 0;
6201 redraw_next = TRUE;
6202 }
6203 else if (char_cells == 2
6204 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006205 && (*mb_off2cells)(off_to, max_off_to) == 1
6206 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 {
6208 /* Writing the second half of a double-cell character over
6209 * a double-cell character: need to redraw the second
6210 * cell. */
6211 ScreenLines[off_to + 2] = 0;
6212 redraw_next = TRUE;
6213 }
6214
6215 if (enc_dbcs == DBCS_JPNU)
6216 ScreenLines2[off_to] = ScreenLines2[off_from];
6217 }
6218 /* When writing a single-width character over a double-width
6219 * character and at the end of the redrawn text, need to clear out
6220 * the right halve of the old character.
6221 * Also required when writing the right halve of a double-width
6222 * char over the left halve of an existing one. */
6223 if (has_mbyte && col + char_cells == endcol
6224 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006225 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006227 && (*mb_off2cells)(off_to, max_off_to) == 1
6228 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 clear_next = TRUE;
6230#endif
6231
6232 ScreenLines[off_to] = ScreenLines[off_from];
6233#ifdef FEAT_MBYTE
6234 if (enc_utf8)
6235 {
6236 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6237 if (ScreenLinesUC[off_from] != 0)
6238 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006239 int i;
6240
6241 for (i = 0; i < Screen_mco; ++i)
6242 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 }
6244 }
6245 if (char_cells == 2)
6246 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6247#endif
6248
6249#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006250 /* The bold trick makes a single column of pixels appear in the
6251 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 * character should be redrawn too. This happens for our own GUI
6253 * and for some xterms. */
6254 if (
6255# ifdef FEAT_GUI
6256 gui.in_use
6257# endif
6258# if defined(FEAT_GUI) && defined(UNIX)
6259 ||
6260# endif
6261# ifdef UNIX
6262 term_is_xterm
6263# endif
6264 )
6265 {
6266 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006267 if (hl > HL_ALL)
6268 hl = syn_attr2attr(hl);
6269 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 redraw_next = TRUE;
6271 }
6272#endif
6273 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6274#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006275 /* For simplicity set the attributes of second half of a
6276 * double-wide character equal to the first half. */
6277 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006279
6280 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 else
6283#endif
6284 screen_char(off_to, row, col + coloff);
6285 }
6286 else if ( p_wiv
6287#ifdef FEAT_GUI
6288 && !gui.in_use
6289#endif
6290 && col + coloff > 0)
6291 {
6292 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6293 {
6294 /*
6295 * Don't output stop-highlight when moving the cursor, it will
6296 * stop the highlighting when it should continue.
6297 */
6298 screen_attr = 0;
6299 }
6300 else if (screen_attr != 0)
6301 screen_stop_highlight();
6302 }
6303
6304 off_to += CHAR_CELLS;
6305 off_from += CHAR_CELLS;
6306 col += CHAR_CELLS;
6307 }
6308
6309#ifdef FEAT_MBYTE
6310 if (clear_next)
6311 {
6312 /* Clear the second half of a double-wide character of which the left
6313 * half was overwritten with a single-wide character. */
6314 ScreenLines[off_to] = ' ';
6315 if (enc_utf8)
6316 ScreenLinesUC[off_to] = 0;
6317 screen_char(off_to, row, col + coloff);
6318 }
6319#endif
6320
6321 if (clear_width > 0
6322#ifdef FEAT_RIGHTLEFT
6323 && !rlflag
6324#endif
6325 )
6326 {
6327#ifdef FEAT_GUI
6328 int startCol = col;
6329#endif
6330
6331 /* blank out the rest of the line */
6332 while (col < clear_width && ScreenLines[off_to] == ' '
6333 && ScreenAttrs[off_to] == 0
6334#ifdef FEAT_MBYTE
6335 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6336#endif
6337 )
6338 {
6339 ++off_to;
6340 ++col;
6341 }
6342 if (col < clear_width)
6343 {
6344#ifdef FEAT_GUI
6345 /*
6346 * In the GUI, clearing the rest of the line may leave pixels
6347 * behind if the first character cleared was bold. Some bold
6348 * fonts spill over the left. In this case we redraw the previous
6349 * character too. If we didn't skip any blanks above, then we
6350 * only redraw if the character wasn't already redrawn anyway.
6351 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006352 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 {
6354 hl = ScreenAttrs[off_to];
6355 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006356 {
6357 int prev_cells = 1;
6358# ifdef FEAT_MBYTE
6359 if (enc_utf8)
6360 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6361 * that its width is 2. */
6362 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6363 else if (enc_dbcs != 0)
6364 {
6365 /* find previous character by counting from first
6366 * column and get its width. */
6367 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006368 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006369
6370 while (off < off_to)
6371 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006372 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006373 off += prev_cells;
6374 }
6375 }
6376
6377 if (enc_dbcs != 0 && prev_cells > 1)
6378 screen_char_2(off_to - prev_cells, row,
6379 col + coloff - prev_cells);
6380 else
6381# endif
6382 screen_char(off_to - prev_cells, row,
6383 col + coloff - prev_cells);
6384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 }
6386#endif
6387 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6388 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 off_to += clear_width - col;
6390 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 }
6392 }
6393
6394 if (clear_width > 0)
6395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 /* For a window that's left of another, draw the separator char. */
6397 if (col + coloff < Columns)
6398 {
6399 int c;
6400
6401 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006402 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006403#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006404 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6405 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 || ScreenAttrs[off_to] != hl)
6408 {
6409 ScreenLines[off_to] = c;
6410 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006411#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 if (enc_utf8)
6413 {
6414 if (c >= 0x80)
6415 {
6416 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006417 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419 else
6420 ScreenLinesUC[off_to] = 0;
6421 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 screen_char(off_to, row, col + coloff);
6424 }
6425 }
6426 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 LineWraps[row] = FALSE;
6428 }
6429}
6430
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006431#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006433 * Mirror text "str" for right-left displaying.
6434 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006436 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006437rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438{
6439 char_u *p1, *p2;
6440 int t;
6441
6442 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6443 {
6444 t = *p1;
6445 *p1 = *p2;
6446 *p2 = t;
6447 }
6448}
6449#endif
6450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451/*
6452 * mark all status lines for redraw; used after first :cd
6453 */
6454 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006455status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
6457 win_T *wp;
6458
Bram Moolenaar29323592016-07-24 22:04:11 +02006459 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 if (wp->w_status_height)
6461 {
6462 wp->w_redr_status = TRUE;
6463 redraw_later(VALID);
6464 }
6465}
6466
6467/*
6468 * mark all status lines of the current buffer for redraw
6469 */
6470 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006471status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472{
6473 win_T *wp;
6474
Bram Moolenaar29323592016-07-24 22:04:11 +02006475 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6477 {
6478 wp->w_redr_status = TRUE;
6479 redraw_later(VALID);
6480 }
6481}
6482
6483/*
6484 * Redraw all status lines that need to be redrawn.
6485 */
6486 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006487redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488{
6489 win_T *wp;
6490
Bram Moolenaar29323592016-07-24 22:04:11 +02006491 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 if (wp->w_redr_status)
6493 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006494 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006495 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
Bram Moolenaar4033c552017-09-16 20:54:51 +02006498#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499/*
6500 * Redraw all status lines at the bottom of frame "frp".
6501 */
6502 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006503win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 if (frp->fr_layout == FR_LEAF)
6506 frp->fr_win->w_redr_status = TRUE;
6507 else if (frp->fr_layout == FR_ROW)
6508 {
6509 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6510 win_redraw_last_status(frp);
6511 }
6512 else /* frp->fr_layout == FR_COL */
6513 {
6514 frp = frp->fr_child;
6515 while (frp->fr_next != NULL)
6516 frp = frp->fr_next;
6517 win_redraw_last_status(frp);
6518 }
6519}
6520#endif
6521
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522/*
6523 * Draw the verticap separator right of window "wp" starting with line "row".
6524 */
6525 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006526draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527{
6528 int hl;
6529 int c;
6530
6531 if (wp->w_vsep_width)
6532 {
6533 /* draw the vertical separator right of this window */
6534 c = fillchar_vsep(&hl);
6535 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6536 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6537 c, ' ', hl);
6538 }
6539}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540
6541#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006542static int status_match_len(expand_T *xp, char_u *s);
6543static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
6545/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006546 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 */
6548 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006549status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
6551 int len = 0;
6552
6553#ifdef FEAT_MENU
6554 int emenu = (xp->xp_context == EXPAND_MENUS
6555 || xp->xp_context == EXPAND_MENUNAMES);
6556
6557 /* Check for menu separators - replace with '|'. */
6558 if (emenu && menu_is_separator(s))
6559 return 1;
6560#endif
6561
6562 while (*s != NUL)
6563 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006564 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006565 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006566 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 }
6568
6569 return len;
6570}
6571
6572/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006573 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006574 * These are backslashes used for escaping. Do show backslashes in help tags.
6575 */
6576 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006577skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006578{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006579 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006580#ifdef FEAT_MENU
6581 || ((xp->xp_context == EXPAND_MENUS
6582 || xp->xp_context == EXPAND_MENUNAMES)
6583 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6584#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006585 )
6586 {
6587#ifndef BACKSLASH_IN_FILENAME
6588 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6589 return 2;
6590#endif
6591 return 1;
6592 }
6593 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006594}
6595
6596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 * Show wildchar matches in the status line.
6598 * Show at least the "match" item.
6599 * We start at item 'first_match' in the list and show all matches that fit.
6600 *
6601 * If inversion is possible we use it. Else '=' characters are used.
6602 */
6603 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006604win_redr_status_matches(
6605 expand_T *xp,
6606 int num_matches,
6607 char_u **matches, /* list of matches */
6608 int match,
6609 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
6611#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6612 int row;
6613 char_u *buf;
6614 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006615 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 int fillchar;
6617 int attr;
6618 int i;
6619 int highlight = TRUE;
6620 char_u *selstart = NULL;
6621 int selstart_col = 0;
6622 char_u *selend = NULL;
6623 static int first_match = 0;
6624 int add_left = FALSE;
6625 char_u *s;
6626#ifdef FEAT_MENU
6627 int emenu;
6628#endif
6629#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6630 int l;
6631#endif
6632
6633 if (matches == NULL) /* interrupted completion? */
6634 return;
6635
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006636#ifdef FEAT_MBYTE
6637 if (has_mbyte)
6638 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6639 else
6640#endif
6641 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 if (buf == NULL)
6643 return;
6644
6645 if (match == -1) /* don't show match but original text */
6646 {
6647 match = 0;
6648 highlight = FALSE;
6649 }
6650 /* count 1 for the ending ">" */
6651 clen = status_match_len(xp, L_MATCH(match)) + 3;
6652 if (match == 0)
6653 first_match = 0;
6654 else if (match < first_match)
6655 {
6656 /* jumping left, as far as we can go */
6657 first_match = match;
6658 add_left = TRUE;
6659 }
6660 else
6661 {
6662 /* check if match fits on the screen */
6663 for (i = first_match; i < match; ++i)
6664 clen += status_match_len(xp, L_MATCH(i)) + 2;
6665 if (first_match > 0)
6666 clen += 2;
6667 /* jumping right, put match at the left */
6668 if ((long)clen > Columns)
6669 {
6670 first_match = match;
6671 /* if showing the last match, we can add some on the left */
6672 clen = 2;
6673 for (i = match; i < num_matches; ++i)
6674 {
6675 clen += status_match_len(xp, L_MATCH(i)) + 2;
6676 if ((long)clen >= Columns)
6677 break;
6678 }
6679 if (i == num_matches)
6680 add_left = TRUE;
6681 }
6682 }
6683 if (add_left)
6684 while (first_match > 0)
6685 {
6686 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6687 if ((long)clen >= Columns)
6688 break;
6689 --first_match;
6690 }
6691
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006692 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
6694 if (first_match == 0)
6695 {
6696 *buf = NUL;
6697 len = 0;
6698 }
6699 else
6700 {
6701 STRCPY(buf, "< ");
6702 len = 2;
6703 }
6704 clen = len;
6705
6706 i = first_match;
6707 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6708 {
6709 if (i == match)
6710 {
6711 selstart = buf + len;
6712 selstart_col = clen;
6713 }
6714
6715 s = L_MATCH(i);
6716 /* Check for menu separators - replace with '|' */
6717#ifdef FEAT_MENU
6718 emenu = (xp->xp_context == EXPAND_MENUS
6719 || xp->xp_context == EXPAND_MENUNAMES);
6720 if (emenu && menu_is_separator(s))
6721 {
6722 STRCPY(buf + len, transchar('|'));
6723 l = (int)STRLEN(buf + len);
6724 len += l;
6725 clen += l;
6726 }
6727 else
6728#endif
6729 for ( ; *s != NUL; ++s)
6730 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006731 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 clen += ptr2cells(s);
6733#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006734 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 {
6736 STRNCPY(buf + len, s, l);
6737 s += l - 1;
6738 len += l;
6739 }
6740 else
6741#endif
6742 {
6743 STRCPY(buf + len, transchar_byte(*s));
6744 len += (int)STRLEN(buf + len);
6745 }
6746 }
6747 if (i == match)
6748 selend = buf + len;
6749
6750 *(buf + len++) = ' ';
6751 *(buf + len++) = ' ';
6752 clen += 2;
6753 if (++i == num_matches)
6754 break;
6755 }
6756
6757 if (i != num_matches)
6758 {
6759 *(buf + len++) = '>';
6760 ++clen;
6761 }
6762
6763 buf[len] = NUL;
6764
6765 row = cmdline_row - 1;
6766 if (row >= 0)
6767 {
6768 if (wild_menu_showing == 0)
6769 {
6770 if (msg_scrolled > 0)
6771 {
6772 /* Put the wildmenu just above the command line. If there is
6773 * no room, scroll the screen one line up. */
6774 if (cmdline_row == Rows - 1)
6775 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006776 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 ++msg_scrolled;
6778 }
6779 else
6780 {
6781 ++cmdline_row;
6782 ++row;
6783 }
6784 wild_menu_showing = WM_SCROLLED;
6785 }
6786 else
6787 {
6788 /* Create status line if needed by setting 'laststatus' to 2.
6789 * Set 'winminheight' to zero to avoid that the window is
6790 * resized. */
6791 if (lastwin->w_status_height == 0)
6792 {
6793 save_p_ls = p_ls;
6794 save_p_wmh = p_wmh;
6795 p_ls = 2;
6796 p_wmh = 0;
6797 last_status(FALSE);
6798 }
6799 wild_menu_showing = WM_SHOWN;
6800 }
6801 }
6802
6803 screen_puts(buf, row, 0, attr);
6804 if (selstart != NULL && highlight)
6805 {
6806 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006807 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 }
6809
6810 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6811 }
6812
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 vim_free(buf);
6815}
6816#endif
6817
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818/*
6819 * Redraw the status line of window wp.
6820 *
6821 * If inversion is possible we use it. Else '=' characters are used.
6822 */
6823 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006824win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825{
6826 int row;
6827 char_u *p;
6828 int len;
6829 int fillchar;
6830 int attr;
6831 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006832 static int busy = FALSE;
6833
6834 /* It's possible to get here recursively when 'statusline' (indirectly)
6835 * invokes ":redrawstatus". Simply ignore the call then. */
6836 if (busy)
6837 return;
6838 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839
6840 wp->w_redr_status = FALSE;
6841 if (wp->w_status_height == 0)
6842 {
6843 /* no status line, can only be last window */
6844 redraw_cmdline = TRUE;
6845 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006846 else if (!redrawing()
6847#ifdef FEAT_INS_EXPAND
6848 /* don't update status line when popup menu is visible and may be
6849 * drawn over it */
6850 || pum_visible()
6851#endif
6852 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {
6854 /* Don't redraw right now, do it later. */
6855 wp->w_redr_status = TRUE;
6856 }
6857#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006858 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {
6860 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006861 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 }
6863#endif
6864 else
6865 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006866 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006868 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 p = NameBuff;
6870 len = (int)STRLEN(p);
6871
Bram Moolenaard85f2712017-07-28 21:51:57 +02006872 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873#ifdef FEAT_QUICKFIX
6874 || wp->w_p_pvw
6875#endif
6876 || bufIsChanged(wp->w_buffer)
6877 || wp->w_buffer->b_p_ro)
6878 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006879 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006881 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 len += (int)STRLEN(p + len);
6883 }
6884#ifdef FEAT_QUICKFIX
6885 if (wp->w_p_pvw)
6886 {
6887 STRCPY(p + len, _("[Preview]"));
6888 len += (int)STRLEN(p + len);
6889 }
6890#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006891 if (bufIsChanged(wp->w_buffer)
6892#ifdef FEAT_TERMINAL
6893 && !bt_terminal(wp->w_buffer)
6894#endif
6895 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 {
6897 STRCPY(p + len, "[+]");
6898 len += 3;
6899 }
6900 if (wp->w_buffer->b_p_ro)
6901 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006902 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006903 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 }
6905
Bram Moolenaar02631462017-09-22 15:20:32 +02006906 this_ru_col = ru_col - (Columns - wp->w_width);
6907 if (this_ru_col < (wp->w_width + 1) / 2)
6908 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 if (this_ru_col <= 1)
6910 {
6911 p = (char_u *)"<"; /* No room for file name! */
6912 len = 1;
6913 }
6914 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915#ifdef FEAT_MBYTE
6916 if (has_mbyte)
6917 {
6918 int clen = 0, i;
6919
6920 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006921 clen = mb_string2cells(p, -1);
6922
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 /* Find first character that will fit.
6924 * Going from start to end is much faster for DBCS. */
6925 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006926 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 clen -= (*mb_ptr2cells)(p + i);
6928 len = clen;
6929 if (i > 0)
6930 {
6931 p = p + i - 1;
6932 *p = '<';
6933 ++len;
6934 }
6935
6936 }
6937 else
6938#endif
6939 if (len > this_ru_col - 1)
6940 {
6941 p += len - (this_ru_col - 1);
6942 *p = '<';
6943 len = this_ru_col - 1;
6944 }
6945
6946 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006947 screen_puts(p, row, wp->w_wincol, attr);
6948 screen_fill(row, row + 1, len + wp->w_wincol,
6949 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006951 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6953 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006954 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956#ifdef FEAT_CMDL_INFO
6957 win_redr_ruler(wp, TRUE);
6958#endif
6959 }
6960
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 /*
6962 * May need to draw the character below the vertical separator.
6963 */
6964 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6965 {
6966 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006967 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 else
6969 fillchar = fillchar_vsep(&attr);
6970 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6971 attr);
6972 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006973 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974}
6975
Bram Moolenaar238a5642006-02-21 22:12:05 +00006976#ifdef FEAT_STL_OPT
6977/*
6978 * Redraw the status line according to 'statusline' and take care of any
6979 * errors encountered.
6980 */
6981 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006982redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006983{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006984 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006985 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006986
6987 /* When called recursively return. This can happen when the statusline
6988 * contains an expression that triggers a redraw. */
6989 if (entered)
6990 return;
6991 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006992
Bram Moolenaara742e082016-04-05 21:10:38 +02006993 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006994 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006995 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006996 {
6997 /* When there is an error disable the statusline, otherwise the
6998 * display is messed up with errors and a redraw triggers the problem
6999 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007000 set_string_option_direct((char_u *)"statusline", -1,
7001 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007002 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007003 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007004 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007005 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007006}
7007#endif
7008
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009/*
7010 * Return TRUE if the status line of window "wp" is connected to the status
7011 * line of the window right of it. If not, then it's a vertical separator.
7012 * Only call if (wp->w_vsep_width != 0).
7013 */
7014 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007015stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016{
7017 frame_T *fr;
7018
7019 fr = wp->w_frame;
7020 while (fr->fr_parent != NULL)
7021 {
7022 if (fr->fr_parent->fr_layout == FR_COL)
7023 {
7024 if (fr->fr_next != NULL)
7025 break;
7026 }
7027 else
7028 {
7029 if (fr->fr_next != NULL)
7030 return TRUE;
7031 }
7032 fr = fr->fr_parent;
7033 }
7034 return FALSE;
7035}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038/*
7039 * Get the value to show for the language mappings, active 'keymap'.
7040 */
7041 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007042get_keymap_str(
7043 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007044 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007045 char_u *buf, /* buffer for the result */
7046 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
7048 char_u *p;
7049
7050 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7051 return FALSE;
7052
7053 {
7054#ifdef FEAT_EVAL
7055 buf_T *old_curbuf = curbuf;
7056 win_T *old_curwin = curwin;
7057 char_u *s;
7058
7059 curbuf = wp->w_buffer;
7060 curwin = wp;
7061 STRCPY(buf, "b:keymap_name"); /* must be writable */
7062 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007063 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 --emsg_skip;
7065 curbuf = old_curbuf;
7066 curwin = old_curwin;
7067 if (p == NULL || *p == NUL)
7068#endif
7069 {
7070#ifdef FEAT_KEYMAP
7071 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7072 p = wp->w_buffer->b_p_keymap;
7073 else
7074#endif
7075 p = (char_u *)"lang";
7076 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007077 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 buf[0] = NUL;
7079#ifdef FEAT_EVAL
7080 vim_free(s);
7081#endif
7082 }
7083 return buf[0] != NUL;
7084}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085
7086#if defined(FEAT_STL_OPT) || defined(PROTO)
7087/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007088 * Redraw the status line or ruler of window "wp".
7089 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 */
7091 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007092win_redr_custom(
7093 win_T *wp,
7094 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007096 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 int attr;
7098 int curattr;
7099 int row;
7100 int col = 0;
7101 int maxwidth;
7102 int width;
7103 int n;
7104 int len;
7105 int fillchar;
7106 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007107 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007109 struct stl_hlrec hltab[STL_MAX_ITEM];
7110 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007111 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007112 win_T *ewp;
7113 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114
Bram Moolenaar1d633412013-12-11 15:52:01 +01007115 /* There is a tiny chance that this gets called recursively: When
7116 * redrawing a status line triggers redrawing the ruler or tabline.
7117 * Avoid trouble by not allowing recursion. */
7118 if (entered)
7119 return;
7120 entered = TRUE;
7121
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007123 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007125 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007126 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007127 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007128 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007129 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007130 maxwidth = Columns;
7131# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007132 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007133# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007135 else
7136 {
7137 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007138 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007139 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007140
7141 if (draw_ruler)
7142 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007143 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007144 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007145 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007146 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007147 if (*++stl == '-')
7148 stl++;
7149 if (atoi((char *)stl))
7150 while (VIM_ISDIGIT(*stl))
7151 stl++;
7152 if (*stl++ != '(')
7153 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007154 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007155 col = ru_col - (Columns - wp->w_width);
7156 if (col < (wp->w_width + 1) / 2)
7157 col = (wp->w_width + 1) / 2;
7158 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007159 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007160 {
7161 row = Rows - 1;
7162 --maxwidth; /* writing in last column may cause scrolling */
7163 fillchar = ' ';
7164 attr = 0;
7165 }
7166
7167# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007168 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007169# endif
7170 }
7171 else
7172 {
7173 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007174 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007175 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007176 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007177# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007178 use_sandbox = was_set_insecurely((char_u *)"statusline",
7179 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007180# endif
7181 }
7182
Bram Moolenaar53f81742017-09-22 14:35:51 +02007183 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007184 }
7185
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007187 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
Bram Moolenaar61452852011-02-01 18:01:11 +01007189 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7190 * the cursor away and back. */
7191 ewp = wp == NULL ? curwin : wp;
7192 p_crb_save = ewp->w_p_crb;
7193 ewp->w_p_crb = FALSE;
7194
Bram Moolenaar362f3562009-11-03 16:20:34 +00007195 /* Make a copy, because the statusline may include a function call that
7196 * might change the option value and free the memory. */
7197 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007198 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007199 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007200 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007201 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007202 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007204 /* Make all characters printable. */
7205 p = transstr(buf);
7206 if (p != NULL)
7207 {
7208 vim_strncpy(buf, p, sizeof(buf) - 1);
7209 vim_free(p);
7210 }
7211
7212 /* fill up with "fillchar" */
7213 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007214 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {
7216#ifdef FEAT_MBYTE
7217 len += (*mb_char2bytes)(fillchar, buf + len);
7218#else
7219 buf[len++] = fillchar;
7220#endif
7221 ++width;
7222 }
7223 buf[len] = NUL;
7224
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007225 /*
7226 * Draw each snippet with the specified highlighting.
7227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 curattr = attr;
7229 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007230 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007232 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 screen_puts_len(p, len, row, col, curattr);
7234 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007235 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007237 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007239 else if (hltab[n].userhl < 0)
7240 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007241#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007242 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7243 && wp->w_status_height != 0)
7244 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007245 else if (wp != NULL && bt_terminal(wp->w_buffer)
7246 && wp->w_status_height != 0)
7247 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007248#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007249 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007250 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007252 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 }
7254 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007255
7256 if (wp == NULL)
7257 {
7258 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7259 col = 0;
7260 len = 0;
7261 p = buf;
7262 fillchar = 0;
7263 for (n = 0; tabtab[n].start != NULL; n++)
7264 {
7265 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7266 while (col < len)
7267 TabPageIdxs[col++] = fillchar;
7268 p = tabtab[n].start;
7269 fillchar = tabtab[n].userhl;
7270 }
7271 while (col < Columns)
7272 TabPageIdxs[col++] = fillchar;
7273 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007274
7275theend:
7276 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277}
7278
7279#endif /* FEAT_STL_OPT */
7280
7281/*
7282 * Output a single character directly to the screen and update ScreenLines.
7283 */
7284 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007285screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 char_u buf[MB_MAXBYTES + 1];
7288
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007289#ifdef FEAT_MBYTE
7290 if (has_mbyte)
7291 buf[(*mb_char2bytes)(c, buf)] = NUL;
7292 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007294 {
7295 buf[0] = c;
7296 buf[1] = NUL;
7297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 screen_puts(buf, row, col, attr);
7299}
7300
7301/*
7302 * Get a single character directly from ScreenLines into "bytes[]".
7303 * Also return its attribute in *attrp;
7304 */
7305 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007306screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
7308 unsigned off;
7309
7310 /* safety check */
7311 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7312 {
7313 off = LineOffset[row] + col;
7314 *attrp = ScreenAttrs[off];
7315 bytes[0] = ScreenLines[off];
7316 bytes[1] = NUL;
7317
7318#ifdef FEAT_MBYTE
7319 if (enc_utf8 && ScreenLinesUC[off] != 0)
7320 bytes[utfc_char2bytes(off, bytes)] = NUL;
7321 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7322 {
7323 bytes[0] = ScreenLines[off];
7324 bytes[1] = ScreenLines2[off];
7325 bytes[2] = NUL;
7326 }
7327 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7328 {
7329 bytes[1] = ScreenLines[off + 1];
7330 bytes[2] = NUL;
7331 }
7332#endif
7333 }
7334}
7335
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007336#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007337static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007338
7339/*
7340 * Return TRUE if composing characters for screen posn "off" differs from
7341 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007342 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007343 */
7344 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007345screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007346{
7347 int i;
7348
7349 for (i = 0; i < Screen_mco; ++i)
7350 {
7351 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7352 return TRUE;
7353 if (u8cc[i] == 0)
7354 break;
7355 }
7356 return FALSE;
7357}
7358#endif
7359
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360/*
7361 * Put string '*text' on the screen at position 'row' and 'col', with
7362 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7363 * Note: only outputs within one row, message is truncated at screen boundary!
7364 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7365 */
7366 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007367screen_puts(
7368 char_u *text,
7369 int row,
7370 int col,
7371 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372{
7373 screen_puts_len(text, -1, row, col, attr);
7374}
7375
7376/*
7377 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7378 * a NUL.
7379 */
7380 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007381screen_puts_len(
7382 char_u *text,
7383 int textlen,
7384 int row,
7385 int col,
7386 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387{
7388 unsigned off;
7389 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007390 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 int c;
7392#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007393 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 int mbyte_blen = 1;
7395 int mbyte_cells = 1;
7396 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007397 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 int clear_next_cell = FALSE;
7399# ifdef FEAT_ARABIC
7400 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007401 int pc, nc, nc1;
7402 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403# endif
7404#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007405#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7406 int force_redraw_this;
7407 int force_redraw_next = FALSE;
7408#endif
7409 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410
7411 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7412 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007413 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
Bram Moolenaarc236c162008-07-13 17:41:49 +00007415#ifdef FEAT_MBYTE
7416 /* When drawing over the right halve of a double-wide char clear out the
7417 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007418 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007419# ifdef FEAT_GUI
7420 && !gui.in_use
7421# endif
7422 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007423 {
7424 ScreenLines[off - 1] = ' ';
7425 ScreenAttrs[off - 1] = 0;
7426 if (enc_utf8)
7427 {
7428 ScreenLinesUC[off - 1] = 0;
7429 ScreenLinesC[0][off - 1] = 0;
7430 }
7431 /* redraw the previous cell, make it empty */
7432 screen_char(off - 1, row, col - 1);
7433 /* force the cell at "col" to be redrawn */
7434 force_redraw_next = TRUE;
7435 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007436#endif
7437
Bram Moolenaar367329b2007-08-30 11:53:22 +00007438#ifdef FEAT_MBYTE
7439 max_off = LineOffset[row] + screen_Columns;
7440#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007441 while (col < screen_Columns
7442 && (len < 0 || (int)(ptr - text) < len)
7443 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 {
7445 c = *ptr;
7446#ifdef FEAT_MBYTE
7447 /* check if this is the first byte of a multibyte */
7448 if (has_mbyte)
7449 {
7450 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007451 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007453 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7455 mbyte_cells = 1;
7456 else if (enc_dbcs != 0)
7457 mbyte_cells = mbyte_blen;
7458 else /* enc_utf8 */
7459 {
7460 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007461 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 (int)((text + len) - ptr));
7463 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007464 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007466# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 /* Non-BMP character: display as ? or fullwidth ?. */
7468 if (u8c >= 0x10000)
7469 {
7470 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7471 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007472 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007474# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475# ifdef FEAT_ARABIC
7476 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7477 {
7478 /* Do Arabic shaping. */
7479 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7480 {
7481 /* Past end of string to be displayed. */
7482 nc = NUL;
7483 nc1 = NUL;
7484 }
7485 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007486 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007487 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7488 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007489 nc1 = pcc[0];
7490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 pc = prev_c;
7492 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007493 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 }
7495 else
7496 prev_c = u8c;
7497# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007498 if (col + mbyte_cells > screen_Columns)
7499 {
7500 /* Only 1 cell left, but character requires 2 cells:
7501 * display a '>' in the last column to avoid wrapping. */
7502 c = '>';
7503 mbyte_cells = 1;
7504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 }
7506 }
7507#endif
7508
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007509#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7510 force_redraw_this = force_redraw_next;
7511 force_redraw_next = FALSE;
7512#endif
7513
7514 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515#ifdef FEAT_MBYTE
7516 || (mbyte_cells == 2
7517 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7518 || (enc_dbcs == DBCS_JPNU
7519 && c == 0x8e
7520 && ScreenLines2[off] != ptr[1])
7521 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007522 && (ScreenLinesUC[off] !=
7523 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7524 || (ScreenLinesUC[off] != 0
7525 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526#endif
7527 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007528 || exmode_active;
7529
7530 if (need_redraw
7531#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7532 || force_redraw_this
7533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 )
7535 {
7536#if defined(FEAT_GUI) || defined(UNIX)
7537 /* The bold trick makes a single row of pixels appear in the next
7538 * character. When a bold character is removed, the next
7539 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007540 * and for some xterms. */
7541 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542# ifdef FEAT_GUI
7543 gui.in_use
7544# endif
7545# if defined(FEAT_GUI) && defined(UNIX)
7546 ||
7547# endif
7548# ifdef UNIX
7549 term_is_xterm
7550# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007551 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007553 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007555 if (n > HL_ALL)
7556 n = syn_attr2attr(n);
7557 if (n & HL_BOLD)
7558 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 }
7560#endif
7561#ifdef FEAT_MBYTE
7562 /* When at the end of the text and overwriting a two-cell
7563 * character with a one-cell character, need to clear the next
7564 * cell. Also when overwriting the left halve of a two-cell char
7565 * with the right halve of a two-cell char. Do this only once
7566 * (mb_off2cells() may return 2 on the right halve). */
7567 if (clear_next_cell)
7568 clear_next_cell = FALSE;
7569 else if (has_mbyte
7570 && (len < 0 ? ptr[mbyte_blen] == NUL
7571 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007572 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007574 && (*mb_off2cells)(off, max_off) == 1
7575 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 clear_next_cell = TRUE;
7577
7578 /* Make sure we never leave a second byte of a double-byte behind,
7579 * it confuses mb_off2cells(). */
7580 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007581 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007583 && (*mb_off2cells)(off, max_off) == 1
7584 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 ScreenLines[off + mbyte_blen] = 0;
7586#endif
7587 ScreenLines[off] = c;
7588 ScreenAttrs[off] = attr;
7589#ifdef FEAT_MBYTE
7590 if (enc_utf8)
7591 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007592 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 ScreenLinesUC[off] = 0;
7594 else
7595 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007596 int i;
7597
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007599 for (i = 0; i < Screen_mco; ++i)
7600 {
7601 ScreenLinesC[i][off] = u8cc[i];
7602 if (u8cc[i] == 0)
7603 break;
7604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 }
7606 if (mbyte_cells == 2)
7607 {
7608 ScreenLines[off + 1] = 0;
7609 ScreenAttrs[off + 1] = attr;
7610 }
7611 screen_char(off, row, col);
7612 }
7613 else if (mbyte_cells == 2)
7614 {
7615 ScreenLines[off + 1] = ptr[1];
7616 ScreenAttrs[off + 1] = attr;
7617 screen_char_2(off, row, col);
7618 }
7619 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7620 {
7621 ScreenLines2[off] = ptr[1];
7622 screen_char(off, row, col);
7623 }
7624 else
7625#endif
7626 screen_char(off, row, col);
7627 }
7628#ifdef FEAT_MBYTE
7629 if (has_mbyte)
7630 {
7631 off += mbyte_cells;
7632 col += mbyte_cells;
7633 ptr += mbyte_blen;
7634 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007635 {
7636 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007638 len = -1;
7639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 }
7641 else
7642#endif
7643 {
7644 ++off;
7645 ++col;
7646 ++ptr;
7647 }
7648 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007649
7650#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7651 /* If we detected the next character needs to be redrawn, but the text
7652 * doesn't extend up to there, update the character here. */
7653 if (force_redraw_next && col < screen_Columns)
7654 {
7655# ifdef FEAT_MBYTE
7656 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7657 screen_char_2(off, row, col);
7658 else
7659# endif
7660 screen_char(off, row, col);
7661 }
7662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663}
7664
7665#ifdef FEAT_SEARCH_EXTRA
7666/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007667 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 */
7669 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007670start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
7672 if (p_hls && !no_hlsearch)
7673 {
7674 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007675 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007676# ifdef FEAT_RELTIME
7677 /* Set the time limit to 'redrawtime'. */
7678 profile_setlimit(p_rdt, &search_hl.tm);
7679# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 }
7681}
7682
7683/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007684 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 */
7686 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007687end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688{
7689 if (search_hl.rm.regprog != NULL)
7690 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007691 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 search_hl.rm.regprog = NULL;
7693 }
7694}
7695
7696/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007697 * Init for calling prepare_search_hl().
7698 */
7699 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007700init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007701{
7702 matchitem_T *cur;
7703
7704 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7705 * match */
7706 cur = wp->w_match_head;
7707 while (cur != NULL)
7708 {
7709 cur->hl.rm = cur->match;
7710 if (cur->hlg_id == 0)
7711 cur->hl.attr = 0;
7712 else
7713 cur->hl.attr = syn_id2attr(cur->hlg_id);
7714 cur->hl.buf = wp->w_buffer;
7715 cur->hl.lnum = 0;
7716 cur->hl.first_lnum = 0;
7717# ifdef FEAT_RELTIME
7718 /* Set the time limit to 'redrawtime'. */
7719 profile_setlimit(p_rdt, &(cur->hl.tm));
7720# endif
7721 cur = cur->next;
7722 }
7723 search_hl.buf = wp->w_buffer;
7724 search_hl.lnum = 0;
7725 search_hl.first_lnum = 0;
7726 /* time limit is set at the toplevel, for all windows */
7727}
7728
7729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 * Advance to the match in window "wp" line "lnum" or past it.
7731 */
7732 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007733prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007735 matchitem_T *cur; /* points to the match list */
7736 match_T *shl; /* points to search_hl or a match */
7737 int shl_flag; /* flag to indicate whether search_hl
7738 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007739 int pos_inprogress; /* marks that position match search is
7740 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 int n;
7742
7743 /*
7744 * When using a multi-line pattern, start searching at the top
7745 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007746 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007748 cur = wp->w_match_head;
7749 shl_flag = FALSE;
7750 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007752 if (shl_flag == FALSE)
7753 {
7754 shl = &search_hl;
7755 shl_flag = TRUE;
7756 }
7757 else
7758 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 if (shl->rm.regprog != NULL
7760 && shl->lnum == 0
7761 && re_multiline(shl->rm.regprog))
7762 {
7763 if (shl->first_lnum == 0)
7764 {
7765# ifdef FEAT_FOLDING
7766 for (shl->first_lnum = lnum;
7767 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7768 if (hasFoldingWin(wp, shl->first_lnum - 1,
7769 NULL, NULL, TRUE, NULL))
7770 break;
7771# else
7772 shl->first_lnum = wp->w_topline;
7773# endif
7774 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007775 if (cur != NULL)
7776 cur->pos.cur = 0;
7777 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007779 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7780 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007782 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7783 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007784 pos_inprogress = cur == NULL || cur->pos.cur == 0
7785 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 if (shl->lnum != 0)
7787 {
7788 shl->first_lnum = shl->lnum
7789 + shl->rm.endpos[0].lnum
7790 - shl->rm.startpos[0].lnum;
7791 n = shl->rm.endpos[0].col;
7792 }
7793 else
7794 {
7795 ++shl->first_lnum;
7796 n = 0;
7797 }
7798 }
7799 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007800 if (shl != &search_hl && cur != NULL)
7801 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 }
7803}
7804
7805/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007806 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 * Uses shl->buf.
7808 * Sets shl->lnum and shl->rm contents.
7809 * Note: Assumes a previous match is always before "lnum", unless
7810 * shl->lnum is zero.
7811 * Careful: Any pointers for buffer lines will become invalid.
7812 */
7813 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007814next_search_hl(
7815 win_T *win,
7816 match_T *shl, /* points to search_hl or a match */
7817 linenr_T lnum,
7818 colnr_T mincol, /* minimal column for a match */
7819 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820{
7821 linenr_T l;
7822 colnr_T matchcol;
7823 long nmatched;
7824
7825 if (shl->lnum != 0)
7826 {
7827 /* Check for three situations:
7828 * 1. If the "lnum" is below a previous match, start a new search.
7829 * 2. If the previous match includes "mincol", use it.
7830 * 3. Continue after the previous match.
7831 */
7832 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7833 if (lnum > l)
7834 shl->lnum = 0;
7835 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7836 return;
7837 }
7838
7839 /*
7840 * Repeat searching for a match until one is found that includes "mincol"
7841 * or none is found in this line.
7842 */
7843 called_emsg = FALSE;
7844 for (;;)
7845 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007846#ifdef FEAT_RELTIME
7847 /* Stop searching after passing the time limit. */
7848 if (profile_passed_limit(&(shl->tm)))
7849 {
7850 shl->lnum = 0; /* no match found in time */
7851 break;
7852 }
7853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 /* Three situations:
7855 * 1. No useful previous match: search from start of line.
7856 * 2. Not Vi compatible or empty match: continue at next character.
7857 * Break the loop if this is beyond the end of the line.
7858 * 3. Vi compatible searching: continue at end of previous match.
7859 */
7860 if (shl->lnum == 0)
7861 matchcol = 0;
7862 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7863 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007864 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007866 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007867
7868 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007869 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007870 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007872 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 shl->lnum = 0;
7874 break;
7875 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007876#ifdef FEAT_MBYTE
7877 if (has_mbyte)
7878 matchcol += mb_ptr2len(ml);
7879 else
7880#endif
7881 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 }
7883 else
7884 matchcol = shl->rm.endpos[0].col;
7885
7886 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007887 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007889 /* Remember whether shl->rm is using a copy of the regprog in
7890 * cur->match. */
7891 int regprog_is_copy = (shl != &search_hl && cur != NULL
7892 && shl == &cur->hl
7893 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007894 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007895
Bram Moolenaarb3414592014-06-17 17:48:32 +02007896 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7897 matchcol,
7898#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007899 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007900#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007901 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007902#endif
7903 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007904 /* Copy the regprog, in case it got freed and recompiled. */
7905 if (regprog_is_copy)
7906 cur->match.regprog = cur->hl.rm.regprog;
7907
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007908 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007909 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007910 /* Error while handling regexp: stop using this regexp. */
7911 if (shl == &search_hl)
7912 {
7913 /* don't free regprog in the match list, it's a copy */
7914 vim_regfree(shl->rm.regprog);
7915 SET_NO_HLSEARCH(TRUE);
7916 }
7917 shl->rm.regprog = NULL;
7918 shl->lnum = 0;
7919 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7920 message */
7921 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007922 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007923 }
7924 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007925 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007926 else
7927 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 if (nmatched == 0)
7929 {
7930 shl->lnum = 0; /* no match found */
7931 break;
7932 }
7933 if (shl->rm.startpos[0].lnum > 0
7934 || shl->rm.startpos[0].col >= mincol
7935 || nmatched > 1
7936 || shl->rm.endpos[0].col > mincol)
7937 {
7938 shl->lnum += shl->rm.startpos[0].lnum;
7939 break; /* useful match found */
7940 }
7941 }
7942}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943
Bram Moolenaar85077472016-10-16 14:35:48 +02007944/*
7945 * If there is a match fill "shl" and return one.
7946 * Return zero otherwise.
7947 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007948 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007949next_search_hl_pos(
7950 match_T *shl, /* points to a match */
7951 linenr_T lnum,
7952 posmatch_T *posmatch, /* match positions */
7953 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007954{
7955 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007956 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007957
Bram Moolenaarb3414592014-06-17 17:48:32 +02007958 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7959 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007960 llpos_T *pos = &posmatch->pos[i];
7961
7962 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007963 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007964 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007965 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007966 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007967 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007968 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007969 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007970 /* if this match comes before the one at "found" then swap
7971 * them */
7972 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007973 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007974 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007975
Bram Moolenaar85077472016-10-16 14:35:48 +02007976 *pos = posmatch->pos[found];
7977 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007978 }
7979 }
7980 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007981 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007982 }
7983 }
7984 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007985 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007986 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007987 colnr_T start = posmatch->pos[found].col == 0
7988 ? 0 : posmatch->pos[found].col - 1;
7989 colnr_T end = posmatch->pos[found].col == 0
7990 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007991
Bram Moolenaar85077472016-10-16 14:35:48 +02007992 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007993 shl->rm.startpos[0].lnum = 0;
7994 shl->rm.startpos[0].col = start;
7995 shl->rm.endpos[0].lnum = 0;
7996 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007997 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007998 posmatch->cur = found + 1;
7999 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008000 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008001 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008002}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008003#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008004
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008006screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007{
8008 attrentry_T *aep = NULL;
8009
8010 screen_attr = attr;
8011 if (full_screen
8012#ifdef WIN3264
8013 && termcap_active
8014#endif
8015 )
8016 {
8017#ifdef FEAT_GUI
8018 if (gui.in_use)
8019 {
8020 char buf[20];
8021
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008022 /* The GUI handles this internally. */
8023 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 OUT_STR(buf);
8025 }
8026 else
8027#endif
8028 {
8029 if (attr > HL_ALL) /* special HL attr. */
8030 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008031 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 aep = syn_cterm_attr2entry(attr);
8033 else
8034 aep = syn_term_attr2entry(attr);
8035 if (aep == NULL) /* did ":syntax clear" */
8036 attr = 0;
8037 else
8038 attr = aep->ae_attr;
8039 }
8040 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8041 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008042 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008043#ifdef FEAT_TERMGUICOLORS
8044 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008045 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008046#endif
8047 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008048#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008049 )
8050#endif
8051 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008052 /* If the Normal FG color has BOLD attribute and the new HL
8053 * has a FG color defined, clear BOLD. */
8054 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8056 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008057 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8058 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 out_str(T_US);
8060 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8061 out_str(T_CZH);
8062 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8063 out_str(T_MR);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008064 if ((attr & HL_STRIKETHROUGH) && T_STS != NULL) /* strike */
8065 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066
8067 /*
8068 * Output the color or start string after bold etc., in case the
8069 * bold etc. override the color setting.
8070 */
8071 if (aep != NULL)
8072 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008073#ifdef FEAT_TERMGUICOLORS
8074 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008076 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008077 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008078 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008079 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 }
8081 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008084 if (t_colors > 1)
8085 {
8086 if (aep->ae_u.cterm.fg_color)
8087 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8088 if (aep->ae_u.cterm.bg_color)
8089 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8090 }
8091 else
8092 {
8093 if (aep->ae_u.term.start != NULL)
8094 out_str(aep->ae_u.term.start);
8095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 }
8097 }
8098 }
8099 }
8100}
8101
8102 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008103screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104{
8105 int do_ME = FALSE; /* output T_ME code */
8106
8107 if (screen_attr != 0
8108#ifdef WIN3264
8109 && termcap_active
8110#endif
8111 )
8112 {
8113#ifdef FEAT_GUI
8114 if (gui.in_use)
8115 {
8116 char buf[20];
8117
8118 /* use internal GUI code */
8119 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8120 OUT_STR(buf);
8121 }
8122 else
8123#endif
8124 {
8125 if (screen_attr > HL_ALL) /* special HL attr. */
8126 {
8127 attrentry_T *aep;
8128
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008129 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {
8131 /*
8132 * Assume that t_me restores the original colors!
8133 */
8134 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008135 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008136#ifdef FEAT_TERMGUICOLORS
8137 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008138 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8139 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008140#endif
8141 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008142#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008143 )
8144#endif
8145 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 do_ME = TRUE;
8147 }
8148 else
8149 {
8150 aep = syn_term_attr2entry(screen_attr);
8151 if (aep != NULL && aep->ae_u.term.stop != NULL)
8152 {
8153 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8154 do_ME = TRUE;
8155 else
8156 out_str(aep->ae_u.term.stop);
8157 }
8158 }
8159 if (aep == NULL) /* did ":syntax clear" */
8160 screen_attr = 0;
8161 else
8162 screen_attr = aep->ae_attr;
8163 }
8164
8165 /*
8166 * Often all ending-codes are equal to T_ME. Avoid outputting the
8167 * same sequence several times.
8168 */
8169 if (screen_attr & HL_STANDOUT)
8170 {
8171 if (STRCMP(T_SE, T_ME) == 0)
8172 do_ME = TRUE;
8173 else
8174 out_str(T_SE);
8175 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008176 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {
8178 if (STRCMP(T_UE, T_ME) == 0)
8179 do_ME = TRUE;
8180 else
8181 out_str(T_UE);
8182 }
8183 if (screen_attr & HL_ITALIC)
8184 {
8185 if (STRCMP(T_CZR, T_ME) == 0)
8186 do_ME = TRUE;
8187 else
8188 out_str(T_CZR);
8189 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008190 if (screen_attr & HL_STRIKETHROUGH)
8191 {
8192 if (STRCMP(T_STE, T_ME) == 0)
8193 do_ME = TRUE;
8194 else
8195 out_str(T_STE);
8196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8198 out_str(T_ME);
8199
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008200#ifdef FEAT_TERMGUICOLORS
8201 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008203 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008204 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008205 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008206 term_bg_rgb_color(cterm_normal_bg_gui_color);
8207 }
8208 else
8209#endif
8210 {
8211 if (t_colors > 1)
8212 {
8213 /* set Normal cterm colors */
8214 if (cterm_normal_fg_color != 0)
8215 term_fg_color(cterm_normal_fg_color - 1);
8216 if (cterm_normal_bg_color != 0)
8217 term_bg_color(cterm_normal_bg_color - 1);
8218 if (cterm_normal_fg_bold)
8219 out_str(T_MD);
8220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 }
8222 }
8223 }
8224 screen_attr = 0;
8225}
8226
8227/*
8228 * Reset the colors for a cterm. Used when leaving Vim.
8229 * The machine specific code may override this again.
8230 */
8231 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008232reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008234 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {
8236 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008237#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008238 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8239 || cterm_normal_bg_gui_color != INVALCOLOR)
8240 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008241#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {
8245 out_str(T_OP);
8246 screen_attr = -1;
8247 }
8248 if (cterm_normal_fg_bold)
8249 {
8250 out_str(T_ME);
8251 screen_attr = -1;
8252 }
8253 }
8254}
8255
8256/*
8257 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8258 * using the attributes from ScreenAttrs["off"].
8259 */
8260 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008261screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262{
8263 int attr;
8264
8265 /* Check for illegal values, just in case (could happen just after
8266 * resizing). */
8267 if (row >= screen_Rows || col >= screen_Columns)
8268 return;
8269
Bram Moolenaar494838a2015-02-10 19:20:37 +01008270 /* Outputting a character in the last cell on the screen may scroll the
8271 * screen up. Only do it when the "xn" termcap property is set, otherwise
8272 * mark the character invalid (update it when scrolled up). */
8273 if (*T_XN == NUL
8274 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275#ifdef FEAT_RIGHTLEFT
8276 /* account for first command-line character in rightleft mode */
8277 && !cmdmsg_rl
8278#endif
8279 )
8280 {
8281 ScreenAttrs[off] = (sattr_T)-1;
8282 return;
8283 }
8284
8285 /*
8286 * Stop highlighting first, so it's easier to move the cursor.
8287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 if (screen_char_attr != 0)
8289 attr = screen_char_attr;
8290 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 attr = ScreenAttrs[off];
8292 if (screen_attr != attr)
8293 screen_stop_highlight();
8294
8295 windgoto(row, col);
8296
8297 if (screen_attr != attr)
8298 screen_start_highlight(attr);
8299
8300#ifdef FEAT_MBYTE
8301 if (enc_utf8 && ScreenLinesUC[off] != 0)
8302 {
8303 char_u buf[MB_MAXBYTES + 1];
8304
8305 /* Convert UTF-8 character to bytes and write it. */
8306
8307 buf[utfc_char2bytes(off, buf)] = NUL;
8308
8309 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008310 if (utf_ambiguous_width(ScreenLinesUC[off]))
8311 screen_cur_col = 9999;
8312 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 ++screen_cur_col;
8314 }
8315 else
8316#endif
8317 {
8318#ifdef FEAT_MBYTE
8319 out_flush_check();
8320#endif
8321 out_char(ScreenLines[off]);
8322#ifdef FEAT_MBYTE
8323 /* double-byte character in single-width cell */
8324 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8325 out_char(ScreenLines2[off]);
8326#endif
8327 }
8328
8329 screen_cur_col++;
8330}
8331
8332#ifdef FEAT_MBYTE
8333
8334/*
8335 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8336 * on the screen at position 'row' and 'col'.
8337 * The attributes of the first byte is used for all. This is required to
8338 * output the two bytes of a double-byte character with nothing in between.
8339 */
8340 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008341screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342{
8343 /* Check for illegal values (could be wrong when screen was resized). */
8344 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8345 return;
8346
8347 /* Outputting the last character on the screen may scrollup the screen.
8348 * Don't to it! Mark the character invalid (update it when scrolled up) */
8349 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8350 {
8351 ScreenAttrs[off] = (sattr_T)-1;
8352 return;
8353 }
8354
8355 /* Output the first byte normally (positions the cursor), then write the
8356 * second byte directly. */
8357 screen_char(off, row, col);
8358 out_char(ScreenLines[off + 1]);
8359 ++screen_cur_col;
8360}
8361#endif
8362
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363/*
8364 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8365 * This uses the contents of ScreenLines[] and doesn't change it.
8366 */
8367 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008368screen_draw_rectangle(
8369 int row,
8370 int col,
8371 int height,
8372 int width,
8373 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374{
8375 int r, c;
8376 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008377#ifdef FEAT_MBYTE
8378 int max_off;
8379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008381 /* Can't use ScreenLines unless initialized */
8382 if (ScreenLines == NULL)
8383 return;
8384
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 if (invert)
8386 screen_char_attr = HL_INVERSE;
8387 for (r = row; r < row + height; ++r)
8388 {
8389 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008390#ifdef FEAT_MBYTE
8391 max_off = off + screen_Columns;
8392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 for (c = col; c < col + width; ++c)
8394 {
8395#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008396 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {
8398 screen_char_2(off + c, r, c);
8399 ++c;
8400 }
8401 else
8402#endif
8403 {
8404 screen_char(off + c, r, c);
8405#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008406 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 ++c;
8408#endif
8409 }
8410 }
8411 }
8412 screen_char_attr = 0;
8413}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415/*
8416 * Redraw the characters for a vertically split window.
8417 */
8418 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008419redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420{
8421 int col;
8422 int width;
8423
8424# ifdef FEAT_CLIPBOARD
8425 clip_may_clear_selection(row, end - 1);
8426# endif
8427
8428 if (wp == NULL)
8429 {
8430 col = 0;
8431 width = Columns;
8432 }
8433 else
8434 {
8435 col = wp->w_wincol;
8436 width = wp->w_width;
8437 }
8438 screen_draw_rectangle(row, col, end - row, width, FALSE);
8439}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008441 static void
8442space_to_screenline(int off, int attr)
8443{
8444 ScreenLines[off] = ' ';
8445 ScreenAttrs[off] = attr;
8446# ifdef FEAT_MBYTE
8447 if (enc_utf8)
8448 ScreenLinesUC[off] = 0;
8449# endif
8450}
8451
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452/*
8453 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8454 * with character 'c1' in first column followed by 'c2' in the other columns.
8455 * Use attributes 'attr'.
8456 */
8457 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008458screen_fill(
8459 int start_row,
8460 int end_row,
8461 int start_col,
8462 int end_col,
8463 int c1,
8464 int c2,
8465 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466{
8467 int row;
8468 int col;
8469 int off;
8470 int end_off;
8471 int did_delete;
8472 int c;
8473 int norm_term;
8474#if defined(FEAT_GUI) || defined(UNIX)
8475 int force_next = FALSE;
8476#endif
8477
8478 if (end_row > screen_Rows) /* safety check */
8479 end_row = screen_Rows;
8480 if (end_col > screen_Columns) /* safety check */
8481 end_col = screen_Columns;
8482 if (ScreenLines == NULL
8483 || start_row >= end_row
8484 || start_col >= end_col) /* nothing to do */
8485 return;
8486
8487 /* it's a "normal" terminal when not in a GUI or cterm */
8488 norm_term = (
8489#ifdef FEAT_GUI
8490 !gui.in_use &&
8491#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008492 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 for (row = start_row; row < end_row; ++row)
8494 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008495#ifdef FEAT_MBYTE
8496 if (has_mbyte
8497# ifdef FEAT_GUI
8498 && !gui.in_use
8499# endif
8500 )
8501 {
8502 /* When drawing over the right halve of a double-wide char clear
8503 * out the left halve. When drawing over the left halve of a
8504 * double wide-char clear out the right halve. Only needed in a
8505 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008506 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008507 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008508 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008509 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008510 }
8511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 /*
8513 * Try to use delete-line termcap code, when no attributes or in a
8514 * "normal" terminal, where a bold/italic space is just a
8515 * space.
8516 */
8517 did_delete = FALSE;
8518 if (c2 == ' '
8519 && end_col == Columns
8520 && can_clear(T_CE)
8521 && (attr == 0
8522 || (norm_term
8523 && attr <= HL_ALL
8524 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8525 {
8526 /*
8527 * check if we really need to clear something
8528 */
8529 col = start_col;
8530 if (c1 != ' ') /* don't clear first char */
8531 ++col;
8532
8533 off = LineOffset[row] + col;
8534 end_off = LineOffset[row] + end_col;
8535
8536 /* skip blanks (used often, keep it fast!) */
8537#ifdef FEAT_MBYTE
8538 if (enc_utf8)
8539 while (off < end_off && ScreenLines[off] == ' '
8540 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8541 ++off;
8542 else
8543#endif
8544 while (off < end_off && ScreenLines[off] == ' '
8545 && ScreenAttrs[off] == 0)
8546 ++off;
8547 if (off < end_off) /* something to be cleared */
8548 {
8549 col = off - LineOffset[row];
8550 screen_stop_highlight();
8551 term_windgoto(row, col);/* clear rest of this screen line */
8552 out_str(T_CE);
8553 screen_start(); /* don't know where cursor is now */
8554 col = end_col - col;
8555 while (col--) /* clear chars in ScreenLines */
8556 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008557 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 ++off;
8559 }
8560 }
8561 did_delete = TRUE; /* the chars are cleared now */
8562 }
8563
8564 off = LineOffset[row] + start_col;
8565 c = c1;
8566 for (col = start_col; col < end_col; ++col)
8567 {
8568 if (ScreenLines[off] != c
8569#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008570 || (enc_utf8 && (int)ScreenLinesUC[off]
8571 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572#endif
8573 || ScreenAttrs[off] != attr
8574#if defined(FEAT_GUI) || defined(UNIX)
8575 || force_next
8576#endif
8577 )
8578 {
8579#if defined(FEAT_GUI) || defined(UNIX)
8580 /* The bold trick may make a single row of pixels appear in
8581 * the next character. When a bold character is removed, the
8582 * next character should be redrawn too. This happens for our
8583 * own GUI and for some xterms. */
8584 if (
8585# ifdef FEAT_GUI
8586 gui.in_use
8587# endif
8588# if defined(FEAT_GUI) && defined(UNIX)
8589 ||
8590# endif
8591# ifdef UNIX
8592 term_is_xterm
8593# endif
8594 )
8595 {
8596 if (ScreenLines[off] != ' '
8597 && (ScreenAttrs[off] > HL_ALL
8598 || ScreenAttrs[off] & HL_BOLD))
8599 force_next = TRUE;
8600 else
8601 force_next = FALSE;
8602 }
8603#endif
8604 ScreenLines[off] = c;
8605#ifdef FEAT_MBYTE
8606 if (enc_utf8)
8607 {
8608 if (c >= 0x80)
8609 {
8610 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008611 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 }
8613 else
8614 ScreenLinesUC[off] = 0;
8615 }
8616#endif
8617 ScreenAttrs[off] = attr;
8618 if (!did_delete || c != ' ')
8619 screen_char(off, row, col);
8620 }
8621 ++off;
8622 if (col == start_col)
8623 {
8624 if (did_delete)
8625 break;
8626 c = c2;
8627 }
8628 }
8629 if (end_col == Columns)
8630 LineWraps[row] = FALSE;
8631 if (row == Rows - 1) /* overwritten the command line */
8632 {
8633 redraw_cmdline = TRUE;
8634 if (c1 == ' ' && c2 == ' ')
8635 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008636 if (start_col == 0)
8637 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 }
8639 }
8640}
8641
8642/*
8643 * Check if there should be a delay. Used before clearing or redrawing the
8644 * screen or the command line.
8645 */
8646 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008647check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648{
8649 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8650 && !did_wait_return
8651 && emsg_silent == 0)
8652 {
8653 out_flush();
8654 ui_delay(1000L, TRUE);
8655 emsg_on_display = FALSE;
8656 if (check_msg_scroll)
8657 msg_scroll = FALSE;
8658 }
8659}
8660
8661/*
8662 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008663 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 * Returns TRUE if there is a valid screen to write to.
8665 * Returns FALSE when starting up and screen not initialized yet.
8666 */
8667 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008668screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008670 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 return (ScreenLines != NULL);
8672}
8673
8674/*
8675 * Resize the shell to Rows and Columns.
8676 * Allocate ScreenLines[] and associated items.
8677 *
8678 * There may be some time between setting Rows and Columns and (re)allocating
8679 * ScreenLines[]. This happens when starting up and when (manually) changing
8680 * the shell size. Always use screen_Rows and screen_Columns to access items
8681 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8682 * final size of the shell is needed.
8683 */
8684 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008685screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686{
8687 int new_row, old_row;
8688#ifdef FEAT_GUI
8689 int old_Rows;
8690#endif
8691 win_T *wp;
8692 int outofmem = FALSE;
8693 int len;
8694 schar_T *new_ScreenLines;
8695#ifdef FEAT_MBYTE
8696 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008697 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008699 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700#endif
8701 sattr_T *new_ScreenAttrs;
8702 unsigned *new_LineOffset;
8703 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008704 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008705 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008707 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008708#ifdef FEAT_AUTOCMD
8709 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008711retry:
8712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 /*
8714 * Allocation of the screen buffers is done only when the size changes and
8715 * when Rows and Columns have been set and we have started doing full
8716 * screen stuff.
8717 */
8718 if ((ScreenLines != NULL
8719 && Rows == screen_Rows
8720 && Columns == screen_Columns
8721#ifdef FEAT_MBYTE
8722 && enc_utf8 == (ScreenLinesUC != NULL)
8723 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008724 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725#endif
8726 )
8727 || Rows == 0
8728 || Columns == 0
8729 || (!full_screen && ScreenLines == NULL))
8730 return;
8731
8732 /*
8733 * It's possible that we produce an out-of-memory message below, which
8734 * will cause this function to be called again. To break the loop, just
8735 * return here.
8736 */
8737 if (entered)
8738 return;
8739 entered = TRUE;
8740
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008741 /*
8742 * Note that the window sizes are updated before reallocating the arrays,
8743 * thus we must not redraw here!
8744 */
8745 ++RedrawingDisabled;
8746
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 win_new_shellsize(); /* fit the windows in the new sized shell */
8748
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 comp_col(); /* recompute columns for shown command and ruler */
8750
8751 /*
8752 * We're changing the size of the screen.
8753 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8754 * - Move lines from the old arrays into the new arrays, clear extra
8755 * lines (unless the screen is going to be cleared).
8756 * - Free the old arrays.
8757 *
8758 * If anything fails, make ScreenLines NULL, so we don't do anything!
8759 * Continuing with the old ScreenLines may result in a crash, because the
8760 * size is wrong.
8761 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008762 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008764#ifdef FEAT_AUTOCMD
8765 if (aucmd_win != NULL)
8766 win_free_lsize(aucmd_win);
8767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768
8769 new_ScreenLines = (schar_T *)lalloc((long_u)(
8770 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8771#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008772 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 if (enc_utf8)
8774 {
8775 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8776 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008777 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008778 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8780 }
8781 if (enc_dbcs == DBCS_JPNU)
8782 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8783 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8784#endif
8785 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8786 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8787 new_LineOffset = (unsigned *)lalloc((long_u)(
8788 Rows * sizeof(unsigned)), FALSE);
8789 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008790 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008792 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 {
8794 if (win_alloc_lines(wp) == FAIL)
8795 {
8796 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008797 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 }
8799 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008800#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008801 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8802 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008803 outofmem = TRUE;
8804#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008805give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008807#ifdef FEAT_MBYTE
8808 for (i = 0; i < p_mco; ++i)
8809 if (new_ScreenLinesC[i] == NULL)
8810 break;
8811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 if (new_ScreenLines == NULL
8813#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008814 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8816#endif
8817 || new_ScreenAttrs == NULL
8818 || new_LineOffset == NULL
8819 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008820 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 || outofmem)
8822 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008823 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008824 {
8825 /* guess the size */
8826 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8827
8828 /* Remember we did this to avoid getting outofmem messages over
8829 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008830 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 vim_free(new_ScreenLines);
8833 new_ScreenLines = NULL;
8834#ifdef FEAT_MBYTE
8835 vim_free(new_ScreenLinesUC);
8836 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008837 for (i = 0; i < p_mco; ++i)
8838 {
8839 vim_free(new_ScreenLinesC[i]);
8840 new_ScreenLinesC[i] = NULL;
8841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 vim_free(new_ScreenLines2);
8843 new_ScreenLines2 = NULL;
8844#endif
8845 vim_free(new_ScreenAttrs);
8846 new_ScreenAttrs = NULL;
8847 vim_free(new_LineOffset);
8848 new_LineOffset = NULL;
8849 vim_free(new_LineWraps);
8850 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008851 vim_free(new_TabPageIdxs);
8852 new_TabPageIdxs = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 }
8854 else
8855 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008856 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008857
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 for (new_row = 0; new_row < Rows; ++new_row)
8859 {
8860 new_LineOffset[new_row] = new_row * Columns;
8861 new_LineWraps[new_row] = FALSE;
8862
8863 /*
8864 * If the screen is not going to be cleared, copy as much as
8865 * possible from the old screen to the new one and clear the rest
8866 * (used when resizing the window at the "--more--" prompt or when
8867 * executing an external command, for the GUI).
8868 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008869 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 {
8871 (void)vim_memset(new_ScreenLines + new_row * Columns,
8872 ' ', (size_t)Columns * sizeof(schar_T));
8873#ifdef FEAT_MBYTE
8874 if (enc_utf8)
8875 {
8876 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8877 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008878 for (i = 0; i < p_mco; ++i)
8879 (void)vim_memset(new_ScreenLinesC[i]
8880 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 0, (size_t)Columns * sizeof(u8char_T));
8882 }
8883 if (enc_dbcs == DBCS_JPNU)
8884 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8885 0, (size_t)Columns * sizeof(schar_T));
8886#endif
8887 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8888 0, (size_t)Columns * sizeof(sattr_T));
8889 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008890 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 {
8892 if (screen_Columns < Columns)
8893 len = screen_Columns;
8894 else
8895 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008896#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008897 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008898 * may be invalid now. Also when p_mco changes. */
8899 if (!(enc_utf8 && ScreenLinesUC == NULL)
8900 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008901#endif
8902 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8903 ScreenLines + LineOffset[old_row],
8904 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008906 if (enc_utf8 && ScreenLinesUC != NULL
8907 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 {
8909 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8910 ScreenLinesUC + LineOffset[old_row],
8911 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008912 for (i = 0; i < p_mco; ++i)
8913 mch_memmove(new_ScreenLinesC[i]
8914 + new_LineOffset[new_row],
8915 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 (size_t)len * sizeof(u8char_T));
8917 }
8918 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8919 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8920 ScreenLines2 + LineOffset[old_row],
8921 (size_t)len * sizeof(schar_T));
8922#endif
8923 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8924 ScreenAttrs + LineOffset[old_row],
8925 (size_t)len * sizeof(sattr_T));
8926 }
8927 }
8928 }
8929 /* Use the last line of the screen for the current line. */
8930 current_ScreenLine = new_ScreenLines + Rows * Columns;
8931 }
8932
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008933 free_screenlines();
8934
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 ScreenLines = new_ScreenLines;
8936#ifdef FEAT_MBYTE
8937 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008938 for (i = 0; i < p_mco; ++i)
8939 ScreenLinesC[i] = new_ScreenLinesC[i];
8940 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 ScreenLines2 = new_ScreenLines2;
8942#endif
8943 ScreenAttrs = new_ScreenAttrs;
8944 LineOffset = new_LineOffset;
8945 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008946 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
8948 /* It's important that screen_Rows and screen_Columns reflect the actual
8949 * size of ScreenLines[]. Set them before calling anything. */
8950#ifdef FEAT_GUI
8951 old_Rows = screen_Rows;
8952#endif
8953 screen_Rows = Rows;
8954 screen_Columns = Columns;
8955
8956 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008957 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 screenclear2();
8959
8960#ifdef FEAT_GUI
8961 else if (gui.in_use
8962 && !gui.starting
8963 && ScreenLines != NULL
8964 && old_Rows != Rows)
8965 {
8966 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8967 /*
8968 * Adjust the position of the cursor, for when executing an external
8969 * command.
8970 */
8971 if (msg_row >= Rows) /* Rows got smaller */
8972 msg_row = Rows - 1; /* put cursor at last row */
8973 else if (Rows > old_Rows) /* Rows got bigger */
8974 msg_row += Rows - old_Rows; /* put cursor in same place */
8975 if (msg_col >= Columns) /* Columns got smaller */
8976 msg_col = Columns - 1; /* put cursor at last column */
8977 }
8978#endif
8979
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008981 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008982
8983#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008984 /*
8985 * Do not apply autocommands more than 3 times to avoid an endless loop
8986 * in case applying autocommands always changes Rows or Columns.
8987 */
8988 if (starting == 0 && ++retry_count <= 3)
8989 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008990 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008991 /* In rare cases, autocommands may have altered Rows or Columns,
8992 * jump back to check if we need to allocate the screen again. */
8993 goto retry;
8994 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996}
8997
8998 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008999free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009000{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009001#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009002 int i;
9003
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009004 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009005 for (i = 0; i < Screen_mco; ++i)
9006 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009007 vim_free(ScreenLines2);
9008#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009009 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009010 vim_free(ScreenAttrs);
9011 vim_free(LineOffset);
9012 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009013 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009014}
9015
9016 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009017screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018{
9019 check_for_delay(FALSE);
9020 screenalloc(FALSE); /* allocate screen buffers if size changed */
9021 screenclear2(); /* clear the screen */
9022}
9023
9024 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009025screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
9027 int i;
9028
9029 if (starting == NO_SCREEN || ScreenLines == NULL
9030#ifdef FEAT_GUI
9031 || (gui.in_use && gui.starting)
9032#endif
9033 )
9034 return;
9035
9036#ifdef FEAT_GUI
9037 if (!gui.in_use)
9038#endif
9039 screen_attr = -1; /* force setting the Normal colors */
9040 screen_stop_highlight(); /* don't want highlighting here */
9041
9042#ifdef FEAT_CLIPBOARD
9043 /* disable selection without redrawing it */
9044 clip_scroll_selection(9999);
9045#endif
9046
9047 /* blank out ScreenLines */
9048 for (i = 0; i < Rows; ++i)
9049 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009050 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 LineWraps[i] = FALSE;
9052 }
9053
9054 if (can_clear(T_CL))
9055 {
9056 out_str(T_CL); /* clear the display */
9057 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009058 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 }
9060 else
9061 {
9062 /* can't clear the screen, mark all chars with invalid attributes */
9063 for (i = 0; i < Rows; ++i)
9064 lineinvalid(LineOffset[i], (int)Columns);
9065 clear_cmdline = TRUE;
9066 }
9067
9068 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9069
9070 win_rest_invalid(firstwin);
9071 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009072 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 if (must_redraw == CLEAR) /* no need to clear again */
9074 must_redraw = NOT_VALID;
9075 compute_cmdrow();
9076 msg_row = cmdline_row; /* put cursor on last line for messages */
9077 msg_col = 0;
9078 screen_start(); /* don't know where cursor is now */
9079 msg_scrolled = 0; /* can't scroll back */
9080 msg_didany = FALSE;
9081 msg_didout = FALSE;
9082}
9083
9084/*
9085 * Clear one line in ScreenLines.
9086 */
9087 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009088lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089{
9090 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9091#ifdef FEAT_MBYTE
9092 if (enc_utf8)
9093 (void)vim_memset(ScreenLinesUC + off, 0,
9094 (size_t)width * sizeof(u8char_T));
9095#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009096 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097}
9098
9099/*
9100 * Mark one line in ScreenLines invalid by setting the attributes to an
9101 * invalid value.
9102 */
9103 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009104lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105{
9106 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9107}
9108
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109/*
9110 * Copy part of a Screenline for vertically split window "wp".
9111 */
9112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009113linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114{
9115 unsigned off_to = LineOffset[to] + wp->w_wincol;
9116 unsigned off_from = LineOffset[from] + wp->w_wincol;
9117
9118 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9119 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009120#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 if (enc_utf8)
9122 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009123 int i;
9124
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9126 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009127 for (i = 0; i < p_mco; ++i)
9128 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9129 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 }
9131 if (enc_dbcs == DBCS_JPNU)
9132 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9133 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9136 wp->w_width * sizeof(sattr_T));
9137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138
9139/*
9140 * Return TRUE if clearing with term string "p" would work.
9141 * It can't work when the string is empty or it won't set the right background.
9142 */
9143 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009144can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 return (*p != NUL && (t_colors <= 1
9147#ifdef FEAT_GUI
9148 || gui.in_use
9149#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009150#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009151 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009152 || (!p_tgc && cterm_normal_bg_color == 0)
9153#else
9154 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009155#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009156 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157}
9158
9159/*
9160 * Reset cursor position. Use whenever cursor was moved because of outputting
9161 * something directly to the screen (shell commands) or a terminal control
9162 * code.
9163 */
9164 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009165screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166{
9167 screen_cur_row = screen_cur_col = 9999;
9168}
9169
9170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 * Move the cursor to position "row","col" in the screen.
9172 * This tries to find the most efficient way to move, minimizing the number of
9173 * characters sent to the terminal.
9174 */
9175 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009176windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009178 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 int i;
9180 int plan;
9181 int cost;
9182 int wouldbe_col;
9183 int noinvcurs;
9184 char_u *bs;
9185 int goto_cost;
9186 int attr;
9187
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009188#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9190
9191#define PLAN_LE 1
9192#define PLAN_CR 2
9193#define PLAN_NL 3
9194#define PLAN_WRITE 4
9195 /* Can't use ScreenLines unless initialized */
9196 if (ScreenLines == NULL)
9197 return;
9198
9199 if (col != screen_cur_col || row != screen_cur_row)
9200 {
9201 /* Check for valid position. */
9202 if (row < 0) /* window without text lines? */
9203 row = 0;
9204 if (row >= screen_Rows)
9205 row = screen_Rows - 1;
9206 if (col >= screen_Columns)
9207 col = screen_Columns - 1;
9208
9209 /* check if no cursor movement is allowed in highlight mode */
9210 if (screen_attr && *T_MS == NUL)
9211 noinvcurs = HIGHL_COST;
9212 else
9213 noinvcurs = 0;
9214 goto_cost = GOTO_COST + noinvcurs;
9215
9216 /*
9217 * Plan how to do the positioning:
9218 * 1. Use CR to move it to column 0, same row.
9219 * 2. Use T_LE to move it a few columns to the left.
9220 * 3. Use NL to move a few lines down, column 0.
9221 * 4. Move a few columns to the right with T_ND or by writing chars.
9222 *
9223 * Don't do this if the cursor went beyond the last column, the cursor
9224 * position is unknown then (some terminals wrap, some don't )
9225 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009226 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 * characters to move the cursor to the right.
9228 */
9229 if (row >= screen_cur_row && screen_cur_col < Columns)
9230 {
9231 /*
9232 * If the cursor is in the same row, bigger col, we can use CR
9233 * or T_LE.
9234 */
9235 bs = NULL; /* init for GCC */
9236 attr = screen_attr;
9237 if (row == screen_cur_row && col < screen_cur_col)
9238 {
9239 /* "le" is preferred over "bc", because "bc" is obsolete */
9240 if (*T_LE)
9241 bs = T_LE; /* "cursor left" */
9242 else
9243 bs = T_BC; /* "backspace character (old) */
9244 if (*bs)
9245 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9246 else
9247 cost = 999;
9248 if (col + 1 < cost) /* using CR is less characters */
9249 {
9250 plan = PLAN_CR;
9251 wouldbe_col = 0;
9252 cost = 1; /* CR is just one character */
9253 }
9254 else
9255 {
9256 plan = PLAN_LE;
9257 wouldbe_col = col;
9258 }
9259 if (noinvcurs) /* will stop highlighting */
9260 {
9261 cost += noinvcurs;
9262 attr = 0;
9263 }
9264 }
9265
9266 /*
9267 * If the cursor is above where we want to be, we can use CR LF.
9268 */
9269 else if (row > screen_cur_row)
9270 {
9271 plan = PLAN_NL;
9272 wouldbe_col = 0;
9273 cost = (row - screen_cur_row) * 2; /* CR LF */
9274 if (noinvcurs) /* will stop highlighting */
9275 {
9276 cost += noinvcurs;
9277 attr = 0;
9278 }
9279 }
9280
9281 /*
9282 * If the cursor is in the same row, smaller col, just use write.
9283 */
9284 else
9285 {
9286 plan = PLAN_WRITE;
9287 wouldbe_col = screen_cur_col;
9288 cost = 0;
9289 }
9290
9291 /*
9292 * Check if any characters that need to be written have the
9293 * correct attributes. Also avoid UTF-8 characters.
9294 */
9295 i = col - wouldbe_col;
9296 if (i > 0)
9297 cost += i;
9298 if (cost < goto_cost && i > 0)
9299 {
9300 /*
9301 * Check if the attributes are correct without additionally
9302 * stopping highlighting.
9303 */
9304 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9305 while (i && *p++ == attr)
9306 --i;
9307 if (i != 0)
9308 {
9309 /*
9310 * Try if it works when highlighting is stopped here.
9311 */
9312 if (*--p == 0)
9313 {
9314 cost += noinvcurs;
9315 while (i && *p++ == 0)
9316 --i;
9317 }
9318 if (i != 0)
9319 cost = 999; /* different attributes, don't do it */
9320 }
9321#ifdef FEAT_MBYTE
9322 if (enc_utf8)
9323 {
9324 /* Don't use an UTF-8 char for positioning, it's slow. */
9325 for (i = wouldbe_col; i < col; ++i)
9326 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9327 {
9328 cost = 999;
9329 break;
9330 }
9331 }
9332#endif
9333 }
9334
9335 /*
9336 * We can do it without term_windgoto()!
9337 */
9338 if (cost < goto_cost)
9339 {
9340 if (plan == PLAN_LE)
9341 {
9342 if (noinvcurs)
9343 screen_stop_highlight();
9344 while (screen_cur_col > col)
9345 {
9346 out_str(bs);
9347 --screen_cur_col;
9348 }
9349 }
9350 else if (plan == PLAN_CR)
9351 {
9352 if (noinvcurs)
9353 screen_stop_highlight();
9354 out_char('\r');
9355 screen_cur_col = 0;
9356 }
9357 else if (plan == PLAN_NL)
9358 {
9359 if (noinvcurs)
9360 screen_stop_highlight();
9361 while (screen_cur_row < row)
9362 {
9363 out_char('\n');
9364 ++screen_cur_row;
9365 }
9366 screen_cur_col = 0;
9367 }
9368
9369 i = col - screen_cur_col;
9370 if (i > 0)
9371 {
9372 /*
9373 * Use cursor-right if it's one character only. Avoids
9374 * removing a line of pixels from the last bold char, when
9375 * using the bold trick in the GUI.
9376 */
9377 if (T_ND[0] != NUL && T_ND[1] == NUL)
9378 {
9379 while (i-- > 0)
9380 out_char(*T_ND);
9381 }
9382 else
9383 {
9384 int off;
9385
9386 off = LineOffset[row] + screen_cur_col;
9387 while (i-- > 0)
9388 {
9389 if (ScreenAttrs[off] != screen_attr)
9390 screen_stop_highlight();
9391#ifdef FEAT_MBYTE
9392 out_flush_check();
9393#endif
9394 out_char(ScreenLines[off]);
9395#ifdef FEAT_MBYTE
9396 if (enc_dbcs == DBCS_JPNU
9397 && ScreenLines[off] == 0x8e)
9398 out_char(ScreenLines2[off]);
9399#endif
9400 ++off;
9401 }
9402 }
9403 }
9404 }
9405 }
9406 else
9407 cost = 999;
9408
9409 if (cost >= goto_cost)
9410 {
9411 if (noinvcurs)
9412 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009413 if (row == screen_cur_row && (col > screen_cur_col)
9414 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 term_cursor_right(col - screen_cur_col);
9416 else
9417 term_windgoto(row, col);
9418 }
9419 screen_cur_row = row;
9420 screen_cur_col = col;
9421 }
9422}
9423
9424/*
9425 * Set cursor to its position in the current window.
9426 */
9427 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009428setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429{
9430 if (redrawing())
9431 {
9432 validate_cursor();
9433 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009434 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009436 /* With 'rightleft' set and the cursor on a double-wide
9437 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009438 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009440 (has_mbyte
9441 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9442 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443# endif
9444 1)) :
9445#endif
9446 curwin->w_wcol));
9447 }
9448}
9449
9450
9451/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009452 * Insert 'line_count' lines at 'row' in window 'wp'.
9453 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9454 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 * scrolling.
9456 * Returns FAIL if the lines are not inserted, OK for success.
9457 */
9458 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009459win_ins_lines(
9460 win_T *wp,
9461 int row,
9462 int line_count,
9463 int invalid,
9464 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465{
9466 int did_delete;
9467 int nextrow;
9468 int lastrow;
9469 int retval;
9470
9471 if (invalid)
9472 wp->w_lines_valid = 0;
9473
9474 if (wp->w_height < 5)
9475 return FAIL;
9476
9477 if (line_count > wp->w_height - row)
9478 line_count = wp->w_height - row;
9479
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009480 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (retval != MAYBE)
9482 return retval;
9483
9484 /*
9485 * If there is a next window or a status line, we first try to delete the
9486 * lines at the bottom to avoid messing what is after the window.
9487 * If this fails and there are following windows, don't do anything to avoid
9488 * messing up those windows, better just redraw.
9489 */
9490 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 if (wp->w_next != NULL || wp->w_status_height)
9492 {
9493 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009494 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 did_delete = TRUE;
9496 else if (wp->w_next)
9497 return FAIL;
9498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 /*
9500 * if no lines deleted, blank the lines that will end up below the window
9501 */
9502 if (!did_delete)
9503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009506 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 lastrow = nextrow + line_count;
9508 if (lastrow > Rows)
9509 lastrow = Rows;
9510 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009511 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 ' ', ' ', 0);
9513 }
9514
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009515 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 == FAIL)
9517 {
9518 /* deletion will have messed up other windows */
9519 if (did_delete)
9520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 win_rest_invalid(W_NEXT(wp));
9523 }
9524 return FAIL;
9525 }
9526
9527 return OK;
9528}
9529
9530/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009531 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9533 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9534 * scrolling
9535 * Return OK for success, FAIL if the lines are not deleted.
9536 */
9537 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009538win_del_lines(
9539 win_T *wp,
9540 int row,
9541 int line_count,
9542 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009543 int mayclear,
9544 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546 int retval;
9547
9548 if (invalid)
9549 wp->w_lines_valid = 0;
9550
9551 if (line_count > wp->w_height - row)
9552 line_count = wp->w_height - row;
9553
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009554 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 if (retval != MAYBE)
9556 return retval;
9557
9558 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009559 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 return FAIL;
9561
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 /*
9563 * If there are windows or status lines below, try to put them at the
9564 * correct place. If we can't do that, they have to be redrawn.
9565 */
9566 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9567 {
9568 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009569 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 {
9571 wp->w_redr_status = TRUE;
9572 win_rest_invalid(wp->w_next);
9573 }
9574 }
9575 /*
9576 * If this is the last window and there is no status line, redraw the
9577 * command line later.
9578 */
9579 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 redraw_cmdline = TRUE;
9581 return OK;
9582}
9583
9584/*
9585 * Common code for win_ins_lines() and win_del_lines().
9586 * Returns OK or FAIL when the work has been done.
9587 * Returns MAYBE when not finished yet.
9588 */
9589 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009590win_do_lines(
9591 win_T *wp,
9592 int row,
9593 int line_count,
9594 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009595 int del,
9596 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
9598 int retval;
9599
9600 if (!redrawing() || line_count <= 0)
9601 return FAIL;
9602
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009603 /* When inserting lines would result in loss of command output, just redraw
9604 * the lines. */
9605 if (no_win_do_lines_ins && !del)
9606 return FAIL;
9607
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009609 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009611 if (!no_win_do_lines_ins)
9612 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 return FAIL;
9614 }
9615
9616 /*
9617 * Delete all remaining lines
9618 */
9619 if (row + line_count >= wp->w_height)
9620 {
9621 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009622 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 ' ', ' ', 0);
9624 return OK;
9625 }
9626
9627 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009628 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009630 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009632 if (!no_win_do_lines_ins)
9633 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634
9635 /*
9636 * If the terminal can set a scroll region, use that.
9637 * Always do this in a vertically split window. This will redraw from
9638 * ScreenLines[] when t_CV isn't defined. That's faster than using
9639 * win_line().
9640 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009641 * a character in the lower right corner of the scroll region may cause a
9642 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009644 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 scroll_region_set(wp, row);
9648 if (del)
9649 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009650 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 else
9652 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009653 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 scroll_region_reset();
9656 return retval;
9657 }
9658
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9660 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661
9662 return MAYBE;
9663}
9664
9665/*
9666 * window 'wp' and everything after it is messed up, mark it for redraw
9667 */
9668 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009669win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 {
9673 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 wp->w_redr_status = TRUE;
9675 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 }
9677 redraw_cmdline = TRUE;
9678}
9679
9680/*
9681 * The rest of the routines in this file perform screen manipulations. The
9682 * given operation is performed physically on the screen. The corresponding
9683 * change is also made to the internal screen image. In this way, the editor
9684 * anticipates the effect of editing changes on the appearance of the screen.
9685 * That way, when we call screenupdate a complete redraw isn't usually
9686 * necessary. Another advantage is that we can keep adding code to anticipate
9687 * screen changes, and in the meantime, everything still works.
9688 */
9689
9690/*
9691 * types for inserting or deleting lines
9692 */
9693#define USE_T_CAL 1
9694#define USE_T_CDL 2
9695#define USE_T_AL 3
9696#define USE_T_CE 4
9697#define USE_T_DL 5
9698#define USE_T_SR 6
9699#define USE_NL 7
9700#define USE_T_CD 8
9701#define USE_REDRAW 9
9702
9703/*
9704 * insert lines on the screen and update ScreenLines[]
9705 * 'end' is the line after the scrolled part. Normally it is Rows.
9706 * When scrolling region used 'off' is the offset from the top for the region.
9707 * 'row' and 'end' are relative to the start of the region.
9708 *
9709 * return FAIL for failure, OK for success.
9710 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009711 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009712screen_ins_lines(
9713 int off,
9714 int row,
9715 int line_count,
9716 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009717 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009718 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719{
9720 int i;
9721 int j;
9722 unsigned temp;
9723 int cursor_row;
9724 int type;
9725 int result_empty;
9726 int can_ce = can_clear(T_CE);
9727
9728 /*
9729 * FAIL if
9730 * - there is no valid screen
9731 * - the screen has to be redrawn completely
9732 * - the line count is less than one
9733 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009734 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009736 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9737#ifdef FEAT_CLIPBOARD
9738 || (clip_star.state != SELECT_CLEARED
9739 && redrawing_for_callback > 0)
9740#endif
9741 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 return FAIL;
9743
9744 /*
9745 * There are seven ways to insert lines:
9746 * 0. When in a vertically split window and t_CV isn't set, redraw the
9747 * characters from ScreenLines[].
9748 * 1. Use T_CD (clear to end of display) if it exists and the result of
9749 * the insert is just empty lines
9750 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9751 * present or line_count > 1. It looks better if we do all the inserts
9752 * at once.
9753 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9754 * insert is just empty lines and T_CE is not present or line_count >
9755 * 1.
9756 * 4. Use T_AL (insert line) if it exists.
9757 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9758 * just empty lines.
9759 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9760 * just empty lines.
9761 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9762 * the 'da' flag is not set or we have clear line capability.
9763 * 8. redraw the characters from ScreenLines[].
9764 *
9765 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9766 * the scrollbar for the window. It does have insert line, use that if it
9767 * exists.
9768 */
9769 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9771 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009772 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 type = USE_T_CD;
9774 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9775 type = USE_T_CAL;
9776 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9777 type = USE_T_CDL;
9778 else if (*T_AL != NUL)
9779 type = USE_T_AL;
9780 else if (can_ce && result_empty)
9781 type = USE_T_CE;
9782 else if (*T_DL != NUL && result_empty)
9783 type = USE_T_DL;
9784 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9785 type = USE_T_SR;
9786 else
9787 return FAIL;
9788
9789 /*
9790 * For clearing the lines screen_del_lines() is used. This will also take
9791 * care of t_db if necessary.
9792 */
9793 if (type == USE_T_CD || type == USE_T_CDL ||
9794 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009795 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796
9797 /*
9798 * If text is retained below the screen, first clear or delete as many
9799 * lines at the bottom of the window as are about to be inserted so that
9800 * the deleted lines won't later surface during a screen_del_lines.
9801 */
9802 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009803 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804
9805#ifdef FEAT_CLIPBOARD
9806 /* Remove a modeless selection when inserting lines halfway the screen
9807 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009808 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009809 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 else
9811 clip_scroll_selection(-line_count);
9812#endif
9813
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814#ifdef FEAT_GUI
9815 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9816 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009817 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818#endif
9819
9820 if (*T_CCS != NUL) /* cursor relative to region */
9821 cursor_row = row;
9822 else
9823 cursor_row = row + off;
9824
9825 /*
9826 * Shift LineOffset[] line_count down to reflect the inserted lines.
9827 * Clear the inserted lines in ScreenLines[].
9828 */
9829 row += off;
9830 end += off;
9831 for (i = 0; i < line_count; ++i)
9832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 if (wp != NULL && wp->w_width != Columns)
9834 {
9835 /* need to copy part of a line */
9836 j = end - 1 - i;
9837 while ((j -= line_count) >= row)
9838 linecopy(j + line_count, j, wp);
9839 j += line_count;
9840 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009841 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9842 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 else
9844 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9845 LineWraps[j] = FALSE;
9846 }
9847 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 {
9849 j = end - 1 - i;
9850 temp = LineOffset[j];
9851 while ((j -= line_count) >= row)
9852 {
9853 LineOffset[j + line_count] = LineOffset[j];
9854 LineWraps[j + line_count] = LineWraps[j];
9855 }
9856 LineOffset[j + line_count] = temp;
9857 LineWraps[j + line_count] = FALSE;
9858 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009859 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 else
9861 lineinvalid(temp, (int)Columns);
9862 }
9863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864
9865 screen_stop_highlight();
9866 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009867 if (clear_attr != 0)
9868 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 /* redraw the characters */
9871 if (type == USE_REDRAW)
9872 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009873 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 {
9875 term_append_lines(line_count);
9876 screen_start(); /* don't know where cursor is now */
9877 }
9878 else
9879 {
9880 for (i = 0; i < line_count; i++)
9881 {
9882 if (type == USE_T_AL)
9883 {
9884 if (i && cursor_row != 0)
9885 windgoto(cursor_row, 0);
9886 out_str(T_AL);
9887 }
9888 else /* type == USE_T_SR */
9889 out_str(T_SR);
9890 screen_start(); /* don't know where cursor is now */
9891 }
9892 }
9893
9894 /*
9895 * With scroll-reverse and 'da' flag set we need to clear the lines that
9896 * have been scrolled down into the region.
9897 */
9898 if (type == USE_T_SR && *T_DA)
9899 {
9900 for (i = 0; i < line_count; ++i)
9901 {
9902 windgoto(off + i, 0);
9903 out_str(T_CE);
9904 screen_start(); /* don't know where cursor is now */
9905 }
9906 }
9907
9908#ifdef FEAT_GUI
9909 gui_can_update_cursor();
9910 if (gui.in_use)
9911 out_flush(); /* always flush after a scroll */
9912#endif
9913 return OK;
9914}
9915
9916/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009917 * Delete lines on the screen and update ScreenLines[].
9918 * "end" is the line after the scrolled part. Normally it is Rows.
9919 * When scrolling region used "off" is the offset from the top for the region.
9920 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 *
9922 * Return OK for success, FAIL if the lines are not deleted.
9923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009925screen_del_lines(
9926 int off,
9927 int row,
9928 int line_count,
9929 int end,
9930 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009931 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009932 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933{
9934 int j;
9935 int i;
9936 unsigned temp;
9937 int cursor_row;
9938 int cursor_end;
9939 int result_empty; /* result is empty until end of region */
9940 int can_delete; /* deleting line codes can be used */
9941 int type;
9942
9943 /*
9944 * FAIL if
9945 * - there is no valid screen
9946 * - the screen has to be redrawn completely
9947 * - the line count is less than one
9948 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009949 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009951 if (!screen_valid(TRUE) || line_count <= 0
9952 || (!force && line_count > p_ttyscroll)
9953#ifdef FEAT_CLIPBOARD
9954 || (clip_star.state != SELECT_CLEARED
9955 && redrawing_for_callback > 0)
9956#endif
9957 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 return FAIL;
9959
9960 /*
9961 * Check if the rest of the current region will become empty.
9962 */
9963 result_empty = row + line_count >= end;
9964
9965 /*
9966 * We can delete lines only when 'db' flag not set or when 'ce' option
9967 * available.
9968 */
9969 can_delete = (*T_DB == NUL || can_clear(T_CE));
9970
9971 /*
9972 * There are six ways to delete lines:
9973 * 0. When in a vertically split window and t_CV isn't set, redraw the
9974 * characters from ScreenLines[].
9975 * 1. Use T_CD if it exists and the result is empty.
9976 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9977 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9978 * none of the other ways work.
9979 * 4. Use T_CE (erase line) if the result is empty.
9980 * 5. Use T_DL (delete line) if it exists.
9981 * 6. redraw the characters from ScreenLines[].
9982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9984 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009985 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 type = USE_T_CD;
9987#if defined(__BEOS__) && defined(BEOS_DR8)
9988 /*
9989 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9990 * its internal termcap... this works okay for tests which test *T_DB !=
9991 * NUL. It has the disadvantage that the user cannot use any :set t_*
9992 * command to get T_DB (back) to empty_option, only :set term=... will do
9993 * the trick...
9994 * Anyway, this hack will hopefully go away with the next OS release.
9995 * (Olaf Seibert)
9996 */
9997 else if (row == 0 && T_DB == empty_option
9998 && (line_count == 1 || *T_CDL == NUL))
9999#else
10000 else if (row == 0 && (
10001#ifndef AMIGA
10002 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10003 * up, so use delete-line command */
10004 line_count == 1 ||
10005#endif
10006 *T_CDL == NUL))
10007#endif
10008 type = USE_NL;
10009 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10010 type = USE_T_CDL;
10011 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010012 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 type = USE_T_CE;
10014 else if (*T_DL != NUL && can_delete)
10015 type = USE_T_DL;
10016 else if (*T_CDL != NUL && can_delete)
10017 type = USE_T_CDL;
10018 else
10019 return FAIL;
10020
10021#ifdef FEAT_CLIPBOARD
10022 /* Remove a modeless selection when deleting lines halfway the screen or
10023 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010024 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010025 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 else
10027 clip_scroll_selection(line_count);
10028#endif
10029
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030#ifdef FEAT_GUI
10031 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10032 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010033 gui_dont_update_cursor(gui.cursor_row >= row + off
10034 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035#endif
10036
10037 if (*T_CCS != NUL) /* cursor relative to region */
10038 {
10039 cursor_row = row;
10040 cursor_end = end;
10041 }
10042 else
10043 {
10044 cursor_row = row + off;
10045 cursor_end = end + off;
10046 }
10047
10048 /*
10049 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10050 * Clear the inserted lines in ScreenLines[].
10051 */
10052 row += off;
10053 end += off;
10054 for (i = 0; i < line_count; ++i)
10055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 if (wp != NULL && wp->w_width != Columns)
10057 {
10058 /* need to copy part of a line */
10059 j = row + i;
10060 while ((j += line_count) <= end - 1)
10061 linecopy(j - line_count, j, wp);
10062 j -= line_count;
10063 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010064 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10065 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 else
10067 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10068 LineWraps[j] = FALSE;
10069 }
10070 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 {
10072 /* whole width, moving the line pointers is faster */
10073 j = row + i;
10074 temp = LineOffset[j];
10075 while ((j += line_count) <= end - 1)
10076 {
10077 LineOffset[j - line_count] = LineOffset[j];
10078 LineWraps[j - line_count] = LineWraps[j];
10079 }
10080 LineOffset[j - line_count] = temp;
10081 LineWraps[j - line_count] = FALSE;
10082 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010083 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 else
10085 lineinvalid(temp, (int)Columns);
10086 }
10087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010089 if (screen_attr != clear_attr)
10090 screen_stop_highlight();
10091 if (clear_attr != 0)
10092 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 /* redraw the characters */
10095 if (type == USE_REDRAW)
10096 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010097 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 {
10099 windgoto(cursor_row, 0);
10100 out_str(T_CD);
10101 screen_start(); /* don't know where cursor is now */
10102 }
10103 else if (type == USE_T_CDL)
10104 {
10105 windgoto(cursor_row, 0);
10106 term_delete_lines(line_count);
10107 screen_start(); /* don't know where cursor is now */
10108 }
10109 /*
10110 * Deleting lines at top of the screen or scroll region: Just scroll
10111 * the whole screen (scroll region) up by outputting newlines on the
10112 * last line.
10113 */
10114 else if (type == USE_NL)
10115 {
10116 windgoto(cursor_end - 1, 0);
10117 for (i = line_count; --i >= 0; )
10118 out_char('\n'); /* cursor will remain on same line */
10119 }
10120 else
10121 {
10122 for (i = line_count; --i >= 0; )
10123 {
10124 if (type == USE_T_DL)
10125 {
10126 windgoto(cursor_row, 0);
10127 out_str(T_DL); /* delete a line */
10128 }
10129 else /* type == USE_T_CE */
10130 {
10131 windgoto(cursor_row + i, 0);
10132 out_str(T_CE); /* erase a line */
10133 }
10134 screen_start(); /* don't know where cursor is now */
10135 }
10136 }
10137
10138 /*
10139 * If the 'db' flag is set, we need to clear the lines that have been
10140 * scrolled up at the bottom of the region.
10141 */
10142 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10143 {
10144 for (i = line_count; i > 0; --i)
10145 {
10146 windgoto(cursor_end - i, 0);
10147 out_str(T_CE); /* erase a line */
10148 screen_start(); /* don't know where cursor is now */
10149 }
10150 }
10151
10152#ifdef FEAT_GUI
10153 gui_can_update_cursor();
10154 if (gui.in_use)
10155 out_flush(); /* always flush after a scroll */
10156#endif
10157
10158 return OK;
10159}
10160
10161/*
10162 * show the current mode and ruler
10163 *
10164 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10165 * If clear_cmdline is FALSE there may be a message there that needs to be
10166 * cleared only if a mode is shown.
10167 * Return the length of the message (0 if no message).
10168 */
10169 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010170showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171{
10172 int need_clear;
10173 int length = 0;
10174 int do_mode;
10175 int attr;
10176 int nwr_save;
10177#ifdef FEAT_INS_EXPAND
10178 int sub_attr;
10179#endif
10180
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010181 do_mode = ((p_smd && msg_silent == 0)
10182 && ((State & INSERT)
10183 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010184 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 if (do_mode || Recording)
10186 {
10187 /*
10188 * Don't show mode right now, when not redrawing or inside a mapping.
10189 * Call char_avail() only when we are going to show something, because
10190 * it takes a bit of time.
10191 */
10192 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10193 {
10194 redraw_cmdline = TRUE; /* show mode later */
10195 return 0;
10196 }
10197
10198 nwr_save = need_wait_return;
10199
10200 /* wait a bit before overwriting an important message */
10201 check_for_delay(FALSE);
10202
10203 /* if the cmdline is more than one line high, erase top lines */
10204 need_clear = clear_cmdline;
10205 if (clear_cmdline && cmdline_row < Rows - 1)
10206 msg_clr_cmdline(); /* will reset clear_cmdline */
10207
10208 /* Position on the last line in the window, column 0 */
10209 msg_pos_mode();
10210 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010211 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 if (do_mode)
10213 {
10214 MSG_PUTS_ATTR("--", attr);
10215#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010216 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010217# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010218 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010219# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010220 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010221# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010222 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010223# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 MSG_PUTS_ATTR(" IM", attr);
10225# else
10226 MSG_PUTS_ATTR(" XIM", attr);
10227# endif
10228#endif
10229#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10230 if (gui.in_use)
10231 {
10232 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010233 {
10234 /* HANGUL */
10235 if (enc_utf8)
10236 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10237 else
10238 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 }
10241#endif
10242#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010243 /* CTRL-X in Insert mode */
10244 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 {
10246 /* These messages can get long, avoid a wrap in a narrow
10247 * window. Prefer showing edit_submode_extra. */
10248 length = (Rows - msg_row) * Columns - 3;
10249 if (edit_submode_extra != NULL)
10250 length -= vim_strsize(edit_submode_extra);
10251 if (length > 0)
10252 {
10253 if (edit_submode_pre != NULL)
10254 length -= vim_strsize(edit_submode_pre);
10255 if (length - vim_strsize(edit_submode) > 0)
10256 {
10257 if (edit_submode_pre != NULL)
10258 msg_puts_attr(edit_submode_pre, attr);
10259 msg_puts_attr(edit_submode, attr);
10260 }
10261 if (edit_submode_extra != NULL)
10262 {
10263 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10264 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010265 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 else
10267 sub_attr = attr;
10268 msg_puts_attr(edit_submode_extra, sub_attr);
10269 }
10270 }
10271 length = 0;
10272 }
10273 else
10274#endif
10275 {
10276#ifdef FEAT_VREPLACE
10277 if (State & VREPLACE_FLAG)
10278 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10279 else
10280#endif
10281 if (State & REPLACE_FLAG)
10282 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10283 else if (State & INSERT)
10284 {
10285#ifdef FEAT_RIGHTLEFT
10286 if (p_ri)
10287 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10288#endif
10289 MSG_PUTS_ATTR(_(" INSERT"), attr);
10290 }
10291 else if (restart_edit == 'I')
10292 MSG_PUTS_ATTR(_(" (insert)"), attr);
10293 else if (restart_edit == 'R')
10294 MSG_PUTS_ATTR(_(" (replace)"), attr);
10295 else if (restart_edit == 'V')
10296 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10297#ifdef FEAT_RIGHTLEFT
10298 if (p_hkmap)
10299 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10300# ifdef FEAT_FKMAP
10301 if (p_fkmap)
10302 MSG_PUTS_ATTR(farsi_text_5, attr);
10303# endif
10304#endif
10305#ifdef FEAT_KEYMAP
10306 if (State & LANGMAP)
10307 {
10308# ifdef FEAT_ARABIC
10309 if (curwin->w_p_arab)
10310 MSG_PUTS_ATTR(_(" Arabic"), attr);
10311 else
10312# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010313 if (get_keymap_str(curwin, (char_u *)" (%s)",
10314 NameBuff, MAXPATHL))
10315 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 }
10317#endif
10318 if ((State & INSERT) && p_paste)
10319 MSG_PUTS_ATTR(_(" (paste)"), attr);
10320
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 if (VIsual_active)
10322 {
10323 char *p;
10324
10325 /* Don't concatenate separate words to avoid translation
10326 * problems. */
10327 switch ((VIsual_select ? 4 : 0)
10328 + (VIsual_mode == Ctrl_V) * 2
10329 + (VIsual_mode == 'V'))
10330 {
10331 case 0: p = N_(" VISUAL"); break;
10332 case 1: p = N_(" VISUAL LINE"); break;
10333 case 2: p = N_(" VISUAL BLOCK"); break;
10334 case 4: p = N_(" SELECT"); break;
10335 case 5: p = N_(" SELECT LINE"); break;
10336 default: p = N_(" SELECT BLOCK"); break;
10337 }
10338 MSG_PUTS_ATTR(_(p), attr);
10339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 MSG_PUTS_ATTR(" --", attr);
10341 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010342
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 need_clear = TRUE;
10344 }
10345 if (Recording
10346#ifdef FEAT_INS_EXPAND
10347 && edit_submode == NULL /* otherwise it gets too long */
10348#endif
10349 )
10350 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010351 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 need_clear = TRUE;
10353 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010354
10355 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 if (need_clear || clear_cmdline)
10357 msg_clr_eos();
10358 msg_didout = FALSE; /* overwrite this message */
10359 length = msg_col;
10360 msg_col = 0;
10361 need_wait_return = nwr_save; /* never ask for hit-return for this */
10362 }
10363 else if (clear_cmdline && msg_silent == 0)
10364 /* Clear the whole command line. Will reset "clear_cmdline". */
10365 msg_clr_cmdline();
10366
10367#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 /* In Visual mode the size of the selected area must be redrawn. */
10369 if (VIsual_active)
10370 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371
10372 /* If the last window has no status line, the ruler is after the mode
10373 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010374 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 win_redr_ruler(lastwin, TRUE);
10376#endif
10377 redraw_cmdline = FALSE;
10378 clear_cmdline = FALSE;
10379
10380 return length;
10381}
10382
10383/*
10384 * Position for a mode message.
10385 */
10386 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010387msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388{
10389 msg_col = 0;
10390 msg_row = Rows - 1;
10391}
10392
10393/*
10394 * Delete mode message. Used when ESC is typed which is expected to end
10395 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010396 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 */
10398 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010399unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400{
10401 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010402 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 */
10404 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10405 redraw_cmdline = TRUE; /* delete mode later */
10406 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010407 clearmode();
10408}
10409
10410/*
10411 * Clear the mode message.
10412 */
10413 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010414clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010415{
10416 msg_pos_mode();
10417 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010418 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010419 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420}
10421
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010422 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010423recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010424{
10425 MSG_PUTS_ATTR(_("recording"), attr);
10426 if (!shortmess(SHM_RECORDING))
10427 {
10428 char_u s[4];
10429 sprintf((char *)s, " @%c", Recording);
10430 MSG_PUTS_ATTR(s, attr);
10431 }
10432}
10433
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010434/*
10435 * Draw the tab pages line at the top of the Vim window.
10436 */
10437 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010438draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010439{
10440 int tabcount = 0;
10441 tabpage_T *tp;
10442 int tabwidth;
10443 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010444 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010445 int attr;
10446 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010447 win_T *cwp;
10448 int wincount;
10449 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010450 int c;
10451 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010452 int attr_sel = HL_ATTR(HLF_TPS);
10453 int attr_nosel = HL_ATTR(HLF_TP);
10454 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010455 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010456 int room;
10457 int use_sep_chars = (t_colors < 8
10458#ifdef FEAT_GUI
10459 && !gui.in_use
10460#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010461#ifdef FEAT_TERMGUICOLORS
10462 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010463#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010464 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010465
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010466 if (ScreenLines == NULL)
10467 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010468 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010469
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010470#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010471 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010472 if (gui_use_tabline())
10473 {
10474 gui_update_tabline();
10475 return;
10476 }
10477#endif
10478
10479 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010480 return;
10481
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010482#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010483
10484 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10485 for (scol = 0; scol < Columns; ++scol)
10486 TabPageIdxs[scol] = 0;
10487
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010488 /* Use the 'tabline' option if it's set. */
10489 if (*p_tal != NUL)
10490 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010491 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010492
10493 /* Check for an error. If there is one we would loop in redrawing the
10494 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010495 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010496 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010497 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010498 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010499 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010500 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010501 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010502 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010503#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010504 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010505 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010506 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010507
Bram Moolenaar238a5642006-02-21 22:12:05 +000010508 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10509 if (tabwidth < 6)
10510 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010511
Bram Moolenaar238a5642006-02-21 22:12:05 +000010512 attr = attr_nosel;
10513 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010514 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010515 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10516 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010517 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010518 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010519
Bram Moolenaar238a5642006-02-21 22:12:05 +000010520 if (tp->tp_topframe == topframe)
10521 attr = attr_sel;
10522 if (use_sep_chars && col > 0)
10523 screen_putchar('|', 0, col++, attr);
10524
10525 if (tp->tp_topframe != topframe)
10526 attr = attr_nosel;
10527
10528 screen_putchar(' ', 0, col++, attr);
10529
10530 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010531 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010532 cwp = curwin;
10533 wp = firstwin;
10534 }
10535 else
10536 {
10537 cwp = tp->tp_curwin;
10538 wp = tp->tp_firstwin;
10539 }
10540
10541 modified = FALSE;
10542 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10543 if (bufIsChanged(wp->w_buffer))
10544 modified = TRUE;
10545 if (modified || wincount > 1)
10546 {
10547 if (wincount > 1)
10548 {
10549 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010550 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010551 if (col + len >= Columns - 3)
10552 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010553 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010554#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010555 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010556#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010557 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010558#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010559 );
10560 col += len;
10561 }
10562 if (modified)
10563 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10564 screen_putchar(' ', 0, col++, attr);
10565 }
10566
10567 room = scol - col + tabwidth - 1;
10568 if (room > 0)
10569 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010570 /* Get buffer name in NameBuff[] */
10571 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010572 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010573 len = vim_strsize(NameBuff);
10574 p = NameBuff;
10575#ifdef FEAT_MBYTE
10576 if (has_mbyte)
10577 while (len > room)
10578 {
10579 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010580 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010581 }
10582 else
10583#endif
10584 if (len > room)
10585 {
10586 p += len - room;
10587 len = room;
10588 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010589 if (len > Columns - col - 1)
10590 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010591
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010592 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010593 col += len;
10594 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010595 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010596
10597 /* Store the tab page number in TabPageIdxs[], so that
10598 * jump_to_mouse() knows where each one is. */
10599 ++tabcount;
10600 while (scol < col)
10601 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010602 }
10603
Bram Moolenaar238a5642006-02-21 22:12:05 +000010604 if (use_sep_chars)
10605 c = '_';
10606 else
10607 c = ' ';
10608 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010609
10610 /* Put an "X" for closing the current tab if there are several. */
10611 if (first_tabpage->tp_next != NULL)
10612 {
10613 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10614 TabPageIdxs[Columns - 1] = -999;
10615 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010616 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010617
10618 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10619 * set. */
10620 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010621}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010622
10623/*
10624 * Get buffer name for "buf" into NameBuff[].
10625 * Takes care of special buffer names and translates special characters.
10626 */
10627 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010628get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010629{
10630 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010631 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010632 else
10633 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10634 trans_characters(NameBuff, MAXPATHL);
10635}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010636
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637/*
10638 * Get the character to use in a status line. Get its attributes in "*attr".
10639 */
10640 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010641fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642{
10643 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010644
10645#ifdef FEAT_TERMINAL
10646 if (bt_terminal(wp->w_buffer))
10647 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010648 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010649 {
10650 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010651 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010652 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010653 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010654 {
10655 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010656 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010657 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010658 }
10659 else
10660#endif
10661 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010663 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 fill = fill_stl;
10665 }
10666 else
10667 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010668 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 fill = fill_stlnc;
10670 }
10671 /* Use fill when there is highlighting, and highlighting of current
10672 * window differs, or the fillchars differ, or this is not the
10673 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010674 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010675 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676 || (fill_stl != fill_stlnc)))
10677 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010678 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 return '^';
10680 return '=';
10681}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683/*
10684 * Get the character to use in a separator between vertically split windows.
10685 * Get its attributes in "*attr".
10686 */
10687 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010688fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010690 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 if (*attr == 0 && fill_vert == ' ')
10692 return '|';
10693 else
10694 return fill_vert;
10695}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696
10697/*
10698 * Return TRUE if redrawing should currently be done.
10699 */
10700 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010701redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010703#ifdef FEAT_EVAL
10704 if (disable_redraw_for_testing)
10705 return 0;
10706 else
10707#endif
10708 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10710}
10711
10712/*
10713 * Return TRUE if printing messages should currently be done.
10714 */
10715 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010716messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717{
10718 return (!(p_lz && char_avail() && !KeyTyped));
10719}
10720
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010721#ifdef FEAT_MENU
10722/*
10723 * Draw the window toolbar.
10724 */
10725 static void
10726redraw_win_toolbar(win_T *wp)
10727{
10728 vimmenu_T *menu;
10729 int item_idx = 0;
10730 int item_count = 0;
10731 int col = 0;
10732 int next_col;
10733 int off = (int)(current_ScreenLine - ScreenLines);
10734 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10735 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10736
10737 vim_free(wp->w_winbar_items);
10738 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10739 ++item_count;
10740 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10741 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10742
10743 /* TODO: use fewer spaces if there is not enough room */
10744 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010745 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010746 {
10747 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010748 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010749 break;
10750 if (col > 1)
10751 {
10752 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010753 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010754 break;
10755 }
10756
10757 wp->w_winbar_items[item_idx].wb_startcol = col;
10758 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010759 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010760 break;
10761
10762 next_col = text_to_screenline(wp, menu->name, col);
10763 while (col < next_col)
10764 {
10765 ScreenAttrs[off + col] = button_attr;
10766 ++col;
10767 }
10768 wp->w_winbar_items[item_idx].wb_endcol = col;
10769 wp->w_winbar_items[item_idx].wb_menu = menu;
10770 ++item_idx;
10771
Bram Moolenaar02631462017-09-22 15:20:32 +020010772 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010773 break;
10774 space_to_screenline(off + col, button_attr);
10775 ++col;
10776 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010777 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010778 {
10779 space_to_screenline(off + col, fill_attr);
10780 ++col;
10781 }
10782 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10783
Bram Moolenaar02631462017-09-22 15:20:32 +020010784 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10785 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010786}
10787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788/*
10789 * Show current status info in ruler and various other places
10790 * If always is FALSE, only show ruler if position has changed.
10791 */
10792 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010793showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794{
10795 if (!always && !redrawing())
10796 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010797#ifdef FEAT_INS_EXPAND
10798 if (pum_visible())
10799 {
10800 /* Don't redraw right now, do it later. */
10801 curwin->w_redr_status = TRUE;
10802 return;
10803 }
10804#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010805#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010806 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010807 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010808 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 else
10811#endif
10812#ifdef FEAT_CMDL_INFO
10813 win_redr_ruler(curwin, always);
10814#endif
10815
10816#ifdef FEAT_TITLE
10817 if (need_maketitle
10818# ifdef FEAT_STL_OPT
10819 || (p_icon && (stl_syntax & STL_IN_ICON))
10820 || (p_title && (stl_syntax & STL_IN_TITLE))
10821# endif
10822 )
10823 maketitle();
10824#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010825 /* Redraw the tab pages line if needed. */
10826 if (redraw_tabline)
10827 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828}
10829
10830#ifdef FEAT_CMDL_INFO
10831 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010832win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010834#define RULER_BUF_LEN 70
10835 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 int row;
10837 int fillchar;
10838 int attr;
10839 int empty_line = FALSE;
10840 colnr_T virtcol;
10841 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010842 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 int this_ru_col;
10845 int off = 0;
10846 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847
10848 /* If 'ruler' off or redrawing disabled, don't do anything */
10849 if (!p_ru)
10850 return;
10851
10852 /*
10853 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10854 * after deleting lines, before cursor.lnum is corrected.
10855 */
10856 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10857 return;
10858
10859#ifdef FEAT_INS_EXPAND
10860 /* Don't draw the ruler while doing insert-completion, it might overwrite
10861 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 if (edit_submode != NULL)
10864 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010865 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10866 if (pum_visible())
10867 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868#endif
10869
10870#ifdef FEAT_STL_OPT
10871 if (*p_ruf)
10872 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010873 int save_called_emsg = called_emsg;
10874
10875 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010877 if (called_emsg)
10878 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010879 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010880 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 return;
10882 }
10883#endif
10884
10885 /*
10886 * Check if not in Insert mode and the line is empty (will show "0-1").
10887 */
10888 if (!(State & INSERT)
10889 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10890 empty_line = TRUE;
10891
10892 /*
10893 * Only draw the ruler when something changed.
10894 */
10895 validate_virtcol_win(wp);
10896 if ( redraw_cmdline
10897 || always
10898 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10899 || wp->w_cursor.col != wp->w_ru_cursor.col
10900 || wp->w_virtcol != wp->w_ru_virtcol
10901#ifdef FEAT_VIRTUALEDIT
10902 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10903#endif
10904 || wp->w_topline != wp->w_ru_topline
10905 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10906#ifdef FEAT_DIFF
10907 || wp->w_topfill != wp->w_ru_topfill
10908#endif
10909 || empty_line != wp->w_ru_empty)
10910 {
10911 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 if (wp->w_status_height)
10913 {
10914 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010915 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010916 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010917 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 }
10919 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 {
10921 row = Rows - 1;
10922 fillchar = ' ';
10923 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 width = Columns;
10925 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 }
10927
10928 /* In list mode virtcol needs to be recomputed */
10929 virtcol = wp->w_virtcol;
10930 if (wp->w_p_list && lcs_tab1 == NUL)
10931 {
10932 wp->w_p_list = FALSE;
10933 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10934 wp->w_p_list = TRUE;
10935 }
10936
10937 /*
10938 * Some sprintfs return the length, some return a pointer.
10939 * To avoid portability problems we use strlen() here.
10940 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010941 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10943 ? 0L
10944 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010945 len = STRLEN(buffer);
10946 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10948 (int)virtcol + 1);
10949
10950 /*
10951 * Add a "50%" if there is room for it.
10952 * On the last line, don't print in the last column (scrolls the
10953 * screen up on some terminals).
10954 */
10955 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010956 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 this_ru_col = ru_col - (Columns - width);
10961 if (this_ru_col < 0)
10962 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 /* Never use more than half the window/screen width, leave the other
10964 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010965 if (this_ru_col < (width + 1) / 2)
10966 this_ru_col = (width + 1) / 2;
10967 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010969 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010970 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 {
10972#ifdef FEAT_MBYTE
10973 if (has_mbyte)
10974 i += (*mb_char2bytes)(fillchar, buffer + i);
10975 else
10976#endif
10977 buffer[i++] = fillchar;
10978 ++o;
10979 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010980 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 }
10982 /* Truncate at window boundary. */
10983#ifdef FEAT_MBYTE
10984 if (has_mbyte)
10985 {
10986 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010987 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 {
10989 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010990 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 {
10992 buffer[i] = NUL;
10993 break;
10994 }
10995 }
10996 }
10997 else
10998#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010999 if (this_ru_col + (int)STRLEN(buffer) > width)
11000 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001
Bram Moolenaar4033c552017-09-16 20:54:51 +020011002 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 i = redraw_cmdline;
11004 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011005 this_ru_col + off + (int)STRLEN(buffer),
11006 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 fillchar, fillchar, attr);
11008 /* don't redraw the cmdline because of showing the ruler */
11009 redraw_cmdline = i;
11010 wp->w_ru_cursor = wp->w_cursor;
11011 wp->w_ru_virtcol = wp->w_virtcol;
11012 wp->w_ru_empty = empty_line;
11013 wp->w_ru_topline = wp->w_topline;
11014 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11015#ifdef FEAT_DIFF
11016 wp->w_ru_topfill = wp->w_topfill;
11017#endif
11018 }
11019}
11020#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011021
11022#if defined(FEAT_LINEBREAK) || defined(PROTO)
11023/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011024 * Return the width of the 'number' and 'relativenumber' column.
11025 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011026 * Otherwise it depends on 'numberwidth' and the line count.
11027 */
11028 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011029number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011030{
11031 int n;
11032 linenr_T lnum;
11033
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011034 if (wp->w_p_rnu && !wp->w_p_nu)
11035 /* cursor line shows "0" */
11036 lnum = wp->w_height;
11037 else
11038 /* cursor line shows absolute line number */
11039 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011040
Bram Moolenaar6b314672015-03-20 15:42:10 +010011041 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011042 return wp->w_nrwidth_width;
11043 wp->w_nrwidth_line_count = lnum;
11044
11045 n = 0;
11046 do
11047 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011048 lnum /= 10;
11049 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011050 } while (lnum > 0);
11051
11052 /* 'numberwidth' gives the minimal width plus one */
11053 if (n < wp->w_p_nuw - 1)
11054 n = wp->w_p_nuw - 1;
11055
11056 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011057 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011058 return n;
11059}
11060#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011061
11062/*
11063 * Return the current cursor column. This is the actual position on the
11064 * screen. First column is 0.
11065 */
11066 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011067screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011068{
11069 return screen_cur_col;
11070}
11071
11072/*
11073 * Return the current cursor row. This is the actual position on the screen.
11074 * First row is 0.
11075 */
11076 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011077screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011078{
11079 return screen_cur_row;
11080}