blob: 8349e5f6b77ed2e142697e457bffce5d91ee8d42 [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{
206 if (wp->w_redr_type < type)
207 {
208 wp->w_redr_type = type;
209 if (type >= NOT_VALID)
210 wp->w_lines_valid = 0;
211 if (must_redraw < type) /* must_redraw is the maximum of all windows */
212 must_redraw = type;
213 }
214}
215
216/*
217 * Force a complete redraw later. Also resets the highlighting. To be used
218 * after executing a shell command that messes up the screen.
219 */
220 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100221redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000224#ifdef FEAT_GUI
225 if (gui.in_use)
226 /* Use a code that will reset gui.highlight_mask in
227 * gui_stop_highlight(). */
228 screen_attr = HL_ALL + 1;
229 else
230#endif
231 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200232 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233}
234
235/*
236 * Mark all windows to be redrawn later.
237 */
238 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100239redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240{
241 win_T *wp;
242
243 FOR_ALL_WINDOWS(wp)
244 {
245 redraw_win_later(wp, type);
246 }
247}
248
249/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000250 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 */
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 redraw_buf_later(curbuf, type);
256}
257
258 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100259redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 win_T *wp;
262
263 FOR_ALL_WINDOWS(wp)
264 {
265 if (wp->w_buffer == buf)
266 redraw_win_later(wp, type);
267 }
268}
269
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200270 void
271redraw_buf_and_status_later(buf_T *buf, int type)
272{
273 win_T *wp;
274
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200275#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200276 if (wild_menu_showing != 0)
277 /* Don't redraw while the command line completion is displayed, it
278 * would disappear. */
279 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200280#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281 FOR_ALL_WINDOWS(wp)
282 {
283 if (wp->w_buffer == buf)
284 {
285 redraw_win_later(wp, type);
286 wp->w_redr_status = TRUE;
287 }
288 }
289}
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200292 * Redraw as soon as possible. When the command line is not scrolled redraw
293 * right away and restore what was on the command line.
294 * Return a code indicating what happened.
295 */
296 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100297redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200298{
299 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200300 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200301 int r;
302 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 schar_T *screenline; /* copy from ScreenLines[] */
304 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200305#ifdef FEAT_MBYTE
306 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200307 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200309 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310#endif
311
312 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 return ret;
315
316 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 if (screenline == NULL || screenattr == NULL)
323 ret = 2;
324#ifdef FEAT_MBYTE
325 if (enc_utf8)
326 {
327 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenlineUC == NULL)
330 ret = 2;
331 for (i = 0; i < p_mco; ++i)
332 {
333 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200334 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (screenlineC[i] == NULL)
336 ret = 2;
337 }
338 }
339 if (enc_dbcs == DBCS_JPNU)
340 {
341 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200342 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenline2 == NULL)
344 ret = 2;
345 }
346#endif
347
348 if (ret != 2)
349 {
350 /* Save the text displayed in the command line area. */
351 for (r = 0; r < rows; ++r)
352 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200353 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200354 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200355 (size_t)cols * sizeof(schar_T));
356 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359#ifdef FEAT_MBYTE
360 if (enc_utf8)
361 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 mch_memmove(screenlineC[i] + r * cols,
367 ScreenLinesC[i] + LineOffset[cmdline_row + r],
368 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200369 }
370 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200373 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374#endif
375 }
376
377 update_screen(0);
378 ret = 3;
379
380 if (must_redraw == 0)
381 {
382 int off = (int)(current_ScreenLine - ScreenLines);
383
384 /* Restore the text displayed in the command line area. */
385 for (r = 0; r < rows; ++r)
386 {
387 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200388 screenline + r * cols,
389 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenattr + r * cols,
392 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393#ifdef FEAT_MBYTE
394 if (enc_utf8)
395 {
396 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenlineUC + r * cols,
398 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 for (i = 0; i < p_mco; ++i)
400 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200401 screenlineC[i] + r * cols,
402 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200403 }
404 if (enc_dbcs == DBCS_JPNU)
405 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200406 screenline2 + r * cols,
407 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200408#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200409 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 }
411 ret = 4;
412 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413 }
414
415 vim_free(screenline);
416 vim_free(screenattr);
417#ifdef FEAT_MBYTE
418 if (enc_utf8)
419 {
420 vim_free(screenlineUC);
421 for (i = 0; i < p_mco; ++i)
422 vim_free(screenlineC[i]);
423 }
424 if (enc_dbcs == DBCS_JPNU)
425 vim_free(screenline2);
426#endif
427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
435
436/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100437 * Invoked after an asynchronous callback is called.
438 * If an echo command was used the cursor needs to be put back where
439 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200440 * If "call_update_screen" is FALSE don't call update_screen() when at the
441 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100442 */
443 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200444redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200446 ++redrawing_for_callback;
447
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100448 if (State == HITRETURN || State == ASKMORE)
449 ; /* do nothing */
450 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200451 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200452 /* Redrawing only works when the screen didn't scroll. Don't clear
453 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454 if (msg_scrolled == 0
455#ifdef FEAT_WILDMENU
456 && wild_menu_showing == 0
457#endif
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200458 && call_update_screen)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200459 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200460 /* Redraw in the same position, so that the user can continue
461 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200462 redrawcmdline_ex(FALSE);
463 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200464 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100465 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 /* keep the command line if possible */
467 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100468 setcursor();
469 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470 cursor_on();
471 out_flush();
472#ifdef FEAT_GUI
473 if (gui.in_use)
474 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200475 /* Don't update the cursor when it is blinking and off to avoid
476 * flicker. */
477 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200478 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100479 gui_mch_flush();
480 }
481#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200482
483 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100484}
485
486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 * Changed something in the current window, at buffer line "lnum", that
488 * requires that line and possibly other lines to be redrawn.
489 * Used when entering/leaving Insert mode with the cursor on a folded line.
490 * Used to remove the "$" from a change command.
491 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
492 * may become invalid and the whole window will have to be redrawn.
493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100495redrawWinline(
496 linenr_T lnum,
497 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499#ifdef FEAT_FOLDING
500 int i;
501#endif
502
503 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
504 curwin->w_redraw_top = lnum;
505 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
506 curwin->w_redraw_bot = lnum;
507 redraw_later(VALID);
508
509#ifdef FEAT_FOLDING
510 if (invalid)
511 {
512 /* A w_lines[] entry for this lnum has become invalid. */
513 i = find_wl_entry(curwin, lnum);
514 if (i >= 0)
515 curwin->w_lines[i].wl_valid = FALSE;
516 }
517#endif
518}
519
520/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200521 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 */
523 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100524update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
526 redraw_curbuf_later(type);
527 update_screen(type);
528}
529
530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 * Based on the current value of curwin->w_topline, transfer a screenfull
532 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200533 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200535 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200536update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200538 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 win_T *wp;
540 static int did_intro = FALSE;
541#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
542 int did_one;
543#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200544#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200545 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200546 int gui_cursor_col;
547 int gui_cursor_row;
548#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200549 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000551 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200553 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200555 if (type == VALID_NO_UPDATE)
556 {
557 no_update = TRUE;
558 type = 0;
559 }
560
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 if (must_redraw)
562 {
563 if (type < must_redraw) /* use maximal type */
564 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000565
566 /* must_redraw is reset here, so that when we run into some weird
567 * reason to redraw while busy redrawing (e.g., asynchronous
568 * scrolling), or update_topline() in win_update() will cause a
569 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 must_redraw = 0;
571 }
572
573 /* Need to update w_lines[]. */
574 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
575 type = NOT_VALID;
576
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000577 /* Postpone the redrawing when it's not needed and when being called
578 * recursively. */
579 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {
581 redraw_later(type); /* remember type for next time */
582 must_redraw = type;
583 if (type > INVERTED_ALL)
584 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200585 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587
588 updating_screen = TRUE;
589#ifdef FEAT_SYN_HL
590 ++display_tick; /* let syntax code know we're in a next round of
591 * display updating */
592#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200593 if (no_update)
594 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
596 /*
597 * if the screen was scrolled up when displaying a message, scroll it down
598 */
599 if (msg_scrolled)
600 {
601 clear_cmdline = TRUE;
602 if (msg_scrolled > Rows - 5) /* clearing is faster */
603 type = CLEAR;
604 else if (type != CLEAR)
605 {
606 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200607 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
608 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 type = CLEAR;
610 FOR_ALL_WINDOWS(wp)
611 {
612 if (W_WINROW(wp) < msg_scrolled)
613 {
614 if (W_WINROW(wp) + wp->w_height > msg_scrolled
615 && wp->w_redr_type < REDRAW_TOP
616 && wp->w_lines_valid > 0
617 && wp->w_topline == wp->w_lines[0].wl_lnum)
618 {
619 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
620 wp->w_redr_type = REDRAW_TOP;
621 }
622 else
623 {
624 wp->w_redr_type = NOT_VALID;
Bram 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. */
4172 if (*ptr == NUL && did_line_attr >= 1)
4173 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 }
4175#endif
4176
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004178 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004180 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4181 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004183 if (diff_hlf == HLF_TXD && ptr - line > change_end
4184 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004186 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004187 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004188 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004191
4192 /* Decide which of the highlight attributes to use. */
4193 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004194#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004195 if (area_attr != 0)
4196 char_attr = hl_combine_attr(line_attr, area_attr);
4197 else if (search_attr != 0)
4198 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004199 /* Use line_attr when not in the Visual or 'incsearch' area
4200 * (area_attr may be 0 when "noinvcur" is set). */
4201 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004202 || vcol < fromcol || vcol_prev < fromcol_prev
4203 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004204 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004205#else
4206 if (area_attr != 0)
4207 char_attr = area_attr;
4208 else if (search_attr != 0)
4209 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004210#endif
4211 else
4212 {
4213 attr_pri = FALSE;
4214#ifdef FEAT_SYN_HL
4215 if (has_syntax)
4216 char_attr = syntax_attr;
4217 else
4218#endif
4219 char_attr = 0;
4220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222
4223 /*
4224 * Get the next character to put on the screen.
4225 */
4226 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004227 * The "p_extra" points to the extra stuff that is inserted to
4228 * represent special characters (non-printable stuff) and other
4229 * things. When all characters are the same, c_extra is used.
4230 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4231 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4233 */
4234 if (n_extra > 0)
4235 {
4236 if (c_extra != NUL)
4237 {
4238 c = c_extra;
4239#ifdef FEAT_MBYTE
4240 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004241 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
4243 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004244 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004245 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 else
4248 mb_utf8 = FALSE;
4249#endif
4250 }
4251 else
4252 {
4253 c = *p_extra;
4254#ifdef FEAT_MBYTE
4255 if (has_mbyte)
4256 {
4257 mb_c = c;
4258 if (enc_utf8)
4259 {
4260 /* If the UTF-8 character is more than one byte:
4261 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004262 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 mb_utf8 = FALSE;
4264 if (mb_l > n_extra)
4265 mb_l = 1;
4266 else if (mb_l > 1)
4267 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004268 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004270 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272 }
4273 else
4274 {
4275 /* if this is a DBCS character, put it in "mb_c" */
4276 mb_l = MB_BYTE2LEN(c);
4277 if (mb_l >= n_extra)
4278 mb_l = 1;
4279 else if (mb_l > 1)
4280 mb_c = (c << 8) + p_extra[1];
4281 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004282 if (mb_l == 0) /* at the NUL at end-of-line */
4283 mb_l = 1;
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 /* If a double-width char doesn't fit display a '>' in the
4286 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004287 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288# ifdef FEAT_RIGHTLEFT
4289 wp->w_p_rl ? (col <= 0) :
4290# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004291 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 && (*mb_char2cells)(mb_c) == 2)
4293 {
4294 c = '>';
4295 mb_c = c;
4296 mb_l = 1;
4297 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004298 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 /* put the pointer back to output the double-width
4300 * character at the start of the next line. */
4301 ++n_extra;
4302 --p_extra;
4303 }
4304 else
4305 {
4306 n_extra -= mb_l - 1;
4307 p_extra += mb_l - 1;
4308 }
4309 }
4310#endif
4311 ++p_extra;
4312 }
4313 --n_extra;
4314 }
4315 else
4316 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004317#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004318 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004319#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004320
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004321 if (p_extra_free != NULL)
4322 {
4323 vim_free(p_extra_free);
4324 p_extra_free = NULL;
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 /*
4327 * Get a character from the line itself.
4328 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004329 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004330#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004331 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333#ifdef FEAT_MBYTE
4334 if (has_mbyte)
4335 {
4336 mb_c = c;
4337 if (enc_utf8)
4338 {
4339 /* If the UTF-8 character is more than one byte: Decode it
4340 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004341 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 mb_utf8 = FALSE;
4343 if (mb_l > 1)
4344 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004345 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 /* Overlong encoded ASCII or ASCII with composing char
4347 * is displayed normally, except a NUL. */
4348 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004349 {
4350 c = mb_c;
4351# ifdef FEAT_LINEBREAK
4352 c0 = mb_c;
4353# endif
4354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004356
4357 /* At start of the line we can have a composing char.
4358 * Draw it as a space with a composing char. */
4359 if (utf_iscomposing(mb_c))
4360 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004361 int i;
4362
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004363 for (i = Screen_mco - 1; i > 0; --i)
4364 u8cc[i] = u8cc[i - 1];
4365 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004366 mb_c = ' ';
4367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369
4370 if ((mb_l == 1 && c >= 0x80)
4371 || (mb_l >= 1 && mb_c == 0)
4372 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004373# ifdef UNICODE16
4374 || mb_c >= 0x10000
4375# endif
4376 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 {
4378 /*
4379 * Illegal UTF-8 byte: display as <xx>.
4380 * Non-BMP character : display as ? or fullwidth ?.
4381 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004382# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
4386 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004387# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 if (wp->w_p_rl) /* reverse */
4389 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004392# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 else if (utf_char2cells(mb_c) != 2)
4394 STRCPY(extra, "?");
4395 else
4396 /* 0xff1f in UTF-8: full-width '?' */
4397 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
4400 p_extra = extra;
4401 c = *p_extra;
4402 mb_c = mb_ptr2char_adv(&p_extra);
4403 mb_utf8 = (c >= 0x80);
4404 n_extra = (int)STRLEN(p_extra);
4405 c_extra = NUL;
4406 if (area_attr == 0 && search_attr == 0)
4407 {
4408 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004409 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 saved_attr2 = char_attr; /* save current attr */
4411 }
4412 }
4413 else if (mb_l == 0) /* at the NUL at end-of-line */
4414 mb_l = 1;
4415#ifdef FEAT_ARABIC
4416 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4417 {
4418 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004419 int pc, pc1, nc;
4420 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422 /* The idea of what is the previous and next
4423 * character depends on 'rightleft'. */
4424 if (wp->w_p_rl)
4425 {
4426 pc = prev_c;
4427 pc1 = prev_c1;
4428 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004429 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431 else
4432 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004433 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004435 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437 prev_c = mb_c;
4438
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004439 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 else
4442 prev_c = mb_c;
4443#endif
4444 }
4445 else /* enc_dbcs */
4446 {
4447 mb_l = MB_BYTE2LEN(c);
4448 if (mb_l == 0) /* at the NUL at end-of-line */
4449 mb_l = 1;
4450 else if (mb_l > 1)
4451 {
4452 /* We assume a second byte below 32 is illegal.
4453 * Hopefully this is OK for all double-byte encodings!
4454 */
4455 if (ptr[1] >= 32)
4456 mb_c = (c << 8) + ptr[1];
4457 else
4458 {
4459 if (ptr[1] == NUL)
4460 {
4461 /* head byte at end of line */
4462 mb_l = 1;
4463 transchar_nonprint(extra, c);
4464 }
4465 else
4466 {
4467 /* illegal tail byte */
4468 mb_l = 2;
4469 STRCPY(extra, "XX");
4470 }
4471 p_extra = extra;
4472 n_extra = (int)STRLEN(extra) - 1;
4473 c_extra = NUL;
4474 c = *p_extra++;
4475 if (area_attr == 0 && search_attr == 0)
4476 {
4477 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004478 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 saved_attr2 = char_attr; /* save current attr */
4480 }
4481 mb_c = c;
4482 }
4483 }
4484 }
4485 /* If a double-width char doesn't fit display a '>' in the
4486 * last column; the character is displayed at the start of the
4487 * next line. */
4488 if ((
4489# ifdef FEAT_RIGHTLEFT
4490 wp->w_p_rl ? (col <= 0) :
4491# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004492 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 && (*mb_char2cells)(mb_c) == 2)
4494 {
4495 c = '>';
4496 mb_c = c;
4497 mb_utf8 = FALSE;
4498 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004499 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 /* Put pointer back so that the character will be
4501 * displayed at the start of the next line. */
4502 --ptr;
4503 }
4504 else if (*ptr != NUL)
4505 ptr += mb_l - 1;
4506
4507 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004508 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004509 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004510 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004513 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 c = ' ';
4515 if (area_attr == 0 && search_attr == 0)
4516 {
4517 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004518 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 saved_attr2 = char_attr; /* save current attr */
4520 }
4521 mb_c = c;
4522 mb_utf8 = FALSE;
4523 mb_l = 1;
4524 }
4525
4526 }
4527#endif
4528 ++ptr;
4529
4530 if (extra_check)
4531 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004532#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004533 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004534#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004535
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004536#ifdef FEAT_TERMINAL
4537 if (get_term_attr)
4538 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004539 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004540
4541 if (!attr_pri)
4542 char_attr = syntax_attr;
4543 else
4544 char_attr = hl_combine_attr(syntax_attr, char_attr);
4545 }
4546#endif
4547
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004548#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 /* Get syntax attribute, unless still at the start of the line
4550 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004551 v = (long)(ptr - line);
4552 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
4554 /* Get the syntax attribute for the character. If there
4555 * is an error, disable syntax highlighting. */
4556 save_did_emsg = did_emsg;
4557 did_emsg = FALSE;
4558
Bram Moolenaar217ad922005-03-20 22:37:15 +00004559 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004560# ifdef FEAT_SPELL
4561 has_spell ? &can_spell :
4562# endif
4563 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
4565 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004566 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004567 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004568 has_syntax = FALSE;
4569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 else
4571 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004572#ifdef SYN_TIME_LIMIT
4573 if (wp->w_s->b_syn_slow)
4574 has_syntax = FALSE;
4575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
4577 /* Need to get the line again, a multi-line regexp may
4578 * have made it invalid. */
4579 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4580 ptr = line + v;
4581
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004582 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004584 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004585 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004586# ifdef FEAT_CONCEAL
4587 /* no concealing past the end of the line, it interferes
4588 * with line highlighting */
4589 if (c == NUL)
4590 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004591 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004592 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004595#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004596
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004597#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004598 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004599 * Only do this when there is no syntax highlighting, the
4600 * @Spell cluster is not used or the current syntax item
4601 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004602 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004603 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004604 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004605# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004606 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004607 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004608# endif
4609 if (c != 0 && (
4610# ifdef FEAT_SYN_HL
4611 !has_syntax ||
4612# endif
4613 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004614 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004615 char_u *prev_ptr, *p;
4616 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004617 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004618# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004619 if (has_mbyte)
4620 {
4621 prev_ptr = ptr - mb_l;
4622 v -= mb_l - 1;
4623 }
4624 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004625# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004626 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004627
4628 /* Use nextline[] if possible, it has the start of the
4629 * next line concatenated. */
4630 if ((prev_ptr - line) - nextlinecol >= 0)
4631 p = nextline + (prev_ptr - line) - nextlinecol;
4632 else
4633 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004634 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004635 len = spell_check(wp, p, &spell_hlf, &cap_col,
4636 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004637 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004638
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004639 /* In Insert mode only highlight a word that
4640 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004641 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004642 && (State & INSERT) != 0
4643 && wp->w_cursor.lnum == lnum
4644 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004645 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004646 && wp->w_cursor.col < (colnr_T)word_end)
4647 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004648 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004649 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004650 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004651
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004652 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004653 && (p - nextline) + len > nextline_idx)
4654 {
4655 /* Remember that the good word continues at the
4656 * start of the next line. */
4657 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004658 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004659 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004660
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004661 /* Turn index into actual attributes. */
4662 if (spell_hlf != HLF_COUNT)
4663 spell_attr = highlight_attr[spell_hlf];
4664
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004665 if (cap_col > 0)
4666 {
4667 if (p != prev_ptr
4668 && (p - nextline) + cap_col >= nextline_idx)
4669 {
4670 /* Remember that the word in the next line
4671 * must start with a capital. */
4672 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004673 cap_col = (int)((p - nextline) + cap_col
4674 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004675 }
4676 else
4677 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004678 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004679 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004680 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004681 }
4682 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004683 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004684 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004685 char_attr = hl_combine_attr(char_attr, spell_attr);
4686 else
4687 char_attr = hl_combine_attr(spell_attr, char_attr);
4688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689#endif
4690#ifdef FEAT_LINEBREAK
4691 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004692 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004694 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004695 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004697# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004698 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004699# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004700 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004702 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004704 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004705
Bram Moolenaar597a4222014-06-25 14:39:50 +02004706 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004707 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004708 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004709 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaara3650912014-11-19 13:21:57 +01004710 n_extra = (int)wp->w_buffer->b_p_ts
4711 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4712
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004713# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004714 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004715# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004717# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004718 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004719 {
4720#ifdef FEAT_CONCEAL
4721 if (c == TAB)
4722 /* See "Tab alignment" below. */
4723 FIX_FOR_BOGUSCOLS;
4724#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004725 if (!wp->w_p_list)
4726 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 }
4729#endif
4730
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004731 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4732 */
4733 if (wp->w_p_list
4734 && (((c == 160
4735#ifdef FEAT_MBYTE
4736 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4737#endif
4738 ) && lcs_nbsp)
4739 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4740 {
4741 c = (c == ' ') ? lcs_space : lcs_nbsp;
4742 if (area_attr == 0 && search_attr == 0)
4743 {
4744 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004745 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004746 saved_attr2 = char_attr; /* save current attr */
4747 }
4748#ifdef FEAT_MBYTE
4749 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004750 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004751 {
4752 mb_utf8 = TRUE;
4753 u8cc[0] = 0;
4754 c = 0xc0;
4755 }
4756 else
4757 mb_utf8 = FALSE;
4758#endif
4759 }
4760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4762 {
4763 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004764 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
4766 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004767 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 saved_attr2 = char_attr; /* save current attr */
4769 }
4770#ifdef FEAT_MBYTE
4771 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004772 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
4774 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004775 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004776 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
4778 else
4779 mb_utf8 = FALSE;
4780#endif
4781 }
4782 }
4783
4784 /*
4785 * Handling of non-printable characters.
4786 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004787 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 {
4789 /*
4790 * when getting a character from the file, we may have to
4791 * turn it into something else on the way to putting it
4792 * into "ScreenLines".
4793 */
4794 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4795 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004796 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004797 long vcol_adjusted = vcol; /* removed showbreak length */
4798#ifdef FEAT_LINEBREAK
4799 /* only adjust the tab_len, when at the first column
4800 * after the showbreak value was drawn */
4801 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4802 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004805 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004806 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4807
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004808#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004809 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004810#endif
4811 /* tab amount depends on current column */
4812 n_extra = tab_len;
4813#ifdef FEAT_LINEBREAK
4814 else
4815 {
4816 char_u *p;
4817 int len = n_extra;
4818 int i;
4819 int saved_nextra = n_extra;
4820
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004821#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004822 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004823 /* there are characters to conceal */
4824 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004825 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4826 */
4827 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4828 && n_extra > tab_len)
4829 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004830#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004831
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004832 /* if n_extra > 0, it gives the number of chars, to
4833 * use for a tab, else we need to calculate the width
4834 * for a tab */
4835#ifdef FEAT_MBYTE
4836 len = (tab_len * mb_char2len(lcs_tab2));
4837 if (n_extra > 0)
4838 len += n_extra - tab_len;
4839#endif
4840 c = lcs_tab1;
4841 p = alloc((unsigned)(len + 1));
4842 vim_memset(p, ' ', len);
4843 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004844 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004845 p_extra_free = p;
4846 for (i = 0; i < tab_len; i++)
4847 {
4848#ifdef FEAT_MBYTE
4849 mb_char2bytes(lcs_tab2, p);
4850 p += mb_char2len(lcs_tab2);
4851 n_extra += mb_char2len(lcs_tab2)
4852 - (saved_nextra > 0 ? 1 : 0);
4853#else
4854 p[i] = lcs_tab2;
4855#endif
4856 }
4857 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004858#ifdef FEAT_CONCEAL
4859 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4860 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004861 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004862 n_extra -= vcol_off;
4863#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004864 }
4865#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004866#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004867 {
4868 int vc_saved = vcol_off;
4869
4870 /* Tab alignment should be identical regardless of
4871 * 'conceallevel' value. So tab compensates of all
4872 * previous concealed characters, and thus resets
4873 * vcol_off and boguscols accumulated so far in the
4874 * line. Note that the tab can be longer than
4875 * 'tabstop' when there are concealed characters. */
4876 FIX_FOR_BOGUSCOLS;
4877
4878 /* Make sure, the highlighting for the tab char will be
4879 * correctly set further below (effectively reverts the
4880 * FIX_FOR_BOGSUCOLS macro */
4881 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004882 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004883 tab_len += vc_saved;
4884 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886#ifdef FEAT_MBYTE
4887 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4888#endif
4889 if (wp->w_p_list)
4890 {
4891 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004892#ifdef FEAT_LINEBREAK
4893 if (wp->w_p_lbr)
4894 c_extra = NUL; /* using p_extra from above */
4895 else
4896#endif
4897 c_extra = lcs_tab2;
4898 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004899 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 saved_attr2 = char_attr; /* save current attr */
4901#ifdef FEAT_MBYTE
4902 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004903 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
4905 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004906 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004907 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909#endif
4910 }
4911 else
4912 {
4913 c_extra = ' ';
4914 c = ' ';
4915 }
4916 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004917 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004918 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004919 || ((fromcol >= 0 || fromcol_prev >= 0)
4920 && tocol > vcol
4921 && VIsual_mode != Ctrl_V
4922 && (
4923# ifdef FEAT_RIGHTLEFT
4924 wp->w_p_rl ? (col >= 0) :
4925# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004926 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004927 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004928 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004929 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004930 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004932 /* Display a '$' after the line or highlight an extra
4933 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4935 /* For a diff line the highlighting continues after the
4936 * "$". */
4937 if (
4938# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004939 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940# ifdef LINE_ATTR
4941 &&
4942# endif
4943# endif
4944# ifdef LINE_ATTR
4945 line_attr == 0
4946# endif
4947 )
4948#endif
4949 {
4950#ifdef FEAT_VIRTUALEDIT
4951 /* In virtualedit, visual selections may extend
4952 * beyond end of line. */
4953 if (area_highlighting && virtual_active()
4954 && tocol != MAXCOL && vcol < tocol)
4955 n_extra = 0;
4956 else
4957#endif
4958 {
4959 p_extra = at_end_str;
4960 n_extra = 1;
4961 c_extra = NUL;
4962 }
4963 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004964 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004965 c = lcs_eol;
4966 else
4967 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 lcs_eol_one = -1;
4969 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004970 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004972 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 n_attr = 1;
4974 }
4975#ifdef FEAT_MBYTE
4976 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004977 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004980 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004981 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
4983 else
4984 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4985#endif
4986 }
4987 else if (c != NUL)
4988 {
4989 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004990 if (n_extra == 0)
4991 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992#ifdef FEAT_RIGHTLEFT
4993 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4994 rl_mirror(p_extra); /* reverse "<12>" */
4995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004997#ifdef FEAT_LINEBREAK
4998 if (wp->w_p_lbr)
4999 {
5000 char_u *p;
5001
5002 c = *p_extra;
5003 p = alloc((unsigned)n_extra + 1);
5004 vim_memset(p, ' ', n_extra);
5005 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5006 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005007 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005008 p_extra_free = p_extra = p;
5009 }
5010 else
5011#endif
5012 {
5013 n_extra = byte2cells(c) - 1;
5014 c = *p_extra++;
5015 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005016 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
5018 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005019 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 saved_attr2 = char_attr; /* save current attr */
5021 }
5022#ifdef FEAT_MBYTE
5023 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5024#endif
5025 }
5026#ifdef FEAT_VIRTUALEDIT
5027 else if (VIsual_active
5028 && (VIsual_mode == Ctrl_V
5029 || VIsual_mode == 'v')
5030 && virtual_active()
5031 && tocol != MAXCOL
5032 && vcol < tocol
5033 && (
5034# ifdef FEAT_RIGHTLEFT
5035 wp->w_p_rl ? (col >= 0) :
5036# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005037 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
5039 c = ' ';
5040 --ptr; /* put it back at the NUL */
5041 }
5042#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005043#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 else if ((
5045# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005046 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005048# ifdef FEAT_TERMINAL
5049 term_attr != 0 ||
5050# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 ) && (
5053# ifdef FEAT_RIGHTLEFT
5054 wp->w_p_rl ? (col >= 0) :
5055# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005056 (col
5057# ifdef FEAT_CONCEAL
5058 - boguscols
5059# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005060 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
5062 /* Highlight until the right side of the window */
5063 c = ' ';
5064 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005065
5066 /* Remember we do the char for line highlighting. */
5067 ++did_line_attr;
5068
5069 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005070 if (line_attr != 0 && char_attr == search_attr
5071 && (did_line_attr > 1
5072 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005073 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074# ifdef FEAT_DIFF
5075 if (diff_hlf == HLF_TXD)
5076 {
5077 diff_hlf = HLF_CHD;
5078 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005079 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005080 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005081 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5082 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005083 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005087# ifdef FEAT_TERMINAL
5088 if (term_attr != 0)
5089 {
5090 char_attr = term_attr;
5091 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5092 char_attr = hl_combine_attr(char_attr,
5093 HL_ATTR(HLF_CUL));
5094 }
5095# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097#endif
5098 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005099
5100#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005101 if ( wp->w_p_cole > 0
5102 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005103 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005104 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005105 && !(lnum_in_visual_area
5106 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005107 {
5108 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005109 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005110 && (syn_get_sub_char() != NUL || match_conc
5111 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005112 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005113 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005114 /* First time at this concealed item: display one
5115 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005116 if (match_conc)
5117 c = match_conc;
5118 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005119 c = syn_get_sub_char();
5120 else if (lcs_conceal != NUL)
5121 c = lcs_conceal;
5122 else
5123 c = ' ';
5124
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005125 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005126
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005127 if (n_extra > 0)
5128 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005129 vcol += n_extra;
5130 if (wp->w_p_wrap && n_extra > 0)
5131 {
5132# ifdef FEAT_RIGHTLEFT
5133 if (wp->w_p_rl)
5134 {
5135 col -= n_extra;
5136 boguscols -= n_extra;
5137 }
5138 else
5139# endif
5140 {
5141 boguscols += n_extra;
5142 col += n_extra;
5143 }
5144 }
5145 n_extra = 0;
5146 n_attr = 0;
5147 }
5148 else if (n_skip == 0)
5149 {
5150 is_concealing = TRUE;
5151 n_skip = 1;
5152 }
5153# ifdef FEAT_MBYTE
5154 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005155 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005156 {
5157 mb_utf8 = TRUE;
5158 u8cc[0] = 0;
5159 c = 0xc0;
5160 }
5161 else
5162 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5163# endif
5164 }
5165 else
5166 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005167 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005168 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005169 }
5170#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005173#ifdef FEAT_CONCEAL
5174 /* In the cursor line and we may be concealing characters: correct
5175 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005176 if (!did_wcol && draw_state == WL_LINE
5177 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005178 && conceal_cursor_line(wp)
5179 && (int)wp->w_virtcol <= vcol + n_skip)
5180 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005181# ifdef FEAT_RIGHTLEFT
5182 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005183 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005184 else
5185# endif
5186 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005187 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005188 did_wcol = TRUE;
5189 }
5190#endif
5191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 /* Don't override visual selection highlighting. */
5193 if (n_attr > 0
5194 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005195 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 char_attr = extra_attr;
5197
Bram Moolenaar81695252004-12-29 20:58:21 +00005198#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 /* XIM don't send preedit_start and preedit_end, but they send
5200 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5201 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005202 if (p_imst == IM_ON_THE_SPOT
5203 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005204 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 && (State & INSERT)
5206 && !p_imdisable
5207 && im_is_preediting()
5208 && draw_state == WL_LINE)
5209 {
5210 colnr_T tcol;
5211
5212 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005213 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 else
5215 tcol = preedit_end_col;
5216 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5217 {
5218 if (feedback_old_attr < 0)
5219 {
5220 feedback_col = 0;
5221 feedback_old_attr = char_attr;
5222 }
5223 char_attr = im_get_feedback_attr(feedback_col);
5224 if (char_attr < 0)
5225 char_attr = feedback_old_attr;
5226 feedback_col++;
5227 }
5228 else if (feedback_old_attr >= 0)
5229 {
5230 char_attr = feedback_old_attr;
5231 feedback_old_attr = -1;
5232 feedback_col = 0;
5233 }
5234 }
5235#endif
5236 /*
5237 * Handle the case where we are in column 0 but not on the first
5238 * character of the line and the user wants us to show us a
5239 * special character (via 'listchars' option "precedes:<char>".
5240 */
5241 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005242 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5244#ifdef FEAT_DIFF
5245 && filler_todo <= 0
5246#endif
5247 && draw_state > WL_NR
5248 && c != NUL)
5249 {
5250 c = lcs_prec;
5251 lcs_prec_todo = NUL;
5252#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005253 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5254 {
5255 /* Double-width character being overwritten by the "precedes"
5256 * character, need to fill up half the character. */
5257 c_extra = MB_FILLER_CHAR;
5258 n_extra = 1;
5259 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005260 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005263 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
5265 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005266 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005267 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 }
5269 else
5270 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5271#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005272 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
5274 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005275 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 n_attr3 = 1;
5277 }
5278 }
5279
5280 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005281 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005283 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005284#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005285 || did_line_attr == 1
5286#endif
5287 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005289#ifdef FEAT_SEARCH_EXTRA
5290 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005291
5292 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005293 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005294 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005295#endif
5296
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005297 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 * highlight match at end of line. If it's beyond the last
5299 * char on the screen, just overwrite that one (tricky!) Not
5300 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005301#ifdef FEAT_SEARCH_EXTRA
5302 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005303 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005304 prevcol_hl_flag = TRUE;
5305 else
5306 {
5307 cur = wp->w_match_head;
5308 while (cur != NULL)
5309 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005310 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005311 {
5312 prevcol_hl_flag = TRUE;
5313 break;
5314 }
5315 cur = cur->next;
5316 }
5317 }
5318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005320 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005321 && (VIsual_mode != Ctrl_V
5322 || lnum == VIsual.lnum
5323 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005324 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325#ifdef FEAT_SEARCH_EXTRA
5326 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005327 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005328# ifdef FEAT_SYN_HL
5329 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5330 && !(wp == curwin && VIsual_active))
5331# endif
5332# ifdef FEAT_DIFF
5333 && diff_hlf == (hlf_T)0
5334# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005335# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005336 && did_line_attr <= 1
5337# endif
5338 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#endif
5340 ))
5341 {
5342 int n = 0;
5343
5344#ifdef FEAT_RIGHTLEFT
5345 if (wp->w_p_rl)
5346 {
5347 if (col < 0)
5348 n = 1;
5349 }
5350 else
5351#endif
5352 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005353 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 n = -1;
5355 }
5356 if (n != 0)
5357 {
5358 /* At the window boundary, highlight the last character
5359 * instead (better than nothing). */
5360 off += n;
5361 col += n;
5362 }
5363 else
5364 {
5365 /* Add a blank character to highlight. */
5366 ScreenLines[off] = ' ';
5367#ifdef FEAT_MBYTE
5368 if (enc_utf8)
5369 ScreenLinesUC[off] = 0;
5370#endif
5371 }
5372#ifdef FEAT_SEARCH_EXTRA
5373 if (area_attr == 0)
5374 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005375 /* Use attributes from match with highest priority among
5376 * 'search_hl' and the match list. */
5377 char_attr = search_hl.attr;
5378 cur = wp->w_match_head;
5379 shl_flag = FALSE;
5380 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005381 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005382 if (shl_flag == FALSE
5383 && ((cur != NULL
5384 && cur->priority > SEARCH_HL_PRIORITY)
5385 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005386 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005387 shl = &search_hl;
5388 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005389 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005390 else
5391 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005392 if ((ptr - line) - 1 == (long)shl->startcol
5393 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005394 char_attr = shl->attr;
5395 if (shl != &search_hl && cur != NULL)
5396 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
5399#endif
5400 ScreenAttrs[off] = char_attr;
5401#ifdef FEAT_RIGHTLEFT
5402 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005405 --off;
5406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 else
5408#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005411 ++off;
5412 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005413 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005414#ifdef FEAT_SYN_HL
5415 eol_hl_off = 1;
5416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
Bram Moolenaar91170f82006-05-05 21:15:17 +00005420 /*
5421 * At end of the text line.
5422 */
5423 if (c == NUL)
5424 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005425#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005426 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5427 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005428 {
5429 /* highlight last char after line */
5430 --col;
5431 --off;
5432 --vcol;
5433 }
5434
Bram Moolenaar1a384422010-07-14 19:53:30 +02005435 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005436 if (wp->w_p_wrap)
5437 v = wp->w_skipcol;
5438 else
5439 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005440
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005441 /* check if line ends before left margin */
5442 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005443 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005444#ifdef FEAT_CONCEAL
5445 /* Get rid of the boguscols now, we want to draw until the right
5446 * edge for 'cursorcolumn'. */
5447 col -= boguscols;
5448 boguscols = 0;
5449#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005450
5451 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005452 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005453
5454 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005455 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5456 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005457 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005458 && lnum != wp->w_cursor.lnum)
5459 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005460# ifdef FEAT_RIGHTLEFT
5461 && !wp->w_p_rl
5462# endif
5463 )
5464 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005465 int rightmost_vcol = 0;
5466 int i;
5467
5468 if (wp->w_p_cuc)
5469 rightmost_vcol = wp->w_virtcol;
5470 if (draw_color_col)
5471 /* determine rightmost colorcolumn to possibly draw */
5472 for (i = 0; color_cols[i] >= 0; ++i)
5473 if (rightmost_vcol < color_cols[i])
5474 rightmost_vcol = color_cols[i];
5475
Bram Moolenaar02631462017-09-22 15:20:32 +02005476 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005477 {
5478 ScreenLines[off] = ' ';
5479#ifdef FEAT_MBYTE
5480 if (enc_utf8)
5481 ScreenLinesUC[off] = 0;
5482#endif
5483 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005484 if (draw_color_col)
5485 draw_color_col = advance_color_col(VCOL_HLC,
5486 &color_cols);
5487
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005488 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005489 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005490 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005491 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005492 else
5493 ScreenAttrs[off++] = 0;
5494
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005495 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005496 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005497
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005498 ++vcol;
5499 }
5500 }
5501#endif
5502
Bram Moolenaar53f81742017-09-22 14:35:51 +02005503 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005504 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 row++;
5506
5507 /*
5508 * Update w_cline_height and w_cline_folded if the cursor line was
5509 * updated (saves a call to plines() later).
5510 */
5511 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5512 {
5513 curwin->w_cline_row = startrow;
5514 curwin->w_cline_height = row - startrow;
5515#ifdef FEAT_FOLDING
5516 curwin->w_cline_folded = FALSE;
5517#endif
5518 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5519 }
5520
5521 break;
5522 }
5523
5524 /* line continues beyond line end */
5525 if (lcs_ext
5526 && !wp->w_p_wrap
5527#ifdef FEAT_DIFF
5528 && filler_todo <= 0
5529#endif
5530 && (
5531#ifdef FEAT_RIGHTLEFT
5532 wp->w_p_rl ? col == 0 :
5533#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005534 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005536 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5538 {
5539 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005540 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541#ifdef FEAT_MBYTE
5542 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005543 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
5545 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005546 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005547 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
5549 else
5550 mb_utf8 = FALSE;
5551#endif
5552 }
5553
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005554#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005555 /* advance to the next 'colorcolumn' */
5556 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005557 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005558
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005559 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005560 * highlight the cursor position itself.
5561 * Also highlight the 'colorcolumn' if it is different than
5562 * 'cursorcolumn' */
5563 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005564 if (draw_state == WL_LINE && !lnum_in_visual_area
5565 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005566 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005567 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005568 && lnum != wp->w_cursor.lnum)
5569 {
5570 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005571 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005572 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005573 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005574 {
5575 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005576 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005577 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005578 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005579#endif
5580
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 /*
5582 * Store character to be displayed.
5583 * Skip characters that are left of the screen for 'nowrap'.
5584 */
5585 vcol_prev = vcol;
5586 if (draw_state < WL_LINE || n_skip <= 0)
5587 {
5588 /*
5589 * Store the character.
5590 */
5591#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5592 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5593 {
5594 /* A double-wide character is: put first halve in left cell. */
5595 --off;
5596 --col;
5597 }
5598#endif
5599 ScreenLines[off] = c;
5600#ifdef FEAT_MBYTE
5601 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005602 {
5603 if ((mb_c & 0xff00) == 0x8e00)
5604 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 else if (enc_utf8)
5608 {
5609 if (mb_utf8)
5610 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005611 int i;
5612
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005614 if ((c & 0xff) == 0)
5615 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005616 for (i = 0; i < Screen_mco; ++i)
5617 {
5618 ScreenLinesC[i][off] = u8cc[i];
5619 if (u8cc[i] == 0)
5620 break;
5621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
5623 else
5624 ScreenLinesUC[off] = 0;
5625 }
5626 if (multi_attr)
5627 {
5628 ScreenAttrs[off] = multi_attr;
5629 multi_attr = 0;
5630 }
5631 else
5632#endif
5633 ScreenAttrs[off] = char_attr;
5634
5635#ifdef FEAT_MBYTE
5636 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5637 {
5638 /* Need to fill two screen columns. */
5639 ++off;
5640 ++col;
5641 if (enc_utf8)
5642 /* UTF-8: Put a 0 in the second screen char. */
5643 ScreenLines[off] = 0;
5644 else
5645 /* DBCS: Put second byte in the second screen char. */
5646 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005647 if (draw_state > WL_NR
5648#ifdef FEAT_DIFF
5649 && filler_todo <= 0
5650#endif
5651 )
5652 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 /* When "tocol" is halfway a character, set it to the end of
5654 * the character, otherwise highlighting won't stop. */
5655 if (tocol == vcol)
5656 ++tocol;
5657#ifdef FEAT_RIGHTLEFT
5658 if (wp->w_p_rl)
5659 {
5660 /* now it's time to backup one cell */
5661 --off;
5662 --col;
5663 }
5664#endif
5665 }
5666#endif
5667#ifdef FEAT_RIGHTLEFT
5668 if (wp->w_p_rl)
5669 {
5670 --off;
5671 --col;
5672 }
5673 else
5674#endif
5675 {
5676 ++off;
5677 ++col;
5678 }
5679 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005680#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005681 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005682 {
5683 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005684 ++vcol_off;
5685 if (n_extra > 0)
5686 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005687 if (wp->w_p_wrap)
5688 {
5689 /*
5690 * Special voodoo required if 'wrap' is on.
5691 *
5692 * Advance the column indicator to force the line
5693 * drawing to wrap early. This will make the line
5694 * take up the same screen space when parts are concealed,
5695 * so that cursor line computations aren't messed up.
5696 *
5697 * To avoid the fictitious advance of 'col' causing
5698 * trailing junk to be written out of the screen line
5699 * we are building, 'boguscols' keeps track of the number
5700 * of bad columns we have advanced.
5701 */
5702 if (n_extra > 0)
5703 {
5704 vcol += n_extra;
5705# ifdef FEAT_RIGHTLEFT
5706 if (wp->w_p_rl)
5707 {
5708 col -= n_extra;
5709 boguscols -= n_extra;
5710 }
5711 else
5712# endif
5713 {
5714 col += n_extra;
5715 boguscols += n_extra;
5716 }
5717 n_extra = 0;
5718 n_attr = 0;
5719 }
5720
5721
5722# ifdef FEAT_MBYTE
5723 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5724 {
5725 /* Need to fill two screen columns. */
5726# ifdef FEAT_RIGHTLEFT
5727 if (wp->w_p_rl)
5728 {
5729 --boguscols;
5730 --col;
5731 }
5732 else
5733# endif
5734 {
5735 ++boguscols;
5736 ++col;
5737 }
5738 }
5739# endif
5740
5741# ifdef FEAT_RIGHTLEFT
5742 if (wp->w_p_rl)
5743 {
5744 --boguscols;
5745 --col;
5746 }
5747 else
5748# endif
5749 {
5750 ++boguscols;
5751 ++col;
5752 }
5753 }
5754 else
5755 {
5756 if (n_extra > 0)
5757 {
5758 vcol += n_extra;
5759 n_extra = 0;
5760 n_attr = 0;
5761 }
5762 }
5763
5764 }
5765#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 else
5767 --n_skip;
5768
Bram Moolenaar64486672010-05-16 15:46:46 +02005769 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5770 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005771 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772#ifdef FEAT_DIFF
5773 && filler_todo <= 0
5774#endif
5775 )
5776 ++vcol;
5777
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005778#ifdef FEAT_SYN_HL
5779 if (vcol_save_attr >= 0)
5780 char_attr = vcol_save_attr;
5781#endif
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 /* restore attributes after "predeces" in 'listchars' */
5784 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5785 char_attr = saved_attr3;
5786
5787 /* restore attributes after last 'listchars' or 'number' char */
5788 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5789 char_attr = saved_attr2;
5790
5791 /*
5792 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005793 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 */
5795 if ((
5796#ifdef FEAT_RIGHTLEFT
5797 wp->w_p_rl ? (col < 0) :
5798#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005799 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 && (*ptr != NUL
5801#ifdef FEAT_DIFF
5802 || filler_todo > 0
5803#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005804 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5806 )
5807 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005808#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005809 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005810 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005811 boguscols = 0;
5812#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005813 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005814 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 ++row;
5817 ++screen_row;
5818
5819 /* When not wrapping and finished diff lines, or when displayed
5820 * '$' and highlighting until last column, break here. */
5821 if ((!wp->w_p_wrap
5822#ifdef FEAT_DIFF
5823 && filler_todo <= 0
5824#endif
5825 ) || lcs_eol_one == -1)
5826 break;
5827
5828 /* When the window is too narrow draw all "@" lines. */
5829 if (draw_state != WL_LINE
5830#ifdef FEAT_DIFF
5831 && filler_todo <= 0
5832#endif
5833 )
5834 {
5835 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 row = endrow;
5838 }
5839
5840 /* When line got too long for screen break here. */
5841 if (row == endrow)
5842 {
5843 ++row;
5844 break;
5845 }
5846
5847 if (screen_cur_row == screen_row - 1
5848#ifdef FEAT_DIFF
5849 && filler_todo <= 0
5850#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005851 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {
5853 /* Remember that the line wraps, used for modeless copy. */
5854 LineWraps[screen_row - 1] = TRUE;
5855
5856 /*
5857 * Special trick to make copy/paste of wrapped lines work with
5858 * xterm/screen: write an extra character beyond the end of
5859 * the line. This will work with all terminal types
5860 * (regardless of the xn,am settings).
5861 * Only do this on a fast tty.
5862 * Only do this if the cursor is on the current line
5863 * (something has been written in it).
5864 * Don't do this for the GUI.
5865 * Don't do this for double-width characters.
5866 * Don't do this for a window not at the right screen border.
5867 */
5868 if (p_tf
5869#ifdef FEAT_GUI
5870 && !gui.in_use
5871#endif
5872#ifdef FEAT_MBYTE
5873 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005874 && ((*mb_off2cells)(LineOffset[screen_row],
5875 LineOffset[screen_row] + screen_Columns)
5876 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005878 + (int)Columns - 2,
5879 LineOffset[screen_row] + screen_Columns)
5880 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881#endif
5882 )
5883 {
5884 /* First make sure we are at the end of the screen line,
5885 * then output the same character again to let the
5886 * terminal know about the wrap. If the terminal doesn't
5887 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005888 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 screen_char(LineOffset[screen_row - 1]
5890 + (unsigned)Columns - 1,
5891 screen_row - 1, (int)(Columns - 1));
5892
5893#ifdef FEAT_MBYTE
5894 /* When there is a multi-byte character, just output a
5895 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005896 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5897 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 out_char(' ');
5899 else
5900#endif
5901 out_char(ScreenLines[LineOffset[screen_row - 1]
5902 + (Columns - 1)]);
5903 /* force a redraw of the first char on the next line */
5904 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5905 screen_start(); /* don't know where cursor is now */
5906 }
5907 }
5908
5909 col = 0;
5910 off = (unsigned)(current_ScreenLine - ScreenLines);
5911#ifdef FEAT_RIGHTLEFT
5912 if (wp->w_p_rl)
5913 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005914 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 off += col;
5916 }
5917#endif
5918
5919 /* reset the drawing state for the start of a wrapped line */
5920 draw_state = WL_START;
5921 saved_n_extra = n_extra;
5922 saved_p_extra = p_extra;
5923 saved_c_extra = c_extra;
5924 saved_char_attr = char_attr;
5925 n_extra = 0;
5926 lcs_prec_todo = lcs_prec;
5927#ifdef FEAT_LINEBREAK
5928# ifdef FEAT_DIFF
5929 if (filler_todo <= 0)
5930# endif
5931 need_showbreak = TRUE;
5932#endif
5933#ifdef FEAT_DIFF
5934 --filler_todo;
5935 /* When the filler lines are actually below the last line of the
5936 * file, don't draw the line itself, break here. */
5937 if (filler_todo == 0 && wp->w_botfill)
5938 break;
5939#endif
5940 }
5941
5942 } /* for every character in the line */
5943
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005944#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005945 /* After an empty line check first word for capital. */
5946 if (*skipwhite(line) == NUL)
5947 {
5948 capcol_lnum = lnum + 1;
5949 cap_col = 0;
5950 }
5951#endif
5952
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005953 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 return row;
5955}
5956
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005957#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005958static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005959
5960/*
5961 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005962 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005963 */
5964 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005965comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005966{
5967 int i;
5968
5969 for (i = 0; i < Screen_mco; ++i)
5970 {
5971 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5972 return TRUE;
5973 if (ScreenLinesC[i][off_from] == 0)
5974 break;
5975 }
5976 return FALSE;
5977}
5978#endif
5979
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980/*
5981 * Check whether the given character needs redrawing:
5982 * - the (first byte of the) character is different
5983 * - the attributes are different
5984 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005985 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 */
5987 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005988char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990 if (cols > 0
5991 && ((ScreenLines[off_from] != ScreenLines[off_to]
5992 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5993
5994#ifdef FEAT_MBYTE
5995 || (enc_dbcs != 0
5996 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5997 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5998 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5999 : (cols > 1 && ScreenLines[off_from + 1]
6000 != ScreenLines[off_to + 1])))
6001 || (enc_utf8
6002 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6003 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006004 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006005 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6006 && ScreenLines[off_from + 1]
6007 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008#endif
6009 ))
6010 return TRUE;
6011 return FALSE;
6012}
6013
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006014#if defined(FEAT_TERMINAL) || defined(PROTO)
6015/*
6016 * Return the index in ScreenLines[] for the current screen line.
6017 */
6018 int
6019screen_get_current_line_off()
6020{
6021 return (int)(current_ScreenLine - ScreenLines);
6022}
6023#endif
6024
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025/*
6026 * Move one "cooked" screen line to the screen, but only the characters that
6027 * have actually changed. Handle insert/delete character.
6028 * "coloff" gives the first column on the screen for this line.
6029 * "endcol" gives the columns where valid characters are.
6030 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6031 * needs to be cleared, negative otherwise.
6032 * "rlflag" is TRUE in a rightleft window:
6033 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6034 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6035 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006036 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006037screen_line(
6038 int row,
6039 int coloff,
6040 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006041 int clear_width,
6042 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 unsigned off_from;
6045 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006046#ifdef FEAT_MBYTE
6047 unsigned max_off_from;
6048 unsigned max_off_to;
6049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 int force = FALSE; /* force update rest of the line */
6053 int redraw_this /* bool: does character need redraw? */
6054#ifdef FEAT_GUI
6055 = TRUE /* For GUI when while-loop empty */
6056#endif
6057 ;
6058 int redraw_next; /* redraw_this for next character */
6059#ifdef FEAT_MBYTE
6060 int clear_next = FALSE;
6061 int char_cells; /* 1: normal char */
6062 /* 2: occupies two display cells */
6063# define CHAR_CELLS char_cells
6064#else
6065# define CHAR_CELLS 1
6066#endif
6067
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006068 /* Check for illegal row and col, just in case. */
6069 if (row >= Rows)
6070 row = Rows - 1;
6071 if (endcol > Columns)
6072 endcol = Columns;
6073
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074# ifdef FEAT_CLIPBOARD
6075 clip_may_clear_selection(row, row);
6076# endif
6077
6078 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6079 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006080#ifdef FEAT_MBYTE
6081 max_off_from = off_from + screen_Columns;
6082 max_off_to = LineOffset[row] + screen_Columns;
6083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084
6085#ifdef FEAT_RIGHTLEFT
6086 if (rlflag)
6087 {
6088 /* Clear rest first, because it's left of the text. */
6089 if (clear_width > 0)
6090 {
6091 while (col <= endcol && ScreenLines[off_to] == ' '
6092 && ScreenAttrs[off_to] == 0
6093# ifdef FEAT_MBYTE
6094 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6095# endif
6096 )
6097 {
6098 ++off_to;
6099 ++col;
6100 }
6101 if (col <= endcol)
6102 screen_fill(row, row + 1, col + coloff,
6103 endcol + coloff + 1, ' ', ' ', 0);
6104 }
6105 col = endcol + 1;
6106 off_to = LineOffset[row] + col + coloff;
6107 off_from += col;
6108 endcol = (clear_width > 0 ? clear_width : -clear_width);
6109 }
6110#endif /* FEAT_RIGHTLEFT */
6111
6112 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6113
6114 while (col < endcol)
6115 {
6116#ifdef FEAT_MBYTE
6117 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006118 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 else
6120 char_cells = 1;
6121#endif
6122
6123 redraw_this = redraw_next;
6124 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6125 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6126
6127#ifdef FEAT_GUI
6128 /* If the next character was bold, then redraw the current character to
6129 * remove any pixels that might have spilt over into us. This only
6130 * happens in the GUI.
6131 */
6132 if (redraw_next && gui.in_use)
6133 {
6134 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006135 if (hl > HL_ALL)
6136 hl = syn_attr2attr(hl);
6137 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 redraw_this = TRUE;
6139 }
6140#endif
6141
6142 if (redraw_this)
6143 {
6144 /*
6145 * Special handling when 'xs' termcap flag set (hpterm):
6146 * Attributes for characters are stored at the position where the
6147 * cursor is when writing the highlighting code. The
6148 * start-highlighting code must be written with the cursor on the
6149 * first highlighted character. The stop-highlighting code must
6150 * be written with the cursor just after the last highlighted
6151 * character.
6152 * Overwriting a character doesn't remove it's highlighting. Need
6153 * to clear the rest of the line, and force redrawing it
6154 * completely.
6155 */
6156 if ( p_wiv
6157 && !force
6158#ifdef FEAT_GUI
6159 && !gui.in_use
6160#endif
6161 && ScreenAttrs[off_to] != 0
6162 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6163 {
6164 /*
6165 * Need to remove highlighting attributes here.
6166 */
6167 windgoto(row, col + coloff);
6168 out_str(T_CE); /* clear rest of this screen line */
6169 screen_start(); /* don't know where cursor is now */
6170 force = TRUE; /* force redraw of rest of the line */
6171 redraw_next = TRUE; /* or else next char would miss out */
6172
6173 /*
6174 * If the previous character was highlighted, need to stop
6175 * highlighting at this character.
6176 */
6177 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6178 {
6179 screen_attr = ScreenAttrs[off_to - 1];
6180 term_windgoto(row, col + coloff);
6181 screen_stop_highlight();
6182 }
6183 else
6184 screen_attr = 0; /* highlighting has stopped */
6185 }
6186#ifdef FEAT_MBYTE
6187 if (enc_dbcs != 0)
6188 {
6189 /* Check if overwriting a double-byte with a single-byte or
6190 * the other way around requires another character to be
6191 * redrawn. For UTF-8 this isn't needed, because comparing
6192 * ScreenLinesUC[] is sufficient. */
6193 if (char_cells == 1
6194 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006195 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
6197 /* Writing a single-cell character over a double-cell
6198 * character: need to redraw the next cell. */
6199 ScreenLines[off_to + 1] = 0;
6200 redraw_next = TRUE;
6201 }
6202 else if (char_cells == 2
6203 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006204 && (*mb_off2cells)(off_to, max_off_to) == 1
6205 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 {
6207 /* Writing the second half of a double-cell character over
6208 * a double-cell character: need to redraw the second
6209 * cell. */
6210 ScreenLines[off_to + 2] = 0;
6211 redraw_next = TRUE;
6212 }
6213
6214 if (enc_dbcs == DBCS_JPNU)
6215 ScreenLines2[off_to] = ScreenLines2[off_from];
6216 }
6217 /* When writing a single-width character over a double-width
6218 * character and at the end of the redrawn text, need to clear out
6219 * the right halve of the old character.
6220 * Also required when writing the right halve of a double-width
6221 * char over the left halve of an existing one. */
6222 if (has_mbyte && col + char_cells == endcol
6223 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006224 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006226 && (*mb_off2cells)(off_to, max_off_to) == 1
6227 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 clear_next = TRUE;
6229#endif
6230
6231 ScreenLines[off_to] = ScreenLines[off_from];
6232#ifdef FEAT_MBYTE
6233 if (enc_utf8)
6234 {
6235 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6236 if (ScreenLinesUC[off_from] != 0)
6237 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006238 int i;
6239
6240 for (i = 0; i < Screen_mco; ++i)
6241 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 }
6243 }
6244 if (char_cells == 2)
6245 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6246#endif
6247
6248#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006249 /* The bold trick makes a single column of pixels appear in the
6250 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 * character should be redrawn too. This happens for our own GUI
6252 * and for some xterms. */
6253 if (
6254# ifdef FEAT_GUI
6255 gui.in_use
6256# endif
6257# if defined(FEAT_GUI) && defined(UNIX)
6258 ||
6259# endif
6260# ifdef UNIX
6261 term_is_xterm
6262# endif
6263 )
6264 {
6265 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006266 if (hl > HL_ALL)
6267 hl = syn_attr2attr(hl);
6268 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 redraw_next = TRUE;
6270 }
6271#endif
6272 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6273#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006274 /* For simplicity set the attributes of second half of a
6275 * double-wide character equal to the first half. */
6276 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006278
6279 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 else
6282#endif
6283 screen_char(off_to, row, col + coloff);
6284 }
6285 else if ( p_wiv
6286#ifdef FEAT_GUI
6287 && !gui.in_use
6288#endif
6289 && col + coloff > 0)
6290 {
6291 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6292 {
6293 /*
6294 * Don't output stop-highlight when moving the cursor, it will
6295 * stop the highlighting when it should continue.
6296 */
6297 screen_attr = 0;
6298 }
6299 else if (screen_attr != 0)
6300 screen_stop_highlight();
6301 }
6302
6303 off_to += CHAR_CELLS;
6304 off_from += CHAR_CELLS;
6305 col += CHAR_CELLS;
6306 }
6307
6308#ifdef FEAT_MBYTE
6309 if (clear_next)
6310 {
6311 /* Clear the second half of a double-wide character of which the left
6312 * half was overwritten with a single-wide character. */
6313 ScreenLines[off_to] = ' ';
6314 if (enc_utf8)
6315 ScreenLinesUC[off_to] = 0;
6316 screen_char(off_to, row, col + coloff);
6317 }
6318#endif
6319
6320 if (clear_width > 0
6321#ifdef FEAT_RIGHTLEFT
6322 && !rlflag
6323#endif
6324 )
6325 {
6326#ifdef FEAT_GUI
6327 int startCol = col;
6328#endif
6329
6330 /* blank out the rest of the line */
6331 while (col < clear_width && ScreenLines[off_to] == ' '
6332 && ScreenAttrs[off_to] == 0
6333#ifdef FEAT_MBYTE
6334 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6335#endif
6336 )
6337 {
6338 ++off_to;
6339 ++col;
6340 }
6341 if (col < clear_width)
6342 {
6343#ifdef FEAT_GUI
6344 /*
6345 * In the GUI, clearing the rest of the line may leave pixels
6346 * behind if the first character cleared was bold. Some bold
6347 * fonts spill over the left. In this case we redraw the previous
6348 * character too. If we didn't skip any blanks above, then we
6349 * only redraw if the character wasn't already redrawn anyway.
6350 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006351 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 {
6353 hl = ScreenAttrs[off_to];
6354 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006355 {
6356 int prev_cells = 1;
6357# ifdef FEAT_MBYTE
6358 if (enc_utf8)
6359 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6360 * that its width is 2. */
6361 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6362 else if (enc_dbcs != 0)
6363 {
6364 /* find previous character by counting from first
6365 * column and get its width. */
6366 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006367 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006368
6369 while (off < off_to)
6370 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006371 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006372 off += prev_cells;
6373 }
6374 }
6375
6376 if (enc_dbcs != 0 && prev_cells > 1)
6377 screen_char_2(off_to - prev_cells, row,
6378 col + coloff - prev_cells);
6379 else
6380# endif
6381 screen_char(off_to - prev_cells, row,
6382 col + coloff - prev_cells);
6383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
6385#endif
6386 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6387 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 off_to += clear_width - col;
6389 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 }
6391 }
6392
6393 if (clear_width > 0)
6394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 /* For a window that's left of another, draw the separator char. */
6396 if (col + coloff < Columns)
6397 {
6398 int c;
6399
6400 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006401 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006402#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006403 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6404 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 || ScreenAttrs[off_to] != hl)
6407 {
6408 ScreenLines[off_to] = c;
6409 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006410#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 if (enc_utf8)
6412 {
6413 if (c >= 0x80)
6414 {
6415 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006416 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 }
6418 else
6419 ScreenLinesUC[off_to] = 0;
6420 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 screen_char(off_to, row, col + coloff);
6423 }
6424 }
6425 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 LineWraps[row] = FALSE;
6427 }
6428}
6429
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006430#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006432 * Mirror text "str" for right-left displaying.
6433 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006435 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006436rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437{
6438 char_u *p1, *p2;
6439 int t;
6440
6441 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6442 {
6443 t = *p1;
6444 *p1 = *p2;
6445 *p2 = t;
6446 }
6447}
6448#endif
6449
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450/*
6451 * mark all status lines for redraw; used after first :cd
6452 */
6453 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006454status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
6456 win_T *wp;
6457
Bram Moolenaar29323592016-07-24 22:04:11 +02006458 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 if (wp->w_status_height)
6460 {
6461 wp->w_redr_status = TRUE;
6462 redraw_later(VALID);
6463 }
6464}
6465
6466/*
6467 * mark all status lines of the current buffer for redraw
6468 */
6469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006470status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 win_T *wp;
6473
Bram Moolenaar29323592016-07-24 22:04:11 +02006474 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6476 {
6477 wp->w_redr_status = TRUE;
6478 redraw_later(VALID);
6479 }
6480}
6481
6482/*
6483 * Redraw all status lines that need to be redrawn.
6484 */
6485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006486redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 win_T *wp;
6489
Bram Moolenaar29323592016-07-24 22:04:11 +02006490 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 if (wp->w_redr_status)
6492 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006493 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006494 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496
Bram Moolenaar4033c552017-09-16 20:54:51 +02006497#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498/*
6499 * Redraw all status lines at the bottom of frame "frp".
6500 */
6501 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006502win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
6504 if (frp->fr_layout == FR_LEAF)
6505 frp->fr_win->w_redr_status = TRUE;
6506 else if (frp->fr_layout == FR_ROW)
6507 {
6508 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6509 win_redraw_last_status(frp);
6510 }
6511 else /* frp->fr_layout == FR_COL */
6512 {
6513 frp = frp->fr_child;
6514 while (frp->fr_next != NULL)
6515 frp = frp->fr_next;
6516 win_redraw_last_status(frp);
6517 }
6518}
6519#endif
6520
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521/*
6522 * Draw the verticap separator right of window "wp" starting with line "row".
6523 */
6524 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006525draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526{
6527 int hl;
6528 int c;
6529
6530 if (wp->w_vsep_width)
6531 {
6532 /* draw the vertical separator right of this window */
6533 c = fillchar_vsep(&hl);
6534 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6535 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6536 c, ' ', hl);
6537 }
6538}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
6540#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006541static int status_match_len(expand_T *xp, char_u *s);
6542static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543
6544/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006545 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 */
6547 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006548status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
6550 int len = 0;
6551
6552#ifdef FEAT_MENU
6553 int emenu = (xp->xp_context == EXPAND_MENUS
6554 || xp->xp_context == EXPAND_MENUNAMES);
6555
6556 /* Check for menu separators - replace with '|'. */
6557 if (emenu && menu_is_separator(s))
6558 return 1;
6559#endif
6560
6561 while (*s != NUL)
6562 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006563 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006564 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006565 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 }
6567
6568 return len;
6569}
6570
6571/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006572 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006573 * These are backslashes used for escaping. Do show backslashes in help tags.
6574 */
6575 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006576skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006577{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006578 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006579#ifdef FEAT_MENU
6580 || ((xp->xp_context == EXPAND_MENUS
6581 || xp->xp_context == EXPAND_MENUNAMES)
6582 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6583#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006584 )
6585 {
6586#ifndef BACKSLASH_IN_FILENAME
6587 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6588 return 2;
6589#endif
6590 return 1;
6591 }
6592 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006593}
6594
6595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 * Show wildchar matches in the status line.
6597 * Show at least the "match" item.
6598 * We start at item 'first_match' in the list and show all matches that fit.
6599 *
6600 * If inversion is possible we use it. Else '=' characters are used.
6601 */
6602 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006603win_redr_status_matches(
6604 expand_T *xp,
6605 int num_matches,
6606 char_u **matches, /* list of matches */
6607 int match,
6608 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609{
6610#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6611 int row;
6612 char_u *buf;
6613 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006614 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 int fillchar;
6616 int attr;
6617 int i;
6618 int highlight = TRUE;
6619 char_u *selstart = NULL;
6620 int selstart_col = 0;
6621 char_u *selend = NULL;
6622 static int first_match = 0;
6623 int add_left = FALSE;
6624 char_u *s;
6625#ifdef FEAT_MENU
6626 int emenu;
6627#endif
6628#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6629 int l;
6630#endif
6631
6632 if (matches == NULL) /* interrupted completion? */
6633 return;
6634
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006635#ifdef FEAT_MBYTE
6636 if (has_mbyte)
6637 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6638 else
6639#endif
6640 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 if (buf == NULL)
6642 return;
6643
6644 if (match == -1) /* don't show match but original text */
6645 {
6646 match = 0;
6647 highlight = FALSE;
6648 }
6649 /* count 1 for the ending ">" */
6650 clen = status_match_len(xp, L_MATCH(match)) + 3;
6651 if (match == 0)
6652 first_match = 0;
6653 else if (match < first_match)
6654 {
6655 /* jumping left, as far as we can go */
6656 first_match = match;
6657 add_left = TRUE;
6658 }
6659 else
6660 {
6661 /* check if match fits on the screen */
6662 for (i = first_match; i < match; ++i)
6663 clen += status_match_len(xp, L_MATCH(i)) + 2;
6664 if (first_match > 0)
6665 clen += 2;
6666 /* jumping right, put match at the left */
6667 if ((long)clen > Columns)
6668 {
6669 first_match = match;
6670 /* if showing the last match, we can add some on the left */
6671 clen = 2;
6672 for (i = match; i < num_matches; ++i)
6673 {
6674 clen += status_match_len(xp, L_MATCH(i)) + 2;
6675 if ((long)clen >= Columns)
6676 break;
6677 }
6678 if (i == num_matches)
6679 add_left = TRUE;
6680 }
6681 }
6682 if (add_left)
6683 while (first_match > 0)
6684 {
6685 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6686 if ((long)clen >= Columns)
6687 break;
6688 --first_match;
6689 }
6690
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006691 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
6693 if (first_match == 0)
6694 {
6695 *buf = NUL;
6696 len = 0;
6697 }
6698 else
6699 {
6700 STRCPY(buf, "< ");
6701 len = 2;
6702 }
6703 clen = len;
6704
6705 i = first_match;
6706 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6707 {
6708 if (i == match)
6709 {
6710 selstart = buf + len;
6711 selstart_col = clen;
6712 }
6713
6714 s = L_MATCH(i);
6715 /* Check for menu separators - replace with '|' */
6716#ifdef FEAT_MENU
6717 emenu = (xp->xp_context == EXPAND_MENUS
6718 || xp->xp_context == EXPAND_MENUNAMES);
6719 if (emenu && menu_is_separator(s))
6720 {
6721 STRCPY(buf + len, transchar('|'));
6722 l = (int)STRLEN(buf + len);
6723 len += l;
6724 clen += l;
6725 }
6726 else
6727#endif
6728 for ( ; *s != NUL; ++s)
6729 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006730 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 clen += ptr2cells(s);
6732#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006733 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {
6735 STRNCPY(buf + len, s, l);
6736 s += l - 1;
6737 len += l;
6738 }
6739 else
6740#endif
6741 {
6742 STRCPY(buf + len, transchar_byte(*s));
6743 len += (int)STRLEN(buf + len);
6744 }
6745 }
6746 if (i == match)
6747 selend = buf + len;
6748
6749 *(buf + len++) = ' ';
6750 *(buf + len++) = ' ';
6751 clen += 2;
6752 if (++i == num_matches)
6753 break;
6754 }
6755
6756 if (i != num_matches)
6757 {
6758 *(buf + len++) = '>';
6759 ++clen;
6760 }
6761
6762 buf[len] = NUL;
6763
6764 row = cmdline_row - 1;
6765 if (row >= 0)
6766 {
6767 if (wild_menu_showing == 0)
6768 {
6769 if (msg_scrolled > 0)
6770 {
6771 /* Put the wildmenu just above the command line. If there is
6772 * no room, scroll the screen one line up. */
6773 if (cmdline_row == Rows - 1)
6774 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006775 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 ++msg_scrolled;
6777 }
6778 else
6779 {
6780 ++cmdline_row;
6781 ++row;
6782 }
6783 wild_menu_showing = WM_SCROLLED;
6784 }
6785 else
6786 {
6787 /* Create status line if needed by setting 'laststatus' to 2.
6788 * Set 'winminheight' to zero to avoid that the window is
6789 * resized. */
6790 if (lastwin->w_status_height == 0)
6791 {
6792 save_p_ls = p_ls;
6793 save_p_wmh = p_wmh;
6794 p_ls = 2;
6795 p_wmh = 0;
6796 last_status(FALSE);
6797 }
6798 wild_menu_showing = WM_SHOWN;
6799 }
6800 }
6801
6802 screen_puts(buf, row, 0, attr);
6803 if (selstart != NULL && highlight)
6804 {
6805 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006806 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808
6809 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6810 }
6811
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 vim_free(buf);
6814}
6815#endif
6816
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817/*
6818 * Redraw the status line of window wp.
6819 *
6820 * If inversion is possible we use it. Else '=' characters are used.
6821 */
6822 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006823win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824{
6825 int row;
6826 char_u *p;
6827 int len;
6828 int fillchar;
6829 int attr;
6830 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006831 static int busy = FALSE;
6832
6833 /* It's possible to get here recursively when 'statusline' (indirectly)
6834 * invokes ":redrawstatus". Simply ignore the call then. */
6835 if (busy)
6836 return;
6837 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
6839 wp->w_redr_status = FALSE;
6840 if (wp->w_status_height == 0)
6841 {
6842 /* no status line, can only be last window */
6843 redraw_cmdline = TRUE;
6844 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006845 else if (!redrawing()
6846#ifdef FEAT_INS_EXPAND
6847 /* don't update status line when popup menu is visible and may be
6848 * drawn over it */
6849 || pum_visible()
6850#endif
6851 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 {
6853 /* Don't redraw right now, do it later. */
6854 wp->w_redr_status = TRUE;
6855 }
6856#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006857 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 {
6859 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006860 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 }
6862#endif
6863 else
6864 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006865 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006867 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 p = NameBuff;
6869 len = (int)STRLEN(p);
6870
Bram Moolenaard85f2712017-07-28 21:51:57 +02006871 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872#ifdef FEAT_QUICKFIX
6873 || wp->w_p_pvw
6874#endif
6875 || bufIsChanged(wp->w_buffer)
6876 || wp->w_buffer->b_p_ro)
6877 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006878 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006880 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 len += (int)STRLEN(p + len);
6882 }
6883#ifdef FEAT_QUICKFIX
6884 if (wp->w_p_pvw)
6885 {
6886 STRCPY(p + len, _("[Preview]"));
6887 len += (int)STRLEN(p + len);
6888 }
6889#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006890 if (bufIsChanged(wp->w_buffer)
6891#ifdef FEAT_TERMINAL
6892 && !bt_terminal(wp->w_buffer)
6893#endif
6894 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {
6896 STRCPY(p + len, "[+]");
6897 len += 3;
6898 }
6899 if (wp->w_buffer->b_p_ro)
6900 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006901 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006902 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
6904
Bram Moolenaar02631462017-09-22 15:20:32 +02006905 this_ru_col = ru_col - (Columns - wp->w_width);
6906 if (this_ru_col < (wp->w_width + 1) / 2)
6907 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 if (this_ru_col <= 1)
6909 {
6910 p = (char_u *)"<"; /* No room for file name! */
6911 len = 1;
6912 }
6913 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914#ifdef FEAT_MBYTE
6915 if (has_mbyte)
6916 {
6917 int clen = 0, i;
6918
6919 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006920 clen = mb_string2cells(p, -1);
6921
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 /* Find first character that will fit.
6923 * Going from start to end is much faster for DBCS. */
6924 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006925 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 clen -= (*mb_ptr2cells)(p + i);
6927 len = clen;
6928 if (i > 0)
6929 {
6930 p = p + i - 1;
6931 *p = '<';
6932 ++len;
6933 }
6934
6935 }
6936 else
6937#endif
6938 if (len > this_ru_col - 1)
6939 {
6940 p += len - (this_ru_col - 1);
6941 *p = '<';
6942 len = this_ru_col - 1;
6943 }
6944
6945 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006946 screen_puts(p, row, wp->w_wincol, attr);
6947 screen_fill(row, row + 1, len + wp->w_wincol,
6948 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006950 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6952 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006953 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955#ifdef FEAT_CMDL_INFO
6956 win_redr_ruler(wp, TRUE);
6957#endif
6958 }
6959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 /*
6961 * May need to draw the character below the vertical separator.
6962 */
6963 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6964 {
6965 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006966 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 else
6968 fillchar = fillchar_vsep(&attr);
6969 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6970 attr);
6971 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006972 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973}
6974
Bram Moolenaar238a5642006-02-21 22:12:05 +00006975#ifdef FEAT_STL_OPT
6976/*
6977 * Redraw the status line according to 'statusline' and take care of any
6978 * errors encountered.
6979 */
6980 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006981redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006982{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006983 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006984 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006985
6986 /* When called recursively return. This can happen when the statusline
6987 * contains an expression that triggers a redraw. */
6988 if (entered)
6989 return;
6990 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006991
Bram Moolenaara742e082016-04-05 21:10:38 +02006992 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006993 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006994 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006995 {
6996 /* When there is an error disable the statusline, otherwise the
6997 * display is messed up with errors and a redraw triggers the problem
6998 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006999 set_string_option_direct((char_u *)"statusline", -1,
7000 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007001 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007002 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007003 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007004 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007005}
7006#endif
7007
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008/*
7009 * Return TRUE if the status line of window "wp" is connected to the status
7010 * line of the window right of it. If not, then it's a vertical separator.
7011 * Only call if (wp->w_vsep_width != 0).
7012 */
7013 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007014stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015{
7016 frame_T *fr;
7017
7018 fr = wp->w_frame;
7019 while (fr->fr_parent != NULL)
7020 {
7021 if (fr->fr_parent->fr_layout == FR_COL)
7022 {
7023 if (fr->fr_next != NULL)
7024 break;
7025 }
7026 else
7027 {
7028 if (fr->fr_next != NULL)
7029 return TRUE;
7030 }
7031 fr = fr->fr_parent;
7032 }
7033 return FALSE;
7034}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037/*
7038 * Get the value to show for the language mappings, active 'keymap'.
7039 */
7040 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007041get_keymap_str(
7042 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007043 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007044 char_u *buf, /* buffer for the result */
7045 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
7047 char_u *p;
7048
7049 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7050 return FALSE;
7051
7052 {
7053#ifdef FEAT_EVAL
7054 buf_T *old_curbuf = curbuf;
7055 win_T *old_curwin = curwin;
7056 char_u *s;
7057
7058 curbuf = wp->w_buffer;
7059 curwin = wp;
7060 STRCPY(buf, "b:keymap_name"); /* must be writable */
7061 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007062 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 --emsg_skip;
7064 curbuf = old_curbuf;
7065 curwin = old_curwin;
7066 if (p == NULL || *p == NUL)
7067#endif
7068 {
7069#ifdef FEAT_KEYMAP
7070 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7071 p = wp->w_buffer->b_p_keymap;
7072 else
7073#endif
7074 p = (char_u *)"lang";
7075 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007076 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 buf[0] = NUL;
7078#ifdef FEAT_EVAL
7079 vim_free(s);
7080#endif
7081 }
7082 return buf[0] != NUL;
7083}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084
7085#if defined(FEAT_STL_OPT) || defined(PROTO)
7086/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007087 * Redraw the status line or ruler of window "wp".
7088 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 */
7090 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007091win_redr_custom(
7092 win_T *wp,
7093 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007095 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 int attr;
7097 int curattr;
7098 int row;
7099 int col = 0;
7100 int maxwidth;
7101 int width;
7102 int n;
7103 int len;
7104 int fillchar;
7105 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007106 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007108 struct stl_hlrec hltab[STL_MAX_ITEM];
7109 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007110 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007111 win_T *ewp;
7112 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113
Bram Moolenaar1d633412013-12-11 15:52:01 +01007114 /* There is a tiny chance that this gets called recursively: When
7115 * redrawing a status line triggers redrawing the ruler or tabline.
7116 * Avoid trouble by not allowing recursion. */
7117 if (entered)
7118 return;
7119 entered = TRUE;
7120
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007122 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007124 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007125 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007126 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007127 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007128 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007129 maxwidth = Columns;
7130# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007131 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007132# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007134 else
7135 {
7136 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007137 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007138 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007139
7140 if (draw_ruler)
7141 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007142 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007143 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007144 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007145 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007146 if (*++stl == '-')
7147 stl++;
7148 if (atoi((char *)stl))
7149 while (VIM_ISDIGIT(*stl))
7150 stl++;
7151 if (*stl++ != '(')
7152 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007153 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007154 col = ru_col - (Columns - wp->w_width);
7155 if (col < (wp->w_width + 1) / 2)
7156 col = (wp->w_width + 1) / 2;
7157 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007158 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007159 {
7160 row = Rows - 1;
7161 --maxwidth; /* writing in last column may cause scrolling */
7162 fillchar = ' ';
7163 attr = 0;
7164 }
7165
7166# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007167 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007168# endif
7169 }
7170 else
7171 {
7172 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007173 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007174 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007175 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007176# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007177 use_sandbox = was_set_insecurely((char_u *)"statusline",
7178 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007179# endif
7180 }
7181
Bram Moolenaar53f81742017-09-22 14:35:51 +02007182 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007183 }
7184
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007186 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
Bram Moolenaar61452852011-02-01 18:01:11 +01007188 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7189 * the cursor away and back. */
7190 ewp = wp == NULL ? curwin : wp;
7191 p_crb_save = ewp->w_p_crb;
7192 ewp->w_p_crb = FALSE;
7193
Bram Moolenaar362f3562009-11-03 16:20:34 +00007194 /* Make a copy, because the statusline may include a function call that
7195 * might change the option value and free the memory. */
7196 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007197 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007198 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007199 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007200 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007201 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007203 /* Make all characters printable. */
7204 p = transstr(buf);
7205 if (p != NULL)
7206 {
7207 vim_strncpy(buf, p, sizeof(buf) - 1);
7208 vim_free(p);
7209 }
7210
7211 /* fill up with "fillchar" */
7212 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007213 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {
7215#ifdef FEAT_MBYTE
7216 len += (*mb_char2bytes)(fillchar, buf + len);
7217#else
7218 buf[len++] = fillchar;
7219#endif
7220 ++width;
7221 }
7222 buf[len] = NUL;
7223
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007224 /*
7225 * Draw each snippet with the specified highlighting.
7226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 curattr = attr;
7228 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007229 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007231 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 screen_puts_len(p, len, row, col, curattr);
7233 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007234 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007236 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007238 else if (hltab[n].userhl < 0)
7239 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007240#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007241 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7242 && wp->w_status_height != 0)
7243 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007244 else if (wp != NULL && bt_terminal(wp->w_buffer)
7245 && wp->w_status_height != 0)
7246 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007247#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007248 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007249 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007251 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 }
7253 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007254
7255 if (wp == NULL)
7256 {
7257 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7258 col = 0;
7259 len = 0;
7260 p = buf;
7261 fillchar = 0;
7262 for (n = 0; tabtab[n].start != NULL; n++)
7263 {
7264 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7265 while (col < len)
7266 TabPageIdxs[col++] = fillchar;
7267 p = tabtab[n].start;
7268 fillchar = tabtab[n].userhl;
7269 }
7270 while (col < Columns)
7271 TabPageIdxs[col++] = fillchar;
7272 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007273
7274theend:
7275 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276}
7277
7278#endif /* FEAT_STL_OPT */
7279
7280/*
7281 * Output a single character directly to the screen and update ScreenLines.
7282 */
7283 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007284screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 char_u buf[MB_MAXBYTES + 1];
7287
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007288#ifdef FEAT_MBYTE
7289 if (has_mbyte)
7290 buf[(*mb_char2bytes)(c, buf)] = NUL;
7291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007293 {
7294 buf[0] = c;
7295 buf[1] = NUL;
7296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 screen_puts(buf, row, col, attr);
7298}
7299
7300/*
7301 * Get a single character directly from ScreenLines into "bytes[]".
7302 * Also return its attribute in *attrp;
7303 */
7304 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007305screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306{
7307 unsigned off;
7308
7309 /* safety check */
7310 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7311 {
7312 off = LineOffset[row] + col;
7313 *attrp = ScreenAttrs[off];
7314 bytes[0] = ScreenLines[off];
7315 bytes[1] = NUL;
7316
7317#ifdef FEAT_MBYTE
7318 if (enc_utf8 && ScreenLinesUC[off] != 0)
7319 bytes[utfc_char2bytes(off, bytes)] = NUL;
7320 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7321 {
7322 bytes[0] = ScreenLines[off];
7323 bytes[1] = ScreenLines2[off];
7324 bytes[2] = NUL;
7325 }
7326 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7327 {
7328 bytes[1] = ScreenLines[off + 1];
7329 bytes[2] = NUL;
7330 }
7331#endif
7332 }
7333}
7334
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007335#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007336static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007337
7338/*
7339 * Return TRUE if composing characters for screen posn "off" differs from
7340 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007341 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007342 */
7343 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007344screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007345{
7346 int i;
7347
7348 for (i = 0; i < Screen_mco; ++i)
7349 {
7350 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7351 return TRUE;
7352 if (u8cc[i] == 0)
7353 break;
7354 }
7355 return FALSE;
7356}
7357#endif
7358
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359/*
7360 * Put string '*text' on the screen at position 'row' and 'col', with
7361 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7362 * Note: only outputs within one row, message is truncated at screen boundary!
7363 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7364 */
7365 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007366screen_puts(
7367 char_u *text,
7368 int row,
7369 int col,
7370 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371{
7372 screen_puts_len(text, -1, row, col, attr);
7373}
7374
7375/*
7376 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7377 * a NUL.
7378 */
7379 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007380screen_puts_len(
7381 char_u *text,
7382 int textlen,
7383 int row,
7384 int col,
7385 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386{
7387 unsigned off;
7388 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007389 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 int c;
7391#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007392 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 int mbyte_blen = 1;
7394 int mbyte_cells = 1;
7395 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007396 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 int clear_next_cell = FALSE;
7398# ifdef FEAT_ARABIC
7399 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007400 int pc, nc, nc1;
7401 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402# endif
7403#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007404#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7405 int force_redraw_this;
7406 int force_redraw_next = FALSE;
7407#endif
7408 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409
7410 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7411 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007412 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
Bram Moolenaarc236c162008-07-13 17:41:49 +00007414#ifdef FEAT_MBYTE
7415 /* When drawing over the right halve of a double-wide char clear out the
7416 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007417 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007418# ifdef FEAT_GUI
7419 && !gui.in_use
7420# endif
7421 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007422 {
7423 ScreenLines[off - 1] = ' ';
7424 ScreenAttrs[off - 1] = 0;
7425 if (enc_utf8)
7426 {
7427 ScreenLinesUC[off - 1] = 0;
7428 ScreenLinesC[0][off - 1] = 0;
7429 }
7430 /* redraw the previous cell, make it empty */
7431 screen_char(off - 1, row, col - 1);
7432 /* force the cell at "col" to be redrawn */
7433 force_redraw_next = TRUE;
7434 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007435#endif
7436
Bram Moolenaar367329b2007-08-30 11:53:22 +00007437#ifdef FEAT_MBYTE
7438 max_off = LineOffset[row] + screen_Columns;
7439#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007440 while (col < screen_Columns
7441 && (len < 0 || (int)(ptr - text) < len)
7442 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 {
7444 c = *ptr;
7445#ifdef FEAT_MBYTE
7446 /* check if this is the first byte of a multibyte */
7447 if (has_mbyte)
7448 {
7449 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007450 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007452 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7454 mbyte_cells = 1;
7455 else if (enc_dbcs != 0)
7456 mbyte_cells = mbyte_blen;
7457 else /* enc_utf8 */
7458 {
7459 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007460 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 (int)((text + len) - ptr));
7462 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007463 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007465# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 /* Non-BMP character: display as ? or fullwidth ?. */
7467 if (u8c >= 0x10000)
7468 {
7469 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7470 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007471 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007473# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474# ifdef FEAT_ARABIC
7475 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7476 {
7477 /* Do Arabic shaping. */
7478 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7479 {
7480 /* Past end of string to be displayed. */
7481 nc = NUL;
7482 nc1 = NUL;
7483 }
7484 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007485 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007486 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7487 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007488 nc1 = pcc[0];
7489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 pc = prev_c;
7491 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007492 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
7494 else
7495 prev_c = u8c;
7496# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007497 if (col + mbyte_cells > screen_Columns)
7498 {
7499 /* Only 1 cell left, but character requires 2 cells:
7500 * display a '>' in the last column to avoid wrapping. */
7501 c = '>';
7502 mbyte_cells = 1;
7503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 }
7505 }
7506#endif
7507
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007508#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7509 force_redraw_this = force_redraw_next;
7510 force_redraw_next = FALSE;
7511#endif
7512
7513 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514#ifdef FEAT_MBYTE
7515 || (mbyte_cells == 2
7516 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7517 || (enc_dbcs == DBCS_JPNU
7518 && c == 0x8e
7519 && ScreenLines2[off] != ptr[1])
7520 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007521 && (ScreenLinesUC[off] !=
7522 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7523 || (ScreenLinesUC[off] != 0
7524 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525#endif
7526 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007527 || exmode_active;
7528
7529 if (need_redraw
7530#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7531 || force_redraw_this
7532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 )
7534 {
7535#if defined(FEAT_GUI) || defined(UNIX)
7536 /* The bold trick makes a single row of pixels appear in the next
7537 * character. When a bold character is removed, the next
7538 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007539 * and for some xterms. */
7540 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541# ifdef FEAT_GUI
7542 gui.in_use
7543# endif
7544# if defined(FEAT_GUI) && defined(UNIX)
7545 ||
7546# endif
7547# ifdef UNIX
7548 term_is_xterm
7549# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007550 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007552 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007554 if (n > HL_ALL)
7555 n = syn_attr2attr(n);
7556 if (n & HL_BOLD)
7557 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 }
7559#endif
7560#ifdef FEAT_MBYTE
7561 /* When at the end of the text and overwriting a two-cell
7562 * character with a one-cell character, need to clear the next
7563 * cell. Also when overwriting the left halve of a two-cell char
7564 * with the right halve of a two-cell char. Do this only once
7565 * (mb_off2cells() may return 2 on the right halve). */
7566 if (clear_next_cell)
7567 clear_next_cell = FALSE;
7568 else if (has_mbyte
7569 && (len < 0 ? ptr[mbyte_blen] == NUL
7570 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007571 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007573 && (*mb_off2cells)(off, max_off) == 1
7574 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 clear_next_cell = TRUE;
7576
7577 /* Make sure we never leave a second byte of a double-byte behind,
7578 * it confuses mb_off2cells(). */
7579 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007580 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007582 && (*mb_off2cells)(off, max_off) == 1
7583 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 ScreenLines[off + mbyte_blen] = 0;
7585#endif
7586 ScreenLines[off] = c;
7587 ScreenAttrs[off] = attr;
7588#ifdef FEAT_MBYTE
7589 if (enc_utf8)
7590 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007591 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 ScreenLinesUC[off] = 0;
7593 else
7594 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007595 int i;
7596
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007598 for (i = 0; i < Screen_mco; ++i)
7599 {
7600 ScreenLinesC[i][off] = u8cc[i];
7601 if (u8cc[i] == 0)
7602 break;
7603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 }
7605 if (mbyte_cells == 2)
7606 {
7607 ScreenLines[off + 1] = 0;
7608 ScreenAttrs[off + 1] = attr;
7609 }
7610 screen_char(off, row, col);
7611 }
7612 else if (mbyte_cells == 2)
7613 {
7614 ScreenLines[off + 1] = ptr[1];
7615 ScreenAttrs[off + 1] = attr;
7616 screen_char_2(off, row, col);
7617 }
7618 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7619 {
7620 ScreenLines2[off] = ptr[1];
7621 screen_char(off, row, col);
7622 }
7623 else
7624#endif
7625 screen_char(off, row, col);
7626 }
7627#ifdef FEAT_MBYTE
7628 if (has_mbyte)
7629 {
7630 off += mbyte_cells;
7631 col += mbyte_cells;
7632 ptr += mbyte_blen;
7633 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007634 {
7635 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007637 len = -1;
7638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 }
7640 else
7641#endif
7642 {
7643 ++off;
7644 ++col;
7645 ++ptr;
7646 }
7647 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007648
7649#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7650 /* If we detected the next character needs to be redrawn, but the text
7651 * doesn't extend up to there, update the character here. */
7652 if (force_redraw_next && col < screen_Columns)
7653 {
7654# ifdef FEAT_MBYTE
7655 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7656 screen_char_2(off, row, col);
7657 else
7658# endif
7659 screen_char(off, row, col);
7660 }
7661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662}
7663
7664#ifdef FEAT_SEARCH_EXTRA
7665/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007666 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 */
7668 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007669start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
7671 if (p_hls && !no_hlsearch)
7672 {
7673 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007674 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007675# ifdef FEAT_RELTIME
7676 /* Set the time limit to 'redrawtime'. */
7677 profile_setlimit(p_rdt, &search_hl.tm);
7678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
7680}
7681
7682/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007683 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 */
7685 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007686end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687{
7688 if (search_hl.rm.regprog != NULL)
7689 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007690 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 search_hl.rm.regprog = NULL;
7692 }
7693}
7694
7695/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007696 * Init for calling prepare_search_hl().
7697 */
7698 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007699init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007700{
7701 matchitem_T *cur;
7702
7703 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7704 * match */
7705 cur = wp->w_match_head;
7706 while (cur != NULL)
7707 {
7708 cur->hl.rm = cur->match;
7709 if (cur->hlg_id == 0)
7710 cur->hl.attr = 0;
7711 else
7712 cur->hl.attr = syn_id2attr(cur->hlg_id);
7713 cur->hl.buf = wp->w_buffer;
7714 cur->hl.lnum = 0;
7715 cur->hl.first_lnum = 0;
7716# ifdef FEAT_RELTIME
7717 /* Set the time limit to 'redrawtime'. */
7718 profile_setlimit(p_rdt, &(cur->hl.tm));
7719# endif
7720 cur = cur->next;
7721 }
7722 search_hl.buf = wp->w_buffer;
7723 search_hl.lnum = 0;
7724 search_hl.first_lnum = 0;
7725 /* time limit is set at the toplevel, for all windows */
7726}
7727
7728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 * Advance to the match in window "wp" line "lnum" or past it.
7730 */
7731 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007732prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007734 matchitem_T *cur; /* points to the match list */
7735 match_T *shl; /* points to search_hl or a match */
7736 int shl_flag; /* flag to indicate whether search_hl
7737 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007738 int pos_inprogress; /* marks that position match search is
7739 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 int n;
7741
7742 /*
7743 * When using a multi-line pattern, start searching at the top
7744 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007745 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007747 cur = wp->w_match_head;
7748 shl_flag = FALSE;
7749 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007751 if (shl_flag == FALSE)
7752 {
7753 shl = &search_hl;
7754 shl_flag = TRUE;
7755 }
7756 else
7757 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 if (shl->rm.regprog != NULL
7759 && shl->lnum == 0
7760 && re_multiline(shl->rm.regprog))
7761 {
7762 if (shl->first_lnum == 0)
7763 {
7764# ifdef FEAT_FOLDING
7765 for (shl->first_lnum = lnum;
7766 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7767 if (hasFoldingWin(wp, shl->first_lnum - 1,
7768 NULL, NULL, TRUE, NULL))
7769 break;
7770# else
7771 shl->first_lnum = wp->w_topline;
7772# endif
7773 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007774 if (cur != NULL)
7775 cur->pos.cur = 0;
7776 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007778 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7779 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007781 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7782 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007783 pos_inprogress = cur == NULL || cur->pos.cur == 0
7784 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 if (shl->lnum != 0)
7786 {
7787 shl->first_lnum = shl->lnum
7788 + shl->rm.endpos[0].lnum
7789 - shl->rm.startpos[0].lnum;
7790 n = shl->rm.endpos[0].col;
7791 }
7792 else
7793 {
7794 ++shl->first_lnum;
7795 n = 0;
7796 }
7797 }
7798 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007799 if (shl != &search_hl && cur != NULL)
7800 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 }
7802}
7803
7804/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007805 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 * Uses shl->buf.
7807 * Sets shl->lnum and shl->rm contents.
7808 * Note: Assumes a previous match is always before "lnum", unless
7809 * shl->lnum is zero.
7810 * Careful: Any pointers for buffer lines will become invalid.
7811 */
7812 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007813next_search_hl(
7814 win_T *win,
7815 match_T *shl, /* points to search_hl or a match */
7816 linenr_T lnum,
7817 colnr_T mincol, /* minimal column for a match */
7818 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 linenr_T l;
7821 colnr_T matchcol;
7822 long nmatched;
7823
7824 if (shl->lnum != 0)
7825 {
7826 /* Check for three situations:
7827 * 1. If the "lnum" is below a previous match, start a new search.
7828 * 2. If the previous match includes "mincol", use it.
7829 * 3. Continue after the previous match.
7830 */
7831 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7832 if (lnum > l)
7833 shl->lnum = 0;
7834 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7835 return;
7836 }
7837
7838 /*
7839 * Repeat searching for a match until one is found that includes "mincol"
7840 * or none is found in this line.
7841 */
7842 called_emsg = FALSE;
7843 for (;;)
7844 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007845#ifdef FEAT_RELTIME
7846 /* Stop searching after passing the time limit. */
7847 if (profile_passed_limit(&(shl->tm)))
7848 {
7849 shl->lnum = 0; /* no match found in time */
7850 break;
7851 }
7852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 /* Three situations:
7854 * 1. No useful previous match: search from start of line.
7855 * 2. Not Vi compatible or empty match: continue at next character.
7856 * Break the loop if this is beyond the end of the line.
7857 * 3. Vi compatible searching: continue at end of previous match.
7858 */
7859 if (shl->lnum == 0)
7860 matchcol = 0;
7861 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7862 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007863 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007865 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007866
7867 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007868 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007869 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007871 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 shl->lnum = 0;
7873 break;
7874 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007875#ifdef FEAT_MBYTE
7876 if (has_mbyte)
7877 matchcol += mb_ptr2len(ml);
7878 else
7879#endif
7880 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 }
7882 else
7883 matchcol = shl->rm.endpos[0].col;
7884
7885 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007886 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007888 /* Remember whether shl->rm is using a copy of the regprog in
7889 * cur->match. */
7890 int regprog_is_copy = (shl != &search_hl && cur != NULL
7891 && shl == &cur->hl
7892 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007893 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007894
Bram Moolenaarb3414592014-06-17 17:48:32 +02007895 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7896 matchcol,
7897#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007898 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007899#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007900 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007901#endif
7902 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007903 /* Copy the regprog, in case it got freed and recompiled. */
7904 if (regprog_is_copy)
7905 cur->match.regprog = cur->hl.rm.regprog;
7906
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007907 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007908 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007909 /* Error while handling regexp: stop using this regexp. */
7910 if (shl == &search_hl)
7911 {
7912 /* don't free regprog in the match list, it's a copy */
7913 vim_regfree(shl->rm.regprog);
7914 SET_NO_HLSEARCH(TRUE);
7915 }
7916 shl->rm.regprog = NULL;
7917 shl->lnum = 0;
7918 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7919 message */
7920 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007921 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007922 }
7923 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007924 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007925 else
7926 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 if (nmatched == 0)
7928 {
7929 shl->lnum = 0; /* no match found */
7930 break;
7931 }
7932 if (shl->rm.startpos[0].lnum > 0
7933 || shl->rm.startpos[0].col >= mincol
7934 || nmatched > 1
7935 || shl->rm.endpos[0].col > mincol)
7936 {
7937 shl->lnum += shl->rm.startpos[0].lnum;
7938 break; /* useful match found */
7939 }
7940 }
7941}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942
Bram Moolenaar85077472016-10-16 14:35:48 +02007943/*
7944 * If there is a match fill "shl" and return one.
7945 * Return zero otherwise.
7946 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007947 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007948next_search_hl_pos(
7949 match_T *shl, /* points to a match */
7950 linenr_T lnum,
7951 posmatch_T *posmatch, /* match positions */
7952 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007953{
7954 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007955 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007956
Bram Moolenaarb3414592014-06-17 17:48:32 +02007957 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7958 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007959 llpos_T *pos = &posmatch->pos[i];
7960
7961 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007962 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007963 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007964 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007965 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007966 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007967 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007968 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007969 /* if this match comes before the one at "found" then swap
7970 * them */
7971 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007972 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007973 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007974
Bram Moolenaar85077472016-10-16 14:35:48 +02007975 *pos = posmatch->pos[found];
7976 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007977 }
7978 }
7979 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007980 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981 }
7982 }
7983 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007984 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007985 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007986 colnr_T start = posmatch->pos[found].col == 0
7987 ? 0 : posmatch->pos[found].col - 1;
7988 colnr_T end = posmatch->pos[found].col == 0
7989 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990
Bram Moolenaar85077472016-10-16 14:35:48 +02007991 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007992 shl->rm.startpos[0].lnum = 0;
7993 shl->rm.startpos[0].col = start;
7994 shl->rm.endpos[0].lnum = 0;
7995 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007996 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007997 posmatch->cur = found + 1;
7998 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007999 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008000 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008001}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008002#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008003
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008005screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
8007 attrentry_T *aep = NULL;
8008
8009 screen_attr = attr;
8010 if (full_screen
8011#ifdef WIN3264
8012 && termcap_active
8013#endif
8014 )
8015 {
8016#ifdef FEAT_GUI
8017 if (gui.in_use)
8018 {
8019 char buf[20];
8020
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008021 /* The GUI handles this internally. */
8022 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 OUT_STR(buf);
8024 }
8025 else
8026#endif
8027 {
8028 if (attr > HL_ALL) /* special HL attr. */
8029 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008030 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 aep = syn_cterm_attr2entry(attr);
8032 else
8033 aep = syn_term_attr2entry(attr);
8034 if (aep == NULL) /* did ":syntax clear" */
8035 attr = 0;
8036 else
8037 attr = aep->ae_attr;
8038 }
8039 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8040 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008041 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008042#ifdef FEAT_TERMGUICOLORS
8043 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008044 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008045#endif
8046 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008047#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008048 )
8049#endif
8050 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008051 /* If the Normal FG color has BOLD attribute and the new HL
8052 * has a FG color defined, clear BOLD. */
8053 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8055 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008056 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8057 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 out_str(T_US);
8059 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8060 out_str(T_CZH);
8061 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8062 out_str(T_MR);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008063 if ((attr & HL_STRIKETHROUGH) && T_STS != NULL) /* strike */
8064 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065
8066 /*
8067 * Output the color or start string after bold etc., in case the
8068 * bold etc. override the color setting.
8069 */
8070 if (aep != NULL)
8071 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008072#ifdef FEAT_TERMGUICOLORS
8073 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008075 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008076 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008077 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008078 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 }
8080 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008083 if (t_colors > 1)
8084 {
8085 if (aep->ae_u.cterm.fg_color)
8086 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8087 if (aep->ae_u.cterm.bg_color)
8088 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8089 }
8090 else
8091 {
8092 if (aep->ae_u.term.start != NULL)
8093 out_str(aep->ae_u.term.start);
8094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 }
8096 }
8097 }
8098 }
8099}
8100
8101 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008102screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103{
8104 int do_ME = FALSE; /* output T_ME code */
8105
8106 if (screen_attr != 0
8107#ifdef WIN3264
8108 && termcap_active
8109#endif
8110 )
8111 {
8112#ifdef FEAT_GUI
8113 if (gui.in_use)
8114 {
8115 char buf[20];
8116
8117 /* use internal GUI code */
8118 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8119 OUT_STR(buf);
8120 }
8121 else
8122#endif
8123 {
8124 if (screen_attr > HL_ALL) /* special HL attr. */
8125 {
8126 attrentry_T *aep;
8127
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008128 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {
8130 /*
8131 * Assume that t_me restores the original colors!
8132 */
8133 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008134 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008135#ifdef FEAT_TERMGUICOLORS
8136 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008137 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8138 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008139#endif
8140 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008141#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008142 )
8143#endif
8144 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 do_ME = TRUE;
8146 }
8147 else
8148 {
8149 aep = syn_term_attr2entry(screen_attr);
8150 if (aep != NULL && aep->ae_u.term.stop != NULL)
8151 {
8152 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8153 do_ME = TRUE;
8154 else
8155 out_str(aep->ae_u.term.stop);
8156 }
8157 }
8158 if (aep == NULL) /* did ":syntax clear" */
8159 screen_attr = 0;
8160 else
8161 screen_attr = aep->ae_attr;
8162 }
8163
8164 /*
8165 * Often all ending-codes are equal to T_ME. Avoid outputting the
8166 * same sequence several times.
8167 */
8168 if (screen_attr & HL_STANDOUT)
8169 {
8170 if (STRCMP(T_SE, T_ME) == 0)
8171 do_ME = TRUE;
8172 else
8173 out_str(T_SE);
8174 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008175 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {
8177 if (STRCMP(T_UE, T_ME) == 0)
8178 do_ME = TRUE;
8179 else
8180 out_str(T_UE);
8181 }
8182 if (screen_attr & HL_ITALIC)
8183 {
8184 if (STRCMP(T_CZR, T_ME) == 0)
8185 do_ME = TRUE;
8186 else
8187 out_str(T_CZR);
8188 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008189 if (screen_attr & HL_STRIKETHROUGH)
8190 {
8191 if (STRCMP(T_STE, T_ME) == 0)
8192 do_ME = TRUE;
8193 else
8194 out_str(T_STE);
8195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8197 out_str(T_ME);
8198
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008199#ifdef FEAT_TERMGUICOLORS
8200 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008202 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008203 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008204 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008205 term_bg_rgb_color(cterm_normal_bg_gui_color);
8206 }
8207 else
8208#endif
8209 {
8210 if (t_colors > 1)
8211 {
8212 /* set Normal cterm colors */
8213 if (cterm_normal_fg_color != 0)
8214 term_fg_color(cterm_normal_fg_color - 1);
8215 if (cterm_normal_bg_color != 0)
8216 term_bg_color(cterm_normal_bg_color - 1);
8217 if (cterm_normal_fg_bold)
8218 out_str(T_MD);
8219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 }
8221 }
8222 }
8223 screen_attr = 0;
8224}
8225
8226/*
8227 * Reset the colors for a cterm. Used when leaving Vim.
8228 * The machine specific code may override this again.
8229 */
8230 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008231reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008233 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {
8235 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008236#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008237 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8238 || cterm_normal_bg_gui_color != INVALCOLOR)
8239 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008240#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
8244 out_str(T_OP);
8245 screen_attr = -1;
8246 }
8247 if (cterm_normal_fg_bold)
8248 {
8249 out_str(T_ME);
8250 screen_attr = -1;
8251 }
8252 }
8253}
8254
8255/*
8256 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8257 * using the attributes from ScreenAttrs["off"].
8258 */
8259 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008260screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
8262 int attr;
8263
8264 /* Check for illegal values, just in case (could happen just after
8265 * resizing). */
8266 if (row >= screen_Rows || col >= screen_Columns)
8267 return;
8268
Bram Moolenaar494838a2015-02-10 19:20:37 +01008269 /* Outputting a character in the last cell on the screen may scroll the
8270 * screen up. Only do it when the "xn" termcap property is set, otherwise
8271 * mark the character invalid (update it when scrolled up). */
8272 if (*T_XN == NUL
8273 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274#ifdef FEAT_RIGHTLEFT
8275 /* account for first command-line character in rightleft mode */
8276 && !cmdmsg_rl
8277#endif
8278 )
8279 {
8280 ScreenAttrs[off] = (sattr_T)-1;
8281 return;
8282 }
8283
8284 /*
8285 * Stop highlighting first, so it's easier to move the cursor.
8286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 if (screen_char_attr != 0)
8288 attr = screen_char_attr;
8289 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 attr = ScreenAttrs[off];
8291 if (screen_attr != attr)
8292 screen_stop_highlight();
8293
8294 windgoto(row, col);
8295
8296 if (screen_attr != attr)
8297 screen_start_highlight(attr);
8298
8299#ifdef FEAT_MBYTE
8300 if (enc_utf8 && ScreenLinesUC[off] != 0)
8301 {
8302 char_u buf[MB_MAXBYTES + 1];
8303
8304 /* Convert UTF-8 character to bytes and write it. */
8305
8306 buf[utfc_char2bytes(off, buf)] = NUL;
8307
8308 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008309 if (utf_ambiguous_width(ScreenLinesUC[off]))
8310 screen_cur_col = 9999;
8311 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 ++screen_cur_col;
8313 }
8314 else
8315#endif
8316 {
8317#ifdef FEAT_MBYTE
8318 out_flush_check();
8319#endif
8320 out_char(ScreenLines[off]);
8321#ifdef FEAT_MBYTE
8322 /* double-byte character in single-width cell */
8323 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8324 out_char(ScreenLines2[off]);
8325#endif
8326 }
8327
8328 screen_cur_col++;
8329}
8330
8331#ifdef FEAT_MBYTE
8332
8333/*
8334 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8335 * on the screen at position 'row' and 'col'.
8336 * The attributes of the first byte is used for all. This is required to
8337 * output the two bytes of a double-byte character with nothing in between.
8338 */
8339 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008340screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341{
8342 /* Check for illegal values (could be wrong when screen was resized). */
8343 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8344 return;
8345
8346 /* Outputting the last character on the screen may scrollup the screen.
8347 * Don't to it! Mark the character invalid (update it when scrolled up) */
8348 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8349 {
8350 ScreenAttrs[off] = (sattr_T)-1;
8351 return;
8352 }
8353
8354 /* Output the first byte normally (positions the cursor), then write the
8355 * second byte directly. */
8356 screen_char(off, row, col);
8357 out_char(ScreenLines[off + 1]);
8358 ++screen_cur_col;
8359}
8360#endif
8361
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362/*
8363 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8364 * This uses the contents of ScreenLines[] and doesn't change it.
8365 */
8366 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008367screen_draw_rectangle(
8368 int row,
8369 int col,
8370 int height,
8371 int width,
8372 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
8374 int r, c;
8375 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008376#ifdef FEAT_MBYTE
8377 int max_off;
8378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008380 /* Can't use ScreenLines unless initialized */
8381 if (ScreenLines == NULL)
8382 return;
8383
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 if (invert)
8385 screen_char_attr = HL_INVERSE;
8386 for (r = row; r < row + height; ++r)
8387 {
8388 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008389#ifdef FEAT_MBYTE
8390 max_off = off + screen_Columns;
8391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 for (c = col; c < col + width; ++c)
8393 {
8394#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008395 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {
8397 screen_char_2(off + c, r, c);
8398 ++c;
8399 }
8400 else
8401#endif
8402 {
8403 screen_char(off + c, r, c);
8404#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008405 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 ++c;
8407#endif
8408 }
8409 }
8410 }
8411 screen_char_attr = 0;
8412}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414/*
8415 * Redraw the characters for a vertically split window.
8416 */
8417 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008418redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 int col;
8421 int width;
8422
8423# ifdef FEAT_CLIPBOARD
8424 clip_may_clear_selection(row, end - 1);
8425# endif
8426
8427 if (wp == NULL)
8428 {
8429 col = 0;
8430 width = Columns;
8431 }
8432 else
8433 {
8434 col = wp->w_wincol;
8435 width = wp->w_width;
8436 }
8437 screen_draw_rectangle(row, col, end - row, width, FALSE);
8438}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008440 static void
8441space_to_screenline(int off, int attr)
8442{
8443 ScreenLines[off] = ' ';
8444 ScreenAttrs[off] = attr;
8445# ifdef FEAT_MBYTE
8446 if (enc_utf8)
8447 ScreenLinesUC[off] = 0;
8448# endif
8449}
8450
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451/*
8452 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8453 * with character 'c1' in first column followed by 'c2' in the other columns.
8454 * Use attributes 'attr'.
8455 */
8456 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008457screen_fill(
8458 int start_row,
8459 int end_row,
8460 int start_col,
8461 int end_col,
8462 int c1,
8463 int c2,
8464 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465{
8466 int row;
8467 int col;
8468 int off;
8469 int end_off;
8470 int did_delete;
8471 int c;
8472 int norm_term;
8473#if defined(FEAT_GUI) || defined(UNIX)
8474 int force_next = FALSE;
8475#endif
8476
8477 if (end_row > screen_Rows) /* safety check */
8478 end_row = screen_Rows;
8479 if (end_col > screen_Columns) /* safety check */
8480 end_col = screen_Columns;
8481 if (ScreenLines == NULL
8482 || start_row >= end_row
8483 || start_col >= end_col) /* nothing to do */
8484 return;
8485
8486 /* it's a "normal" terminal when not in a GUI or cterm */
8487 norm_term = (
8488#ifdef FEAT_GUI
8489 !gui.in_use &&
8490#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008491 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 for (row = start_row; row < end_row; ++row)
8493 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008494#ifdef FEAT_MBYTE
8495 if (has_mbyte
8496# ifdef FEAT_GUI
8497 && !gui.in_use
8498# endif
8499 )
8500 {
8501 /* When drawing over the right halve of a double-wide char clear
8502 * out the left halve. When drawing over the left halve of a
8503 * double wide-char clear out the right halve. Only needed in a
8504 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008505 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008506 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008507 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008508 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008509 }
8510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 /*
8512 * Try to use delete-line termcap code, when no attributes or in a
8513 * "normal" terminal, where a bold/italic space is just a
8514 * space.
8515 */
8516 did_delete = FALSE;
8517 if (c2 == ' '
8518 && end_col == Columns
8519 && can_clear(T_CE)
8520 && (attr == 0
8521 || (norm_term
8522 && attr <= HL_ALL
8523 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8524 {
8525 /*
8526 * check if we really need to clear something
8527 */
8528 col = start_col;
8529 if (c1 != ' ') /* don't clear first char */
8530 ++col;
8531
8532 off = LineOffset[row] + col;
8533 end_off = LineOffset[row] + end_col;
8534
8535 /* skip blanks (used often, keep it fast!) */
8536#ifdef FEAT_MBYTE
8537 if (enc_utf8)
8538 while (off < end_off && ScreenLines[off] == ' '
8539 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8540 ++off;
8541 else
8542#endif
8543 while (off < end_off && ScreenLines[off] == ' '
8544 && ScreenAttrs[off] == 0)
8545 ++off;
8546 if (off < end_off) /* something to be cleared */
8547 {
8548 col = off - LineOffset[row];
8549 screen_stop_highlight();
8550 term_windgoto(row, col);/* clear rest of this screen line */
8551 out_str(T_CE);
8552 screen_start(); /* don't know where cursor is now */
8553 col = end_col - col;
8554 while (col--) /* clear chars in ScreenLines */
8555 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008556 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 ++off;
8558 }
8559 }
8560 did_delete = TRUE; /* the chars are cleared now */
8561 }
8562
8563 off = LineOffset[row] + start_col;
8564 c = c1;
8565 for (col = start_col; col < end_col; ++col)
8566 {
8567 if (ScreenLines[off] != c
8568#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008569 || (enc_utf8 && (int)ScreenLinesUC[off]
8570 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571#endif
8572 || ScreenAttrs[off] != attr
8573#if defined(FEAT_GUI) || defined(UNIX)
8574 || force_next
8575#endif
8576 )
8577 {
8578#if defined(FEAT_GUI) || defined(UNIX)
8579 /* The bold trick may make a single row of pixels appear in
8580 * the next character. When a bold character is removed, the
8581 * next character should be redrawn too. This happens for our
8582 * own GUI and for some xterms. */
8583 if (
8584# ifdef FEAT_GUI
8585 gui.in_use
8586# endif
8587# if defined(FEAT_GUI) && defined(UNIX)
8588 ||
8589# endif
8590# ifdef UNIX
8591 term_is_xterm
8592# endif
8593 )
8594 {
8595 if (ScreenLines[off] != ' '
8596 && (ScreenAttrs[off] > HL_ALL
8597 || ScreenAttrs[off] & HL_BOLD))
8598 force_next = TRUE;
8599 else
8600 force_next = FALSE;
8601 }
8602#endif
8603 ScreenLines[off] = c;
8604#ifdef FEAT_MBYTE
8605 if (enc_utf8)
8606 {
8607 if (c >= 0x80)
8608 {
8609 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008610 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 }
8612 else
8613 ScreenLinesUC[off] = 0;
8614 }
8615#endif
8616 ScreenAttrs[off] = attr;
8617 if (!did_delete || c != ' ')
8618 screen_char(off, row, col);
8619 }
8620 ++off;
8621 if (col == start_col)
8622 {
8623 if (did_delete)
8624 break;
8625 c = c2;
8626 }
8627 }
8628 if (end_col == Columns)
8629 LineWraps[row] = FALSE;
8630 if (row == Rows - 1) /* overwritten the command line */
8631 {
8632 redraw_cmdline = TRUE;
8633 if (c1 == ' ' && c2 == ' ')
8634 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008635 if (start_col == 0)
8636 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 }
8638 }
8639}
8640
8641/*
8642 * Check if there should be a delay. Used before clearing or redrawing the
8643 * screen or the command line.
8644 */
8645 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008646check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647{
8648 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8649 && !did_wait_return
8650 && emsg_silent == 0)
8651 {
8652 out_flush();
8653 ui_delay(1000L, TRUE);
8654 emsg_on_display = FALSE;
8655 if (check_msg_scroll)
8656 msg_scroll = FALSE;
8657 }
8658}
8659
8660/*
8661 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008662 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 * Returns TRUE if there is a valid screen to write to.
8664 * Returns FALSE when starting up and screen not initialized yet.
8665 */
8666 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008667screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008669 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 return (ScreenLines != NULL);
8671}
8672
8673/*
8674 * Resize the shell to Rows and Columns.
8675 * Allocate ScreenLines[] and associated items.
8676 *
8677 * There may be some time between setting Rows and Columns and (re)allocating
8678 * ScreenLines[]. This happens when starting up and when (manually) changing
8679 * the shell size. Always use screen_Rows and screen_Columns to access items
8680 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8681 * final size of the shell is needed.
8682 */
8683 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008684screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
8686 int new_row, old_row;
8687#ifdef FEAT_GUI
8688 int old_Rows;
8689#endif
8690 win_T *wp;
8691 int outofmem = FALSE;
8692 int len;
8693 schar_T *new_ScreenLines;
8694#ifdef FEAT_MBYTE
8695 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008696 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008698 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699#endif
8700 sattr_T *new_ScreenAttrs;
8701 unsigned *new_LineOffset;
8702 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008703 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008704 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008706 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008707#ifdef FEAT_AUTOCMD
8708 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008710retry:
8711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 /*
8713 * Allocation of the screen buffers is done only when the size changes and
8714 * when Rows and Columns have been set and we have started doing full
8715 * screen stuff.
8716 */
8717 if ((ScreenLines != NULL
8718 && Rows == screen_Rows
8719 && Columns == screen_Columns
8720#ifdef FEAT_MBYTE
8721 && enc_utf8 == (ScreenLinesUC != NULL)
8722 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008723 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724#endif
8725 )
8726 || Rows == 0
8727 || Columns == 0
8728 || (!full_screen && ScreenLines == NULL))
8729 return;
8730
8731 /*
8732 * It's possible that we produce an out-of-memory message below, which
8733 * will cause this function to be called again. To break the loop, just
8734 * return here.
8735 */
8736 if (entered)
8737 return;
8738 entered = TRUE;
8739
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008740 /*
8741 * Note that the window sizes are updated before reallocating the arrays,
8742 * thus we must not redraw here!
8743 */
8744 ++RedrawingDisabled;
8745
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 win_new_shellsize(); /* fit the windows in the new sized shell */
8747
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 comp_col(); /* recompute columns for shown command and ruler */
8749
8750 /*
8751 * We're changing the size of the screen.
8752 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8753 * - Move lines from the old arrays into the new arrays, clear extra
8754 * lines (unless the screen is going to be cleared).
8755 * - Free the old arrays.
8756 *
8757 * If anything fails, make ScreenLines NULL, so we don't do anything!
8758 * Continuing with the old ScreenLines may result in a crash, because the
8759 * size is wrong.
8760 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008761 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008763#ifdef FEAT_AUTOCMD
8764 if (aucmd_win != NULL)
8765 win_free_lsize(aucmd_win);
8766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767
8768 new_ScreenLines = (schar_T *)lalloc((long_u)(
8769 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8770#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008771 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 if (enc_utf8)
8773 {
8774 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8775 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008776 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008777 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8779 }
8780 if (enc_dbcs == DBCS_JPNU)
8781 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8782 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8783#endif
8784 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8785 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8786 new_LineOffset = (unsigned *)lalloc((long_u)(
8787 Rows * sizeof(unsigned)), FALSE);
8788 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008789 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008791 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 {
8793 if (win_alloc_lines(wp) == FAIL)
8794 {
8795 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008796 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 }
8798 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008799#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008800 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8801 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008802 outofmem = TRUE;
8803#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008804give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008806#ifdef FEAT_MBYTE
8807 for (i = 0; i < p_mco; ++i)
8808 if (new_ScreenLinesC[i] == NULL)
8809 break;
8810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 if (new_ScreenLines == NULL
8812#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008813 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8815#endif
8816 || new_ScreenAttrs == NULL
8817 || new_LineOffset == NULL
8818 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008819 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 || outofmem)
8821 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008822 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008823 {
8824 /* guess the size */
8825 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8826
8827 /* Remember we did this to avoid getting outofmem messages over
8828 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008829 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 vim_free(new_ScreenLines);
8832 new_ScreenLines = NULL;
8833#ifdef FEAT_MBYTE
8834 vim_free(new_ScreenLinesUC);
8835 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008836 for (i = 0; i < p_mco; ++i)
8837 {
8838 vim_free(new_ScreenLinesC[i]);
8839 new_ScreenLinesC[i] = NULL;
8840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 vim_free(new_ScreenLines2);
8842 new_ScreenLines2 = NULL;
8843#endif
8844 vim_free(new_ScreenAttrs);
8845 new_ScreenAttrs = NULL;
8846 vim_free(new_LineOffset);
8847 new_LineOffset = NULL;
8848 vim_free(new_LineWraps);
8849 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008850 vim_free(new_TabPageIdxs);
8851 new_TabPageIdxs = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 }
8853 else
8854 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008855 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008856
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 for (new_row = 0; new_row < Rows; ++new_row)
8858 {
8859 new_LineOffset[new_row] = new_row * Columns;
8860 new_LineWraps[new_row] = FALSE;
8861
8862 /*
8863 * If the screen is not going to be cleared, copy as much as
8864 * possible from the old screen to the new one and clear the rest
8865 * (used when resizing the window at the "--more--" prompt or when
8866 * executing an external command, for the GUI).
8867 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008868 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 {
8870 (void)vim_memset(new_ScreenLines + new_row * Columns,
8871 ' ', (size_t)Columns * sizeof(schar_T));
8872#ifdef FEAT_MBYTE
8873 if (enc_utf8)
8874 {
8875 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8876 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008877 for (i = 0; i < p_mco; ++i)
8878 (void)vim_memset(new_ScreenLinesC[i]
8879 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 0, (size_t)Columns * sizeof(u8char_T));
8881 }
8882 if (enc_dbcs == DBCS_JPNU)
8883 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8884 0, (size_t)Columns * sizeof(schar_T));
8885#endif
8886 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8887 0, (size_t)Columns * sizeof(sattr_T));
8888 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008889 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 {
8891 if (screen_Columns < Columns)
8892 len = screen_Columns;
8893 else
8894 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008895#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008896 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008897 * may be invalid now. Also when p_mco changes. */
8898 if (!(enc_utf8 && ScreenLinesUC == NULL)
8899 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008900#endif
8901 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8902 ScreenLines + LineOffset[old_row],
8903 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008905 if (enc_utf8 && ScreenLinesUC != NULL
8906 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 {
8908 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8909 ScreenLinesUC + LineOffset[old_row],
8910 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008911 for (i = 0; i < p_mco; ++i)
8912 mch_memmove(new_ScreenLinesC[i]
8913 + new_LineOffset[new_row],
8914 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 (size_t)len * sizeof(u8char_T));
8916 }
8917 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8918 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8919 ScreenLines2 + LineOffset[old_row],
8920 (size_t)len * sizeof(schar_T));
8921#endif
8922 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8923 ScreenAttrs + LineOffset[old_row],
8924 (size_t)len * sizeof(sattr_T));
8925 }
8926 }
8927 }
8928 /* Use the last line of the screen for the current line. */
8929 current_ScreenLine = new_ScreenLines + Rows * Columns;
8930 }
8931
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008932 free_screenlines();
8933
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 ScreenLines = new_ScreenLines;
8935#ifdef FEAT_MBYTE
8936 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008937 for (i = 0; i < p_mco; ++i)
8938 ScreenLinesC[i] = new_ScreenLinesC[i];
8939 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 ScreenLines2 = new_ScreenLines2;
8941#endif
8942 ScreenAttrs = new_ScreenAttrs;
8943 LineOffset = new_LineOffset;
8944 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008945 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946
8947 /* It's important that screen_Rows and screen_Columns reflect the actual
8948 * size of ScreenLines[]. Set them before calling anything. */
8949#ifdef FEAT_GUI
8950 old_Rows = screen_Rows;
8951#endif
8952 screen_Rows = Rows;
8953 screen_Columns = Columns;
8954
8955 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008956 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 screenclear2();
8958
8959#ifdef FEAT_GUI
8960 else if (gui.in_use
8961 && !gui.starting
8962 && ScreenLines != NULL
8963 && old_Rows != Rows)
8964 {
8965 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8966 /*
8967 * Adjust the position of the cursor, for when executing an external
8968 * command.
8969 */
8970 if (msg_row >= Rows) /* Rows got smaller */
8971 msg_row = Rows - 1; /* put cursor at last row */
8972 else if (Rows > old_Rows) /* Rows got bigger */
8973 msg_row += Rows - old_Rows; /* put cursor in same place */
8974 if (msg_col >= Columns) /* Columns got smaller */
8975 msg_col = Columns - 1; /* put cursor at last column */
8976 }
8977#endif
8978
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008980 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008981
8982#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008983 /*
8984 * Do not apply autocommands more than 3 times to avoid an endless loop
8985 * in case applying autocommands always changes Rows or Columns.
8986 */
8987 if (starting == 0 && ++retry_count <= 3)
8988 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008989 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008990 /* In rare cases, autocommands may have altered Rows or Columns,
8991 * jump back to check if we need to allocate the screen again. */
8992 goto retry;
8993 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995}
8996
8997 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008998free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008999{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009000#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009001 int i;
9002
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009003 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009004 for (i = 0; i < Screen_mco; ++i)
9005 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009006 vim_free(ScreenLines2);
9007#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009008 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009009 vim_free(ScreenAttrs);
9010 vim_free(LineOffset);
9011 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009012 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009013}
9014
9015 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009016screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017{
9018 check_for_delay(FALSE);
9019 screenalloc(FALSE); /* allocate screen buffers if size changed */
9020 screenclear2(); /* clear the screen */
9021}
9022
9023 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009024screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025{
9026 int i;
9027
9028 if (starting == NO_SCREEN || ScreenLines == NULL
9029#ifdef FEAT_GUI
9030 || (gui.in_use && gui.starting)
9031#endif
9032 )
9033 return;
9034
9035#ifdef FEAT_GUI
9036 if (!gui.in_use)
9037#endif
9038 screen_attr = -1; /* force setting the Normal colors */
9039 screen_stop_highlight(); /* don't want highlighting here */
9040
9041#ifdef FEAT_CLIPBOARD
9042 /* disable selection without redrawing it */
9043 clip_scroll_selection(9999);
9044#endif
9045
9046 /* blank out ScreenLines */
9047 for (i = 0; i < Rows; ++i)
9048 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009049 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 LineWraps[i] = FALSE;
9051 }
9052
9053 if (can_clear(T_CL))
9054 {
9055 out_str(T_CL); /* clear the display */
9056 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009057 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 }
9059 else
9060 {
9061 /* can't clear the screen, mark all chars with invalid attributes */
9062 for (i = 0; i < Rows; ++i)
9063 lineinvalid(LineOffset[i], (int)Columns);
9064 clear_cmdline = TRUE;
9065 }
9066
9067 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9068
9069 win_rest_invalid(firstwin);
9070 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009071 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 if (must_redraw == CLEAR) /* no need to clear again */
9073 must_redraw = NOT_VALID;
9074 compute_cmdrow();
9075 msg_row = cmdline_row; /* put cursor on last line for messages */
9076 msg_col = 0;
9077 screen_start(); /* don't know where cursor is now */
9078 msg_scrolled = 0; /* can't scroll back */
9079 msg_didany = FALSE;
9080 msg_didout = FALSE;
9081}
9082
9083/*
9084 * Clear one line in ScreenLines.
9085 */
9086 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009087lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088{
9089 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9090#ifdef FEAT_MBYTE
9091 if (enc_utf8)
9092 (void)vim_memset(ScreenLinesUC + off, 0,
9093 (size_t)width * sizeof(u8char_T));
9094#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009095 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096}
9097
9098/*
9099 * Mark one line in ScreenLines invalid by setting the attributes to an
9100 * invalid value.
9101 */
9102 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009103lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104{
9105 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9106}
9107
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108/*
9109 * Copy part of a Screenline for vertically split window "wp".
9110 */
9111 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009112linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113{
9114 unsigned off_to = LineOffset[to] + wp->w_wincol;
9115 unsigned off_from = LineOffset[from] + wp->w_wincol;
9116
9117 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9118 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009119#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 if (enc_utf8)
9121 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009122 int i;
9123
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9125 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009126 for (i = 0; i < p_mco; ++i)
9127 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9128 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 }
9130 if (enc_dbcs == DBCS_JPNU)
9131 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9132 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9135 wp->w_width * sizeof(sattr_T));
9136}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137
9138/*
9139 * Return TRUE if clearing with term string "p" would work.
9140 * It can't work when the string is empty or it won't set the right background.
9141 */
9142 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009143can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144{
9145 return (*p != NUL && (t_colors <= 1
9146#ifdef FEAT_GUI
9147 || gui.in_use
9148#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009149#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009150 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009151 || (!p_tgc && cterm_normal_bg_color == 0)
9152#else
9153 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009154#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009155 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156}
9157
9158/*
9159 * Reset cursor position. Use whenever cursor was moved because of outputting
9160 * something directly to the screen (shell commands) or a terminal control
9161 * code.
9162 */
9163 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009164screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165{
9166 screen_cur_row = screen_cur_col = 9999;
9167}
9168
9169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 * Move the cursor to position "row","col" in the screen.
9171 * This tries to find the most efficient way to move, minimizing the number of
9172 * characters sent to the terminal.
9173 */
9174 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009175windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009177 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 int i;
9179 int plan;
9180 int cost;
9181 int wouldbe_col;
9182 int noinvcurs;
9183 char_u *bs;
9184 int goto_cost;
9185 int attr;
9186
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009187#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9189
9190#define PLAN_LE 1
9191#define PLAN_CR 2
9192#define PLAN_NL 3
9193#define PLAN_WRITE 4
9194 /* Can't use ScreenLines unless initialized */
9195 if (ScreenLines == NULL)
9196 return;
9197
9198 if (col != screen_cur_col || row != screen_cur_row)
9199 {
9200 /* Check for valid position. */
9201 if (row < 0) /* window without text lines? */
9202 row = 0;
9203 if (row >= screen_Rows)
9204 row = screen_Rows - 1;
9205 if (col >= screen_Columns)
9206 col = screen_Columns - 1;
9207
9208 /* check if no cursor movement is allowed in highlight mode */
9209 if (screen_attr && *T_MS == NUL)
9210 noinvcurs = HIGHL_COST;
9211 else
9212 noinvcurs = 0;
9213 goto_cost = GOTO_COST + noinvcurs;
9214
9215 /*
9216 * Plan how to do the positioning:
9217 * 1. Use CR to move it to column 0, same row.
9218 * 2. Use T_LE to move it a few columns to the left.
9219 * 3. Use NL to move a few lines down, column 0.
9220 * 4. Move a few columns to the right with T_ND or by writing chars.
9221 *
9222 * Don't do this if the cursor went beyond the last column, the cursor
9223 * position is unknown then (some terminals wrap, some don't )
9224 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009225 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 * characters to move the cursor to the right.
9227 */
9228 if (row >= screen_cur_row && screen_cur_col < Columns)
9229 {
9230 /*
9231 * If the cursor is in the same row, bigger col, we can use CR
9232 * or T_LE.
9233 */
9234 bs = NULL; /* init for GCC */
9235 attr = screen_attr;
9236 if (row == screen_cur_row && col < screen_cur_col)
9237 {
9238 /* "le" is preferred over "bc", because "bc" is obsolete */
9239 if (*T_LE)
9240 bs = T_LE; /* "cursor left" */
9241 else
9242 bs = T_BC; /* "backspace character (old) */
9243 if (*bs)
9244 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9245 else
9246 cost = 999;
9247 if (col + 1 < cost) /* using CR is less characters */
9248 {
9249 plan = PLAN_CR;
9250 wouldbe_col = 0;
9251 cost = 1; /* CR is just one character */
9252 }
9253 else
9254 {
9255 plan = PLAN_LE;
9256 wouldbe_col = col;
9257 }
9258 if (noinvcurs) /* will stop highlighting */
9259 {
9260 cost += noinvcurs;
9261 attr = 0;
9262 }
9263 }
9264
9265 /*
9266 * If the cursor is above where we want to be, we can use CR LF.
9267 */
9268 else if (row > screen_cur_row)
9269 {
9270 plan = PLAN_NL;
9271 wouldbe_col = 0;
9272 cost = (row - screen_cur_row) * 2; /* CR LF */
9273 if (noinvcurs) /* will stop highlighting */
9274 {
9275 cost += noinvcurs;
9276 attr = 0;
9277 }
9278 }
9279
9280 /*
9281 * If the cursor is in the same row, smaller col, just use write.
9282 */
9283 else
9284 {
9285 plan = PLAN_WRITE;
9286 wouldbe_col = screen_cur_col;
9287 cost = 0;
9288 }
9289
9290 /*
9291 * Check if any characters that need to be written have the
9292 * correct attributes. Also avoid UTF-8 characters.
9293 */
9294 i = col - wouldbe_col;
9295 if (i > 0)
9296 cost += i;
9297 if (cost < goto_cost && i > 0)
9298 {
9299 /*
9300 * Check if the attributes are correct without additionally
9301 * stopping highlighting.
9302 */
9303 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9304 while (i && *p++ == attr)
9305 --i;
9306 if (i != 0)
9307 {
9308 /*
9309 * Try if it works when highlighting is stopped here.
9310 */
9311 if (*--p == 0)
9312 {
9313 cost += noinvcurs;
9314 while (i && *p++ == 0)
9315 --i;
9316 }
9317 if (i != 0)
9318 cost = 999; /* different attributes, don't do it */
9319 }
9320#ifdef FEAT_MBYTE
9321 if (enc_utf8)
9322 {
9323 /* Don't use an UTF-8 char for positioning, it's slow. */
9324 for (i = wouldbe_col; i < col; ++i)
9325 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9326 {
9327 cost = 999;
9328 break;
9329 }
9330 }
9331#endif
9332 }
9333
9334 /*
9335 * We can do it without term_windgoto()!
9336 */
9337 if (cost < goto_cost)
9338 {
9339 if (plan == PLAN_LE)
9340 {
9341 if (noinvcurs)
9342 screen_stop_highlight();
9343 while (screen_cur_col > col)
9344 {
9345 out_str(bs);
9346 --screen_cur_col;
9347 }
9348 }
9349 else if (plan == PLAN_CR)
9350 {
9351 if (noinvcurs)
9352 screen_stop_highlight();
9353 out_char('\r');
9354 screen_cur_col = 0;
9355 }
9356 else if (plan == PLAN_NL)
9357 {
9358 if (noinvcurs)
9359 screen_stop_highlight();
9360 while (screen_cur_row < row)
9361 {
9362 out_char('\n');
9363 ++screen_cur_row;
9364 }
9365 screen_cur_col = 0;
9366 }
9367
9368 i = col - screen_cur_col;
9369 if (i > 0)
9370 {
9371 /*
9372 * Use cursor-right if it's one character only. Avoids
9373 * removing a line of pixels from the last bold char, when
9374 * using the bold trick in the GUI.
9375 */
9376 if (T_ND[0] != NUL && T_ND[1] == NUL)
9377 {
9378 while (i-- > 0)
9379 out_char(*T_ND);
9380 }
9381 else
9382 {
9383 int off;
9384
9385 off = LineOffset[row] + screen_cur_col;
9386 while (i-- > 0)
9387 {
9388 if (ScreenAttrs[off] != screen_attr)
9389 screen_stop_highlight();
9390#ifdef FEAT_MBYTE
9391 out_flush_check();
9392#endif
9393 out_char(ScreenLines[off]);
9394#ifdef FEAT_MBYTE
9395 if (enc_dbcs == DBCS_JPNU
9396 && ScreenLines[off] == 0x8e)
9397 out_char(ScreenLines2[off]);
9398#endif
9399 ++off;
9400 }
9401 }
9402 }
9403 }
9404 }
9405 else
9406 cost = 999;
9407
9408 if (cost >= goto_cost)
9409 {
9410 if (noinvcurs)
9411 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009412 if (row == screen_cur_row && (col > screen_cur_col)
9413 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 term_cursor_right(col - screen_cur_col);
9415 else
9416 term_windgoto(row, col);
9417 }
9418 screen_cur_row = row;
9419 screen_cur_col = col;
9420 }
9421}
9422
9423/*
9424 * Set cursor to its position in the current window.
9425 */
9426 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009427setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428{
9429 if (redrawing())
9430 {
9431 validate_cursor();
9432 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009433 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009435 /* With 'rightleft' set and the cursor on a double-wide
9436 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009437 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009439 (has_mbyte
9440 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9441 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442# endif
9443 1)) :
9444#endif
9445 curwin->w_wcol));
9446 }
9447}
9448
9449
9450/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009451 * Insert 'line_count' lines at 'row' in window 'wp'.
9452 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9453 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 * scrolling.
9455 * Returns FAIL if the lines are not inserted, OK for success.
9456 */
9457 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009458win_ins_lines(
9459 win_T *wp,
9460 int row,
9461 int line_count,
9462 int invalid,
9463 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464{
9465 int did_delete;
9466 int nextrow;
9467 int lastrow;
9468 int retval;
9469
9470 if (invalid)
9471 wp->w_lines_valid = 0;
9472
9473 if (wp->w_height < 5)
9474 return FAIL;
9475
9476 if (line_count > wp->w_height - row)
9477 line_count = wp->w_height - row;
9478
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009479 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 if (retval != MAYBE)
9481 return retval;
9482
9483 /*
9484 * If there is a next window or a status line, we first try to delete the
9485 * lines at the bottom to avoid messing what is after the window.
9486 * If this fails and there are following windows, don't do anything to avoid
9487 * messing up those windows, better just redraw.
9488 */
9489 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 if (wp->w_next != NULL || wp->w_status_height)
9491 {
9492 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009493 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 did_delete = TRUE;
9495 else if (wp->w_next)
9496 return FAIL;
9497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 /*
9499 * if no lines deleted, blank the lines that will end up below the window
9500 */
9501 if (!did_delete)
9502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009505 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 lastrow = nextrow + line_count;
9507 if (lastrow > Rows)
9508 lastrow = Rows;
9509 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009510 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 ' ', ' ', 0);
9512 }
9513
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009514 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 == FAIL)
9516 {
9517 /* deletion will have messed up other windows */
9518 if (did_delete)
9519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 win_rest_invalid(W_NEXT(wp));
9522 }
9523 return FAIL;
9524 }
9525
9526 return OK;
9527}
9528
9529/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009530 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9532 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9533 * scrolling
9534 * Return OK for success, FAIL if the lines are not deleted.
9535 */
9536 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009537win_del_lines(
9538 win_T *wp,
9539 int row,
9540 int line_count,
9541 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009542 int mayclear,
9543 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544{
9545 int retval;
9546
9547 if (invalid)
9548 wp->w_lines_valid = 0;
9549
9550 if (line_count > wp->w_height - row)
9551 line_count = wp->w_height - row;
9552
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009553 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 if (retval != MAYBE)
9555 return retval;
9556
9557 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009558 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 return FAIL;
9560
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 /*
9562 * If there are windows or status lines below, try to put them at the
9563 * correct place. If we can't do that, they have to be redrawn.
9564 */
9565 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9566 {
9567 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009568 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 {
9570 wp->w_redr_status = TRUE;
9571 win_rest_invalid(wp->w_next);
9572 }
9573 }
9574 /*
9575 * If this is the last window and there is no status line, redraw the
9576 * command line later.
9577 */
9578 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 redraw_cmdline = TRUE;
9580 return OK;
9581}
9582
9583/*
9584 * Common code for win_ins_lines() and win_del_lines().
9585 * Returns OK or FAIL when the work has been done.
9586 * Returns MAYBE when not finished yet.
9587 */
9588 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009589win_do_lines(
9590 win_T *wp,
9591 int row,
9592 int line_count,
9593 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009594 int del,
9595 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
9597 int retval;
9598
9599 if (!redrawing() || line_count <= 0)
9600 return FAIL;
9601
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009602 /* When inserting lines would result in loss of command output, just redraw
9603 * the lines. */
9604 if (no_win_do_lines_ins && !del)
9605 return FAIL;
9606
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009608 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009610 if (!no_win_do_lines_ins)
9611 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 return FAIL;
9613 }
9614
9615 /*
9616 * Delete all remaining lines
9617 */
9618 if (row + line_count >= wp->w_height)
9619 {
9620 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009621 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 ' ', ' ', 0);
9623 return OK;
9624 }
9625
9626 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009627 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009629 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009631 if (!no_win_do_lines_ins)
9632 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633
9634 /*
9635 * If the terminal can set a scroll region, use that.
9636 * Always do this in a vertically split window. This will redraw from
9637 * ScreenLines[] when t_CV isn't defined. That's faster than using
9638 * win_line().
9639 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009640 * a character in the lower right corner of the scroll region may cause a
9641 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009643 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 scroll_region_set(wp, row);
9647 if (del)
9648 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009649 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 else
9651 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009652 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 scroll_region_reset();
9655 return retval;
9656 }
9657
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9659 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660
9661 return MAYBE;
9662}
9663
9664/*
9665 * window 'wp' and everything after it is messed up, mark it for redraw
9666 */
9667 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009668win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 {
9672 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 wp->w_redr_status = TRUE;
9674 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 }
9676 redraw_cmdline = TRUE;
9677}
9678
9679/*
9680 * The rest of the routines in this file perform screen manipulations. The
9681 * given operation is performed physically on the screen. The corresponding
9682 * change is also made to the internal screen image. In this way, the editor
9683 * anticipates the effect of editing changes on the appearance of the screen.
9684 * That way, when we call screenupdate a complete redraw isn't usually
9685 * necessary. Another advantage is that we can keep adding code to anticipate
9686 * screen changes, and in the meantime, everything still works.
9687 */
9688
9689/*
9690 * types for inserting or deleting lines
9691 */
9692#define USE_T_CAL 1
9693#define USE_T_CDL 2
9694#define USE_T_AL 3
9695#define USE_T_CE 4
9696#define USE_T_DL 5
9697#define USE_T_SR 6
9698#define USE_NL 7
9699#define USE_T_CD 8
9700#define USE_REDRAW 9
9701
9702/*
9703 * insert lines on the screen and update ScreenLines[]
9704 * 'end' is the line after the scrolled part. Normally it is Rows.
9705 * When scrolling region used 'off' is the offset from the top for the region.
9706 * 'row' and 'end' are relative to the start of the region.
9707 *
9708 * return FAIL for failure, OK for success.
9709 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009710 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009711screen_ins_lines(
9712 int off,
9713 int row,
9714 int line_count,
9715 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009716 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009717 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
9719 int i;
9720 int j;
9721 unsigned temp;
9722 int cursor_row;
9723 int type;
9724 int result_empty;
9725 int can_ce = can_clear(T_CE);
9726
9727 /*
9728 * FAIL if
9729 * - there is no valid screen
9730 * - the screen has to be redrawn completely
9731 * - the line count is less than one
9732 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009733 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009735 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9736#ifdef FEAT_CLIPBOARD
9737 || (clip_star.state != SELECT_CLEARED
9738 && redrawing_for_callback > 0)
9739#endif
9740 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 return FAIL;
9742
9743 /*
9744 * There are seven ways to insert lines:
9745 * 0. When in a vertically split window and t_CV isn't set, redraw the
9746 * characters from ScreenLines[].
9747 * 1. Use T_CD (clear to end of display) if it exists and the result of
9748 * the insert is just empty lines
9749 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9750 * present or line_count > 1. It looks better if we do all the inserts
9751 * at once.
9752 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9753 * insert is just empty lines and T_CE is not present or line_count >
9754 * 1.
9755 * 4. Use T_AL (insert line) if it exists.
9756 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9757 * just empty lines.
9758 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9759 * just empty lines.
9760 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9761 * the 'da' flag is not set or we have clear line capability.
9762 * 8. redraw the characters from ScreenLines[].
9763 *
9764 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9765 * the scrollbar for the window. It does have insert line, use that if it
9766 * exists.
9767 */
9768 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9770 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009771 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 type = USE_T_CD;
9773 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9774 type = USE_T_CAL;
9775 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9776 type = USE_T_CDL;
9777 else if (*T_AL != NUL)
9778 type = USE_T_AL;
9779 else if (can_ce && result_empty)
9780 type = USE_T_CE;
9781 else if (*T_DL != NUL && result_empty)
9782 type = USE_T_DL;
9783 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9784 type = USE_T_SR;
9785 else
9786 return FAIL;
9787
9788 /*
9789 * For clearing the lines screen_del_lines() is used. This will also take
9790 * care of t_db if necessary.
9791 */
9792 if (type == USE_T_CD || type == USE_T_CDL ||
9793 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009794 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795
9796 /*
9797 * If text is retained below the screen, first clear or delete as many
9798 * lines at the bottom of the window as are about to be inserted so that
9799 * the deleted lines won't later surface during a screen_del_lines.
9800 */
9801 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009802 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803
9804#ifdef FEAT_CLIPBOARD
9805 /* Remove a modeless selection when inserting lines halfway the screen
9806 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009807 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009808 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 else
9810 clip_scroll_selection(-line_count);
9811#endif
9812
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813#ifdef FEAT_GUI
9814 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9815 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009816 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817#endif
9818
9819 if (*T_CCS != NUL) /* cursor relative to region */
9820 cursor_row = row;
9821 else
9822 cursor_row = row + off;
9823
9824 /*
9825 * Shift LineOffset[] line_count down to reflect the inserted lines.
9826 * Clear the inserted lines in ScreenLines[].
9827 */
9828 row += off;
9829 end += off;
9830 for (i = 0; i < line_count; ++i)
9831 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 if (wp != NULL && wp->w_width != Columns)
9833 {
9834 /* need to copy part of a line */
9835 j = end - 1 - i;
9836 while ((j -= line_count) >= row)
9837 linecopy(j + line_count, j, wp);
9838 j += line_count;
9839 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009840 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9841 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 else
9843 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9844 LineWraps[j] = FALSE;
9845 }
9846 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 {
9848 j = end - 1 - i;
9849 temp = LineOffset[j];
9850 while ((j -= line_count) >= row)
9851 {
9852 LineOffset[j + line_count] = LineOffset[j];
9853 LineWraps[j + line_count] = LineWraps[j];
9854 }
9855 LineOffset[j + line_count] = temp;
9856 LineWraps[j + line_count] = FALSE;
9857 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009858 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 else
9860 lineinvalid(temp, (int)Columns);
9861 }
9862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863
9864 screen_stop_highlight();
9865 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009866 if (clear_attr != 0)
9867 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 /* redraw the characters */
9870 if (type == USE_REDRAW)
9871 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009872 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 {
9874 term_append_lines(line_count);
9875 screen_start(); /* don't know where cursor is now */
9876 }
9877 else
9878 {
9879 for (i = 0; i < line_count; i++)
9880 {
9881 if (type == USE_T_AL)
9882 {
9883 if (i && cursor_row != 0)
9884 windgoto(cursor_row, 0);
9885 out_str(T_AL);
9886 }
9887 else /* type == USE_T_SR */
9888 out_str(T_SR);
9889 screen_start(); /* don't know where cursor is now */
9890 }
9891 }
9892
9893 /*
9894 * With scroll-reverse and 'da' flag set we need to clear the lines that
9895 * have been scrolled down into the region.
9896 */
9897 if (type == USE_T_SR && *T_DA)
9898 {
9899 for (i = 0; i < line_count; ++i)
9900 {
9901 windgoto(off + i, 0);
9902 out_str(T_CE);
9903 screen_start(); /* don't know where cursor is now */
9904 }
9905 }
9906
9907#ifdef FEAT_GUI
9908 gui_can_update_cursor();
9909 if (gui.in_use)
9910 out_flush(); /* always flush after a scroll */
9911#endif
9912 return OK;
9913}
9914
9915/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009916 * Delete lines on the screen and update ScreenLines[].
9917 * "end" is the line after the scrolled part. Normally it is Rows.
9918 * When scrolling region used "off" is the offset from the top for the region.
9919 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 *
9921 * Return OK for success, FAIL if the lines are not deleted.
9922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009924screen_del_lines(
9925 int off,
9926 int row,
9927 int line_count,
9928 int end,
9929 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009930 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009931 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932{
9933 int j;
9934 int i;
9935 unsigned temp;
9936 int cursor_row;
9937 int cursor_end;
9938 int result_empty; /* result is empty until end of region */
9939 int can_delete; /* deleting line codes can be used */
9940 int type;
9941
9942 /*
9943 * FAIL if
9944 * - there is no valid screen
9945 * - the screen has to be redrawn completely
9946 * - the line count is less than one
9947 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009948 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009950 if (!screen_valid(TRUE) || line_count <= 0
9951 || (!force && line_count > p_ttyscroll)
9952#ifdef FEAT_CLIPBOARD
9953 || (clip_star.state != SELECT_CLEARED
9954 && redrawing_for_callback > 0)
9955#endif
9956 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 return FAIL;
9958
9959 /*
9960 * Check if the rest of the current region will become empty.
9961 */
9962 result_empty = row + line_count >= end;
9963
9964 /*
9965 * We can delete lines only when 'db' flag not set or when 'ce' option
9966 * available.
9967 */
9968 can_delete = (*T_DB == NUL || can_clear(T_CE));
9969
9970 /*
9971 * There are six ways to delete lines:
9972 * 0. When in a vertically split window and t_CV isn't set, redraw the
9973 * characters from ScreenLines[].
9974 * 1. Use T_CD if it exists and the result is empty.
9975 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9976 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9977 * none of the other ways work.
9978 * 4. Use T_CE (erase line) if the result is empty.
9979 * 5. Use T_DL (delete line) if it exists.
9980 * 6. redraw the characters from ScreenLines[].
9981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9983 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009984 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 type = USE_T_CD;
9986#if defined(__BEOS__) && defined(BEOS_DR8)
9987 /*
9988 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9989 * its internal termcap... this works okay for tests which test *T_DB !=
9990 * NUL. It has the disadvantage that the user cannot use any :set t_*
9991 * command to get T_DB (back) to empty_option, only :set term=... will do
9992 * the trick...
9993 * Anyway, this hack will hopefully go away with the next OS release.
9994 * (Olaf Seibert)
9995 */
9996 else if (row == 0 && T_DB == empty_option
9997 && (line_count == 1 || *T_CDL == NUL))
9998#else
9999 else if (row == 0 && (
10000#ifndef AMIGA
10001 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10002 * up, so use delete-line command */
10003 line_count == 1 ||
10004#endif
10005 *T_CDL == NUL))
10006#endif
10007 type = USE_NL;
10008 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10009 type = USE_T_CDL;
10010 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010011 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 type = USE_T_CE;
10013 else if (*T_DL != NUL && can_delete)
10014 type = USE_T_DL;
10015 else if (*T_CDL != NUL && can_delete)
10016 type = USE_T_CDL;
10017 else
10018 return FAIL;
10019
10020#ifdef FEAT_CLIPBOARD
10021 /* Remove a modeless selection when deleting lines halfway the screen or
10022 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010023 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010024 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 else
10026 clip_scroll_selection(line_count);
10027#endif
10028
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029#ifdef FEAT_GUI
10030 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10031 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010032 gui_dont_update_cursor(gui.cursor_row >= row + off
10033 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034#endif
10035
10036 if (*T_CCS != NUL) /* cursor relative to region */
10037 {
10038 cursor_row = row;
10039 cursor_end = end;
10040 }
10041 else
10042 {
10043 cursor_row = row + off;
10044 cursor_end = end + off;
10045 }
10046
10047 /*
10048 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10049 * Clear the inserted lines in ScreenLines[].
10050 */
10051 row += off;
10052 end += off;
10053 for (i = 0; i < line_count; ++i)
10054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 if (wp != NULL && wp->w_width != Columns)
10056 {
10057 /* need to copy part of a line */
10058 j = row + i;
10059 while ((j += line_count) <= end - 1)
10060 linecopy(j - line_count, j, wp);
10061 j -= line_count;
10062 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010063 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10064 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 else
10066 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10067 LineWraps[j] = FALSE;
10068 }
10069 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 {
10071 /* whole width, moving the line pointers is faster */
10072 j = row + i;
10073 temp = LineOffset[j];
10074 while ((j += line_count) <= end - 1)
10075 {
10076 LineOffset[j - line_count] = LineOffset[j];
10077 LineWraps[j - line_count] = LineWraps[j];
10078 }
10079 LineOffset[j - line_count] = temp;
10080 LineWraps[j - line_count] = FALSE;
10081 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010082 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 else
10084 lineinvalid(temp, (int)Columns);
10085 }
10086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010088 if (screen_attr != clear_attr)
10089 screen_stop_highlight();
10090 if (clear_attr != 0)
10091 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 /* redraw the characters */
10094 if (type == USE_REDRAW)
10095 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010096 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 {
10098 windgoto(cursor_row, 0);
10099 out_str(T_CD);
10100 screen_start(); /* don't know where cursor is now */
10101 }
10102 else if (type == USE_T_CDL)
10103 {
10104 windgoto(cursor_row, 0);
10105 term_delete_lines(line_count);
10106 screen_start(); /* don't know where cursor is now */
10107 }
10108 /*
10109 * Deleting lines at top of the screen or scroll region: Just scroll
10110 * the whole screen (scroll region) up by outputting newlines on the
10111 * last line.
10112 */
10113 else if (type == USE_NL)
10114 {
10115 windgoto(cursor_end - 1, 0);
10116 for (i = line_count; --i >= 0; )
10117 out_char('\n'); /* cursor will remain on same line */
10118 }
10119 else
10120 {
10121 for (i = line_count; --i >= 0; )
10122 {
10123 if (type == USE_T_DL)
10124 {
10125 windgoto(cursor_row, 0);
10126 out_str(T_DL); /* delete a line */
10127 }
10128 else /* type == USE_T_CE */
10129 {
10130 windgoto(cursor_row + i, 0);
10131 out_str(T_CE); /* erase a line */
10132 }
10133 screen_start(); /* don't know where cursor is now */
10134 }
10135 }
10136
10137 /*
10138 * If the 'db' flag is set, we need to clear the lines that have been
10139 * scrolled up at the bottom of the region.
10140 */
10141 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10142 {
10143 for (i = line_count; i > 0; --i)
10144 {
10145 windgoto(cursor_end - i, 0);
10146 out_str(T_CE); /* erase a line */
10147 screen_start(); /* don't know where cursor is now */
10148 }
10149 }
10150
10151#ifdef FEAT_GUI
10152 gui_can_update_cursor();
10153 if (gui.in_use)
10154 out_flush(); /* always flush after a scroll */
10155#endif
10156
10157 return OK;
10158}
10159
10160/*
10161 * show the current mode and ruler
10162 *
10163 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10164 * If clear_cmdline is FALSE there may be a message there that needs to be
10165 * cleared only if a mode is shown.
10166 * Return the length of the message (0 if no message).
10167 */
10168 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010169showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170{
10171 int need_clear;
10172 int length = 0;
10173 int do_mode;
10174 int attr;
10175 int nwr_save;
10176#ifdef FEAT_INS_EXPAND
10177 int sub_attr;
10178#endif
10179
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010180 do_mode = ((p_smd && msg_silent == 0)
10181 && ((State & INSERT)
10182 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010183 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 if (do_mode || Recording)
10185 {
10186 /*
10187 * Don't show mode right now, when not redrawing or inside a mapping.
10188 * Call char_avail() only when we are going to show something, because
10189 * it takes a bit of time.
10190 */
10191 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10192 {
10193 redraw_cmdline = TRUE; /* show mode later */
10194 return 0;
10195 }
10196
10197 nwr_save = need_wait_return;
10198
10199 /* wait a bit before overwriting an important message */
10200 check_for_delay(FALSE);
10201
10202 /* if the cmdline is more than one line high, erase top lines */
10203 need_clear = clear_cmdline;
10204 if (clear_cmdline && cmdline_row < Rows - 1)
10205 msg_clr_cmdline(); /* will reset clear_cmdline */
10206
10207 /* Position on the last line in the window, column 0 */
10208 msg_pos_mode();
10209 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010210 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 if (do_mode)
10212 {
10213 MSG_PUTS_ATTR("--", attr);
10214#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010215 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010216# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010217 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010218# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010219 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010220# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010221 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010222# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 MSG_PUTS_ATTR(" IM", attr);
10224# else
10225 MSG_PUTS_ATTR(" XIM", attr);
10226# endif
10227#endif
10228#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10229 if (gui.in_use)
10230 {
10231 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010232 {
10233 /* HANGUL */
10234 if (enc_utf8)
10235 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10236 else
10237 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 }
10240#endif
10241#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010242 /* CTRL-X in Insert mode */
10243 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 {
10245 /* These messages can get long, avoid a wrap in a narrow
10246 * window. Prefer showing edit_submode_extra. */
10247 length = (Rows - msg_row) * Columns - 3;
10248 if (edit_submode_extra != NULL)
10249 length -= vim_strsize(edit_submode_extra);
10250 if (length > 0)
10251 {
10252 if (edit_submode_pre != NULL)
10253 length -= vim_strsize(edit_submode_pre);
10254 if (length - vim_strsize(edit_submode) > 0)
10255 {
10256 if (edit_submode_pre != NULL)
10257 msg_puts_attr(edit_submode_pre, attr);
10258 msg_puts_attr(edit_submode, attr);
10259 }
10260 if (edit_submode_extra != NULL)
10261 {
10262 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10263 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010264 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 else
10266 sub_attr = attr;
10267 msg_puts_attr(edit_submode_extra, sub_attr);
10268 }
10269 }
10270 length = 0;
10271 }
10272 else
10273#endif
10274 {
10275#ifdef FEAT_VREPLACE
10276 if (State & VREPLACE_FLAG)
10277 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10278 else
10279#endif
10280 if (State & REPLACE_FLAG)
10281 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10282 else if (State & INSERT)
10283 {
10284#ifdef FEAT_RIGHTLEFT
10285 if (p_ri)
10286 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10287#endif
10288 MSG_PUTS_ATTR(_(" INSERT"), attr);
10289 }
10290 else if (restart_edit == 'I')
10291 MSG_PUTS_ATTR(_(" (insert)"), attr);
10292 else if (restart_edit == 'R')
10293 MSG_PUTS_ATTR(_(" (replace)"), attr);
10294 else if (restart_edit == 'V')
10295 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10296#ifdef FEAT_RIGHTLEFT
10297 if (p_hkmap)
10298 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10299# ifdef FEAT_FKMAP
10300 if (p_fkmap)
10301 MSG_PUTS_ATTR(farsi_text_5, attr);
10302# endif
10303#endif
10304#ifdef FEAT_KEYMAP
10305 if (State & LANGMAP)
10306 {
10307# ifdef FEAT_ARABIC
10308 if (curwin->w_p_arab)
10309 MSG_PUTS_ATTR(_(" Arabic"), attr);
10310 else
10311# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010312 if (get_keymap_str(curwin, (char_u *)" (%s)",
10313 NameBuff, MAXPATHL))
10314 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 }
10316#endif
10317 if ((State & INSERT) && p_paste)
10318 MSG_PUTS_ATTR(_(" (paste)"), attr);
10319
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 if (VIsual_active)
10321 {
10322 char *p;
10323
10324 /* Don't concatenate separate words to avoid translation
10325 * problems. */
10326 switch ((VIsual_select ? 4 : 0)
10327 + (VIsual_mode == Ctrl_V) * 2
10328 + (VIsual_mode == 'V'))
10329 {
10330 case 0: p = N_(" VISUAL"); break;
10331 case 1: p = N_(" VISUAL LINE"); break;
10332 case 2: p = N_(" VISUAL BLOCK"); break;
10333 case 4: p = N_(" SELECT"); break;
10334 case 5: p = N_(" SELECT LINE"); break;
10335 default: p = N_(" SELECT BLOCK"); break;
10336 }
10337 MSG_PUTS_ATTR(_(p), attr);
10338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 MSG_PUTS_ATTR(" --", attr);
10340 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010341
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 need_clear = TRUE;
10343 }
10344 if (Recording
10345#ifdef FEAT_INS_EXPAND
10346 && edit_submode == NULL /* otherwise it gets too long */
10347#endif
10348 )
10349 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010350 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 need_clear = TRUE;
10352 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010353
10354 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 if (need_clear || clear_cmdline)
10356 msg_clr_eos();
10357 msg_didout = FALSE; /* overwrite this message */
10358 length = msg_col;
10359 msg_col = 0;
10360 need_wait_return = nwr_save; /* never ask for hit-return for this */
10361 }
10362 else if (clear_cmdline && msg_silent == 0)
10363 /* Clear the whole command line. Will reset "clear_cmdline". */
10364 msg_clr_cmdline();
10365
10366#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 /* In Visual mode the size of the selected area must be redrawn. */
10368 if (VIsual_active)
10369 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370
10371 /* If the last window has no status line, the ruler is after the mode
10372 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010373 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 win_redr_ruler(lastwin, TRUE);
10375#endif
10376 redraw_cmdline = FALSE;
10377 clear_cmdline = FALSE;
10378
10379 return length;
10380}
10381
10382/*
10383 * Position for a mode message.
10384 */
10385 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010386msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387{
10388 msg_col = 0;
10389 msg_row = Rows - 1;
10390}
10391
10392/*
10393 * Delete mode message. Used when ESC is typed which is expected to end
10394 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010395 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 */
10397 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010398unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399{
10400 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010401 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 */
10403 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10404 redraw_cmdline = TRUE; /* delete mode later */
10405 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010406 clearmode();
10407}
10408
10409/*
10410 * Clear the mode message.
10411 */
10412 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010413clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010414{
10415 msg_pos_mode();
10416 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010417 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010418 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419}
10420
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010421 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010422recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010423{
10424 MSG_PUTS_ATTR(_("recording"), attr);
10425 if (!shortmess(SHM_RECORDING))
10426 {
10427 char_u s[4];
10428 sprintf((char *)s, " @%c", Recording);
10429 MSG_PUTS_ATTR(s, attr);
10430 }
10431}
10432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010433/*
10434 * Draw the tab pages line at the top of the Vim window.
10435 */
10436 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010437draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010438{
10439 int tabcount = 0;
10440 tabpage_T *tp;
10441 int tabwidth;
10442 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010443 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010444 int attr;
10445 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010446 win_T *cwp;
10447 int wincount;
10448 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010449 int c;
10450 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010451 int attr_sel = HL_ATTR(HLF_TPS);
10452 int attr_nosel = HL_ATTR(HLF_TP);
10453 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010454 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010455 int room;
10456 int use_sep_chars = (t_colors < 8
10457#ifdef FEAT_GUI
10458 && !gui.in_use
10459#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010460#ifdef FEAT_TERMGUICOLORS
10461 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010462#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010463 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010464
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010465 if (ScreenLines == NULL)
10466 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010467 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010468
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010469#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010470 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010471 if (gui_use_tabline())
10472 {
10473 gui_update_tabline();
10474 return;
10475 }
10476#endif
10477
10478 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010479 return;
10480
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010481#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010482
10483 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10484 for (scol = 0; scol < Columns; ++scol)
10485 TabPageIdxs[scol] = 0;
10486
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010487 /* Use the 'tabline' option if it's set. */
10488 if (*p_tal != NUL)
10489 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010490 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010491
10492 /* Check for an error. If there is one we would loop in redrawing the
10493 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010494 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010495 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010496 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010497 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010498 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010499 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010500 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010501 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010502#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010503 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010504 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010505 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010506
Bram Moolenaar238a5642006-02-21 22:12:05 +000010507 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10508 if (tabwidth < 6)
10509 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010510
Bram Moolenaar238a5642006-02-21 22:12:05 +000010511 attr = attr_nosel;
10512 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010513 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010514 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10515 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010516 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010517 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010518
Bram Moolenaar238a5642006-02-21 22:12:05 +000010519 if (tp->tp_topframe == topframe)
10520 attr = attr_sel;
10521 if (use_sep_chars && col > 0)
10522 screen_putchar('|', 0, col++, attr);
10523
10524 if (tp->tp_topframe != topframe)
10525 attr = attr_nosel;
10526
10527 screen_putchar(' ', 0, col++, attr);
10528
10529 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010530 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010531 cwp = curwin;
10532 wp = firstwin;
10533 }
10534 else
10535 {
10536 cwp = tp->tp_curwin;
10537 wp = tp->tp_firstwin;
10538 }
10539
10540 modified = FALSE;
10541 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10542 if (bufIsChanged(wp->w_buffer))
10543 modified = TRUE;
10544 if (modified || wincount > 1)
10545 {
10546 if (wincount > 1)
10547 {
10548 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010549 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010550 if (col + len >= Columns - 3)
10551 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010552 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010553#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010554 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010555#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010556 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010557#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010558 );
10559 col += len;
10560 }
10561 if (modified)
10562 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10563 screen_putchar(' ', 0, col++, attr);
10564 }
10565
10566 room = scol - col + tabwidth - 1;
10567 if (room > 0)
10568 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010569 /* Get buffer name in NameBuff[] */
10570 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010571 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010572 len = vim_strsize(NameBuff);
10573 p = NameBuff;
10574#ifdef FEAT_MBYTE
10575 if (has_mbyte)
10576 while (len > room)
10577 {
10578 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010579 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010580 }
10581 else
10582#endif
10583 if (len > room)
10584 {
10585 p += len - room;
10586 len = room;
10587 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010588 if (len > Columns - col - 1)
10589 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010590
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010591 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010592 col += len;
10593 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010594 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010595
10596 /* Store the tab page number in TabPageIdxs[], so that
10597 * jump_to_mouse() knows where each one is. */
10598 ++tabcount;
10599 while (scol < col)
10600 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010601 }
10602
Bram Moolenaar238a5642006-02-21 22:12:05 +000010603 if (use_sep_chars)
10604 c = '_';
10605 else
10606 c = ' ';
10607 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010608
10609 /* Put an "X" for closing the current tab if there are several. */
10610 if (first_tabpage->tp_next != NULL)
10611 {
10612 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10613 TabPageIdxs[Columns - 1] = -999;
10614 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010615 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010616
10617 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10618 * set. */
10619 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010620}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010621
10622/*
10623 * Get buffer name for "buf" into NameBuff[].
10624 * Takes care of special buffer names and translates special characters.
10625 */
10626 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010627get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010628{
10629 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010630 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010631 else
10632 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10633 trans_characters(NameBuff, MAXPATHL);
10634}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010635
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636/*
10637 * Get the character to use in a status line. Get its attributes in "*attr".
10638 */
10639 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010640fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641{
10642 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010643
10644#ifdef FEAT_TERMINAL
10645 if (bt_terminal(wp->w_buffer))
10646 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010647 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010648 {
10649 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010650 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010651 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010652 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010653 {
10654 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010655 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010656 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010657 }
10658 else
10659#endif
10660 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010662 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 fill = fill_stl;
10664 }
10665 else
10666 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010667 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 fill = fill_stlnc;
10669 }
10670 /* Use fill when there is highlighting, and highlighting of current
10671 * window differs, or the fillchars differ, or this is not the
10672 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010673 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010674 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 || (fill_stl != fill_stlnc)))
10676 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010677 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 return '^';
10679 return '=';
10680}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682/*
10683 * Get the character to use in a separator between vertically split windows.
10684 * Get its attributes in "*attr".
10685 */
10686 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010687fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010689 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 if (*attr == 0 && fill_vert == ' ')
10691 return '|';
10692 else
10693 return fill_vert;
10694}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695
10696/*
10697 * Return TRUE if redrawing should currently be done.
10698 */
10699 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010700redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010702#ifdef FEAT_EVAL
10703 if (disable_redraw_for_testing)
10704 return 0;
10705 else
10706#endif
10707 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10709}
10710
10711/*
10712 * Return TRUE if printing messages should currently be done.
10713 */
10714 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010715messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716{
10717 return (!(p_lz && char_avail() && !KeyTyped));
10718}
10719
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010720#ifdef FEAT_MENU
10721/*
10722 * Draw the window toolbar.
10723 */
10724 static void
10725redraw_win_toolbar(win_T *wp)
10726{
10727 vimmenu_T *menu;
10728 int item_idx = 0;
10729 int item_count = 0;
10730 int col = 0;
10731 int next_col;
10732 int off = (int)(current_ScreenLine - ScreenLines);
10733 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10734 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10735
10736 vim_free(wp->w_winbar_items);
10737 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10738 ++item_count;
10739 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10740 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10741
10742 /* TODO: use fewer spaces if there is not enough room */
10743 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010744 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010745 {
10746 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010747 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010748 break;
10749 if (col > 1)
10750 {
10751 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010752 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010753 break;
10754 }
10755
10756 wp->w_winbar_items[item_idx].wb_startcol = col;
10757 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010758 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010759 break;
10760
10761 next_col = text_to_screenline(wp, menu->name, col);
10762 while (col < next_col)
10763 {
10764 ScreenAttrs[off + col] = button_attr;
10765 ++col;
10766 }
10767 wp->w_winbar_items[item_idx].wb_endcol = col;
10768 wp->w_winbar_items[item_idx].wb_menu = menu;
10769 ++item_idx;
10770
Bram Moolenaar02631462017-09-22 15:20:32 +020010771 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010772 break;
10773 space_to_screenline(off + col, button_attr);
10774 ++col;
10775 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010776 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010777 {
10778 space_to_screenline(off + col, fill_attr);
10779 ++col;
10780 }
10781 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10782
Bram Moolenaar02631462017-09-22 15:20:32 +020010783 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10784 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010785}
10786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787/*
10788 * Show current status info in ruler and various other places
10789 * If always is FALSE, only show ruler if position has changed.
10790 */
10791 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010792showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793{
10794 if (!always && !redrawing())
10795 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010796#ifdef FEAT_INS_EXPAND
10797 if (pum_visible())
10798 {
10799 /* Don't redraw right now, do it later. */
10800 curwin->w_redr_status = TRUE;
10801 return;
10802 }
10803#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010804#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010805 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010806 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010807 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 else
10810#endif
10811#ifdef FEAT_CMDL_INFO
10812 win_redr_ruler(curwin, always);
10813#endif
10814
10815#ifdef FEAT_TITLE
10816 if (need_maketitle
10817# ifdef FEAT_STL_OPT
10818 || (p_icon && (stl_syntax & STL_IN_ICON))
10819 || (p_title && (stl_syntax & STL_IN_TITLE))
10820# endif
10821 )
10822 maketitle();
10823#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010824 /* Redraw the tab pages line if needed. */
10825 if (redraw_tabline)
10826 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827}
10828
10829#ifdef FEAT_CMDL_INFO
10830 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010831win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010833#define RULER_BUF_LEN 70
10834 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 int row;
10836 int fillchar;
10837 int attr;
10838 int empty_line = FALSE;
10839 colnr_T virtcol;
10840 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010841 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 int this_ru_col;
10844 int off = 0;
10845 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846
10847 /* If 'ruler' off or redrawing disabled, don't do anything */
10848 if (!p_ru)
10849 return;
10850
10851 /*
10852 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10853 * after deleting lines, before cursor.lnum is corrected.
10854 */
10855 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10856 return;
10857
10858#ifdef FEAT_INS_EXPAND
10859 /* Don't draw the ruler while doing insert-completion, it might overwrite
10860 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 if (edit_submode != NULL)
10863 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010864 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10865 if (pum_visible())
10866 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867#endif
10868
10869#ifdef FEAT_STL_OPT
10870 if (*p_ruf)
10871 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010872 int save_called_emsg = called_emsg;
10873
10874 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010876 if (called_emsg)
10877 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010878 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010879 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 return;
10881 }
10882#endif
10883
10884 /*
10885 * Check if not in Insert mode and the line is empty (will show "0-1").
10886 */
10887 if (!(State & INSERT)
10888 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10889 empty_line = TRUE;
10890
10891 /*
10892 * Only draw the ruler when something changed.
10893 */
10894 validate_virtcol_win(wp);
10895 if ( redraw_cmdline
10896 || always
10897 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10898 || wp->w_cursor.col != wp->w_ru_cursor.col
10899 || wp->w_virtcol != wp->w_ru_virtcol
10900#ifdef FEAT_VIRTUALEDIT
10901 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10902#endif
10903 || wp->w_topline != wp->w_ru_topline
10904 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10905#ifdef FEAT_DIFF
10906 || wp->w_topfill != wp->w_ru_topfill
10907#endif
10908 || empty_line != wp->w_ru_empty)
10909 {
10910 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 if (wp->w_status_height)
10912 {
10913 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010914 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010915 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010916 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 }
10918 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 {
10920 row = Rows - 1;
10921 fillchar = ' ';
10922 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 width = Columns;
10924 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 }
10926
10927 /* In list mode virtcol needs to be recomputed */
10928 virtcol = wp->w_virtcol;
10929 if (wp->w_p_list && lcs_tab1 == NUL)
10930 {
10931 wp->w_p_list = FALSE;
10932 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10933 wp->w_p_list = TRUE;
10934 }
10935
10936 /*
10937 * Some sprintfs return the length, some return a pointer.
10938 * To avoid portability problems we use strlen() here.
10939 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010940 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10942 ? 0L
10943 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010944 len = STRLEN(buffer);
10945 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10947 (int)virtcol + 1);
10948
10949 /*
10950 * Add a "50%" if there is room for it.
10951 * On the last line, don't print in the last column (scrolls the
10952 * screen up on some terminals).
10953 */
10954 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010955 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 this_ru_col = ru_col - (Columns - width);
10960 if (this_ru_col < 0)
10961 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 /* Never use more than half the window/screen width, leave the other
10963 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010964 if (this_ru_col < (width + 1) / 2)
10965 this_ru_col = (width + 1) / 2;
10966 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010968 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010969 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 {
10971#ifdef FEAT_MBYTE
10972 if (has_mbyte)
10973 i += (*mb_char2bytes)(fillchar, buffer + i);
10974 else
10975#endif
10976 buffer[i++] = fillchar;
10977 ++o;
10978 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010979 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 }
10981 /* Truncate at window boundary. */
10982#ifdef FEAT_MBYTE
10983 if (has_mbyte)
10984 {
10985 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010986 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 {
10988 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010989 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 {
10991 buffer[i] = NUL;
10992 break;
10993 }
10994 }
10995 }
10996 else
10997#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010998 if (this_ru_col + (int)STRLEN(buffer) > width)
10999 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000
Bram Moolenaar4033c552017-09-16 20:54:51 +020011001 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 i = redraw_cmdline;
11003 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011004 this_ru_col + off + (int)STRLEN(buffer),
11005 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 fillchar, fillchar, attr);
11007 /* don't redraw the cmdline because of showing the ruler */
11008 redraw_cmdline = i;
11009 wp->w_ru_cursor = wp->w_cursor;
11010 wp->w_ru_virtcol = wp->w_virtcol;
11011 wp->w_ru_empty = empty_line;
11012 wp->w_ru_topline = wp->w_topline;
11013 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11014#ifdef FEAT_DIFF
11015 wp->w_ru_topfill = wp->w_topfill;
11016#endif
11017 }
11018}
11019#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011020
11021#if defined(FEAT_LINEBREAK) || defined(PROTO)
11022/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011023 * Return the width of the 'number' and 'relativenumber' column.
11024 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011025 * Otherwise it depends on 'numberwidth' and the line count.
11026 */
11027 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011028number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011029{
11030 int n;
11031 linenr_T lnum;
11032
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011033 if (wp->w_p_rnu && !wp->w_p_nu)
11034 /* cursor line shows "0" */
11035 lnum = wp->w_height;
11036 else
11037 /* cursor line shows absolute line number */
11038 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011039
Bram Moolenaar6b314672015-03-20 15:42:10 +010011040 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011041 return wp->w_nrwidth_width;
11042 wp->w_nrwidth_line_count = lnum;
11043
11044 n = 0;
11045 do
11046 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011047 lnum /= 10;
11048 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011049 } while (lnum > 0);
11050
11051 /* 'numberwidth' gives the minimal width plus one */
11052 if (n < wp->w_p_nuw - 1)
11053 n = wp->w_p_nuw - 1;
11054
11055 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011056 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011057 return n;
11058}
11059#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011060
11061/*
11062 * Return the current cursor column. This is the actual position on the
11063 * screen. First column is 0.
11064 */
11065 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011066screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011067{
11068 return screen_cur_col;
11069}
11070
11071/*
11072 * Return the current cursor row. This is the actual position on the screen.
11073 * First row is 0.
11074 */
11075 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011076screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011077{
11078 return screen_cur_row;
11079}