blob: f365be2231b218f073736e7bd5e558ab85f4b75d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200110#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
111static int text_to_screenline(win_T *wp, char_u *text, int col);
112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_FOLDING
114static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
117
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200118/* Flag that is set when drawing for a callback, not from the main command
119 * loop. */
120static int redrawing_for_callback = 0;
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122/*
123 * Buffer for one screen line (characters and attributes).
124 */
125static schar_T *current_ScreenLine;
126
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void win_update(win_T *wp);
128static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
131static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
132static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200134static int win_line(win_T *, linenr_T, int, int, int nochange);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200141# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void start_search_hl(void);
143static void end_search_hl(void);
144static void init_search_hl(win_T *wp);
145static void prepare_search_hl(win_T *wp, linenr_T lnum);
146static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
147static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screen_start_highlight(int attr);
150static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200155static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void lineinvalid(unsigned off, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void linecopy(int to, int from, win_T *wp);
158static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200159static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void win_rest_invalid(win_T *wp);
161static void msg_pos_mode(void);
162static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200164static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200166#ifdef FEAT_MENU
167static void redraw_win_toolbar(win_T *wp);
168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#endif
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176/* Ugly global: overrule attribute used by screen_char() */
177static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200179#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
180/* Can limit syntax highlight time to 'redrawtime'. */
181# define SYN_TIME_LIMIT 1
182#endif
183
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200184#ifdef FEAT_RIGHTLEFT
185# define HAS_RIGHTLEFT(x) x
186#else
187# define HAS_RIGHTLEFT(x) FALSE
188#endif
189
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190/*
191 * Redraw the current window later, with update_screen(type).
192 * Set must_redraw only if not already set to a higher value.
193 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
194 */
195 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100196redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197{
198 redraw_win_later(curwin, type);
199}
200
201 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100202redraw_win_later(
203 win_T *wp,
204 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200206 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
208 wp->w_redr_type = type;
209 if (type >= NOT_VALID)
210 wp->w_lines_valid = 0;
211 if (must_redraw < type) /* must_redraw is the maximum of all windows */
212 must_redraw = type;
213 }
214}
215
216/*
217 * Force a complete redraw later. Also resets the highlighting. To be used
218 * after executing a shell command that messes up the screen.
219 */
220 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100221redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000224#ifdef FEAT_GUI
225 if (gui.in_use)
226 /* Use a code that will reset gui.highlight_mask in
227 * gui_stop_highlight(). */
228 screen_attr = HL_ALL + 1;
229 else
230#endif
231 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200232 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233}
234
235/*
236 * Mark all windows to be redrawn later.
237 */
238 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100239redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240{
241 win_T *wp;
242
243 FOR_ALL_WINDOWS(wp)
244 {
245 redraw_win_later(wp, type);
246 }
247}
248
249/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000250 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 */
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 redraw_buf_later(curbuf, type);
256}
257
258 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100259redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 win_T *wp;
262
263 FOR_ALL_WINDOWS(wp)
264 {
265 if (wp->w_buffer == buf)
266 redraw_win_later(wp, type);
267 }
268}
269
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200270 void
271redraw_buf_and_status_later(buf_T *buf, int type)
272{
273 win_T *wp;
274
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200275#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200276 if (wild_menu_showing != 0)
277 /* Don't redraw while the command line completion is displayed, it
278 * would disappear. */
279 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200280#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281 FOR_ALL_WINDOWS(wp)
282 {
283 if (wp->w_buffer == buf)
284 {
285 redraw_win_later(wp, type);
286 wp->w_redr_status = TRUE;
287 }
288 }
289}
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200292 * Redraw as soon as possible. When the command line is not scrolled redraw
293 * right away and restore what was on the command line.
294 * Return a code indicating what happened.
295 */
296 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100297redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200298{
299 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200300 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200301 int r;
302 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 schar_T *screenline; /* copy from ScreenLines[] */
304 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200305#ifdef FEAT_MBYTE
306 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200307 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200309 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310#endif
311
312 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 return ret;
315
316 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 if (screenline == NULL || screenattr == NULL)
323 ret = 2;
324#ifdef FEAT_MBYTE
325 if (enc_utf8)
326 {
327 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenlineUC == NULL)
330 ret = 2;
331 for (i = 0; i < p_mco; ++i)
332 {
333 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200334 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (screenlineC[i] == NULL)
336 ret = 2;
337 }
338 }
339 if (enc_dbcs == DBCS_JPNU)
340 {
341 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200342 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenline2 == NULL)
344 ret = 2;
345 }
346#endif
347
348 if (ret != 2)
349 {
350 /* Save the text displayed in the command line area. */
351 for (r = 0; r < rows; ++r)
352 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200353 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200354 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200355 (size_t)cols * sizeof(schar_T));
356 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359#ifdef FEAT_MBYTE
360 if (enc_utf8)
361 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 mch_memmove(screenlineC[i] + r * cols,
367 ScreenLinesC[i] + LineOffset[cmdline_row + r],
368 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200369 }
370 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200373 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374#endif
375 }
376
377 update_screen(0);
378 ret = 3;
379
380 if (must_redraw == 0)
381 {
382 int off = (int)(current_ScreenLine - ScreenLines);
383
384 /* Restore the text displayed in the command line area. */
385 for (r = 0; r < rows; ++r)
386 {
387 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200388 screenline + r * cols,
389 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenattr + r * cols,
392 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393#ifdef FEAT_MBYTE
394 if (enc_utf8)
395 {
396 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenlineUC + r * cols,
398 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 for (i = 0; i < p_mco; ++i)
400 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200401 screenlineC[i] + r * cols,
402 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200403 }
404 if (enc_dbcs == DBCS_JPNU)
405 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200406 screenline2 + r * cols,
407 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200408#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200409 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 }
411 ret = 4;
412 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413 }
414
415 vim_free(screenline);
416 vim_free(screenattr);
417#ifdef FEAT_MBYTE
418 if (enc_utf8)
419 {
420 vim_free(screenlineUC);
421 for (i = 0; i < p_mco; ++i)
422 vim_free(screenlineC[i]);
423 }
424 if (enc_dbcs == DBCS_JPNU)
425 vim_free(screenline2);
426#endif
427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
435
436/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100437 * Invoked after an asynchronous callback is called.
438 * If an echo command was used the cursor needs to be put back where
439 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200440 * If "call_update_screen" is FALSE don't call update_screen() when at the
441 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100442 */
443 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200444redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200446 ++redrawing_for_callback;
447
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100448 if (State == HITRETURN || State == ASKMORE)
449 ; /* do nothing */
450 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200451 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200452 /* Redrawing only works when the screen didn't scroll. Don't clear
453 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454 if (msg_scrolled == 0
455#ifdef FEAT_WILDMENU
456 && wild_menu_showing == 0
457#endif
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200458 && call_update_screen)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200459 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200460 /* Redraw in the same position, so that the user can continue
461 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200462 redrawcmdline_ex(FALSE);
463 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200464 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100465 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 /* keep the command line if possible */
467 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100468 setcursor();
469 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100471#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100472 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200473 /* Don't update the cursor when it is blinking and off to avoid
474 * flicker. */
Bram Moolenaara338adc2018-01-31 20:51:47 +0100475 out_flush_cursor(FALSE, FALSE);
476 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100478 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200479
480 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100481}
482
483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 * Changed something in the current window, at buffer line "lnum", that
485 * requires that line and possibly other lines to be redrawn.
486 * Used when entering/leaving Insert mode with the cursor on a folded line.
487 * Used to remove the "$" from a change command.
488 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
489 * may become invalid and the whole window will have to be redrawn.
490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100492redrawWinline(
493 linenr_T lnum,
494 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496#ifdef FEAT_FOLDING
497 int i;
498#endif
499
500 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
501 curwin->w_redraw_top = lnum;
502 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
503 curwin->w_redraw_bot = lnum;
504 redraw_later(VALID);
505
506#ifdef FEAT_FOLDING
507 if (invalid)
508 {
509 /* A w_lines[] entry for this lnum has become invalid. */
510 i = find_wl_entry(curwin, lnum);
511 if (i >= 0)
512 curwin->w_lines[i].wl_valid = FALSE;
513 }
514#endif
515}
516
517/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200518 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 */
520 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100521update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523 redraw_curbuf_later(type);
524 update_screen(type);
525}
526
527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * Based on the current value of curwin->w_topline, transfer a screenfull
529 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200530 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200532 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200533update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200535 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 win_T *wp;
537 static int did_intro = FALSE;
538#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
539 int did_one;
540#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200541#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200542 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200543 int gui_cursor_col;
544 int gui_cursor_row;
545#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000548 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200550 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552 if (type == VALID_NO_UPDATE)
553 {
554 no_update = TRUE;
555 type = 0;
556 }
557
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 if (must_redraw)
559 {
560 if (type < must_redraw) /* use maximal type */
561 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000562
563 /* must_redraw is reset here, so that when we run into some weird
564 * reason to redraw while busy redrawing (e.g., asynchronous
565 * scrolling), or update_topline() in win_update() will cause a
566 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 must_redraw = 0;
568 }
569
570 /* Need to update w_lines[]. */
571 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
572 type = NOT_VALID;
573
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000574 /* Postpone the redrawing when it's not needed and when being called
575 * recursively. */
576 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {
578 redraw_later(type); /* remember type for next time */
579 must_redraw = type;
580 if (type > INVERTED_ALL)
581 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200582 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 }
584
585 updating_screen = TRUE;
586#ifdef FEAT_SYN_HL
587 ++display_tick; /* let syntax code know we're in a next round of
588 * display updating */
589#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200590 if (no_update)
591 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 /*
594 * if the screen was scrolled up when displaying a message, scroll it down
595 */
596 if (msg_scrolled)
597 {
598 clear_cmdline = TRUE;
599 if (msg_scrolled > Rows - 5) /* clearing is faster */
600 type = CLEAR;
601 else if (type != CLEAR)
602 {
603 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200604 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
605 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 type = CLEAR;
607 FOR_ALL_WINDOWS(wp)
608 {
609 if (W_WINROW(wp) < msg_scrolled)
610 {
611 if (W_WINROW(wp) + wp->w_height > msg_scrolled
612 && wp->w_redr_type < REDRAW_TOP
613 && wp->w_lines_valid > 0
614 && wp->w_topline == wp->w_lines[0].wl_lnum)
615 {
616 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
617 wp->w_redr_type = REDRAW_TOP;
618 }
619 else
620 {
621 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200622 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
623 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 }
626 }
627 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200628 if (!no_update)
629 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000630 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 }
632 msg_scrolled = 0;
633 need_wait_return = FALSE;
634 }
635
636 /* reset cmdline_row now (may have been changed temporarily) */
637 compute_cmdrow();
638
639 /* Check for changed highlighting */
640 if (need_highlight_changed)
641 highlight_changed();
642
643 if (type == CLEAR) /* first clear screen */
644 {
645 screenclear(); /* will reset clear_cmdline */
646 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200647 /* must_redraw may be set indirectly, avoid another redraw later */
648 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 }
650
651 if (clear_cmdline) /* going to clear cmdline (done below) */
652 check_for_delay(FALSE);
653
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000654#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200655 /* Force redraw when width of 'number' or 'relativenumber' column
656 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000657 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200658 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
659 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000660 curwin->w_redr_type = NOT_VALID;
661#endif
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 /*
664 * Only start redrawing if there is really something to do.
665 */
666 if (type == INVERTED)
667 update_curswant();
668 if (curwin->w_redr_type < type
669 && !((type == VALID
670 && curwin->w_lines[0].wl_valid
671#ifdef FEAT_DIFF
672 && curwin->w_topfill == curwin->w_old_topfill
673 && curwin->w_botfill == curwin->w_old_botfill
674#endif
675 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000677 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
679 && curwin->w_old_visual_mode == VIsual_mode
680 && (curwin->w_valid & VALID_VIRTCOL)
681 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 ))
683 curwin->w_redr_type = type;
684
Bram Moolenaar5a305422006-04-28 22:38:25 +0000685 /* Redraw the tab pages line if needed. */
686 if (redraw_tabline || type >= NOT_VALID)
687 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#ifdef FEAT_SYN_HL
690 /*
691 * Correct stored syntax highlighting info for changes in each displayed
692 * buffer. Each buffer must only be done once.
693 */
694 FOR_ALL_WINDOWS(wp)
695 {
696 if (wp->w_buffer->b_mod_set)
697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 win_T *wwp;
699
700 /* Check if we already did this buffer. */
701 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
702 if (wwp->w_buffer == wp->w_buffer)
703 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200704 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 syn_stack_apply_changes(wp->w_buffer);
706 }
707 }
708#endif
709
710 /*
711 * Go from top to bottom through the windows, redrawing the ones that need
712 * it.
713 */
714#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
715 did_one = FALSE;
716#endif
717#ifdef FEAT_SEARCH_EXTRA
718 search_hl.rm.regprog = NULL;
719#endif
720 FOR_ALL_WINDOWS(wp)
721 {
722 if (wp->w_redr_type != 0)
723 {
724 cursor_off();
725#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
726 if (!did_one)
727 {
728 did_one = TRUE;
729# ifdef FEAT_SEARCH_EXTRA
730 start_search_hl();
731# endif
732# ifdef FEAT_CLIPBOARD
733 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200734 if (clip_star.available && clip_isautosel_star())
735 clip_update_selection(&clip_star);
736 if (clip_plus.available && clip_isautosel_plus())
737 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738# endif
739#ifdef FEAT_GUI
740 /* Remove the cursor before starting to do anything, because
741 * scrolling may make it difficult to redraw the text under
742 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200743 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200744 {
745 gui_cursor_col = gui.cursor_col;
746 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200748 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750#endif
751 }
752#endif
753 win_update(wp);
754 }
755
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 /* redraw status line after the window to minimize cursor movement */
757 if (wp->w_redr_status)
758 {
759 cursor_off();
760 win_redr_status(wp);
761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 }
763#if defined(FEAT_SEARCH_EXTRA)
764 end_search_hl();
765#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100766#ifdef FEAT_INS_EXPAND
767 /* May need to redraw the popup menu. */
768 if (pum_visible())
769 pum_redraw();
770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 /* Reset b_mod_set flags. Going through all windows is probably faster
773 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200774 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
777 updating_screen = FALSE;
778#ifdef FEAT_GUI
779 gui_may_resize_shell();
780#endif
781
782 /* Clear or redraw the command line. Done last, because scrolling may
783 * mess up the command line. */
784 if (clear_cmdline || redraw_cmdline)
785 showmode();
786
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200787 if (no_update)
788 --no_win_do_lines_ins;
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200791 if (!did_intro)
792 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 did_intro = TRUE;
794
795#ifdef FEAT_GUI
796 /* Redraw the cursor and update the scrollbars when all screen updating is
797 * done. */
798 if (gui.in_use)
799 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200800 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200801 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100802 mch_disable_flush();
803 out_flush(); /* required before updating the cursor */
804 mch_enable_flush();
805
Bram Moolenaar144445d2016-07-08 21:41:54 +0200806 /* Put the GUI position where the cursor was, gui_update_cursor()
807 * uses that. */
808 gui.col = gui_cursor_col;
809 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200810# ifdef FEAT_MBYTE
811 gui.col = mb_fix_col(gui.col, gui.row);
812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100814 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200815 screen_cur_col = gui.col;
816 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200817 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100818 else
819 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 gui_update_scrollbars(FALSE);
821 }
822#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200823 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824}
825
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100826#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
827/*
828 * Prepare for updating one or more windows.
829 * Caller must check for "updating_screen" already set to avoid recursiveness.
830 */
831 static void
832update_prepare(void)
833{
834 cursor_off();
835 updating_screen = TRUE;
836#ifdef FEAT_GUI
837 /* Remove the cursor before starting to do anything, because scrolling may
838 * make it difficult to redraw the text under it. */
839 if (gui.in_use)
840 gui_undraw_cursor();
841#endif
842#ifdef FEAT_SEARCH_EXTRA
843 start_search_hl();
844#endif
845}
846
847/*
848 * Finish updating one or more windows.
849 */
850 static void
851update_finish(void)
852{
853 if (redraw_cmdline)
854 showmode();
855
856# ifdef FEAT_SEARCH_EXTRA
857 end_search_hl();
858# endif
859
860 updating_screen = FALSE;
861
862# ifdef FEAT_GUI
863 gui_may_resize_shell();
864
865 /* Redraw the cursor and update the scrollbars when all screen updating is
866 * done. */
867 if (gui.in_use)
868 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100869 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100870 gui_update_scrollbars(FALSE);
871 }
872# endif
873}
874#endif
875
Bram Moolenaar860cae12010-06-05 23:22:07 +0200876#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200877/*
878 * Return TRUE if the cursor line in window "wp" may be concealed, according
879 * to the 'concealcursor' option.
880 */
881 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100882conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200883{
884 int c;
885
886 if (*wp->w_p_cocu == NUL)
887 return FALSE;
888 if (get_real_state() & VISUAL)
889 c = 'v';
890 else if (State & INSERT)
891 c = 'i';
892 else if (State & NORMAL)
893 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200894 else if (State & CMDLINE)
895 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200896 else
897 return FALSE;
898 return vim_strchr(wp->w_p_cocu, c) != NULL;
899}
900
901/*
902 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
903 */
904 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100905conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200906{
907 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
908 {
909 need_cursor_line_redraw = TRUE;
910 /* Need to recompute cursor column, e.g., when starting Visual mode
911 * without concealing. */
912 curs_columns(TRUE);
913 }
914}
915
Bram Moolenaar860cae12010-06-05 23:22:07 +0200916 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100917update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200918{
919 int row;
920 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200921#ifdef SYN_TIME_LIMIT
922 proftime_T syntax_tm;
923#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200924
Bram Moolenaar908be432016-05-24 10:51:30 +0200925 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100926 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200927 return;
928
Bram Moolenaar860cae12010-06-05 23:22:07 +0200929 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200930 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200931 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200932#ifdef SYN_TIME_LIMIT
933 /* Set the time limit to 'redrawtime'. */
934 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200935 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200936#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100937 update_prepare();
938
Bram Moolenaar860cae12010-06-05 23:22:07 +0200939 row = 0;
940 for (j = 0; j < wp->w_lines_valid; ++j)
941 {
942 if (lnum == wp->w_lines[j].wl_lnum)
943 {
944 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200945# ifdef FEAT_SEARCH_EXTRA
946 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200947 start_search_hl();
948 prepare_search_hl(wp, lnum);
949# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200950 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951# if defined(FEAT_SEARCH_EXTRA)
952 end_search_hl();
953# endif
954 break;
955 }
956 row += wp->w_lines[j].wl_size;
957 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100958
959 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200960
961#ifdef SYN_TIME_LIMIT
962 syn_set_timeout(NULL);
963#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200964 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200965 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967#endif
968
969#if defined(FEAT_SIGNS) || defined(PROTO)
970 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100971update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 win_T *wp;
974 int doit = FALSE;
975
976# ifdef FEAT_FOLDING
977 win_foldinfo.fi_level = 0;
978# endif
979
980 /* update/delete a specific mark */
981 FOR_ALL_WINDOWS(wp)
982 {
983 if (buf != NULL && lnum > 0)
984 {
985 if (wp->w_buffer == buf && lnum >= wp->w_topline
986 && lnum < wp->w_botline)
987 {
988 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
989 wp->w_redraw_top = lnum;
990 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
991 wp->w_redraw_bot = lnum;
992 redraw_win_later(wp, VALID);
993 }
994 }
995 else
996 redraw_win_later(wp, VALID);
997 if (wp->w_redr_type != 0)
998 doit = TRUE;
999 }
1000
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001001 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001002 * happening (recursive call), messages on the screen or still starting up.
1003 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001004 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001005 || State == ASKMORE || State == HITRETURN
1006 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001007#ifdef FEAT_GUI
1008 || gui.starting
1009#endif
1010 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 return;
1012
1013 /* update all windows that need updating */
1014 update_prepare();
1015
Bram Moolenaar29323592016-07-24 22:04:11 +02001016 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {
1018 if (wp->w_redr_type != 0)
1019 win_update(wp);
1020 if (wp->w_redr_status)
1021 win_redr_status(wp);
1022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 update_finish();
1025}
1026#endif
1027
1028
1029#if defined(FEAT_GUI) || defined(PROTO)
1030/*
1031 * Update a single window, its status line and maybe the command line msg.
1032 * Used for the GUI scrollbar.
1033 */
1034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001035updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001037 /* return if already busy updating */
1038 if (updating_screen)
1039 return;
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 update_prepare();
1042
1043#ifdef FEAT_CLIPBOARD
1044 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001045 if (clip_star.available && clip_isautosel_star())
1046 clip_update_selection(&clip_star);
1047 if (clip_plus.available && clip_isautosel_plus())
1048 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001050
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001052
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001053 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001054 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001055 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (wp->w_redr_status
1058# ifdef FEAT_CMDL_INFO
1059 || p_ru
1060# endif
1061# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001062 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063# endif
1064 )
1065 win_redr_status(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
1067 update_finish();
1068}
1069#endif
1070
1071/*
1072 * Update a single window.
1073 *
1074 * This may cause the windows below it also to be redrawn (when clearing the
1075 * screen or scrolling lines).
1076 *
1077 * How the window is redrawn depends on wp->w_redr_type. Each type also
1078 * implies the one below it.
1079 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001080 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1082 * INVERTED redraw the changed part of the Visual area
1083 * INVERTED_ALL redraw the whole Visual area
1084 * VALID 1. scroll up/down to adjust for a changed w_topline
1085 * 2. update lines at the top when scrolled down
1086 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001087 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 * b_mod_top and b_mod_bot.
1089 * - if wp->w_redraw_top non-zero, redraw lines between
1090 * wp->w_redraw_top and wp->w_redr_bot.
1091 * - continue redrawing when syntax status is invalid.
1092 * 4. if scrolled up, update lines at the bottom.
1093 * This results in three areas that may need updating:
1094 * top: from first row to top_end (when scrolled down)
1095 * mid: from mid_start to mid_end (update inversion or changed text)
1096 * bot: from bot_start to last row (when scrolled up)
1097 */
1098 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001099win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100{
1101 buf_T *buf = wp->w_buffer;
1102 int type;
1103 int top_end = 0; /* Below last row of the top area that needs
1104 updating. 0 when no top area updating. */
1105 int mid_start = 999;/* first row of the mid area that needs
1106 updating. 999 when no mid area updating. */
1107 int mid_end = 0; /* Below last row of the mid area that needs
1108 updating. 0 when no mid area updating. */
1109 int bot_start = 999;/* first row of the bot area that needs
1110 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 int scrolled_down = FALSE; /* TRUE when scrolled down when
1112 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001114 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 int top_to_mod = FALSE; /* redraw above mod_top */
1116#endif
1117
1118 int row; /* current window row to display */
1119 linenr_T lnum; /* current buffer lnum to display */
1120 int idx; /* current index in w_lines[] */
1121 int srow; /* starting row of the current line */
1122
1123 int eof = FALSE; /* if TRUE, we hit the end of the file */
1124 int didline = FALSE; /* if TRUE, we finished the last line */
1125 int i;
1126 long j;
1127 static int recursive = FALSE; /* being called recursively */
1128 int old_botline = wp->w_botline;
1129#ifdef FEAT_FOLDING
1130 long fold_count;
1131#endif
1132#ifdef FEAT_SYN_HL
1133 /* remember what happened to the previous line, to know if
1134 * check_visual_highlight() can be used */
1135#define DID_NONE 1 /* didn't update a line */
1136#define DID_LINE 2 /* updated a normal line */
1137#define DID_FOLD 3 /* updated a folded line */
1138 int did_update = DID_NONE;
1139 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1140#endif
1141 linenr_T mod_top = 0;
1142 linenr_T mod_bot = 0;
1143#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1144 int save_got_int;
1145#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001146#ifdef SYN_TIME_LIMIT
1147 proftime_T syntax_tm;
1148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
1150 type = wp->w_redr_type;
1151
1152 if (type == NOT_VALID)
1153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 wp->w_lines_valid = 0;
1156 }
1157
1158 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001159 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
1161 wp->w_redr_type = 0;
1162 return;
1163 }
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 /* Window is zero-width: Only need to draw the separator. */
1166 if (wp->w_width == 0)
1167 {
1168 /* draw the vertical separator right of this window */
1169 draw_vsep_win(wp, 0);
1170 wp->w_redr_type = 0;
1171 return;
1172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001174#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001175 /* If this window contains a terminal, redraw works completely differently.
1176 */
1177 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001178 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001179 wp->w_redr_type = 0;
1180 return;
1181 }
1182#endif
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001185 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186#endif
1187
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001188#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001189 /* Force redraw when width of 'number' or 'relativenumber' column
1190 * changes. */
1191 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001192 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001193 {
1194 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001195 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001196 }
1197 else
1198#endif
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1201 {
1202 /*
1203 * When there are both inserted/deleted lines and specific lines to be
1204 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1205 * everything (only happens when redrawing is off for while).
1206 */
1207 type = NOT_VALID;
1208 }
1209 else
1210 {
1211 /*
1212 * Set mod_top to the first line that needs displaying because of
1213 * changes. Set mod_bot to the first line after the changes.
1214 */
1215 mod_top = wp->w_redraw_top;
1216 if (wp->w_redraw_bot != 0)
1217 mod_bot = wp->w_redraw_bot + 1;
1218 else
1219 mod_bot = 0;
1220 wp->w_redraw_top = 0; /* reset for next time */
1221 wp->w_redraw_bot = 0;
1222 if (buf->b_mod_set)
1223 {
1224 if (mod_top == 0 || mod_top > buf->b_mod_top)
1225 {
1226 mod_top = buf->b_mod_top;
1227#ifdef FEAT_SYN_HL
1228 /* Need to redraw lines above the change that may be included
1229 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001230 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001232 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (mod_top < 1)
1234 mod_top = 1;
1235 }
1236#endif
1237 }
1238 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1239 mod_bot = buf->b_mod_bot;
1240
1241#ifdef FEAT_SEARCH_EXTRA
1242 /* When 'hlsearch' is on and using a multi-line search pattern, a
1243 * change in one line may make the Search highlighting in a
1244 * previous line invalid. Simple solution: redraw all visible
1245 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001246 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001248 if (search_hl.rm.regprog != NULL
1249 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001251 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001252 {
1253 cur = wp->w_match_head;
1254 while (cur != NULL)
1255 {
1256 if (cur->match.regprog != NULL
1257 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001258 {
1259 top_to_mod = TRUE;
1260 break;
1261 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001262 cur = cur->next;
1263 }
1264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265#endif
1266 }
1267#ifdef FEAT_FOLDING
1268 if (mod_top != 0 && hasAnyFolding(wp))
1269 {
1270 linenr_T lnumt, lnumb;
1271
1272 /*
1273 * A change in a line can cause lines above it to become folded or
1274 * unfolded. Find the top most buffer line that may be affected.
1275 * If the line was previously folded and displayed, get the first
1276 * line of that fold. If the line is folded now, get the first
1277 * folded line. Use the minimum of these two.
1278 */
1279
1280 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1281 * the line below it. If there is no valid entry, use w_topline.
1282 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1283 * to this line. If there is no valid entry, use MAXLNUM. */
1284 lnumt = wp->w_topline;
1285 lnumb = MAXLNUM;
1286 for (i = 0; i < wp->w_lines_valid; ++i)
1287 if (wp->w_lines[i].wl_valid)
1288 {
1289 if (wp->w_lines[i].wl_lastlnum < mod_top)
1290 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1291 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1292 {
1293 lnumb = wp->w_lines[i].wl_lnum;
1294 /* When there is a fold column it might need updating
1295 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001296 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 ++lnumb;
1298 }
1299 }
1300
1301 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1302 if (mod_top > lnumt)
1303 mod_top = lnumt;
1304
1305 /* Now do the same for the bottom line (one above mod_bot). */
1306 --mod_bot;
1307 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1308 ++mod_bot;
1309 if (mod_bot < lnumb)
1310 mod_bot = lnumb;
1311 }
1312#endif
1313
1314 /* When a change starts above w_topline and the end is below
1315 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001316 * If the end of the change is above w_topline: do like no change was
1317 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (mod_top != 0 && mod_top < wp->w_topline)
1319 {
1320 if (mod_bot > wp->w_topline)
1321 mod_top = wp->w_topline;
1322#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001323 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 top_end = 1;
1325#endif
1326 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001327
1328 /* When line numbers are displayed need to redraw all lines below
1329 * inserted/deleted lines. */
1330 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1331 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 }
1333
1334 /*
1335 * When only displaying the lines at the top, set top_end. Used when
1336 * window has scrolled down for msg_scrolled.
1337 */
1338 if (type == REDRAW_TOP)
1339 {
1340 j = 0;
1341 for (i = 0; i < wp->w_lines_valid; ++i)
1342 {
1343 j += wp->w_lines[i].wl_size;
1344 if (j >= wp->w_upd_rows)
1345 {
1346 top_end = j;
1347 break;
1348 }
1349 }
1350 if (top_end == 0)
1351 /* not found (cannot happen?): redraw everything */
1352 type = NOT_VALID;
1353 else
1354 /* top area defined, the rest is VALID */
1355 type = VALID;
1356 }
1357
Bram Moolenaar367329b2007-08-30 11:53:22 +00001358 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001359 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1360 * non-zero and thus not FALSE) will indicate that screenclear() was not
1361 * called. */
1362 if (screen_cleared)
1363 screen_cleared = MAYBE;
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 /*
1366 * If there are no changes on the screen that require a complete redraw,
1367 * handle three cases:
1368 * 1: we are off the top of the screen by a few lines: scroll down
1369 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1370 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1371 * w_lines[] that needs updating.
1372 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001373 if ((type == VALID || type == SOME_VALID
1374 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375#ifdef FEAT_DIFF
1376 && !wp->w_botfill && !wp->w_old_botfill
1377#endif
1378 )
1379 {
1380 if (mod_top != 0 && wp->w_topline == mod_top)
1381 {
1382 /*
1383 * w_topline is the first changed line, the scrolling will be done
1384 * further down.
1385 */
1386 }
1387 else if (wp->w_lines[0].wl_valid
1388 && (wp->w_topline < wp->w_lines[0].wl_lnum
1389#ifdef FEAT_DIFF
1390 || (wp->w_topline == wp->w_lines[0].wl_lnum
1391 && wp->w_topfill > wp->w_old_topfill)
1392#endif
1393 ))
1394 {
1395 /*
1396 * New topline is above old topline: May scroll down.
1397 */
1398#ifdef FEAT_FOLDING
1399 if (hasAnyFolding(wp))
1400 {
1401 linenr_T ln;
1402
1403 /* count the number of lines we are off, counting a sequence
1404 * of folded lines as one */
1405 j = 0;
1406 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1407 {
1408 ++j;
1409 if (j >= wp->w_height - 2)
1410 break;
1411 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1412 }
1413 }
1414 else
1415#endif
1416 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1417 if (j < wp->w_height - 2) /* not too far off */
1418 {
1419 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1420#ifdef FEAT_DIFF
1421 /* insert extra lines for previously invisible filler lines */
1422 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1423 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1424 - wp->w_old_topfill;
1425#endif
1426 if (i < wp->w_height - 2) /* less than a screen off */
1427 {
1428 /*
1429 * Try to insert the correct number of lines.
1430 * If not the last window, delete the lines at the bottom.
1431 * win_ins_lines may fail when the terminal can't do it.
1432 */
1433 if (i > 0)
1434 check_for_delay(FALSE);
1435 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1436 {
1437 if (wp->w_lines_valid != 0)
1438 {
1439 /* Need to update rows that are new, stop at the
1440 * first one that scrolled down. */
1441 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
1444 /* Move the entries that were scrolled, disable
1445 * the entries for the lines to be redrawn. */
1446 if ((wp->w_lines_valid += j) > wp->w_height)
1447 wp->w_lines_valid = wp->w_height;
1448 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1449 wp->w_lines[idx] = wp->w_lines[idx - j];
1450 while (idx >= 0)
1451 wp->w_lines[idx--].wl_valid = FALSE;
1452 }
1453 }
1454 else
1455 mid_start = 0; /* redraw all lines */
1456 }
1457 else
1458 mid_start = 0; /* redraw all lines */
1459 }
1460 else
1461 mid_start = 0; /* redraw all lines */
1462 }
1463 else
1464 {
1465 /*
1466 * New topline is at or below old topline: May scroll up.
1467 * When topline didn't change, find first entry in w_lines[] that
1468 * needs updating.
1469 */
1470
1471 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1472 j = -1;
1473 row = 0;
1474 for (i = 0; i < wp->w_lines_valid; i++)
1475 {
1476 if (wp->w_lines[i].wl_valid
1477 && wp->w_lines[i].wl_lnum == wp->w_topline)
1478 {
1479 j = i;
1480 break;
1481 }
1482 row += wp->w_lines[i].wl_size;
1483 }
1484 if (j == -1)
1485 {
1486 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1487 * lines */
1488 mid_start = 0;
1489 }
1490 else
1491 {
1492 /*
1493 * Try to delete the correct number of lines.
1494 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1495 */
1496#ifdef FEAT_DIFF
1497 /* If the topline didn't change, delete old filler lines,
1498 * otherwise delete filler lines of the new topline... */
1499 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1500 row += wp->w_old_topfill;
1501 else
1502 row += diff_check_fill(wp, wp->w_topline);
1503 /* ... but don't delete new filler lines. */
1504 row -= wp->w_topfill;
1505#endif
1506 if (row > 0)
1507 {
1508 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001509 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1510 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 bot_start = wp->w_height - row;
1512 else
1513 mid_start = 0; /* redraw all lines */
1514 }
1515 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1516 {
1517 /*
1518 * Skip the lines (below the deleted lines) that are still
1519 * valid and don't need redrawing. Copy their info
1520 * upwards, to compensate for the deleted lines. Set
1521 * bot_start to the first row that needs redrawing.
1522 */
1523 bot_start = 0;
1524 idx = 0;
1525 for (;;)
1526 {
1527 wp->w_lines[idx] = wp->w_lines[j];
1528 /* stop at line that didn't fit, unless it is still
1529 * valid (no lines deleted) */
1530 if (row > 0 && bot_start + row
1531 + (int)wp->w_lines[j].wl_size > wp->w_height)
1532 {
1533 wp->w_lines_valid = idx + 1;
1534 break;
1535 }
1536 bot_start += wp->w_lines[idx++].wl_size;
1537
1538 /* stop at the last valid entry in w_lines[].wl_size */
1539 if (++j >= wp->w_lines_valid)
1540 {
1541 wp->w_lines_valid = idx;
1542 break;
1543 }
1544 }
1545#ifdef FEAT_DIFF
1546 /* Correct the first entry for filler lines at the top
1547 * when it won't get updated below. */
1548 if (wp->w_p_diff && bot_start > 0)
1549 wp->w_lines[0].wl_size =
1550 plines_win_nofill(wp, wp->w_topline, TRUE)
1551 + wp->w_topfill;
1552#endif
1553 }
1554 }
1555 }
1556
1557 /* When starting redraw in the first line, redraw all lines. When
1558 * there is only one window it's probably faster to clear the screen
1559 * first. */
1560 if (mid_start == 0)
1561 {
1562 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001563 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001564 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001565 /* Clear the screen when it was not done by win_del_lines() or
1566 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1567 * then. */
1568 if (screen_cleared != TRUE)
1569 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001570 /* The screen was cleared, redraw the tab pages line. */
1571 if (redraw_tabline)
1572 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001575
1576 /* When win_del_lines() or win_ins_lines() caused the screen to be
1577 * cleared (only happens for the first window) or when screenclear()
1578 * was called directly above, "must_redraw" will have been set to
1579 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1580 if (screen_cleared == TRUE)
1581 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
1583 else
1584 {
1585 /* Not VALID or INVERTED: redraw all lines. */
1586 mid_start = 0;
1587 mid_end = wp->w_height;
1588 }
1589
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001590 if (type == SOME_VALID)
1591 {
1592 /* SOME_VALID: redraw all lines. */
1593 mid_start = 0;
1594 mid_end = wp->w_height;
1595 type = NOT_VALID;
1596 }
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 /* check if we are updating or removing the inverted part */
1599 if ((VIsual_active && buf == curwin->w_buffer)
1600 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1601 {
1602 linenr_T from, to;
1603
1604 if (VIsual_active)
1605 {
1606 if (VIsual_active
1607 && (VIsual_mode != wp->w_old_visual_mode
1608 || type == INVERTED_ALL))
1609 {
1610 /*
1611 * If the type of Visual selection changed, redraw the whole
1612 * selection. Also when the ownership of the X selection is
1613 * gained or lost.
1614 */
1615 if (curwin->w_cursor.lnum < VIsual.lnum)
1616 {
1617 from = curwin->w_cursor.lnum;
1618 to = VIsual.lnum;
1619 }
1620 else
1621 {
1622 from = VIsual.lnum;
1623 to = curwin->w_cursor.lnum;
1624 }
1625 /* redraw more when the cursor moved as well */
1626 if (wp->w_old_cursor_lnum < from)
1627 from = wp->w_old_cursor_lnum;
1628 if (wp->w_old_cursor_lnum > to)
1629 to = wp->w_old_cursor_lnum;
1630 if (wp->w_old_visual_lnum < from)
1631 from = wp->w_old_visual_lnum;
1632 if (wp->w_old_visual_lnum > to)
1633 to = wp->w_old_visual_lnum;
1634 }
1635 else
1636 {
1637 /*
1638 * Find the line numbers that need to be updated: The lines
1639 * between the old cursor position and the current cursor
1640 * position. Also check if the Visual position changed.
1641 */
1642 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1643 {
1644 from = curwin->w_cursor.lnum;
1645 to = wp->w_old_cursor_lnum;
1646 }
1647 else
1648 {
1649 from = wp->w_old_cursor_lnum;
1650 to = curwin->w_cursor.lnum;
1651 if (from == 0) /* Visual mode just started */
1652 from = to;
1653 }
1654
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001655 if (VIsual.lnum != wp->w_old_visual_lnum
1656 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
1658 if (wp->w_old_visual_lnum < from
1659 && wp->w_old_visual_lnum != 0)
1660 from = wp->w_old_visual_lnum;
1661 if (wp->w_old_visual_lnum > to)
1662 to = wp->w_old_visual_lnum;
1663 if (VIsual.lnum < from)
1664 from = VIsual.lnum;
1665 if (VIsual.lnum > to)
1666 to = VIsual.lnum;
1667 }
1668 }
1669
1670 /*
1671 * If in block mode and changed column or curwin->w_curswant:
1672 * update all lines.
1673 * First compute the actual start and end column.
1674 */
1675 if (VIsual_mode == Ctrl_V)
1676 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001677 colnr_T fromc, toc;
1678#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1679 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
Bram Moolenaar404406a2014-10-09 13:24:43 +02001681 if (curwin->w_p_lbr)
1682 ve_flags = VE_ALL;
1683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001685#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1686 ve_flags = save_ve_flags;
1687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 ++toc;
1689 if (curwin->w_curswant == MAXCOL)
1690 toc = MAXCOL;
1691
1692 if (fromc != wp->w_old_cursor_fcol
1693 || toc != wp->w_old_cursor_lcol)
1694 {
1695 if (from > VIsual.lnum)
1696 from = VIsual.lnum;
1697 if (to < VIsual.lnum)
1698 to = VIsual.lnum;
1699 }
1700 wp->w_old_cursor_fcol = fromc;
1701 wp->w_old_cursor_lcol = toc;
1702 }
1703 }
1704 else
1705 {
1706 /* Use the line numbers of the old Visual area. */
1707 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1708 {
1709 from = wp->w_old_cursor_lnum;
1710 to = wp->w_old_visual_lnum;
1711 }
1712 else
1713 {
1714 from = wp->w_old_visual_lnum;
1715 to = wp->w_old_cursor_lnum;
1716 }
1717 }
1718
1719 /*
1720 * There is no need to update lines above the top of the window.
1721 */
1722 if (from < wp->w_topline)
1723 from = wp->w_topline;
1724
1725 /*
1726 * If we know the value of w_botline, use it to restrict the update to
1727 * the lines that are visible in the window.
1728 */
1729 if (wp->w_valid & VALID_BOTLINE)
1730 {
1731 if (from >= wp->w_botline)
1732 from = wp->w_botline - 1;
1733 if (to >= wp->w_botline)
1734 to = wp->w_botline - 1;
1735 }
1736
1737 /*
1738 * Find the minimal part to be updated.
1739 * Watch out for scrolling that made entries in w_lines[] invalid.
1740 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1741 * top_end; need to redraw from top_end to the "to" line.
1742 * A middle mouse click with a Visual selection may change the text
1743 * above the Visual area and reset wl_valid, do count these for
1744 * mid_end (in srow).
1745 */
1746 if (mid_start > 0)
1747 {
1748 lnum = wp->w_topline;
1749 idx = 0;
1750 srow = 0;
1751 if (scrolled_down)
1752 mid_start = top_end;
1753 else
1754 mid_start = 0;
1755 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1756 {
1757 if (wp->w_lines[idx].wl_valid)
1758 mid_start += wp->w_lines[idx].wl_size;
1759 else if (!scrolled_down)
1760 srow += wp->w_lines[idx].wl_size;
1761 ++idx;
1762# ifdef FEAT_FOLDING
1763 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1764 lnum = wp->w_lines[idx].wl_lnum;
1765 else
1766# endif
1767 ++lnum;
1768 }
1769 srow += mid_start;
1770 mid_end = wp->w_height;
1771 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1772 {
1773 if (wp->w_lines[idx].wl_valid
1774 && wp->w_lines[idx].wl_lnum >= to + 1)
1775 {
1776 /* Only update until first row of this line */
1777 mid_end = srow;
1778 break;
1779 }
1780 srow += wp->w_lines[idx].wl_size;
1781 }
1782 }
1783 }
1784
1785 if (VIsual_active && buf == curwin->w_buffer)
1786 {
1787 wp->w_old_visual_mode = VIsual_mode;
1788 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1789 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001790 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 wp->w_old_curswant = curwin->w_curswant;
1792 }
1793 else
1794 {
1795 wp->w_old_visual_mode = 0;
1796 wp->w_old_cursor_lnum = 0;
1797 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001798 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
1801#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1802 /* reset got_int, otherwise regexp won't work */
1803 save_got_int = got_int;
1804 got_int = 0;
1805#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001806#ifdef SYN_TIME_LIMIT
1807 /* Set the time limit to 'redrawtime'. */
1808 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001809 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811#ifdef FEAT_FOLDING
1812 win_foldinfo.fi_level = 0;
1813#endif
1814
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001815#ifdef FEAT_MENU
1816 /*
1817 * Draw the window toolbar, if there is one.
1818 * TODO: only when needed.
1819 */
1820 if (winbar_height(wp) > 0)
1821 redraw_win_toolbar(wp);
1822#endif
1823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 /*
1825 * Update all the window rows.
1826 */
1827 idx = 0; /* first entry in w_lines[].wl_size */
1828 row = 0;
1829 srow = 0;
1830 lnum = wp->w_topline; /* first line shown in window */
1831 for (;;)
1832 {
1833 /* stop updating when reached the end of the window (check for _past_
1834 * the end of the window is at the end of the loop) */
1835 if (row == wp->w_height)
1836 {
1837 didline = TRUE;
1838 break;
1839 }
1840
1841 /* stop updating when hit the end of the file */
1842 if (lnum > buf->b_ml.ml_line_count)
1843 {
1844 eof = TRUE;
1845 break;
1846 }
1847
1848 /* Remember the starting row of the line that is going to be dealt
1849 * with. It is used further down when the line doesn't fit. */
1850 srow = row;
1851
1852 /*
1853 * Update a line when it is in an area that needs updating, when it
1854 * has changes or w_lines[idx] is invalid.
1855 * bot_start may be halfway a wrapped line after using
1856 * win_del_lines(), check if the current line includes it.
1857 * When syntax folding is being used, the saved syntax states will
1858 * already have been updated, we can't see where the syntax state is
1859 * the same again, just update until the end of the window.
1860 */
1861 if (row < top_end
1862 || (row >= mid_start && row < mid_end)
1863#ifdef FEAT_SEARCH_EXTRA
1864 || top_to_mod
1865#endif
1866 || idx >= wp->w_lines_valid
1867 || (row + wp->w_lines[idx].wl_size > bot_start)
1868 || (mod_top != 0
1869 && (lnum == mod_top
1870 || (lnum >= mod_top
1871 && (lnum < mod_bot
1872#ifdef FEAT_SYN_HL
1873 || did_update == DID_FOLD
1874 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001875 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 && (
1877# ifdef FEAT_FOLDING
1878 (foldmethodIsSyntax(wp)
1879 && hasAnyFolding(wp)) ||
1880# endif
1881 syntax_check_changed(lnum)))
1882#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001883#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001884 /* match in fixed position might need redraw
1885 * if lines were inserted or deleted */
1886 || (wp->w_match_head != NULL
1887 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 )))))
1890 {
1891#ifdef FEAT_SEARCH_EXTRA
1892 if (lnum == mod_top)
1893 top_to_mod = FALSE;
1894#endif
1895
1896 /*
1897 * When at start of changed lines: May scroll following lines
1898 * up or down to minimize redrawing.
1899 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001900 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 */
1902 if (lnum == mod_top
1903 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001904 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 int old_rows = 0;
1907 int new_rows = 0;
1908 int xtra_rows;
1909 linenr_T l;
1910
1911 /* Count the old number of window rows, using w_lines[], which
1912 * should still contain the sizes for the lines as they are
1913 * currently displayed. */
1914 for (i = idx; i < wp->w_lines_valid; ++i)
1915 {
1916 /* Only valid lines have a meaningful wl_lnum. Invalid
1917 * lines are part of the changed area. */
1918 if (wp->w_lines[i].wl_valid
1919 && wp->w_lines[i].wl_lnum == mod_bot)
1920 break;
1921 old_rows += wp->w_lines[i].wl_size;
1922#ifdef FEAT_FOLDING
1923 if (wp->w_lines[i].wl_valid
1924 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1925 {
1926 /* Must have found the last valid entry above mod_bot.
1927 * Add following invalid entries. */
1928 ++i;
1929 while (i < wp->w_lines_valid
1930 && !wp->w_lines[i].wl_valid)
1931 old_rows += wp->w_lines[i++].wl_size;
1932 break;
1933 }
1934#endif
1935 }
1936
1937 if (i >= wp->w_lines_valid)
1938 {
1939 /* We can't find a valid line below the changed lines,
1940 * need to redraw until the end of the window.
1941 * Inserting/deleting lines has no use. */
1942 bot_start = 0;
1943 }
1944 else
1945 {
1946 /* Able to count old number of rows: Count new window
1947 * rows, and may insert/delete lines */
1948 j = idx;
1949 for (l = lnum; l < mod_bot; ++l)
1950 {
1951#ifdef FEAT_FOLDING
1952 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1953 ++new_rows;
1954 else
1955#endif
1956#ifdef FEAT_DIFF
1957 if (l == wp->w_topline)
1958 new_rows += plines_win_nofill(wp, l, TRUE)
1959 + wp->w_topfill;
1960 else
1961#endif
1962 new_rows += plines_win(wp, l, TRUE);
1963 ++j;
1964 if (new_rows > wp->w_height - row - 2)
1965 {
1966 /* it's getting too much, must redraw the rest */
1967 new_rows = 9999;
1968 break;
1969 }
1970 }
1971 xtra_rows = new_rows - old_rows;
1972 if (xtra_rows < 0)
1973 {
1974 /* May scroll text up. If there is not enough
1975 * remaining text or scrolling fails, must redraw the
1976 * rest. If scrolling works, must redraw the text
1977 * below the scrolled text. */
1978 if (row - xtra_rows >= wp->w_height - 2)
1979 mod_bot = MAXLNUM;
1980 else
1981 {
1982 check_for_delay(FALSE);
1983 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001984 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 mod_bot = MAXLNUM;
1986 else
1987 bot_start = wp->w_height + xtra_rows;
1988 }
1989 }
1990 else if (xtra_rows > 0)
1991 {
1992 /* May scroll text down. If there is not enough
1993 * remaining text of scrolling fails, must redraw the
1994 * rest. */
1995 if (row + xtra_rows >= wp->w_height - 2)
1996 mod_bot = MAXLNUM;
1997 else
1998 {
1999 check_for_delay(FALSE);
2000 if (win_ins_lines(wp, row + old_rows,
2001 xtra_rows, FALSE, FALSE) == FAIL)
2002 mod_bot = MAXLNUM;
2003 else if (top_end > row + old_rows)
2004 /* Scrolled the part at the top that requires
2005 * updating down. */
2006 top_end += xtra_rows;
2007 }
2008 }
2009
2010 /* When not updating the rest, may need to move w_lines[]
2011 * entries. */
2012 if (mod_bot != MAXLNUM && i != j)
2013 {
2014 if (j < i)
2015 {
2016 int x = row + new_rows;
2017
2018 /* move entries in w_lines[] upwards */
2019 for (;;)
2020 {
2021 /* stop at last valid entry in w_lines[] */
2022 if (i >= wp->w_lines_valid)
2023 {
2024 wp->w_lines_valid = j;
2025 break;
2026 }
2027 wp->w_lines[j] = wp->w_lines[i];
2028 /* stop at a line that won't fit */
2029 if (x + (int)wp->w_lines[j].wl_size
2030 > wp->w_height)
2031 {
2032 wp->w_lines_valid = j + 1;
2033 break;
2034 }
2035 x += wp->w_lines[j++].wl_size;
2036 ++i;
2037 }
2038 if (bot_start > x)
2039 bot_start = x;
2040 }
2041 else /* j > i */
2042 {
2043 /* move entries in w_lines[] downwards */
2044 j -= i;
2045 wp->w_lines_valid += j;
2046 if (wp->w_lines_valid > wp->w_height)
2047 wp->w_lines_valid = wp->w_height;
2048 for (i = wp->w_lines_valid; i - j >= idx; --i)
2049 wp->w_lines[i] = wp->w_lines[i - j];
2050
2051 /* The w_lines[] entries for inserted lines are
2052 * now invalid, but wl_size may be used above.
2053 * Reset to zero. */
2054 while (i >= idx)
2055 {
2056 wp->w_lines[i].wl_size = 0;
2057 wp->w_lines[i--].wl_valid = FALSE;
2058 }
2059 }
2060 }
2061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
2063
2064#ifdef FEAT_FOLDING
2065 /*
2066 * When lines are folded, display one line for all of them.
2067 * Otherwise, display normally (can be several display lines when
2068 * 'wrap' is on).
2069 */
2070 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2071 if (fold_count != 0)
2072 {
2073 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2074 ++row;
2075 --fold_count;
2076 wp->w_lines[idx].wl_folded = TRUE;
2077 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2078# ifdef FEAT_SYN_HL
2079 did_update = DID_FOLD;
2080# endif
2081 }
2082 else
2083#endif
2084 if (idx < wp->w_lines_valid
2085 && wp->w_lines[idx].wl_valid
2086 && wp->w_lines[idx].wl_lnum == lnum
2087 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002088 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 && srow + wp->w_lines[idx].wl_size > wp->w_height
2090#ifdef FEAT_DIFF
2091 && diff_check_fill(wp, lnum) == 0
2092#endif
2093 )
2094 {
2095 /* This line is not going to fit. Don't draw anything here,
2096 * will draw "@ " lines below. */
2097 row = wp->w_height + 1;
2098 }
2099 else
2100 {
2101#ifdef FEAT_SEARCH_EXTRA
2102 prepare_search_hl(wp, lnum);
2103#endif
2104#ifdef FEAT_SYN_HL
2105 /* Let the syntax stuff know we skipped a few lines. */
2106 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002107 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 syntax_end_parsing(syntax_last_parsed + 1);
2109#endif
2110
2111 /*
2112 * Display one line.
2113 */
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002114 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
2116#ifdef FEAT_FOLDING
2117 wp->w_lines[idx].wl_folded = FALSE;
2118 wp->w_lines[idx].wl_lastlnum = lnum;
2119#endif
2120#ifdef FEAT_SYN_HL
2121 did_update = DID_LINE;
2122 syntax_last_parsed = lnum;
2123#endif
2124 }
2125
2126 wp->w_lines[idx].wl_lnum = lnum;
2127 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002128
2129 /* Past end of the window or end of the screen. Note that after
2130 * resizing wp->w_height may be end up too big. That's a problem
2131 * elsewhere, but prevent a crash here. */
2132 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
2134 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002135 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2137 ++idx;
2138 break;
2139 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002140 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 wp->w_lines[idx].wl_size = row - srow;
2142 ++idx;
2143#ifdef FEAT_FOLDING
2144 lnum += fold_count + 1;
2145#else
2146 ++lnum;
2147#endif
2148 }
2149 else
2150 {
2151 /* This line does not need updating, advance to the next one */
2152 row += wp->w_lines[idx++].wl_size;
2153 if (row > wp->w_height) /* past end of screen */
2154 break;
2155#ifdef FEAT_FOLDING
2156 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2157#else
2158 ++lnum;
2159#endif
2160#ifdef FEAT_SYN_HL
2161 did_update = DID_NONE;
2162#endif
2163 }
2164
2165 if (lnum > buf->b_ml.ml_line_count)
2166 {
2167 eof = TRUE;
2168 break;
2169 }
2170 }
2171 /*
2172 * End of loop over all window lines.
2173 */
2174
2175
2176 if (idx > wp->w_lines_valid)
2177 wp->w_lines_valid = idx;
2178
2179#ifdef FEAT_SYN_HL
2180 /*
2181 * Let the syntax stuff know we stop parsing here.
2182 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002183 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 syntax_end_parsing(syntax_last_parsed + 1);
2185#endif
2186
2187 /*
2188 * If we didn't hit the end of the file, and we didn't finish the last
2189 * line we were working on, then the line didn't fit.
2190 */
2191 wp->w_empty_rows = 0;
2192#ifdef FEAT_DIFF
2193 wp->w_filler_rows = 0;
2194#endif
2195 if (!eof && !didline)
2196 {
2197 if (lnum == wp->w_topline)
2198 {
2199 /*
2200 * Single line that does not fit!
2201 * Don't overwrite it, it can be edited.
2202 */
2203 wp->w_botline = lnum + 1;
2204 }
2205#ifdef FEAT_DIFF
2206 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2207 {
2208 /* Window ends in filler lines. */
2209 wp->w_botline = lnum;
2210 wp->w_filler_rows = wp->w_height - srow;
2211 }
2212#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002213 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2214 {
2215 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2216
2217 /*
2218 * Last line isn't finished: Display "@@@" in the last screen line.
2219 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002220 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002221 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002222 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002223 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002224 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002225 set_empty_rows(wp, srow);
2226 wp->w_botline = lnum;
2227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2229 {
2230 /*
2231 * Last line isn't finished: Display "@@@" at the end.
2232 */
2233 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2234 W_WINROW(wp) + wp->w_height,
2235 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002236 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 set_empty_rows(wp, srow);
2238 wp->w_botline = lnum;
2239 }
2240 else
2241 {
2242 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2243 wp->w_botline = lnum;
2244 }
2245 }
2246 else
2247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (eof) /* we hit the end of the file */
2250 {
2251 wp->w_botline = buf->b_ml.ml_line_count + 1;
2252#ifdef FEAT_DIFF
2253 j = diff_check_fill(wp, wp->w_botline);
2254 if (j > 0 && !wp->w_botfill)
2255 {
2256 /*
2257 * Display filler lines at the end of the file
2258 */
2259 if (char2cells(fill_diff) > 1)
2260 i = '-';
2261 else
2262 i = fill_diff;
2263 if (row + j > wp->w_height)
2264 j = wp->w_height - row;
2265 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2266 row += j;
2267 }
2268#endif
2269 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002270 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 wp->w_botline = lnum;
2272
2273 /* make sure the rest of the screen is blank */
2274 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002275 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
2277
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002278#ifdef SYN_TIME_LIMIT
2279 syn_set_timeout(NULL);
2280#endif
2281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 /* Reset the type of redrawing required, the window has been updated. */
2283 wp->w_redr_type = 0;
2284#ifdef FEAT_DIFF
2285 wp->w_old_topfill = wp->w_topfill;
2286 wp->w_old_botfill = wp->w_botfill;
2287#endif
2288
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002289 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 /*
2292 * There is a trick with w_botline. If we invalidate it on each
2293 * change that might modify it, this will cause a lot of expensive
2294 * calls to plines() in update_topline() each time. Therefore the
2295 * value of w_botline is often approximated, and this value is used to
2296 * compute the value of w_topline. If the value of w_botline was
2297 * wrong, check that the value of w_topline is correct (cursor is on
2298 * the visible part of the text). If it's not, we need to redraw
2299 * again. Mostly this just means scrolling up a few lines, so it
2300 * doesn't look too bad. Only do this for the current window (where
2301 * changes are relevant).
2302 */
2303 wp->w_valid |= VALID_BOTLINE;
2304 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2305 {
2306 recursive = TRUE;
2307 curwin->w_valid &= ~VALID_TOPLINE;
2308 update_topline(); /* may invalidate w_botline again */
2309 if (must_redraw != 0)
2310 {
2311 /* Don't update for changes in buffer again. */
2312 i = curbuf->b_mod_set;
2313 curbuf->b_mod_set = FALSE;
2314 win_update(curwin);
2315 must_redraw = 0;
2316 curbuf->b_mod_set = i;
2317 }
2318 recursive = FALSE;
2319 }
2320 }
2321
2322#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2323 /* restore got_int, unless CTRL-C was hit while redrawing */
2324 if (!got_int)
2325 got_int = save_got_int;
2326#endif
2327}
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329/*
2330 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2331 * as the filler character.
2332 */
2333 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002334win_draw_end(
2335 win_T *wp,
2336 int c1,
2337 int c2,
2338 int row,
2339 int endrow,
2340 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
2342#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2343 int n = 0;
2344# define FDC_OFF n
2345#else
2346# define FDC_OFF 0
2347#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002348#ifdef FEAT_FOLDING
2349 int fdc = compute_foldcolumn(wp, 0);
2350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352#ifdef FEAT_RIGHTLEFT
2353 if (wp->w_p_rl)
2354 {
2355 /* No check for cmdline window: should never be right-left. */
2356# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002357 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359 if (n > 0)
2360 {
2361 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002362 if (n > wp->w_width)
2363 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2365 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002366 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368# endif
2369# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002370 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
2372 int nn = n + 2;
2373
2374 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002375 if (nn > wp->w_width)
2376 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2378 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002379 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 n = nn;
2381 }
2382# endif
2383 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002384 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002385 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2387 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002388 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390 else
2391#endif
2392 {
2393#ifdef FEAT_CMDWIN
2394 if (cmdwin_type != 0 && wp == curwin)
2395 {
2396 /* draw the cmdline character in the leftmost column */
2397 n = 1;
2398 if (n > wp->w_width)
2399 n = wp->w_width;
2400 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002401 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002402 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404#endif
2405#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002406 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002408 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002411 if (nn > wp->w_width)
2412 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002414 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002415 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 n = nn;
2417 }
2418#endif
2419#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002420 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 int nn = n + 2;
2423
2424 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002425 if (nn > wp->w_width)
2426 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002428 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002429 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 n = nn;
2431 }
2432#endif
2433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002434 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002435 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437 set_empty_rows(wp, row);
2438}
2439
Bram Moolenaar1a384422010-07-14 19:53:30 +02002440#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002441static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002442
2443/*
2444 * Advance **color_cols and return TRUE when there are columns to draw.
2445 */
2446 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002447advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002448{
2449 while (**color_cols >= 0 && vcol > **color_cols)
2450 ++*color_cols;
2451 return (**color_cols >= 0);
2452}
2453#endif
2454
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002455#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2456/*
2457 * Copy "text" to ScreenLines using "attr".
2458 * Returns the next screen column.
2459 */
2460 static int
2461text_to_screenline(win_T *wp, char_u *text, int col)
2462{
2463 int off = (int)(current_ScreenLine - ScreenLines);
2464
2465#ifdef FEAT_MBYTE
2466 if (has_mbyte)
2467 {
2468 int cells;
2469 int u8c, u8cc[MAX_MCO];
2470 int i;
2471 int idx;
2472 int c_len;
2473 char_u *p;
2474# ifdef FEAT_ARABIC
2475 int prev_c = 0; /* previous Arabic character */
2476 int prev_c1 = 0; /* first composing char for prev_c */
2477# endif
2478
2479# ifdef FEAT_RIGHTLEFT
2480 if (wp->w_p_rl)
2481 idx = off;
2482 else
2483# endif
2484 idx = off + col;
2485
2486 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2487 for (p = text; *p != NUL; )
2488 {
2489 cells = (*mb_ptr2cells)(p);
2490 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002491 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002492# ifdef FEAT_RIGHTLEFT
2493 - (wp->w_p_rl ? col : 0)
2494# endif
2495 )
2496 break;
2497 ScreenLines[idx] = *p;
2498 if (enc_utf8)
2499 {
2500 u8c = utfc_ptr2char(p, u8cc);
2501 if (*p < 0x80 && u8cc[0] == 0)
2502 {
2503 ScreenLinesUC[idx] = 0;
2504#ifdef FEAT_ARABIC
2505 prev_c = u8c;
2506#endif
2507 }
2508 else
2509 {
2510#ifdef FEAT_ARABIC
2511 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2512 {
2513 /* Do Arabic shaping. */
2514 int pc, pc1, nc;
2515 int pcc[MAX_MCO];
2516 int firstbyte = *p;
2517
2518 /* The idea of what is the previous and next
2519 * character depends on 'rightleft'. */
2520 if (wp->w_p_rl)
2521 {
2522 pc = prev_c;
2523 pc1 = prev_c1;
2524 nc = utf_ptr2char(p + c_len);
2525 prev_c1 = u8cc[0];
2526 }
2527 else
2528 {
2529 pc = utfc_ptr2char(p + c_len, pcc);
2530 nc = prev_c;
2531 pc1 = pcc[0];
2532 }
2533 prev_c = u8c;
2534
2535 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2536 pc, pc1, nc);
2537 ScreenLines[idx] = firstbyte;
2538 }
2539 else
2540 prev_c = u8c;
2541#endif
2542 /* Non-BMP character: display as ? or fullwidth ?. */
2543#ifdef UNICODE16
2544 if (u8c >= 0x10000)
2545 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2546 else
2547#endif
2548 ScreenLinesUC[idx] = u8c;
2549 for (i = 0; i < Screen_mco; ++i)
2550 {
2551 ScreenLinesC[i][idx] = u8cc[i];
2552 if (u8cc[i] == 0)
2553 break;
2554 }
2555 }
2556 if (cells > 1)
2557 ScreenLines[idx + 1] = 0;
2558 }
2559 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2560 /* double-byte single width character */
2561 ScreenLines2[idx] = p[1];
2562 else if (cells > 1)
2563 /* double-width character */
2564 ScreenLines[idx + 1] = p[1];
2565 col += cells;
2566 idx += cells;
2567 p += c_len;
2568 }
2569 }
2570 else
2571#endif
2572 {
2573 int len = (int)STRLEN(text);
2574
Bram Moolenaar02631462017-09-22 15:20:32 +02002575 if (len > wp->w_width - col)
2576 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002577 if (len > 0)
2578 {
2579#ifdef FEAT_RIGHTLEFT
2580 if (wp->w_p_rl)
2581 STRNCPY(current_ScreenLine, text, len);
2582 else
2583#endif
2584 STRNCPY(current_ScreenLine + col, text, len);
2585 col += len;
2586 }
2587 }
2588 return col;
2589}
2590#endif
2591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592#ifdef FEAT_FOLDING
2593/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002594 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2595 * space is available for window "wp", minus "col".
2596 */
2597 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002598compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002599{
2600 int fdc = wp->w_p_fdc;
2601 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002602 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002603
2604 if (fdc > wwidth - (col + wmw))
2605 fdc = wwidth - (col + wmw);
2606 return fdc;
2607}
2608
2609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 * Display one folded line.
2611 */
2612 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002613fold_line(
2614 win_T *wp,
2615 long fold_count,
2616 foldinfo_T *foldinfo,
2617 linenr_T lnum,
2618 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002620 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 pos_T *top, *bot;
2622 linenr_T lnume = lnum + fold_count - 1;
2623 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002624 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 int col;
2627 int txtcol;
2628 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002629 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630
2631 /* Build the fold line:
2632 * 1. Add the cmdwin_type for the command-line window
2633 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002634 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 * 4. Compose the text
2636 * 5. Add the text
2637 * 6. set highlighting for the Visual area an other text
2638 */
2639 col = 0;
2640
2641 /*
2642 * 1. Add the cmdwin_type for the command-line window
2643 * Ignores 'rightleft', this window is never right-left.
2644 */
2645#ifdef FEAT_CMDWIN
2646 if (cmdwin_type != 0 && wp == curwin)
2647 {
2648 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002649 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650#ifdef FEAT_MBYTE
2651 if (enc_utf8)
2652 ScreenLinesUC[off] = 0;
2653#endif
2654 ++col;
2655 }
2656#endif
2657
2658 /*
2659 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002660 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002662 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (fdc > 0)
2664 {
2665 fill_foldcolumn(buf, wp, TRUE, lnum);
2666#ifdef FEAT_RIGHTLEFT
2667 if (wp->w_p_rl)
2668 {
2669 int i;
2670
Bram Moolenaar02631462017-09-22 15:20:32 +02002671 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002672 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 /* reverse the fold column */
2674 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002675 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
2677 else
2678#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002679 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 col += fdc;
2681 }
2682
2683#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002684# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2685 for (ri = 0; ri < l; ++ri) \
Bram Moolenaar02631462017-09-22 15:20:32 +02002686 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002687 else \
2688 for (ri = 0; ri < l; ++ri) \
2689 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002691# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2692 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693#endif
2694
Bram Moolenaar64486672010-05-16 15:46:46 +02002695 /* Set all attributes of the 'number' or 'relativenumber' column and the
2696 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002697 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699#ifdef FEAT_SIGNS
2700 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002701 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002703 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (len > 0)
2705 {
2706 if (len > 2)
2707 len = 2;
2708# ifdef FEAT_RIGHTLEFT
2709 if (wp->w_p_rl)
2710 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002711 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002712 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 else
2714# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002715 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 col += len;
2717 }
2718 }
2719#endif
2720
2721 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002722 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002724 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002726 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (len > 0)
2728 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002729 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002730 long num;
2731 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002732
2733 if (len > w + 1)
2734 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002735
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002736 if (wp->w_p_nu && !wp->w_p_rnu)
2737 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002738 num = (long)lnum;
2739 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002740 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002741 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002742 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002743 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002744 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002745 /* 'number' + 'relativenumber': cursor line shows absolute
2746 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002747 num = lnum;
2748 fmt = "%-*ld ";
2749 }
2750 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002751
Bram Moolenaar700e7342013-01-30 12:31:36 +01002752 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753#ifdef FEAT_RIGHTLEFT
2754 if (wp->w_p_rl)
2755 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002756 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002757 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 else
2759#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002760 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 col += len;
2762 }
2763 }
2764
2765 /*
2766 * 4. Compose the folded-line string with 'foldtext', if set.
2767 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002768 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
2770 txtcol = col; /* remember where text starts */
2771
2772 /*
2773 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2774 * Right-left text is put in columns 0 - number-col, normal text is put
2775 * in columns number-col - window-width.
2776 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002777 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
2779 /* Fill the rest of the line with the fold filler */
2780#ifdef FEAT_RIGHTLEFT
2781 if (wp->w_p_rl)
2782 col -= txtcol;
2783#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002784 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785#ifdef FEAT_RIGHTLEFT
2786 - (wp->w_p_rl ? txtcol : 0)
2787#endif
2788 )
2789 {
2790#ifdef FEAT_MBYTE
2791 if (enc_utf8)
2792 {
2793 if (fill_fold >= 0x80)
2794 {
2795 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002796 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002797 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
2799 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002800 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002802 ScreenLines[off + col] = fill_fold;
2803 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002804 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002806 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002808 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
2810
2811 if (text != buf)
2812 vim_free(text);
2813
2814 /*
2815 * 6. set highlighting for the Visual area an other text.
2816 * If all folded lines are in the Visual area, highlight the line.
2817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2819 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002820 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
2822 /* Visual is after curwin->w_cursor */
2823 top = &curwin->w_cursor;
2824 bot = &VIsual;
2825 }
2826 else
2827 {
2828 /* Visual is before curwin->w_cursor */
2829 top = &VIsual;
2830 bot = &curwin->w_cursor;
2831 }
2832 if (lnum >= top->lnum
2833 && lnume <= bot->lnum
2834 && (VIsual_mode != 'v'
2835 || ((lnum > top->lnum
2836 || (lnum == top->lnum
2837 && top->col == 0))
2838 && (lnume < bot->lnum
2839 || (lnume == bot->lnum
2840 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002841 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 {
2843 if (VIsual_mode == Ctrl_V)
2844 {
2845 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002846 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002848 if (wp->w_old_cursor_lcol != MAXCOL
2849 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002850 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 len = wp->w_old_cursor_lcol;
2852 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002853 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002854 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002855 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857 }
2858 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002859 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002861 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
2864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002866#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002867 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2868 if (wp->w_p_cc_cols)
2869 {
2870 int i = 0;
2871 int j = wp->w_p_cc_cols[i];
2872 int old_txtcol = txtcol;
2873
2874 while (j > -1)
2875 {
2876 txtcol += j;
2877 if (wp->w_p_wrap)
2878 txtcol -= wp->w_skipcol;
2879 else
2880 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002881 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002882 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002883 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002884 txtcol = old_txtcol;
2885 j = wp->w_p_cc_cols[++i];
2886 }
2887 }
2888
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002889 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002890 if (wp->w_p_cuc)
2891 {
2892 txtcol += wp->w_virtcol;
2893 if (wp->w_p_wrap)
2894 txtcol -= wp->w_skipcol;
2895 else
2896 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002897 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002898 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002899 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002900 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
Bram Moolenaar02631462017-09-22 15:20:32 +02002903 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2904 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905
2906 /*
2907 * Update w_cline_height and w_cline_folded if the cursor line was
2908 * updated (saves a call to plines() later).
2909 */
2910 if (wp == curwin
2911 && lnum <= curwin->w_cursor.lnum
2912 && lnume >= curwin->w_cursor.lnum)
2913 {
2914 curwin->w_cline_row = row;
2915 curwin->w_cline_height = 1;
2916 curwin->w_cline_folded = TRUE;
2917 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2918 }
2919}
2920
2921/*
2922 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2923 */
2924 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002925copy_text_attr(
2926 int off,
2927 char_u *buf,
2928 int len,
2929 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002931 int i;
2932
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 mch_memmove(ScreenLines + off, buf, (size_t)len);
2934# ifdef FEAT_MBYTE
2935 if (enc_utf8)
2936 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2937# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002938 for (i = 0; i < len; ++i)
2939 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940}
2941
2942/*
2943 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002944 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 */
2946 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002947fill_foldcolumn(
2948 char_u *p,
2949 win_T *wp,
2950 int closed, /* TRUE of FALSE */
2951 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
2953 int i = 0;
2954 int level;
2955 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002956 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002957 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
2959 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002960 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 level = win_foldinfo.fi_level;
2963 if (level > 0)
2964 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002965 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002966 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 /* If the column is too narrow, we start at the lowest level that
2969 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002970 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 if (first_level < 1)
2972 first_level = 1;
2973
Bram Moolenaar1c934292015-01-27 16:39:29 +01002974 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 {
2976 if (win_foldinfo.fi_lnum == lnum
2977 && first_level + i >= win_foldinfo.fi_low_level)
2978 p[i] = '-';
2979 else if (first_level == 1)
2980 p[i] = '|';
2981 else if (first_level + i <= 9)
2982 p[i] = '0' + first_level + i;
2983 else
2984 p[i] = '>';
2985 if (first_level + i == level)
2986 break;
2987 }
2988 }
2989 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002990 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991}
2992#endif /* FEAT_FOLDING */
2993
2994/*
2995 * Display line "lnum" of window 'wp' on the screen.
2996 * Start at row "startrow", stop when "endrow" is reached.
2997 * wp->w_virtcol needs to be valid.
2998 *
2999 * Return the number of last row the line occupies.
3000 */
3001 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003002win_line(
3003 win_T *wp,
3004 linenr_T lnum,
3005 int startrow,
3006 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003007 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003009 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3011 int c = 0; /* init for GCC */
3012 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003013#ifdef FEAT_LINEBREAK
3014 long vcol_sbr = -1; /* virtual column after showbreak */
3015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 long vcol_prev = -1; /* "vcol" of previous character */
3017 char_u *line; /* current line */
3018 char_u *ptr; /* current position in "line" */
3019 int row; /* row in the window, excl w_winrow */
3020 int screen_row; /* row on the screen, incl w_winrow */
3021
3022 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3023 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003024 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003025 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 int c_extra = NUL; /* extra chars, all the same */
3027 int extra_attr = 0; /* attributes when n_extra != 0 */
3028 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3029 displaying lcs_eol at end-of-line */
3030 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3031 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3032
3033 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3034 int saved_n_extra = 0;
3035 char_u *saved_p_extra = NULL;
3036 int saved_c_extra = 0;
3037 int saved_char_attr = 0;
3038
3039 int n_attr = 0; /* chars with special attr */
3040 int saved_attr2 = 0; /* char_attr saved for n_attr */
3041 int n_attr3 = 0; /* chars with overruling special attr */
3042 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3043
3044 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3045
3046 int fromcol, tocol; /* start/end of inverting */
3047 int fromcol_prev = -2; /* start of inverting after cursor */
3048 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003050 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 pos_T pos;
3052 long v;
3053
3054 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003055 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3057 in this line */
3058 int attr = 0; /* attributes for area highlighting */
3059 int area_attr = 0; /* attributes desired by highlighting */
3060 int search_attr = 0; /* attributes desired by 'hlsearch' */
3061#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003062 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 int syntax_attr = 0; /* attributes desired by syntax */
3064 int has_syntax = FALSE; /* this buffer has syntax highl. */
3065 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003066 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003067 int draw_color_col = FALSE; /* highlight colorcolumn */
3068 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003069#endif
3070#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003071 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003072# define SPWORDLEN 150
3073 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003074 int nextlinecol = 0; /* column where nextline[] starts */
3075 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003076 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003077 int spell_attr = 0; /* attributes desired by spelling */
3078 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003079 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3080 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003081 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003082 static int cap_col = -1; /* column to check for Cap word */
3083 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003084 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085#endif
3086 int extra_check; /* has syntax or linebreak */
3087#ifdef FEAT_MBYTE
3088 int multi_attr = 0; /* attributes desired by multibyte */
3089 int mb_l = 1; /* multi-byte byte length */
3090 int mb_c = 0; /* decoded multi-byte character */
3091 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003092 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093#endif
3094#ifdef FEAT_DIFF
3095 int filler_lines; /* nr of filler lines to be drawn */
3096 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003097 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 int change_start = MAXCOL; /* first col of changed area */
3099 int change_end = -1; /* last col of changed area */
3100#endif
3101 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3102#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003103 int need_showbreak = FALSE; /* overlong line, skipping first x
3104 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003106#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003107 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003109 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110#endif
3111#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003112 matchitem_T *cur; /* points to the match list */
3113 match_T *shl; /* points to search_hl or a match */
3114 int shl_flag; /* flag to indicate whether search_hl
3115 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003116 int pos_inprogress; /* marks that position match search is
3117 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003118 int prevcol_hl_flag; /* flag to indicate whether prevcol
3119 equals startcol of search_hl or one
3120 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#endif
3122#ifdef FEAT_ARABIC
3123 int prev_c = 0; /* previous Arabic character */
3124 int prev_c1 = 0; /* first composing char for prev_c */
3125#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003126#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003127 int did_line_attr = 0;
3128#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003129#ifdef FEAT_TERMINAL
3130 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003131 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
3134 /* draw_state: items that are drawn in sequence: */
3135#define WL_START 0 /* nothing done yet */
3136#ifdef FEAT_CMDWIN
3137# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3138#else
3139# define WL_CMDLINE WL_START
3140#endif
3141#ifdef FEAT_FOLDING
3142# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3143#else
3144# define WL_FOLD WL_CMDLINE
3145#endif
3146#ifdef FEAT_SIGNS
3147# define WL_SIGN WL_FOLD + 1 /* column for signs */
3148#else
3149# define WL_SIGN WL_FOLD /* column for signs */
3150#endif
3151#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003152#ifdef FEAT_LINEBREAK
3153# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003155# define WL_BRI WL_NR
3156#endif
3157#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3158# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3159#else
3160# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#endif
3162#define WL_LINE WL_SBR + 1 /* text in the line */
3163 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003164#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 int feedback_col = 0;
3166 int feedback_old_attr = -1;
3167#endif
3168
Bram Moolenaar860cae12010-06-05 23:22:07 +02003169#ifdef FEAT_CONCEAL
3170 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003171 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003172 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003173 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003174 int is_concealing = FALSE;
3175 int boguscols = 0; /* nonexistent columns added to force
3176 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003177 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003178 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003179 int match_conc = 0; /* cchar for match functions */
3180 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003181 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003182# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003183# define FIX_FOR_BOGUSCOLS \
3184 { \
3185 n_extra += vcol_off; \
3186 vcol -= vcol_off; \
3187 vcol_off = 0; \
3188 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003189 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003190 boguscols = 0; \
3191 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003192#else
3193# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196 if (startrow > endrow) /* past the end already! */
3197 return startrow;
3198
3199 row = startrow;
3200 screen_row = row + W_WINROW(wp);
3201
3202 /*
3203 * To speed up the loop below, set extra_check when there is linebreak,
3204 * trailing white space and/or syntax processing to be done.
3205 */
3206#ifdef FEAT_LINEBREAK
3207 extra_check = wp->w_p_lbr;
3208#else
3209 extra_check = 0;
3210#endif
3211#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003212 if (syntax_present(wp) && !wp->w_s->b_syn_error
3213# ifdef SYN_TIME_LIMIT
3214 && !wp->w_s->b_syn_slow
3215# endif
3216 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 {
3218 /* Prepare for syntax highlighting in this line. When there is an
3219 * error, stop syntax highlighting. */
3220 save_did_emsg = did_emsg;
3221 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003222 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003224 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 else
3226 {
3227 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003228#ifdef SYN_TIME_LIMIT
3229 if (!wp->w_s->b_syn_slow)
3230#endif
3231 {
3232 has_syntax = TRUE;
3233 extra_check = TRUE;
3234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
3236 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003237
3238 /* Check for columns to display for 'colorcolumn'. */
3239 color_cols = wp->w_p_cc_cols;
3240 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003241 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003242#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003243
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003244#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003245 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003246 {
3247 extra_check = TRUE;
3248 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003249 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003250 }
3251#endif
3252
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003253#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003254 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003255 && *wp->w_s->b_p_spl != NUL
3256 && wp->w_s->b_langp.ga_len > 0
3257 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003258 {
3259 /* Prepare for spell checking. */
3260 has_spell = TRUE;
3261 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003262
3263 /* Get the start of the next line, so that words that wrap to the next
3264 * line are found too: "et<line-break>al.".
3265 * Trick: skip a few chars for C/shell/Vim comments */
3266 nextline[SPWORDLEN] = NUL;
3267 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3268 {
3269 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3270 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3271 }
3272
3273 /* When a word wrapped from the previous line the start of the current
3274 * line is valid. */
3275 if (lnum == checked_lnum)
3276 cur_checked_col = checked_col;
3277 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003278
3279 /* When there was a sentence end in the previous line may require a
3280 * word starting with capital in this line. In line 1 always check
3281 * the first word. */
3282 if (lnum != capcol_lnum)
3283 cap_col = -1;
3284 if (lnum == 1)
3285 cap_col = 0;
3286 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288#endif
3289
3290 /*
3291 * handle visual active in this window
3292 */
3293 fromcol = -10;
3294 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3296 {
3297 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003298 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
3300 top = &curwin->w_cursor;
3301 bot = &VIsual;
3302 }
3303 else /* Visual is before curwin->w_cursor */
3304 {
3305 top = &VIsual;
3306 bot = &curwin->w_cursor;
3307 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003308 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 if (VIsual_mode == Ctrl_V) /* block mode */
3310 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003311 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 fromcol = wp->w_old_cursor_fcol;
3314 tocol = wp->w_old_cursor_lcol;
3315 }
3316 }
3317 else /* non-block mode */
3318 {
3319 if (lnum > top->lnum && lnum <= bot->lnum)
3320 fromcol = 0;
3321 else if (lnum == top->lnum)
3322 {
3323 if (VIsual_mode == 'V') /* linewise */
3324 fromcol = 0;
3325 else
3326 {
3327 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3328 if (gchar_pos(top) == NUL)
3329 tocol = fromcol + 1;
3330 }
3331 }
3332 if (VIsual_mode != 'V' && lnum == bot->lnum)
3333 {
3334 if (*p_sel == 'e' && bot->col == 0
3335#ifdef FEAT_VIRTUALEDIT
3336 && bot->coladd == 0
3337#endif
3338 )
3339 {
3340 fromcol = -10;
3341 tocol = MAXCOL;
3342 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003343 else if (bot->col == MAXCOL)
3344 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 else
3346 {
3347 pos = *bot;
3348 if (*p_sel == 'e')
3349 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3350 else
3351 {
3352 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3353 ++tocol;
3354 }
3355 }
3356 }
3357 }
3358
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 /* Check if the character under the cursor should not be inverted */
3360 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003361#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 )
3365 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367 /* if inverting in this line set area_highlighting */
3368 if (fromcol >= 0)
3369 {
3370 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003371 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003373 if ((clip_star.available && !clip_star.owned
3374 && clip_isautosel_star())
3375 || (clip_plus.available && !clip_plus.owned
3376 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003377 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378#endif
3379 }
3380 }
3381
3382 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003383 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003385 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 && wp == curwin
3387 && lnum >= curwin->w_cursor.lnum
3388 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3389 {
3390 if (lnum == curwin->w_cursor.lnum)
3391 getvcol(curwin, &(curwin->w_cursor),
3392 (colnr_T *)&fromcol, NULL, NULL);
3393 else
3394 fromcol = 0;
3395 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3396 {
3397 pos.lnum = lnum;
3398 pos.col = search_match_endcol;
3399 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3400 }
3401 else
3402 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003403 /* do at least one character; happens when past end of line */
3404 if (fromcol == tocol)
3405 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003407 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
3409
3410#ifdef FEAT_DIFF
3411 filler_lines = diff_check(wp, lnum);
3412 if (filler_lines < 0)
3413 {
3414 if (filler_lines == -1)
3415 {
3416 if (diff_find_change(wp, lnum, &change_start, &change_end))
3417 diff_hlf = HLF_ADD; /* added line */
3418 else if (change_start == 0)
3419 diff_hlf = HLF_TXD; /* changed text */
3420 else
3421 diff_hlf = HLF_CHD; /* changed line */
3422 }
3423 else
3424 diff_hlf = HLF_ADD; /* added line */
3425 filler_lines = 0;
3426 area_highlighting = TRUE;
3427 }
3428 if (lnum == wp->w_topline)
3429 filler_lines = wp->w_topfill;
3430 filler_todo = filler_lines;
3431#endif
3432
3433#ifdef LINE_ATTR
3434# ifdef FEAT_SIGNS
3435 /* If this line has a sign with line highlighting set line_attr. */
3436 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3437 if (v != 0)
3438 line_attr = sign_get_attr((int)v, TRUE);
3439# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003440# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003442 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003443 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444# endif
3445 if (line_attr != 0)
3446 area_highlighting = TRUE;
3447#endif
3448
3449 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3450 ptr = line;
3451
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003452#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003453 if (has_spell)
3454 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003455 /* For checking first word with a capital skip white space. */
3456 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003457 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003458
Bram Moolenaar30abd282005-06-22 22:35:10 +00003459 /* To be able to spell-check over line boundaries copy the end of the
3460 * current line into nextline[]. Above the start of the next line was
3461 * copied to nextline[SPWORDLEN]. */
3462 if (nextline[SPWORDLEN] == NUL)
3463 {
3464 /* No next line or it is empty. */
3465 nextlinecol = MAXCOL;
3466 nextline_idx = 0;
3467 }
3468 else
3469 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003470 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003471 if (v < SPWORDLEN)
3472 {
3473 /* Short line, use it completely and append the start of the
3474 * next line. */
3475 nextlinecol = 0;
3476 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003477 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003478 nextline_idx = v + 1;
3479 }
3480 else
3481 {
3482 /* Long line, use only the last SPWORDLEN bytes. */
3483 nextlinecol = v - SPWORDLEN;
3484 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3485 nextline_idx = SPWORDLEN + 1;
3486 }
3487 }
3488 }
3489#endif
3490
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003491 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003493 if (lcs_space || lcs_trail)
3494 extra_check = TRUE;
3495 /* find start of trailing whitespace */
3496 if (lcs_trail)
3497 {
3498 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003499 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003500 --trailcol;
3501 trailcol += (colnr_T) (ptr - line);
3502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504
3505 /*
3506 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3507 * first character to be displayed.
3508 */
3509 if (wp->w_p_wrap)
3510 v = wp->w_skipcol;
3511 else
3512 v = wp->w_leftcol;
3513 if (v > 0)
3514 {
3515#ifdef FEAT_MBYTE
3516 char_u *prev_ptr = ptr;
3517#endif
3518 while (vcol < v && *ptr != NUL)
3519 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003520 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 vcol += c;
3522#ifdef FEAT_MBYTE
3523 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003525 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003528 /* When:
3529 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003530 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003531 * - 'virtualedit' is set, or
3532 * - the visual mode is active,
3533 * the end of the line may be before the start of the displayed part.
3534 */
3535 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003536#ifdef FEAT_SYN_HL
3537 wp->w_p_cuc || draw_color_col ||
3538#endif
3539#ifdef FEAT_VIRTUALEDIT
3540 virtual_active() ||
3541#endif
3542 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
3547 /* Handle a character that's not completely on the screen: Put ptr at
3548 * that character but skip the first few screen characters. */
3549 if (vcol > v)
3550 {
3551 vcol -= c;
3552#ifdef FEAT_MBYTE
3553 ptr = prev_ptr;
3554#else
3555 --ptr;
3556#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003557 /* If the character fits on the screen, don't need to skip it.
3558 * Except for a TAB. */
3559 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003560#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003561 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003562#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003563 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003564 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566
3567 /*
3568 * Adjust for when the inverted text is before the screen,
3569 * and when the start of the inverted text is before the screen.
3570 */
3571 if (tocol <= vcol)
3572 fromcol = 0;
3573 else if (fromcol >= 0 && fromcol < vcol)
3574 fromcol = vcol;
3575
3576#ifdef FEAT_LINEBREAK
3577 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3578 if (wp->w_p_wrap)
3579 need_showbreak = TRUE;
3580#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003581#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003582 /* When spell checking a word we need to figure out the start of the
3583 * word and if it's badly spelled or not. */
3584 if (has_spell)
3585 {
3586 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003587 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003588 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003589
3590 pos = wp->w_cursor;
3591 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003592 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003593 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003594
3595 /* spell_move_to() may call ml_get() and make "line" invalid */
3596 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3597 ptr = line + linecol;
3598
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003599 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003600 {
3601 /* no bad word found at line start, don't check until end of a
3602 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003603 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003604 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003605 }
3606 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003607 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003608 /* bad word found, use attributes until end of word */
3609 word_end = wp->w_cursor.col + len + 1;
3610
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003611 /* Turn index into actual attributes. */
3612 if (spell_hlf != HLF_COUNT)
3613 spell_attr = highlight_attr[spell_hlf];
3614 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003615 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003616
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003617# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003618 /* Need to restart syntax highlighting for this line. */
3619 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003620 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003621# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003622 }
3623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
3625
3626 /*
3627 * Correct highlighting for cursor that can't be disabled.
3628 * Avoids having to check this for each character.
3629 */
3630 if (fromcol >= 0)
3631 {
3632 if (noinvcur)
3633 {
3634 if ((colnr_T)fromcol == wp->w_virtcol)
3635 {
3636 /* highlighting starts at cursor, let it start just after the
3637 * cursor */
3638 fromcol_prev = fromcol;
3639 fromcol = -1;
3640 }
3641 else if ((colnr_T)fromcol < wp->w_virtcol)
3642 /* restart highlighting after the cursor */
3643 fromcol_prev = wp->w_virtcol;
3644 }
3645 if (fromcol >= tocol)
3646 fromcol = -1;
3647 }
3648
3649#ifdef FEAT_SEARCH_EXTRA
3650 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003651 * Handle highlighting the last used search pattern and matches.
3652 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003654 cur = wp->w_match_head;
3655 shl_flag = FALSE;
3656 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003658 if (shl_flag == FALSE)
3659 {
3660 shl = &search_hl;
3661 shl_flag = TRUE;
3662 }
3663 else
3664 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003665 shl->startcol = MAXCOL;
3666 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003668 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003669 v = (long)(ptr - line);
3670 if (cur != NULL)
3671 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003672 next_search_hl(wp, shl, lnum, (colnr_T)v,
3673 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003674
3675 /* Need to get the line again, a multi-line regexp may have made it
3676 * invalid. */
3677 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3678 ptr = line + v;
3679
3680 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003682 if (shl->lnum == lnum)
3683 shl->startcol = shl->rm.startpos[0].col;
3684 else
3685 shl->startcol = 0;
3686 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3687 - shl->rm.startpos[0].lnum)
3688 shl->endcol = shl->rm.endpos[0].col;
3689 else
3690 shl->endcol = MAXCOL;
3691 /* Highlight one character for an empty match. */
3692 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003695 if (has_mbyte && line[shl->endcol] != NUL)
3696 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3697 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003699 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003701 if ((long)shl->startcol < v) /* match at leftcol */
3702 {
3703 shl->attr_cur = shl->attr;
3704 search_attr = shl->attr;
3705 }
3706 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003708 if (shl != &search_hl && cur != NULL)
3709 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711#endif
3712
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003713#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003714 /* Cursor line highlighting for 'cursorline' in the current window. Not
3715 * when Visual mode is active, because it's not clear what is selected
3716 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003717 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003718 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003719 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003720 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003721 area_highlighting = TRUE;
3722 }
3723#endif
3724
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003725 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 col = 0;
3727#ifdef FEAT_RIGHTLEFT
3728 if (wp->w_p_rl)
3729 {
3730 /* Rightleft window: process the text in the normal direction, but put
3731 * it in current_ScreenLine[] from right to left. Start at the
3732 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003733 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 off += col;
3735 }
3736#endif
3737
3738 /*
3739 * Repeat for the whole displayed line.
3740 */
3741 for (;;)
3742 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003743#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003744 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 /* Skip this quickly when working on the text. */
3747 if (draw_state != WL_LINE)
3748 {
3749#ifdef FEAT_CMDWIN
3750 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3751 {
3752 draw_state = WL_CMDLINE;
3753 if (cmdwin_type != 0 && wp == curwin)
3754 {
3755 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003757 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003758 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
3760 }
3761#endif
3762
3763#ifdef FEAT_FOLDING
3764 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3765 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003766 int fdc = compute_foldcolumn(wp, 0);
3767
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003769 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003771 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003772 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003773 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003774 p_extra_free = alloc(12 + 1);
3775
3776 if (p_extra_free != NULL)
3777 {
3778 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3779 n_extra = fdc;
3780 p_extra_free[n_extra] = NUL;
3781 p_extra = p_extra_free;
3782 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003783 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786 }
3787#endif
3788
3789#ifdef FEAT_SIGNS
3790 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3791 {
3792 draw_state = WL_SIGN;
3793 /* Show the sign column when there are any signs in this
3794 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003795 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003797 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003799 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800# endif
3801
3802 /* Draw two cells with the sign value or blank. */
3803 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003804 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 n_extra = 2;
3806
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003807 if (row == startrow
3808#ifdef FEAT_DIFF
3809 + filler_lines && filler_todo <= 0
3810#endif
3811 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
3813 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3814 SIGN_TEXT);
3815# ifdef FEAT_SIGN_ICONS
3816 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3817 SIGN_ICON);
3818 if (gui.in_use && icon_sign != 0)
3819 {
3820 /* Use the image in this position. */
3821 c_extra = SIGN_BYTE;
3822# ifdef FEAT_NETBEANS_INTG
3823 if (buf_signcount(wp->w_buffer, lnum) > 1)
3824 c_extra = MULTISIGN_BYTE;
3825# endif
3826 char_attr = icon_sign;
3827 }
3828 else
3829# endif
3830 if (text_sign != 0)
3831 {
3832 p_extra = sign_get_text(text_sign);
3833 if (p_extra != NULL)
3834 {
3835 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003836 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 char_attr = sign_get_attr(text_sign, FALSE);
3839 }
3840 }
3841 }
3842 }
3843#endif
3844
3845 if (draw_state == WL_NR - 1 && n_extra == 0)
3846 {
3847 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003848 /* Display the absolute or relative line number. After the
3849 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3850 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 && (row == startrow
3852#ifdef FEAT_DIFF
3853 + filler_lines
3854#endif
3855 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3856 {
3857 /* Draw the line number (empty space after wrapping). */
3858 if (row == startrow
3859#ifdef FEAT_DIFF
3860 + filler_lines
3861#endif
3862 )
3863 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003864 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003865 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003866
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003867 if (wp->w_p_nu && !wp->w_p_rnu)
3868 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003869 num = (long)lnum;
3870 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003871 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003872 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003873 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003874 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003875 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003876 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003877 num = lnum;
3878 fmt = "%-*ld ";
3879 }
3880 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003881
Bram Moolenaar700e7342013-01-30 12:31:36 +01003882 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003883 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (wp->w_skipcol > 0)
3885 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3886 *p_extra = '-';
3887#ifdef FEAT_RIGHTLEFT
3888 if (wp->w_p_rl) /* reverse line numbers */
3889 rl_mirror(extra);
3890#endif
3891 p_extra = extra;
3892 c_extra = NUL;
3893 }
3894 else
3895 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003896 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003897 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003898#ifdef FEAT_SYN_HL
3899 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003900 * the current line differently.
3901 * TODO: Can we use CursorLine instead of CursorLineNr
3902 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003903 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003904 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003905 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 }
3909
Bram Moolenaar597a4222014-06-25 14:39:50 +02003910#ifdef FEAT_LINEBREAK
3911 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3912 && n_extra == 0 && *p_sbr != NUL)
3913 /* draw indent after showbreak value */
3914 draw_state = WL_BRI;
3915 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3916 /* After the showbreak, draw the breakindent */
3917 draw_state = WL_BRI - 1;
3918
3919 /* draw 'breakindent': indent wrapped text accordingly */
3920 if (draw_state == WL_BRI - 1 && n_extra == 0)
3921 {
3922 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003923 /* if need_showbreak is set, breakindent also applies */
3924 if (wp->w_p_bri && n_extra == 0
3925 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003926# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003927 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003928# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003929 )
3930 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003931 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003932# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003933 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003934 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003935 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003936# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003937 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3938 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003939 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003940# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003941 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003942# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003943 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003944 c_extra = ' ';
3945 n_extra = get_breakindent_win(wp,
3946 ml_get_buf(wp->w_buffer, lnum, FALSE));
3947 /* Correct end of highlighted area for 'breakindent',
3948 * required when 'linebreak' is also set. */
3949 if (tocol == vcol)
3950 tocol += n_extra;
3951 }
3952 }
3953#endif
3954
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3956 if (draw_state == WL_SBR - 1 && n_extra == 0)
3957 {
3958 draw_state = WL_SBR;
3959# ifdef FEAT_DIFF
3960 if (filler_todo > 0)
3961 {
3962 /* Draw "deleted" diff line(s). */
3963 if (char2cells(fill_diff) > 1)
3964 c_extra = '-';
3965 else
3966 c_extra = fill_diff;
3967# ifdef FEAT_RIGHTLEFT
3968 if (wp->w_p_rl)
3969 n_extra = col + 1;
3970 else
3971# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02003972 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003973 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975# endif
3976# ifdef FEAT_LINEBREAK
3977 if (*p_sbr != NUL && need_showbreak)
3978 {
3979 /* Draw 'showbreak' at the start of each broken line. */
3980 p_extra = p_sbr;
3981 c_extra = NUL;
3982 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003983 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003985 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 /* Correct end of highlighted area for 'showbreak',
3987 * required when 'linebreak' is also set. */
3988 if (tocol == vcol)
3989 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003990#ifdef FEAT_SYN_HL
3991 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003992 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003993 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003994 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997# endif
3998 }
3999#endif
4000
4001 if (draw_state == WL_LINE - 1 && n_extra == 0)
4002 {
4003 draw_state = WL_LINE;
4004 if (saved_n_extra)
4005 {
4006 /* Continue item from end of wrapped line. */
4007 n_extra = saved_n_extra;
4008 c_extra = saved_c_extra;
4009 p_extra = saved_p_extra;
4010 char_attr = saved_char_attr;
4011 }
4012 else
4013 char_attr = 0;
4014 }
4015 }
4016
4017 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004018 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004019 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#ifdef FEAT_DIFF
4021 && filler_todo <= 0
4022#endif
4023 )
4024 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004025 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004026 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004027 /* Pretend we have finished updating the window. Except when
4028 * 'cursorcolumn' is set. */
4029#ifdef FEAT_SYN_HL
4030 if (wp->w_p_cuc)
4031 row = wp->w_cline_row + wp->w_cline_height;
4032 else
4033#endif
4034 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 break;
4036 }
4037
4038 if (draw_state == WL_LINE && area_highlighting)
4039 {
4040 /* handle Visual or match highlighting in this line */
4041 if (vcol == fromcol
4042#ifdef FEAT_MBYTE
4043 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4044 && (*mb_ptr2cells)(ptr) > 1)
4045#endif
4046 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004047 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 && vcol < tocol))
4049 area_attr = attr; /* start highlighting */
4050 else if (area_attr != 0
4051 && (vcol == tocol
4052 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055#ifdef FEAT_SEARCH_EXTRA
4056 if (!n_extra)
4057 {
4058 /*
4059 * Check for start/end of search pattern match.
4060 * After end, check for start/end of next match.
4061 * When another match, have to check for start again.
4062 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004063 * Do this for 'search_hl' and the match list (ordered by
4064 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004066 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004067 cur = wp->w_match_head;
4068 shl_flag = FALSE;
4069 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004071 if (shl_flag == FALSE
4072 && ((cur != NULL
4073 && cur->priority > SEARCH_HL_PRIORITY)
4074 || cur == NULL))
4075 {
4076 shl = &search_hl;
4077 shl_flag = TRUE;
4078 }
4079 else
4080 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004081 if (cur != NULL)
4082 cur->pos.cur = 0;
4083 pos_inprogress = TRUE;
4084 while (shl->rm.regprog != NULL
4085 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004087 if (shl->startcol != MAXCOL
4088 && v >= (long)shl->startcol
4089 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004091#ifdef FEAT_MBYTE
4092 int tmp_col = v + MB_PTR2LEN(ptr);
4093
4094 if (shl->endcol < tmp_col)
4095 shl->endcol = tmp_col;
4096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004098#ifdef FEAT_CONCEAL
4099 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4100 == cur->hlg_id)
4101 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004102 has_match_conc =
4103 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004104 match_conc = cur->conceal_char;
4105 }
4106 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004107 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004110 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 {
4112 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004113 next_search_hl(wp, shl, lnum, (colnr_T)v,
4114 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004115 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004116 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
4118 /* Need to get the line again, a multi-line regexp
4119 * may have made it invalid. */
4120 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4121 ptr = line + v;
4122
4123 if (shl->lnum == lnum)
4124 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004125 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004127 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004129 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004131 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
4133 /* highlight empty match, try again after
4134 * it */
4135#ifdef FEAT_MBYTE
4136 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004137 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004138 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 else
4140#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004141 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143
4144 /* Loop to check if the match starts at the
4145 * current position */
4146 continue;
4147 }
4148 }
4149 break;
4150 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004151 if (shl != &search_hl && cur != NULL)
4152 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004154
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004155 /* Use attributes from match with highest priority among
4156 * 'search_hl' and the match list. */
4157 search_attr = search_hl.attr_cur;
4158 cur = wp->w_match_head;
4159 shl_flag = FALSE;
4160 while (cur != NULL || shl_flag == FALSE)
4161 {
4162 if (shl_flag == FALSE
4163 && ((cur != NULL
4164 && cur->priority > SEARCH_HL_PRIORITY)
4165 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004166 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004167 shl = &search_hl;
4168 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004169 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004170 else
4171 shl = &cur->hl;
4172 if (shl->attr_cur != 0)
4173 search_attr = shl->attr_cur;
4174 if (shl != &search_hl && cur != NULL)
4175 cur = cur->next;
4176 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004177 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004178 if (*ptr == NUL && (did_line_attr >= 1
4179 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004180 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182#endif
4183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004185 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004187 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4188 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004190 if (diff_hlf == HLF_TXD && ptr - line > change_end
4191 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004193 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004194 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004195 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004198
4199 /* Decide which of the highlight attributes to use. */
4200 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004201#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004202 if (area_attr != 0)
4203 char_attr = hl_combine_attr(line_attr, area_attr);
4204 else if (search_attr != 0)
4205 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004206 /* Use line_attr when not in the Visual or 'incsearch' area
4207 * (area_attr may be 0 when "noinvcur" is set). */
4208 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004209 || vcol < fromcol || vcol_prev < fromcol_prev
4210 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004211 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004212#else
4213 if (area_attr != 0)
4214 char_attr = area_attr;
4215 else if (search_attr != 0)
4216 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004217#endif
4218 else
4219 {
4220 attr_pri = FALSE;
4221#ifdef FEAT_SYN_HL
4222 if (has_syntax)
4223 char_attr = syntax_attr;
4224 else
4225#endif
4226 char_attr = 0;
4227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229
4230 /*
4231 * Get the next character to put on the screen.
4232 */
4233 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004234 * The "p_extra" points to the extra stuff that is inserted to
4235 * represent special characters (non-printable stuff) and other
4236 * things. When all characters are the same, c_extra is used.
4237 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4238 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4240 */
4241 if (n_extra > 0)
4242 {
4243 if (c_extra != NUL)
4244 {
4245 c = c_extra;
4246#ifdef FEAT_MBYTE
4247 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004248 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004251 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004252 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 else
4255 mb_utf8 = FALSE;
4256#endif
4257 }
4258 else
4259 {
4260 c = *p_extra;
4261#ifdef FEAT_MBYTE
4262 if (has_mbyte)
4263 {
4264 mb_c = c;
4265 if (enc_utf8)
4266 {
4267 /* If the UTF-8 character is more than one byte:
4268 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004269 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 mb_utf8 = FALSE;
4271 if (mb_l > n_extra)
4272 mb_l = 1;
4273 else if (mb_l > 1)
4274 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004275 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004277 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 }
4280 else
4281 {
4282 /* if this is a DBCS character, put it in "mb_c" */
4283 mb_l = MB_BYTE2LEN(c);
4284 if (mb_l >= n_extra)
4285 mb_l = 1;
4286 else if (mb_l > 1)
4287 mb_c = (c << 8) + p_extra[1];
4288 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004289 if (mb_l == 0) /* at the NUL at end-of-line */
4290 mb_l = 1;
4291
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 /* If a double-width char doesn't fit display a '>' in the
4293 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004294 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295# ifdef FEAT_RIGHTLEFT
4296 wp->w_p_rl ? (col <= 0) :
4297# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004298 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 && (*mb_char2cells)(mb_c) == 2)
4300 {
4301 c = '>';
4302 mb_c = c;
4303 mb_l = 1;
4304 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004305 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 /* put the pointer back to output the double-width
4307 * character at the start of the next line. */
4308 ++n_extra;
4309 --p_extra;
4310 }
4311 else
4312 {
4313 n_extra -= mb_l - 1;
4314 p_extra += mb_l - 1;
4315 }
4316 }
4317#endif
4318 ++p_extra;
4319 }
4320 --n_extra;
4321 }
4322 else
4323 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004324#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004325 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004326#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004327
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004328 if (p_extra_free != NULL)
4329 {
4330 vim_free(p_extra_free);
4331 p_extra_free = NULL;
4332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 /*
4334 * Get a character from the line itself.
4335 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004336 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004337#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004338 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340#ifdef FEAT_MBYTE
4341 if (has_mbyte)
4342 {
4343 mb_c = c;
4344 if (enc_utf8)
4345 {
4346 /* If the UTF-8 character is more than one byte: Decode it
4347 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004348 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 mb_utf8 = FALSE;
4350 if (mb_l > 1)
4351 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004352 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 /* Overlong encoded ASCII or ASCII with composing char
4354 * is displayed normally, except a NUL. */
4355 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004356 {
4357 c = mb_c;
4358# ifdef FEAT_LINEBREAK
4359 c0 = mb_c;
4360# endif
4361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004363
4364 /* At start of the line we can have a composing char.
4365 * Draw it as a space with a composing char. */
4366 if (utf_iscomposing(mb_c))
4367 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004368 int i;
4369
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004370 for (i = Screen_mco - 1; i > 0; --i)
4371 u8cc[i] = u8cc[i - 1];
4372 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004373 mb_c = ' ';
4374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376
4377 if ((mb_l == 1 && c >= 0x80)
4378 || (mb_l >= 1 && mb_c == 0)
4379 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004380# ifdef UNICODE16
4381 || mb_c >= 0x10000
4382# endif
4383 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 {
4385 /*
4386 * Illegal UTF-8 byte: display as <xx>.
4387 * Non-BMP character : display as ? or fullwidth ?.
4388 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004389# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
4393 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004394# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 if (wp->w_p_rl) /* reverse */
4396 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004399# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 else if (utf_char2cells(mb_c) != 2)
4401 STRCPY(extra, "?");
4402 else
4403 /* 0xff1f in UTF-8: full-width '?' */
4404 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
4407 p_extra = extra;
4408 c = *p_extra;
4409 mb_c = mb_ptr2char_adv(&p_extra);
4410 mb_utf8 = (c >= 0x80);
4411 n_extra = (int)STRLEN(p_extra);
4412 c_extra = NUL;
4413 if (area_attr == 0 && search_attr == 0)
4414 {
4415 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004416 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 saved_attr2 = char_attr; /* save current attr */
4418 }
4419 }
4420 else if (mb_l == 0) /* at the NUL at end-of-line */
4421 mb_l = 1;
4422#ifdef FEAT_ARABIC
4423 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4424 {
4425 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004426 int pc, pc1, nc;
4427 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429 /* The idea of what is the previous and next
4430 * character depends on 'rightleft'. */
4431 if (wp->w_p_rl)
4432 {
4433 pc = prev_c;
4434 pc1 = prev_c1;
4435 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004436 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 else
4439 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004440 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004442 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444 prev_c = mb_c;
4445
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004446 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 else
4449 prev_c = mb_c;
4450#endif
4451 }
4452 else /* enc_dbcs */
4453 {
4454 mb_l = MB_BYTE2LEN(c);
4455 if (mb_l == 0) /* at the NUL at end-of-line */
4456 mb_l = 1;
4457 else if (mb_l > 1)
4458 {
4459 /* We assume a second byte below 32 is illegal.
4460 * Hopefully this is OK for all double-byte encodings!
4461 */
4462 if (ptr[1] >= 32)
4463 mb_c = (c << 8) + ptr[1];
4464 else
4465 {
4466 if (ptr[1] == NUL)
4467 {
4468 /* head byte at end of line */
4469 mb_l = 1;
4470 transchar_nonprint(extra, c);
4471 }
4472 else
4473 {
4474 /* illegal tail byte */
4475 mb_l = 2;
4476 STRCPY(extra, "XX");
4477 }
4478 p_extra = extra;
4479 n_extra = (int)STRLEN(extra) - 1;
4480 c_extra = NUL;
4481 c = *p_extra++;
4482 if (area_attr == 0 && search_attr == 0)
4483 {
4484 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004485 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 saved_attr2 = char_attr; /* save current attr */
4487 }
4488 mb_c = c;
4489 }
4490 }
4491 }
4492 /* If a double-width char doesn't fit display a '>' in the
4493 * last column; the character is displayed at the start of the
4494 * next line. */
4495 if ((
4496# ifdef FEAT_RIGHTLEFT
4497 wp->w_p_rl ? (col <= 0) :
4498# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004499 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 && (*mb_char2cells)(mb_c) == 2)
4501 {
4502 c = '>';
4503 mb_c = c;
4504 mb_utf8 = FALSE;
4505 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004506 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 /* Put pointer back so that the character will be
4508 * displayed at the start of the next line. */
4509 --ptr;
4510 }
4511 else if (*ptr != NUL)
4512 ptr += mb_l - 1;
4513
4514 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004515 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004516 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004517 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004520 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 c = ' ';
4522 if (area_attr == 0 && search_attr == 0)
4523 {
4524 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004525 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 saved_attr2 = char_attr; /* save current attr */
4527 }
4528 mb_c = c;
4529 mb_utf8 = FALSE;
4530 mb_l = 1;
4531 }
4532
4533 }
4534#endif
4535 ++ptr;
4536
4537 if (extra_check)
4538 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004539#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004540 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004541#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004542
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004543#ifdef FEAT_TERMINAL
4544 if (get_term_attr)
4545 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004546 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004547
4548 if (!attr_pri)
4549 char_attr = syntax_attr;
4550 else
4551 char_attr = hl_combine_attr(syntax_attr, char_attr);
4552 }
4553#endif
4554
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004555#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 /* Get syntax attribute, unless still at the start of the line
4557 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004558 v = (long)(ptr - line);
4559 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
4561 /* Get the syntax attribute for the character. If there
4562 * is an error, disable syntax highlighting. */
4563 save_did_emsg = did_emsg;
4564 did_emsg = FALSE;
4565
Bram Moolenaar217ad922005-03-20 22:37:15 +00004566 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004567# ifdef FEAT_SPELL
4568 has_spell ? &can_spell :
4569# endif
4570 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571
4572 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004573 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004574 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004575 has_syntax = FALSE;
4576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 else
4578 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004579#ifdef SYN_TIME_LIMIT
4580 if (wp->w_s->b_syn_slow)
4581 has_syntax = FALSE;
4582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583
4584 /* Need to get the line again, a multi-line regexp may
4585 * have made it invalid. */
4586 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4587 ptr = line + v;
4588
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004589 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004591 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004592 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004593# ifdef FEAT_CONCEAL
4594 /* no concealing past the end of the line, it interferes
4595 * with line highlighting */
4596 if (c == NUL)
4597 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004598 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004599 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004600# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004602#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004603
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004604#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004605 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004606 * Only do this when there is no syntax highlighting, the
4607 * @Spell cluster is not used or the current syntax item
4608 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004609 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004610 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004611 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004612# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004613 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004614 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004615# endif
4616 if (c != 0 && (
4617# ifdef FEAT_SYN_HL
4618 !has_syntax ||
4619# endif
4620 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004621 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004622 char_u *prev_ptr, *p;
4623 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004624 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004625# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004626 if (has_mbyte)
4627 {
4628 prev_ptr = ptr - mb_l;
4629 v -= mb_l - 1;
4630 }
4631 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004632# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004633 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004634
4635 /* Use nextline[] if possible, it has the start of the
4636 * next line concatenated. */
4637 if ((prev_ptr - line) - nextlinecol >= 0)
4638 p = nextline + (prev_ptr - line) - nextlinecol;
4639 else
4640 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004641 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004642 len = spell_check(wp, p, &spell_hlf, &cap_col,
4643 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004644 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004645
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004646 /* In Insert mode only highlight a word that
4647 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004648 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004649 && (State & INSERT) != 0
4650 && wp->w_cursor.lnum == lnum
4651 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004652 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004653 && wp->w_cursor.col < (colnr_T)word_end)
4654 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004655 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004656 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004657 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004658
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004659 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004660 && (p - nextline) + len > nextline_idx)
4661 {
4662 /* Remember that the good word continues at the
4663 * start of the next line. */
4664 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004665 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004666 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004667
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004668 /* Turn index into actual attributes. */
4669 if (spell_hlf != HLF_COUNT)
4670 spell_attr = highlight_attr[spell_hlf];
4671
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004672 if (cap_col > 0)
4673 {
4674 if (p != prev_ptr
4675 && (p - nextline) + cap_col >= nextline_idx)
4676 {
4677 /* Remember that the word in the next line
4678 * must start with a capital. */
4679 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004680 cap_col = (int)((p - nextline) + cap_col
4681 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004682 }
4683 else
4684 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004685 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004686 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004687 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004688 }
4689 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004690 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004691 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004692 char_attr = hl_combine_attr(char_attr, spell_attr);
4693 else
4694 char_attr = hl_combine_attr(spell_attr, char_attr);
4695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696#endif
4697#ifdef FEAT_LINEBREAK
4698 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004699 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004701 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004702 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004704# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004705 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004706# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004707 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004709 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004711 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004712
Bram Moolenaar597a4222014-06-25 14:39:50 +02004713 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004714 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004715 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004716 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaara3650912014-11-19 13:21:57 +01004717 n_extra = (int)wp->w_buffer->b_p_ts
4718 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4719
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004720# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004721 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004722# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004724# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004725 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004726 {
4727#ifdef FEAT_CONCEAL
4728 if (c == TAB)
4729 /* See "Tab alignment" below. */
4730 FIX_FOR_BOGUSCOLS;
4731#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004732 if (!wp->w_p_list)
4733 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736#endif
4737
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004738 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4739 */
4740 if (wp->w_p_list
4741 && (((c == 160
4742#ifdef FEAT_MBYTE
4743 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4744#endif
4745 ) && lcs_nbsp)
4746 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4747 {
4748 c = (c == ' ') ? lcs_space : lcs_nbsp;
4749 if (area_attr == 0 && search_attr == 0)
4750 {
4751 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004752 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004753 saved_attr2 = char_attr; /* save current attr */
4754 }
4755#ifdef FEAT_MBYTE
4756 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004757 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004758 {
4759 mb_utf8 = TRUE;
4760 u8cc[0] = 0;
4761 c = 0xc0;
4762 }
4763 else
4764 mb_utf8 = FALSE;
4765#endif
4766 }
4767
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4769 {
4770 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004771 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 {
4773 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004774 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 saved_attr2 = char_attr; /* save current attr */
4776 }
4777#ifdef FEAT_MBYTE
4778 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004779 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 {
4781 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004782 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004783 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785 else
4786 mb_utf8 = FALSE;
4787#endif
4788 }
4789 }
4790
4791 /*
4792 * Handling of non-printable characters.
4793 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004794 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
4796 /*
4797 * when getting a character from the file, we may have to
4798 * turn it into something else on the way to putting it
4799 * into "ScreenLines".
4800 */
4801 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4802 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004803 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004804 long vcol_adjusted = vcol; /* removed showbreak length */
4805#ifdef FEAT_LINEBREAK
4806 /* only adjust the tab_len, when at the first column
4807 * after the showbreak value was drawn */
4808 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4809 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004812 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004813 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4814
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004815#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004816 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004817#endif
4818 /* tab amount depends on current column */
4819 n_extra = tab_len;
4820#ifdef FEAT_LINEBREAK
4821 else
4822 {
4823 char_u *p;
4824 int len = n_extra;
4825 int i;
4826 int saved_nextra = n_extra;
4827
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004828#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004829 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004830 /* there are characters to conceal */
4831 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004832 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4833 */
4834 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4835 && n_extra > tab_len)
4836 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004837#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004838
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004839 /* if n_extra > 0, it gives the number of chars, to
4840 * use for a tab, else we need to calculate the width
4841 * for a tab */
4842#ifdef FEAT_MBYTE
4843 len = (tab_len * mb_char2len(lcs_tab2));
4844 if (n_extra > 0)
4845 len += n_extra - tab_len;
4846#endif
4847 c = lcs_tab1;
4848 p = alloc((unsigned)(len + 1));
4849 vim_memset(p, ' ', len);
4850 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004851 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004852 p_extra_free = p;
4853 for (i = 0; i < tab_len; i++)
4854 {
4855#ifdef FEAT_MBYTE
4856 mb_char2bytes(lcs_tab2, p);
4857 p += mb_char2len(lcs_tab2);
4858 n_extra += mb_char2len(lcs_tab2)
4859 - (saved_nextra > 0 ? 1 : 0);
4860#else
4861 p[i] = lcs_tab2;
4862#endif
4863 }
4864 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004865#ifdef FEAT_CONCEAL
4866 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4867 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004868 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004869 n_extra -= vcol_off;
4870#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004871 }
4872#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004873#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004874 {
4875 int vc_saved = vcol_off;
4876
4877 /* Tab alignment should be identical regardless of
4878 * 'conceallevel' value. So tab compensates of all
4879 * previous concealed characters, and thus resets
4880 * vcol_off and boguscols accumulated so far in the
4881 * line. Note that the tab can be longer than
4882 * 'tabstop' when there are concealed characters. */
4883 FIX_FOR_BOGUSCOLS;
4884
4885 /* Make sure, the highlighting for the tab char will be
4886 * correctly set further below (effectively reverts the
4887 * FIX_FOR_BOGSUCOLS macro */
4888 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004889 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004890 tab_len += vc_saved;
4891 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893#ifdef FEAT_MBYTE
4894 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4895#endif
4896 if (wp->w_p_list)
4897 {
4898 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004899#ifdef FEAT_LINEBREAK
4900 if (wp->w_p_lbr)
4901 c_extra = NUL; /* using p_extra from above */
4902 else
4903#endif
4904 c_extra = lcs_tab2;
4905 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004906 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 saved_attr2 = char_attr; /* save current attr */
4908#ifdef FEAT_MBYTE
4909 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004910 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
4912 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004913 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004914 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916#endif
4917 }
4918 else
4919 {
4920 c_extra = ' ';
4921 c = ' ';
4922 }
4923 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004924 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004925 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004926 || ((fromcol >= 0 || fromcol_prev >= 0)
4927 && tocol > vcol
4928 && VIsual_mode != Ctrl_V
4929 && (
4930# ifdef FEAT_RIGHTLEFT
4931 wp->w_p_rl ? (col >= 0) :
4932# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004933 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004934 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004935 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004936 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004937 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004939 /* Display a '$' after the line or highlight an extra
4940 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4942 /* For a diff line the highlighting continues after the
4943 * "$". */
4944 if (
4945# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004946 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947# ifdef LINE_ATTR
4948 &&
4949# endif
4950# endif
4951# ifdef LINE_ATTR
4952 line_attr == 0
4953# endif
4954 )
4955#endif
4956 {
4957#ifdef FEAT_VIRTUALEDIT
4958 /* In virtualedit, visual selections may extend
4959 * beyond end of line. */
4960 if (area_highlighting && virtual_active()
4961 && tocol != MAXCOL && vcol < tocol)
4962 n_extra = 0;
4963 else
4964#endif
4965 {
4966 p_extra = at_end_str;
4967 n_extra = 1;
4968 c_extra = NUL;
4969 }
4970 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004971 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004972 c = lcs_eol;
4973 else
4974 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 lcs_eol_one = -1;
4976 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004977 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004979 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 n_attr = 1;
4981 }
4982#ifdef FEAT_MBYTE
4983 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004984 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004987 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004988 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990 else
4991 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4992#endif
4993 }
4994 else if (c != NUL)
4995 {
4996 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004997 if (n_extra == 0)
4998 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999#ifdef FEAT_RIGHTLEFT
5000 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5001 rl_mirror(p_extra); /* reverse "<12>" */
5002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005004#ifdef FEAT_LINEBREAK
5005 if (wp->w_p_lbr)
5006 {
5007 char_u *p;
5008
5009 c = *p_extra;
5010 p = alloc((unsigned)n_extra + 1);
5011 vim_memset(p, ' ', n_extra);
5012 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5013 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005014 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005015 p_extra_free = p_extra = p;
5016 }
5017 else
5018#endif
5019 {
5020 n_extra = byte2cells(c) - 1;
5021 c = *p_extra++;
5022 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005023 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
5025 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005026 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 saved_attr2 = char_attr; /* save current attr */
5028 }
5029#ifdef FEAT_MBYTE
5030 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5031#endif
5032 }
5033#ifdef FEAT_VIRTUALEDIT
5034 else if (VIsual_active
5035 && (VIsual_mode == Ctrl_V
5036 || VIsual_mode == 'v')
5037 && virtual_active()
5038 && tocol != MAXCOL
5039 && vcol < tocol
5040 && (
5041# ifdef FEAT_RIGHTLEFT
5042 wp->w_p_rl ? (col >= 0) :
5043# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005044 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {
5046 c = ' ';
5047 --ptr; /* put it back at the NUL */
5048 }
5049#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005050#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 else if ((
5052# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005053 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005055# ifdef FEAT_TERMINAL
5056 term_attr != 0 ||
5057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 ) && (
5060# ifdef FEAT_RIGHTLEFT
5061 wp->w_p_rl ? (col >= 0) :
5062# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005063 (col
5064# ifdef FEAT_CONCEAL
5065 - boguscols
5066# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005067 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
5069 /* Highlight until the right side of the window */
5070 c = ' ';
5071 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005072
5073 /* Remember we do the char for line highlighting. */
5074 ++did_line_attr;
5075
5076 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005077 if (line_attr != 0 && char_attr == search_attr
5078 && (did_line_attr > 1
5079 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005080 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081# ifdef FEAT_DIFF
5082 if (diff_hlf == HLF_TXD)
5083 {
5084 diff_hlf = HLF_CHD;
5085 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005086 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005087 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005088 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5089 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005090 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005094# ifdef FEAT_TERMINAL
5095 if (term_attr != 0)
5096 {
5097 char_attr = term_attr;
5098 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5099 char_attr = hl_combine_attr(char_attr,
5100 HL_ATTR(HLF_CUL));
5101 }
5102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
5104#endif
5105 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005106
5107#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005108 if ( wp->w_p_cole > 0
5109 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005110 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005111 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005112 && !(lnum_in_visual_area
5113 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005114 {
5115 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005116 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005117 && (syn_get_sub_char() != NUL || match_conc
5118 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005119 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005120 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005121 /* First time at this concealed item: display one
5122 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005123 if (match_conc)
5124 c = match_conc;
5125 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005126 c = syn_get_sub_char();
5127 else if (lcs_conceal != NUL)
5128 c = lcs_conceal;
5129 else
5130 c = ' ';
5131
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005132 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005133
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005134 if (n_extra > 0)
5135 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005136 vcol += n_extra;
5137 if (wp->w_p_wrap && n_extra > 0)
5138 {
5139# ifdef FEAT_RIGHTLEFT
5140 if (wp->w_p_rl)
5141 {
5142 col -= n_extra;
5143 boguscols -= n_extra;
5144 }
5145 else
5146# endif
5147 {
5148 boguscols += n_extra;
5149 col += n_extra;
5150 }
5151 }
5152 n_extra = 0;
5153 n_attr = 0;
5154 }
5155 else if (n_skip == 0)
5156 {
5157 is_concealing = TRUE;
5158 n_skip = 1;
5159 }
5160# ifdef FEAT_MBYTE
5161 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005162 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005163 {
5164 mb_utf8 = TRUE;
5165 u8cc[0] = 0;
5166 c = 0xc0;
5167 }
5168 else
5169 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5170# endif
5171 }
5172 else
5173 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005174 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005175 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005176 }
5177#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005180#ifdef FEAT_CONCEAL
5181 /* In the cursor line and we may be concealing characters: correct
5182 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005183 if (!did_wcol && draw_state == WL_LINE
5184 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005185 && conceal_cursor_line(wp)
5186 && (int)wp->w_virtcol <= vcol + n_skip)
5187 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005188# ifdef FEAT_RIGHTLEFT
5189 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005190 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005191 else
5192# endif
5193 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005194 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005195 did_wcol = TRUE;
5196 }
5197#endif
5198
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 /* Don't override visual selection highlighting. */
5200 if (n_attr > 0
5201 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005202 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 char_attr = extra_attr;
5204
Bram Moolenaar81695252004-12-29 20:58:21 +00005205#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 /* XIM don't send preedit_start and preedit_end, but they send
5207 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5208 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005209 if (p_imst == IM_ON_THE_SPOT
5210 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005211 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 && (State & INSERT)
5213 && !p_imdisable
5214 && im_is_preediting()
5215 && draw_state == WL_LINE)
5216 {
5217 colnr_T tcol;
5218
5219 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005220 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 else
5222 tcol = preedit_end_col;
5223 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5224 {
5225 if (feedback_old_attr < 0)
5226 {
5227 feedback_col = 0;
5228 feedback_old_attr = char_attr;
5229 }
5230 char_attr = im_get_feedback_attr(feedback_col);
5231 if (char_attr < 0)
5232 char_attr = feedback_old_attr;
5233 feedback_col++;
5234 }
5235 else if (feedback_old_attr >= 0)
5236 {
5237 char_attr = feedback_old_attr;
5238 feedback_old_attr = -1;
5239 feedback_col = 0;
5240 }
5241 }
5242#endif
5243 /*
5244 * Handle the case where we are in column 0 but not on the first
5245 * character of the line and the user wants us to show us a
5246 * special character (via 'listchars' option "precedes:<char>".
5247 */
5248 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005249 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5251#ifdef FEAT_DIFF
5252 && filler_todo <= 0
5253#endif
5254 && draw_state > WL_NR
5255 && c != NUL)
5256 {
5257 c = lcs_prec;
5258 lcs_prec_todo = NUL;
5259#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005260 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5261 {
5262 /* Double-width character being overwritten by the "precedes"
5263 * character, need to fill up half the character. */
5264 c_extra = MB_FILLER_CHAR;
5265 n_extra = 1;
5266 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005267 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005270 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 {
5272 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005273 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005274 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276 else
5277 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5278#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005279 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {
5281 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005282 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 n_attr3 = 1;
5284 }
5285 }
5286
5287 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005288 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005290 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005291#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005292 || did_line_attr == 1
5293#endif
5294 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005296#ifdef FEAT_SEARCH_EXTRA
5297 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005298
5299 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005300 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005301 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005302#endif
5303
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005304 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 * highlight match at end of line. If it's beyond the last
5306 * char on the screen, just overwrite that one (tricky!) Not
5307 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005308#ifdef FEAT_SEARCH_EXTRA
5309 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005310 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005311 prevcol_hl_flag = TRUE;
5312 else
5313 {
5314 cur = wp->w_match_head;
5315 while (cur != NULL)
5316 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005317 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005318 {
5319 prevcol_hl_flag = TRUE;
5320 break;
5321 }
5322 cur = cur->next;
5323 }
5324 }
5325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005327 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005328 && (VIsual_mode != Ctrl_V
5329 || lnum == VIsual.lnum
5330 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005331 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332#ifdef FEAT_SEARCH_EXTRA
5333 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005334 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005335# ifdef FEAT_SYN_HL
5336 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5337 && !(wp == curwin && VIsual_active))
5338# endif
5339# ifdef FEAT_DIFF
5340 && diff_hlf == (hlf_T)0
5341# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005342# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005343 && did_line_attr <= 1
5344# endif
5345 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346#endif
5347 ))
5348 {
5349 int n = 0;
5350
5351#ifdef FEAT_RIGHTLEFT
5352 if (wp->w_p_rl)
5353 {
5354 if (col < 0)
5355 n = 1;
5356 }
5357 else
5358#endif
5359 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005360 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 n = -1;
5362 }
5363 if (n != 0)
5364 {
5365 /* At the window boundary, highlight the last character
5366 * instead (better than nothing). */
5367 off += n;
5368 col += n;
5369 }
5370 else
5371 {
5372 /* Add a blank character to highlight. */
5373 ScreenLines[off] = ' ';
5374#ifdef FEAT_MBYTE
5375 if (enc_utf8)
5376 ScreenLinesUC[off] = 0;
5377#endif
5378 }
5379#ifdef FEAT_SEARCH_EXTRA
5380 if (area_attr == 0)
5381 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005382 /* Use attributes from match with highest priority among
5383 * 'search_hl' and the match list. */
5384 char_attr = search_hl.attr;
5385 cur = wp->w_match_head;
5386 shl_flag = FALSE;
5387 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005388 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005389 if (shl_flag == FALSE
5390 && ((cur != NULL
5391 && cur->priority > SEARCH_HL_PRIORITY)
5392 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005393 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005394 shl = &search_hl;
5395 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005396 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005397 else
5398 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005399 if ((ptr - line) - 1 == (long)shl->startcol
5400 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005401 char_attr = shl->attr;
5402 if (shl != &search_hl && cur != NULL)
5403 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 }
5406#endif
5407 ScreenAttrs[off] = char_attr;
5408#ifdef FEAT_RIGHTLEFT
5409 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005412 --off;
5413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 else
5415#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005418 ++off;
5419 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005420 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005421#ifdef FEAT_SYN_HL
5422 eol_hl_off = 1;
5423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
Bram Moolenaar91170f82006-05-05 21:15:17 +00005427 /*
5428 * At end of the text line.
5429 */
5430 if (c == NUL)
5431 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005432#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005433 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5434 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005435 {
5436 /* highlight last char after line */
5437 --col;
5438 --off;
5439 --vcol;
5440 }
5441
Bram Moolenaar1a384422010-07-14 19:53:30 +02005442 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005443 if (wp->w_p_wrap)
5444 v = wp->w_skipcol;
5445 else
5446 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005447
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005448 /* check if line ends before left margin */
5449 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005450 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005451#ifdef FEAT_CONCEAL
5452 /* Get rid of the boguscols now, we want to draw until the right
5453 * edge for 'cursorcolumn'. */
5454 col -= boguscols;
5455 boguscols = 0;
5456#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005457
5458 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005459 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005460
5461 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005462 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5463 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005464 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005465 && lnum != wp->w_cursor.lnum)
5466 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005467# ifdef FEAT_RIGHTLEFT
5468 && !wp->w_p_rl
5469# endif
5470 )
5471 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005472 int rightmost_vcol = 0;
5473 int i;
5474
5475 if (wp->w_p_cuc)
5476 rightmost_vcol = wp->w_virtcol;
5477 if (draw_color_col)
5478 /* determine rightmost colorcolumn to possibly draw */
5479 for (i = 0; color_cols[i] >= 0; ++i)
5480 if (rightmost_vcol < color_cols[i])
5481 rightmost_vcol = color_cols[i];
5482
Bram Moolenaar02631462017-09-22 15:20:32 +02005483 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005484 {
5485 ScreenLines[off] = ' ';
5486#ifdef FEAT_MBYTE
5487 if (enc_utf8)
5488 ScreenLinesUC[off] = 0;
5489#endif
5490 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005491 if (draw_color_col)
5492 draw_color_col = advance_color_col(VCOL_HLC,
5493 &color_cols);
5494
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005495 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005496 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005497 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005498 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005499 else
5500 ScreenAttrs[off++] = 0;
5501
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005502 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005503 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005504
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005505 ++vcol;
5506 }
5507 }
5508#endif
5509
Bram Moolenaar53f81742017-09-22 14:35:51 +02005510 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005511 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 row++;
5513
5514 /*
5515 * Update w_cline_height and w_cline_folded if the cursor line was
5516 * updated (saves a call to plines() later).
5517 */
5518 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5519 {
5520 curwin->w_cline_row = startrow;
5521 curwin->w_cline_height = row - startrow;
5522#ifdef FEAT_FOLDING
5523 curwin->w_cline_folded = FALSE;
5524#endif
5525 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5526 }
5527
5528 break;
5529 }
5530
5531 /* line continues beyond line end */
5532 if (lcs_ext
5533 && !wp->w_p_wrap
5534#ifdef FEAT_DIFF
5535 && filler_todo <= 0
5536#endif
5537 && (
5538#ifdef FEAT_RIGHTLEFT
5539 wp->w_p_rl ? col == 0 :
5540#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005541 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005543 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5545 {
5546 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005547 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548#ifdef FEAT_MBYTE
5549 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005550 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 {
5552 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005553 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005554 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556 else
5557 mb_utf8 = FALSE;
5558#endif
5559 }
5560
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005561#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005562 /* advance to the next 'colorcolumn' */
5563 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005564 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005565
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005566 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005567 * highlight the cursor position itself.
5568 * Also highlight the 'colorcolumn' if it is different than
5569 * 'cursorcolumn' */
5570 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005571 if (draw_state == WL_LINE && !lnum_in_visual_area
5572 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005573 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005574 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005575 && lnum != wp->w_cursor.lnum)
5576 {
5577 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005578 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005579 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005580 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005581 {
5582 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005583 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005584 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005585 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005586#endif
5587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 /*
5589 * Store character to be displayed.
5590 * Skip characters that are left of the screen for 'nowrap'.
5591 */
5592 vcol_prev = vcol;
5593 if (draw_state < WL_LINE || n_skip <= 0)
5594 {
5595 /*
5596 * Store the character.
5597 */
5598#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5599 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5600 {
5601 /* A double-wide character is: put first halve in left cell. */
5602 --off;
5603 --col;
5604 }
5605#endif
5606 ScreenLines[off] = c;
5607#ifdef FEAT_MBYTE
5608 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005609 {
5610 if ((mb_c & 0xff00) == 0x8e00)
5611 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 else if (enc_utf8)
5615 {
5616 if (mb_utf8)
5617 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005618 int i;
5619
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005621 if ((c & 0xff) == 0)
5622 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005623 for (i = 0; i < Screen_mco; ++i)
5624 {
5625 ScreenLinesC[i][off] = u8cc[i];
5626 if (u8cc[i] == 0)
5627 break;
5628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630 else
5631 ScreenLinesUC[off] = 0;
5632 }
5633 if (multi_attr)
5634 {
5635 ScreenAttrs[off] = multi_attr;
5636 multi_attr = 0;
5637 }
5638 else
5639#endif
5640 ScreenAttrs[off] = char_attr;
5641
5642#ifdef FEAT_MBYTE
5643 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5644 {
5645 /* Need to fill two screen columns. */
5646 ++off;
5647 ++col;
5648 if (enc_utf8)
5649 /* UTF-8: Put a 0 in the second screen char. */
5650 ScreenLines[off] = 0;
5651 else
5652 /* DBCS: Put second byte in the second screen char. */
5653 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005654 if (draw_state > WL_NR
5655#ifdef FEAT_DIFF
5656 && filler_todo <= 0
5657#endif
5658 )
5659 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 /* When "tocol" is halfway a character, set it to the end of
5661 * the character, otherwise highlighting won't stop. */
5662 if (tocol == vcol)
5663 ++tocol;
5664#ifdef FEAT_RIGHTLEFT
5665 if (wp->w_p_rl)
5666 {
5667 /* now it's time to backup one cell */
5668 --off;
5669 --col;
5670 }
5671#endif
5672 }
5673#endif
5674#ifdef FEAT_RIGHTLEFT
5675 if (wp->w_p_rl)
5676 {
5677 --off;
5678 --col;
5679 }
5680 else
5681#endif
5682 {
5683 ++off;
5684 ++col;
5685 }
5686 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005687#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005688 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005689 {
5690 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005691 ++vcol_off;
5692 if (n_extra > 0)
5693 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005694 if (wp->w_p_wrap)
5695 {
5696 /*
5697 * Special voodoo required if 'wrap' is on.
5698 *
5699 * Advance the column indicator to force the line
5700 * drawing to wrap early. This will make the line
5701 * take up the same screen space when parts are concealed,
5702 * so that cursor line computations aren't messed up.
5703 *
5704 * To avoid the fictitious advance of 'col' causing
5705 * trailing junk to be written out of the screen line
5706 * we are building, 'boguscols' keeps track of the number
5707 * of bad columns we have advanced.
5708 */
5709 if (n_extra > 0)
5710 {
5711 vcol += n_extra;
5712# ifdef FEAT_RIGHTLEFT
5713 if (wp->w_p_rl)
5714 {
5715 col -= n_extra;
5716 boguscols -= n_extra;
5717 }
5718 else
5719# endif
5720 {
5721 col += n_extra;
5722 boguscols += n_extra;
5723 }
5724 n_extra = 0;
5725 n_attr = 0;
5726 }
5727
5728
5729# ifdef FEAT_MBYTE
5730 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5731 {
5732 /* Need to fill two screen columns. */
5733# ifdef FEAT_RIGHTLEFT
5734 if (wp->w_p_rl)
5735 {
5736 --boguscols;
5737 --col;
5738 }
5739 else
5740# endif
5741 {
5742 ++boguscols;
5743 ++col;
5744 }
5745 }
5746# endif
5747
5748# ifdef FEAT_RIGHTLEFT
5749 if (wp->w_p_rl)
5750 {
5751 --boguscols;
5752 --col;
5753 }
5754 else
5755# endif
5756 {
5757 ++boguscols;
5758 ++col;
5759 }
5760 }
5761 else
5762 {
5763 if (n_extra > 0)
5764 {
5765 vcol += n_extra;
5766 n_extra = 0;
5767 n_attr = 0;
5768 }
5769 }
5770
5771 }
5772#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 else
5774 --n_skip;
5775
Bram Moolenaar64486672010-05-16 15:46:46 +02005776 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5777 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005778 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779#ifdef FEAT_DIFF
5780 && filler_todo <= 0
5781#endif
5782 )
5783 ++vcol;
5784
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005785#ifdef FEAT_SYN_HL
5786 if (vcol_save_attr >= 0)
5787 char_attr = vcol_save_attr;
5788#endif
5789
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 /* restore attributes after "predeces" in 'listchars' */
5791 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5792 char_attr = saved_attr3;
5793
5794 /* restore attributes after last 'listchars' or 'number' char */
5795 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5796 char_attr = saved_attr2;
5797
5798 /*
5799 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005800 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 */
5802 if ((
5803#ifdef FEAT_RIGHTLEFT
5804 wp->w_p_rl ? (col < 0) :
5805#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005806 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 && (*ptr != NUL
5808#ifdef FEAT_DIFF
5809 || filler_todo > 0
5810#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005811 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5813 )
5814 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005815#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005816 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005817 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005818 boguscols = 0;
5819#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005820 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005821 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 ++row;
5824 ++screen_row;
5825
5826 /* When not wrapping and finished diff lines, or when displayed
5827 * '$' and highlighting until last column, break here. */
5828 if ((!wp->w_p_wrap
5829#ifdef FEAT_DIFF
5830 && filler_todo <= 0
5831#endif
5832 ) || lcs_eol_one == -1)
5833 break;
5834
5835 /* When the window is too narrow draw all "@" lines. */
5836 if (draw_state != WL_LINE
5837#ifdef FEAT_DIFF
5838 && filler_todo <= 0
5839#endif
5840 )
5841 {
5842 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 row = endrow;
5845 }
5846
5847 /* When line got too long for screen break here. */
5848 if (row == endrow)
5849 {
5850 ++row;
5851 break;
5852 }
5853
5854 if (screen_cur_row == screen_row - 1
5855#ifdef FEAT_DIFF
5856 && filler_todo <= 0
5857#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005858 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 {
5860 /* Remember that the line wraps, used for modeless copy. */
5861 LineWraps[screen_row - 1] = TRUE;
5862
5863 /*
5864 * Special trick to make copy/paste of wrapped lines work with
5865 * xterm/screen: write an extra character beyond the end of
5866 * the line. This will work with all terminal types
5867 * (regardless of the xn,am settings).
5868 * Only do this on a fast tty.
5869 * Only do this if the cursor is on the current line
5870 * (something has been written in it).
5871 * Don't do this for the GUI.
5872 * Don't do this for double-width characters.
5873 * Don't do this for a window not at the right screen border.
5874 */
5875 if (p_tf
5876#ifdef FEAT_GUI
5877 && !gui.in_use
5878#endif
5879#ifdef FEAT_MBYTE
5880 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005881 && ((*mb_off2cells)(LineOffset[screen_row],
5882 LineOffset[screen_row] + screen_Columns)
5883 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005885 + (int)Columns - 2,
5886 LineOffset[screen_row] + screen_Columns)
5887 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888#endif
5889 )
5890 {
5891 /* First make sure we are at the end of the screen line,
5892 * then output the same character again to let the
5893 * terminal know about the wrap. If the terminal doesn't
5894 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005895 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 screen_char(LineOffset[screen_row - 1]
5897 + (unsigned)Columns - 1,
5898 screen_row - 1, (int)(Columns - 1));
5899
5900#ifdef FEAT_MBYTE
5901 /* When there is a multi-byte character, just output a
5902 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005903 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5904 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 out_char(' ');
5906 else
5907#endif
5908 out_char(ScreenLines[LineOffset[screen_row - 1]
5909 + (Columns - 1)]);
5910 /* force a redraw of the first char on the next line */
5911 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5912 screen_start(); /* don't know where cursor is now */
5913 }
5914 }
5915
5916 col = 0;
5917 off = (unsigned)(current_ScreenLine - ScreenLines);
5918#ifdef FEAT_RIGHTLEFT
5919 if (wp->w_p_rl)
5920 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005921 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 off += col;
5923 }
5924#endif
5925
5926 /* reset the drawing state for the start of a wrapped line */
5927 draw_state = WL_START;
5928 saved_n_extra = n_extra;
5929 saved_p_extra = p_extra;
5930 saved_c_extra = c_extra;
5931 saved_char_attr = char_attr;
5932 n_extra = 0;
5933 lcs_prec_todo = lcs_prec;
5934#ifdef FEAT_LINEBREAK
5935# ifdef FEAT_DIFF
5936 if (filler_todo <= 0)
5937# endif
5938 need_showbreak = TRUE;
5939#endif
5940#ifdef FEAT_DIFF
5941 --filler_todo;
5942 /* When the filler lines are actually below the last line of the
5943 * file, don't draw the line itself, break here. */
5944 if (filler_todo == 0 && wp->w_botfill)
5945 break;
5946#endif
5947 }
5948
5949 } /* for every character in the line */
5950
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005951#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005952 /* After an empty line check first word for capital. */
5953 if (*skipwhite(line) == NUL)
5954 {
5955 capcol_lnum = lnum + 1;
5956 cap_col = 0;
5957 }
5958#endif
5959
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005960 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 return row;
5962}
5963
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005964#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005965static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005966
5967/*
5968 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005969 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005970 */
5971 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005972comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005973{
5974 int i;
5975
5976 for (i = 0; i < Screen_mco; ++i)
5977 {
5978 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5979 return TRUE;
5980 if (ScreenLinesC[i][off_from] == 0)
5981 break;
5982 }
5983 return FALSE;
5984}
5985#endif
5986
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987/*
5988 * Check whether the given character needs redrawing:
5989 * - the (first byte of the) character is different
5990 * - the attributes are different
5991 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005992 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 */
5994 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005995char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996{
5997 if (cols > 0
5998 && ((ScreenLines[off_from] != ScreenLines[off_to]
5999 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6000
6001#ifdef FEAT_MBYTE
6002 || (enc_dbcs != 0
6003 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6004 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6005 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6006 : (cols > 1 && ScreenLines[off_from + 1]
6007 != ScreenLines[off_to + 1])))
6008 || (enc_utf8
6009 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6010 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006011 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006012 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6013 && ScreenLines[off_from + 1]
6014 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015#endif
6016 ))
6017 return TRUE;
6018 return FALSE;
6019}
6020
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006021#if defined(FEAT_TERMINAL) || defined(PROTO)
6022/*
6023 * Return the index in ScreenLines[] for the current screen line.
6024 */
6025 int
6026screen_get_current_line_off()
6027{
6028 return (int)(current_ScreenLine - ScreenLines);
6029}
6030#endif
6031
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032/*
6033 * Move one "cooked" screen line to the screen, but only the characters that
6034 * have actually changed. Handle insert/delete character.
6035 * "coloff" gives the first column on the screen for this line.
6036 * "endcol" gives the columns where valid characters are.
6037 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6038 * needs to be cleared, negative otherwise.
6039 * "rlflag" is TRUE in a rightleft window:
6040 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6041 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6042 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006043 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006044screen_line(
6045 int row,
6046 int coloff,
6047 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006048 int clear_width,
6049 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
6051 unsigned off_from;
6052 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006053#ifdef FEAT_MBYTE
6054 unsigned max_off_from;
6055 unsigned max_off_to;
6056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 int force = FALSE; /* force update rest of the line */
6060 int redraw_this /* bool: does character need redraw? */
6061#ifdef FEAT_GUI
6062 = TRUE /* For GUI when while-loop empty */
6063#endif
6064 ;
6065 int redraw_next; /* redraw_this for next character */
6066#ifdef FEAT_MBYTE
6067 int clear_next = FALSE;
6068 int char_cells; /* 1: normal char */
6069 /* 2: occupies two display cells */
6070# define CHAR_CELLS char_cells
6071#else
6072# define CHAR_CELLS 1
6073#endif
6074
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006075 /* Check for illegal row and col, just in case. */
6076 if (row >= Rows)
6077 row = Rows - 1;
6078 if (endcol > Columns)
6079 endcol = Columns;
6080
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081# ifdef FEAT_CLIPBOARD
6082 clip_may_clear_selection(row, row);
6083# endif
6084
6085 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6086 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006087#ifdef FEAT_MBYTE
6088 max_off_from = off_from + screen_Columns;
6089 max_off_to = LineOffset[row] + screen_Columns;
6090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
6092#ifdef FEAT_RIGHTLEFT
6093 if (rlflag)
6094 {
6095 /* Clear rest first, because it's left of the text. */
6096 if (clear_width > 0)
6097 {
6098 while (col <= endcol && ScreenLines[off_to] == ' '
6099 && ScreenAttrs[off_to] == 0
6100# ifdef FEAT_MBYTE
6101 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6102# endif
6103 )
6104 {
6105 ++off_to;
6106 ++col;
6107 }
6108 if (col <= endcol)
6109 screen_fill(row, row + 1, col + coloff,
6110 endcol + coloff + 1, ' ', ' ', 0);
6111 }
6112 col = endcol + 1;
6113 off_to = LineOffset[row] + col + coloff;
6114 off_from += col;
6115 endcol = (clear_width > 0 ? clear_width : -clear_width);
6116 }
6117#endif /* FEAT_RIGHTLEFT */
6118
6119 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6120
6121 while (col < endcol)
6122 {
6123#ifdef FEAT_MBYTE
6124 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006125 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 else
6127 char_cells = 1;
6128#endif
6129
6130 redraw_this = redraw_next;
6131 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6132 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6133
6134#ifdef FEAT_GUI
6135 /* If the next character was bold, then redraw the current character to
6136 * remove any pixels that might have spilt over into us. This only
6137 * happens in the GUI.
6138 */
6139 if (redraw_next && gui.in_use)
6140 {
6141 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006142 if (hl > HL_ALL)
6143 hl = syn_attr2attr(hl);
6144 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 redraw_this = TRUE;
6146 }
6147#endif
6148
6149 if (redraw_this)
6150 {
6151 /*
6152 * Special handling when 'xs' termcap flag set (hpterm):
6153 * Attributes for characters are stored at the position where the
6154 * cursor is when writing the highlighting code. The
6155 * start-highlighting code must be written with the cursor on the
6156 * first highlighted character. The stop-highlighting code must
6157 * be written with the cursor just after the last highlighted
6158 * character.
6159 * Overwriting a character doesn't remove it's highlighting. Need
6160 * to clear the rest of the line, and force redrawing it
6161 * completely.
6162 */
6163 if ( p_wiv
6164 && !force
6165#ifdef FEAT_GUI
6166 && !gui.in_use
6167#endif
6168 && ScreenAttrs[off_to] != 0
6169 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6170 {
6171 /*
6172 * Need to remove highlighting attributes here.
6173 */
6174 windgoto(row, col + coloff);
6175 out_str(T_CE); /* clear rest of this screen line */
6176 screen_start(); /* don't know where cursor is now */
6177 force = TRUE; /* force redraw of rest of the line */
6178 redraw_next = TRUE; /* or else next char would miss out */
6179
6180 /*
6181 * If the previous character was highlighted, need to stop
6182 * highlighting at this character.
6183 */
6184 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6185 {
6186 screen_attr = ScreenAttrs[off_to - 1];
6187 term_windgoto(row, col + coloff);
6188 screen_stop_highlight();
6189 }
6190 else
6191 screen_attr = 0; /* highlighting has stopped */
6192 }
6193#ifdef FEAT_MBYTE
6194 if (enc_dbcs != 0)
6195 {
6196 /* Check if overwriting a double-byte with a single-byte or
6197 * the other way around requires another character to be
6198 * redrawn. For UTF-8 this isn't needed, because comparing
6199 * ScreenLinesUC[] is sufficient. */
6200 if (char_cells == 1
6201 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006202 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {
6204 /* Writing a single-cell character over a double-cell
6205 * character: need to redraw the next cell. */
6206 ScreenLines[off_to + 1] = 0;
6207 redraw_next = TRUE;
6208 }
6209 else if (char_cells == 2
6210 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006211 && (*mb_off2cells)(off_to, max_off_to) == 1
6212 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 {
6214 /* Writing the second half of a double-cell character over
6215 * a double-cell character: need to redraw the second
6216 * cell. */
6217 ScreenLines[off_to + 2] = 0;
6218 redraw_next = TRUE;
6219 }
6220
6221 if (enc_dbcs == DBCS_JPNU)
6222 ScreenLines2[off_to] = ScreenLines2[off_from];
6223 }
6224 /* When writing a single-width character over a double-width
6225 * character and at the end of the redrawn text, need to clear out
6226 * the right halve of the old character.
6227 * Also required when writing the right halve of a double-width
6228 * char over the left halve of an existing one. */
6229 if (has_mbyte && col + char_cells == endcol
6230 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006231 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006233 && (*mb_off2cells)(off_to, max_off_to) == 1
6234 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 clear_next = TRUE;
6236#endif
6237
6238 ScreenLines[off_to] = ScreenLines[off_from];
6239#ifdef FEAT_MBYTE
6240 if (enc_utf8)
6241 {
6242 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6243 if (ScreenLinesUC[off_from] != 0)
6244 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006245 int i;
6246
6247 for (i = 0; i < Screen_mco; ++i)
6248 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 }
6250 }
6251 if (char_cells == 2)
6252 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6253#endif
6254
6255#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006256 /* The bold trick makes a single column of pixels appear in the
6257 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 * character should be redrawn too. This happens for our own GUI
6259 * and for some xterms. */
6260 if (
6261# ifdef FEAT_GUI
6262 gui.in_use
6263# endif
6264# if defined(FEAT_GUI) && defined(UNIX)
6265 ||
6266# endif
6267# ifdef UNIX
6268 term_is_xterm
6269# endif
6270 )
6271 {
6272 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006273 if (hl > HL_ALL)
6274 hl = syn_attr2attr(hl);
6275 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 redraw_next = TRUE;
6277 }
6278#endif
6279 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6280#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006281 /* For simplicity set the attributes of second half of a
6282 * double-wide character equal to the first half. */
6283 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006285
6286 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 else
6289#endif
6290 screen_char(off_to, row, col + coloff);
6291 }
6292 else if ( p_wiv
6293#ifdef FEAT_GUI
6294 && !gui.in_use
6295#endif
6296 && col + coloff > 0)
6297 {
6298 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6299 {
6300 /*
6301 * Don't output stop-highlight when moving the cursor, it will
6302 * stop the highlighting when it should continue.
6303 */
6304 screen_attr = 0;
6305 }
6306 else if (screen_attr != 0)
6307 screen_stop_highlight();
6308 }
6309
6310 off_to += CHAR_CELLS;
6311 off_from += CHAR_CELLS;
6312 col += CHAR_CELLS;
6313 }
6314
6315#ifdef FEAT_MBYTE
6316 if (clear_next)
6317 {
6318 /* Clear the second half of a double-wide character of which the left
6319 * half was overwritten with a single-wide character. */
6320 ScreenLines[off_to] = ' ';
6321 if (enc_utf8)
6322 ScreenLinesUC[off_to] = 0;
6323 screen_char(off_to, row, col + coloff);
6324 }
6325#endif
6326
6327 if (clear_width > 0
6328#ifdef FEAT_RIGHTLEFT
6329 && !rlflag
6330#endif
6331 )
6332 {
6333#ifdef FEAT_GUI
6334 int startCol = col;
6335#endif
6336
6337 /* blank out the rest of the line */
6338 while (col < clear_width && ScreenLines[off_to] == ' '
6339 && ScreenAttrs[off_to] == 0
6340#ifdef FEAT_MBYTE
6341 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6342#endif
6343 )
6344 {
6345 ++off_to;
6346 ++col;
6347 }
6348 if (col < clear_width)
6349 {
6350#ifdef FEAT_GUI
6351 /*
6352 * In the GUI, clearing the rest of the line may leave pixels
6353 * behind if the first character cleared was bold. Some bold
6354 * fonts spill over the left. In this case we redraw the previous
6355 * character too. If we didn't skip any blanks above, then we
6356 * only redraw if the character wasn't already redrawn anyway.
6357 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006358 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 {
6360 hl = ScreenAttrs[off_to];
6361 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006362 {
6363 int prev_cells = 1;
6364# ifdef FEAT_MBYTE
6365 if (enc_utf8)
6366 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6367 * that its width is 2. */
6368 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6369 else if (enc_dbcs != 0)
6370 {
6371 /* find previous character by counting from first
6372 * column and get its width. */
6373 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006374 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006375
6376 while (off < off_to)
6377 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006378 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006379 off += prev_cells;
6380 }
6381 }
6382
6383 if (enc_dbcs != 0 && prev_cells > 1)
6384 screen_char_2(off_to - prev_cells, row,
6385 col + coloff - prev_cells);
6386 else
6387# endif
6388 screen_char(off_to - prev_cells, row,
6389 col + coloff - prev_cells);
6390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 }
6392#endif
6393 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6394 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 off_to += clear_width - col;
6396 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 }
6398 }
6399
6400 if (clear_width > 0)
6401 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 /* For a window that's left of another, draw the separator char. */
6403 if (col + coloff < Columns)
6404 {
6405 int c;
6406
6407 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006408 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006409#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006410 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6411 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 || ScreenAttrs[off_to] != hl)
6414 {
6415 ScreenLines[off_to] = c;
6416 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006417#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 if (enc_utf8)
6419 {
6420 if (c >= 0x80)
6421 {
6422 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006423 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 }
6425 else
6426 ScreenLinesUC[off_to] = 0;
6427 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 screen_char(off_to, row, col + coloff);
6430 }
6431 }
6432 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 LineWraps[row] = FALSE;
6434 }
6435}
6436
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006437#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006439 * Mirror text "str" for right-left displaying.
6440 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006442 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006443rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
6445 char_u *p1, *p2;
6446 int t;
6447
6448 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6449 {
6450 t = *p1;
6451 *p1 = *p2;
6452 *p2 = t;
6453 }
6454}
6455#endif
6456
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457/*
6458 * mark all status lines for redraw; used after first :cd
6459 */
6460 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006461status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
6463 win_T *wp;
6464
Bram Moolenaar29323592016-07-24 22:04:11 +02006465 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 if (wp->w_status_height)
6467 {
6468 wp->w_redr_status = TRUE;
6469 redraw_later(VALID);
6470 }
6471}
6472
6473/*
6474 * mark all status lines of the current buffer for redraw
6475 */
6476 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006477status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
6479 win_T *wp;
6480
Bram Moolenaar29323592016-07-24 22:04:11 +02006481 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6483 {
6484 wp->w_redr_status = TRUE;
6485 redraw_later(VALID);
6486 }
6487}
6488
6489/*
6490 * Redraw all status lines that need to be redrawn.
6491 */
6492 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006493redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
6495 win_T *wp;
6496
Bram Moolenaar29323592016-07-24 22:04:11 +02006497 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 if (wp->w_redr_status)
6499 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006500 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006501 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
Bram Moolenaar4033c552017-09-16 20:54:51 +02006504#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505/*
6506 * Redraw all status lines at the bottom of frame "frp".
6507 */
6508 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006509win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 if (frp->fr_layout == FR_LEAF)
6512 frp->fr_win->w_redr_status = TRUE;
6513 else if (frp->fr_layout == FR_ROW)
6514 {
6515 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6516 win_redraw_last_status(frp);
6517 }
6518 else /* frp->fr_layout == FR_COL */
6519 {
6520 frp = frp->fr_child;
6521 while (frp->fr_next != NULL)
6522 frp = frp->fr_next;
6523 win_redraw_last_status(frp);
6524 }
6525}
6526#endif
6527
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528/*
6529 * Draw the verticap separator right of window "wp" starting with line "row".
6530 */
6531 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006532draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
6534 int hl;
6535 int c;
6536
6537 if (wp->w_vsep_width)
6538 {
6539 /* draw the vertical separator right of this window */
6540 c = fillchar_vsep(&hl);
6541 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6542 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6543 c, ' ', hl);
6544 }
6545}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
6547#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006548static int status_match_len(expand_T *xp, char_u *s);
6549static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
6551/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006552 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 */
6554 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006555status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556{
6557 int len = 0;
6558
6559#ifdef FEAT_MENU
6560 int emenu = (xp->xp_context == EXPAND_MENUS
6561 || xp->xp_context == EXPAND_MENUNAMES);
6562
6563 /* Check for menu separators - replace with '|'. */
6564 if (emenu && menu_is_separator(s))
6565 return 1;
6566#endif
6567
6568 while (*s != NUL)
6569 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006570 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006571 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006572 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 }
6574
6575 return len;
6576}
6577
6578/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006579 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006580 * These are backslashes used for escaping. Do show backslashes in help tags.
6581 */
6582 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006583skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006584{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006585 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006586#ifdef FEAT_MENU
6587 || ((xp->xp_context == EXPAND_MENUS
6588 || xp->xp_context == EXPAND_MENUNAMES)
6589 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6590#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006591 )
6592 {
6593#ifndef BACKSLASH_IN_FILENAME
6594 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6595 return 2;
6596#endif
6597 return 1;
6598 }
6599 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006600}
6601
6602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 * Show wildchar matches in the status line.
6604 * Show at least the "match" item.
6605 * We start at item 'first_match' in the list and show all matches that fit.
6606 *
6607 * If inversion is possible we use it. Else '=' characters are used.
6608 */
6609 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006610win_redr_status_matches(
6611 expand_T *xp,
6612 int num_matches,
6613 char_u **matches, /* list of matches */
6614 int match,
6615 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6618 int row;
6619 char_u *buf;
6620 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006621 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 int fillchar;
6623 int attr;
6624 int i;
6625 int highlight = TRUE;
6626 char_u *selstart = NULL;
6627 int selstart_col = 0;
6628 char_u *selend = NULL;
6629 static int first_match = 0;
6630 int add_left = FALSE;
6631 char_u *s;
6632#ifdef FEAT_MENU
6633 int emenu;
6634#endif
6635#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6636 int l;
6637#endif
6638
6639 if (matches == NULL) /* interrupted completion? */
6640 return;
6641
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006642#ifdef FEAT_MBYTE
6643 if (has_mbyte)
6644 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6645 else
6646#endif
6647 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 if (buf == NULL)
6649 return;
6650
6651 if (match == -1) /* don't show match but original text */
6652 {
6653 match = 0;
6654 highlight = FALSE;
6655 }
6656 /* count 1 for the ending ">" */
6657 clen = status_match_len(xp, L_MATCH(match)) + 3;
6658 if (match == 0)
6659 first_match = 0;
6660 else if (match < first_match)
6661 {
6662 /* jumping left, as far as we can go */
6663 first_match = match;
6664 add_left = TRUE;
6665 }
6666 else
6667 {
6668 /* check if match fits on the screen */
6669 for (i = first_match; i < match; ++i)
6670 clen += status_match_len(xp, L_MATCH(i)) + 2;
6671 if (first_match > 0)
6672 clen += 2;
6673 /* jumping right, put match at the left */
6674 if ((long)clen > Columns)
6675 {
6676 first_match = match;
6677 /* if showing the last match, we can add some on the left */
6678 clen = 2;
6679 for (i = match; i < num_matches; ++i)
6680 {
6681 clen += status_match_len(xp, L_MATCH(i)) + 2;
6682 if ((long)clen >= Columns)
6683 break;
6684 }
6685 if (i == num_matches)
6686 add_left = TRUE;
6687 }
6688 }
6689 if (add_left)
6690 while (first_match > 0)
6691 {
6692 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6693 if ((long)clen >= Columns)
6694 break;
6695 --first_match;
6696 }
6697
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006698 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699
6700 if (first_match == 0)
6701 {
6702 *buf = NUL;
6703 len = 0;
6704 }
6705 else
6706 {
6707 STRCPY(buf, "< ");
6708 len = 2;
6709 }
6710 clen = len;
6711
6712 i = first_match;
6713 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6714 {
6715 if (i == match)
6716 {
6717 selstart = buf + len;
6718 selstart_col = clen;
6719 }
6720
6721 s = L_MATCH(i);
6722 /* Check for menu separators - replace with '|' */
6723#ifdef FEAT_MENU
6724 emenu = (xp->xp_context == EXPAND_MENUS
6725 || xp->xp_context == EXPAND_MENUNAMES);
6726 if (emenu && menu_is_separator(s))
6727 {
6728 STRCPY(buf + len, transchar('|'));
6729 l = (int)STRLEN(buf + len);
6730 len += l;
6731 clen += l;
6732 }
6733 else
6734#endif
6735 for ( ; *s != NUL; ++s)
6736 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006737 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 clen += ptr2cells(s);
6739#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006740 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 {
6742 STRNCPY(buf + len, s, l);
6743 s += l - 1;
6744 len += l;
6745 }
6746 else
6747#endif
6748 {
6749 STRCPY(buf + len, transchar_byte(*s));
6750 len += (int)STRLEN(buf + len);
6751 }
6752 }
6753 if (i == match)
6754 selend = buf + len;
6755
6756 *(buf + len++) = ' ';
6757 *(buf + len++) = ' ';
6758 clen += 2;
6759 if (++i == num_matches)
6760 break;
6761 }
6762
6763 if (i != num_matches)
6764 {
6765 *(buf + len++) = '>';
6766 ++clen;
6767 }
6768
6769 buf[len] = NUL;
6770
6771 row = cmdline_row - 1;
6772 if (row >= 0)
6773 {
6774 if (wild_menu_showing == 0)
6775 {
6776 if (msg_scrolled > 0)
6777 {
6778 /* Put the wildmenu just above the command line. If there is
6779 * no room, scroll the screen one line up. */
6780 if (cmdline_row == Rows - 1)
6781 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006782 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 ++msg_scrolled;
6784 }
6785 else
6786 {
6787 ++cmdline_row;
6788 ++row;
6789 }
6790 wild_menu_showing = WM_SCROLLED;
6791 }
6792 else
6793 {
6794 /* Create status line if needed by setting 'laststatus' to 2.
6795 * Set 'winminheight' to zero to avoid that the window is
6796 * resized. */
6797 if (lastwin->w_status_height == 0)
6798 {
6799 save_p_ls = p_ls;
6800 save_p_wmh = p_wmh;
6801 p_ls = 2;
6802 p_wmh = 0;
6803 last_status(FALSE);
6804 }
6805 wild_menu_showing = WM_SHOWN;
6806 }
6807 }
6808
6809 screen_puts(buf, row, 0, attr);
6810 if (selstart != NULL && highlight)
6811 {
6812 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006813 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 }
6815
6816 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6817 }
6818
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 vim_free(buf);
6821}
6822#endif
6823
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824/*
6825 * Redraw the status line of window wp.
6826 *
6827 * If inversion is possible we use it. Else '=' characters are used.
6828 */
6829 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006830win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831{
6832 int row;
6833 char_u *p;
6834 int len;
6835 int fillchar;
6836 int attr;
6837 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006838 static int busy = FALSE;
6839
6840 /* It's possible to get here recursively when 'statusline' (indirectly)
6841 * invokes ":redrawstatus". Simply ignore the call then. */
6842 if (busy)
6843 return;
6844 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845
6846 wp->w_redr_status = FALSE;
6847 if (wp->w_status_height == 0)
6848 {
6849 /* no status line, can only be last window */
6850 redraw_cmdline = TRUE;
6851 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006852 else if (!redrawing()
6853#ifdef FEAT_INS_EXPAND
6854 /* don't update status line when popup menu is visible and may be
6855 * drawn over it */
6856 || pum_visible()
6857#endif
6858 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {
6860 /* Don't redraw right now, do it later. */
6861 wp->w_redr_status = TRUE;
6862 }
6863#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006864 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {
6866 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006867 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 }
6869#endif
6870 else
6871 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006872 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006874 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 p = NameBuff;
6876 len = (int)STRLEN(p);
6877
Bram Moolenaard85f2712017-07-28 21:51:57 +02006878 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879#ifdef FEAT_QUICKFIX
6880 || wp->w_p_pvw
6881#endif
6882 || bufIsChanged(wp->w_buffer)
6883 || wp->w_buffer->b_p_ro)
6884 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006885 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006887 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 len += (int)STRLEN(p + len);
6889 }
6890#ifdef FEAT_QUICKFIX
6891 if (wp->w_p_pvw)
6892 {
6893 STRCPY(p + len, _("[Preview]"));
6894 len += (int)STRLEN(p + len);
6895 }
6896#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006897 if (bufIsChanged(wp->w_buffer)
6898#ifdef FEAT_TERMINAL
6899 && !bt_terminal(wp->w_buffer)
6900#endif
6901 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 {
6903 STRCPY(p + len, "[+]");
6904 len += 3;
6905 }
6906 if (wp->w_buffer->b_p_ro)
6907 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006908 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006909 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 }
6911
Bram Moolenaar02631462017-09-22 15:20:32 +02006912 this_ru_col = ru_col - (Columns - wp->w_width);
6913 if (this_ru_col < (wp->w_width + 1) / 2)
6914 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 if (this_ru_col <= 1)
6916 {
6917 p = (char_u *)"<"; /* No room for file name! */
6918 len = 1;
6919 }
6920 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921#ifdef FEAT_MBYTE
6922 if (has_mbyte)
6923 {
6924 int clen = 0, i;
6925
6926 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006927 clen = mb_string2cells(p, -1);
6928
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 /* Find first character that will fit.
6930 * Going from start to end is much faster for DBCS. */
6931 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006932 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 clen -= (*mb_ptr2cells)(p + i);
6934 len = clen;
6935 if (i > 0)
6936 {
6937 p = p + i - 1;
6938 *p = '<';
6939 ++len;
6940 }
6941
6942 }
6943 else
6944#endif
6945 if (len > this_ru_col - 1)
6946 {
6947 p += len - (this_ru_col - 1);
6948 *p = '<';
6949 len = this_ru_col - 1;
6950 }
6951
6952 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006953 screen_puts(p, row, wp->w_wincol, attr);
6954 screen_fill(row, row + 1, len + wp->w_wincol,
6955 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006957 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6959 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006960 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961
6962#ifdef FEAT_CMDL_INFO
6963 win_redr_ruler(wp, TRUE);
6964#endif
6965 }
6966
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 /*
6968 * May need to draw the character below the vertical separator.
6969 */
6970 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6971 {
6972 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006973 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 else
6975 fillchar = fillchar_vsep(&attr);
6976 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6977 attr);
6978 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006979 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980}
6981
Bram Moolenaar238a5642006-02-21 22:12:05 +00006982#ifdef FEAT_STL_OPT
6983/*
6984 * Redraw the status line according to 'statusline' and take care of any
6985 * errors encountered.
6986 */
6987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006988redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006989{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006990 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006991 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006992
6993 /* When called recursively return. This can happen when the statusline
6994 * contains an expression that triggers a redraw. */
6995 if (entered)
6996 return;
6997 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006998
Bram Moolenaara742e082016-04-05 21:10:38 +02006999 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007000 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007001 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007002 {
7003 /* When there is an error disable the statusline, otherwise the
7004 * display is messed up with errors and a redraw triggers the problem
7005 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007006 set_string_option_direct((char_u *)"statusline", -1,
7007 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007008 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007009 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007010 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007011 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007012}
7013#endif
7014
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015/*
7016 * Return TRUE if the status line of window "wp" is connected to the status
7017 * line of the window right of it. If not, then it's a vertical separator.
7018 * Only call if (wp->w_vsep_width != 0).
7019 */
7020 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007021stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023 frame_T *fr;
7024
7025 fr = wp->w_frame;
7026 while (fr->fr_parent != NULL)
7027 {
7028 if (fr->fr_parent->fr_layout == FR_COL)
7029 {
7030 if (fr->fr_next != NULL)
7031 break;
7032 }
7033 else
7034 {
7035 if (fr->fr_next != NULL)
7036 return TRUE;
7037 }
7038 fr = fr->fr_parent;
7039 }
7040 return FALSE;
7041}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044/*
7045 * Get the value to show for the language mappings, active 'keymap'.
7046 */
7047 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007048get_keymap_str(
7049 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007050 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007051 char_u *buf, /* buffer for the result */
7052 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053{
7054 char_u *p;
7055
7056 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7057 return FALSE;
7058
7059 {
7060#ifdef FEAT_EVAL
7061 buf_T *old_curbuf = curbuf;
7062 win_T *old_curwin = curwin;
7063 char_u *s;
7064
7065 curbuf = wp->w_buffer;
7066 curwin = wp;
7067 STRCPY(buf, "b:keymap_name"); /* must be writable */
7068 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007069 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 --emsg_skip;
7071 curbuf = old_curbuf;
7072 curwin = old_curwin;
7073 if (p == NULL || *p == NUL)
7074#endif
7075 {
7076#ifdef FEAT_KEYMAP
7077 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7078 p = wp->w_buffer->b_p_keymap;
7079 else
7080#endif
7081 p = (char_u *)"lang";
7082 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007083 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 buf[0] = NUL;
7085#ifdef FEAT_EVAL
7086 vim_free(s);
7087#endif
7088 }
7089 return buf[0] != NUL;
7090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091
7092#if defined(FEAT_STL_OPT) || defined(PROTO)
7093/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007094 * Redraw the status line or ruler of window "wp".
7095 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 */
7097 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007098win_redr_custom(
7099 win_T *wp,
7100 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007102 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 int attr;
7104 int curattr;
7105 int row;
7106 int col = 0;
7107 int maxwidth;
7108 int width;
7109 int n;
7110 int len;
7111 int fillchar;
7112 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007113 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007115 struct stl_hlrec hltab[STL_MAX_ITEM];
7116 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007117 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007118 win_T *ewp;
7119 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120
Bram Moolenaar1d633412013-12-11 15:52:01 +01007121 /* There is a tiny chance that this gets called recursively: When
7122 * redrawing a status line triggers redrawing the ruler or tabline.
7123 * Avoid trouble by not allowing recursion. */
7124 if (entered)
7125 return;
7126 entered = TRUE;
7127
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007129 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007131 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007132 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007133 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007134 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007135 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007136 maxwidth = Columns;
7137# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007138 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007141 else
7142 {
7143 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007144 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007145 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007146
7147 if (draw_ruler)
7148 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007149 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007150 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007151 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007152 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007153 if (*++stl == '-')
7154 stl++;
7155 if (atoi((char *)stl))
7156 while (VIM_ISDIGIT(*stl))
7157 stl++;
7158 if (*stl++ != '(')
7159 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007160 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007161 col = ru_col - (Columns - wp->w_width);
7162 if (col < (wp->w_width + 1) / 2)
7163 col = (wp->w_width + 1) / 2;
7164 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007165 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007166 {
7167 row = Rows - 1;
7168 --maxwidth; /* writing in last column may cause scrolling */
7169 fillchar = ' ';
7170 attr = 0;
7171 }
7172
7173# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007174 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007175# endif
7176 }
7177 else
7178 {
7179 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007180 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007181 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007182 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007183# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007184 use_sandbox = was_set_insecurely((char_u *)"statusline",
7185 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007186# endif
7187 }
7188
Bram Moolenaar53f81742017-09-22 14:35:51 +02007189 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007190 }
7191
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007193 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
Bram Moolenaar61452852011-02-01 18:01:11 +01007195 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7196 * the cursor away and back. */
7197 ewp = wp == NULL ? curwin : wp;
7198 p_crb_save = ewp->w_p_crb;
7199 ewp->w_p_crb = FALSE;
7200
Bram Moolenaar362f3562009-11-03 16:20:34 +00007201 /* Make a copy, because the statusline may include a function call that
7202 * might change the option value and free the memory. */
7203 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007204 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007205 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007206 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007207 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007208 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007210 /* Make all characters printable. */
7211 p = transstr(buf);
7212 if (p != NULL)
7213 {
7214 vim_strncpy(buf, p, sizeof(buf) - 1);
7215 vim_free(p);
7216 }
7217
7218 /* fill up with "fillchar" */
7219 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007220 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {
7222#ifdef FEAT_MBYTE
7223 len += (*mb_char2bytes)(fillchar, buf + len);
7224#else
7225 buf[len++] = fillchar;
7226#endif
7227 ++width;
7228 }
7229 buf[len] = NUL;
7230
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007231 /*
7232 * Draw each snippet with the specified highlighting.
7233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 curattr = attr;
7235 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007236 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007238 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 screen_puts_len(p, len, row, col, curattr);
7240 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007241 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007243 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007245 else if (hltab[n].userhl < 0)
7246 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007247#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007248 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7249 && wp->w_status_height != 0)
7250 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007251 else if (wp != NULL && bt_terminal(wp->w_buffer)
7252 && wp->w_status_height != 0)
7253 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007254#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007255 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007256 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007258 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 }
7260 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007261
7262 if (wp == NULL)
7263 {
7264 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7265 col = 0;
7266 len = 0;
7267 p = buf;
7268 fillchar = 0;
7269 for (n = 0; tabtab[n].start != NULL; n++)
7270 {
7271 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7272 while (col < len)
7273 TabPageIdxs[col++] = fillchar;
7274 p = tabtab[n].start;
7275 fillchar = tabtab[n].userhl;
7276 }
7277 while (col < Columns)
7278 TabPageIdxs[col++] = fillchar;
7279 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007280
7281theend:
7282 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283}
7284
7285#endif /* FEAT_STL_OPT */
7286
7287/*
7288 * Output a single character directly to the screen and update ScreenLines.
7289 */
7290 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007291screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 char_u buf[MB_MAXBYTES + 1];
7294
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007295#ifdef FEAT_MBYTE
7296 if (has_mbyte)
7297 buf[(*mb_char2bytes)(c, buf)] = NUL;
7298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007300 {
7301 buf[0] = c;
7302 buf[1] = NUL;
7303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 screen_puts(buf, row, col, attr);
7305}
7306
7307/*
7308 * Get a single character directly from ScreenLines into "bytes[]".
7309 * Also return its attribute in *attrp;
7310 */
7311 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007312screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313{
7314 unsigned off;
7315
7316 /* safety check */
7317 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7318 {
7319 off = LineOffset[row] + col;
7320 *attrp = ScreenAttrs[off];
7321 bytes[0] = ScreenLines[off];
7322 bytes[1] = NUL;
7323
7324#ifdef FEAT_MBYTE
7325 if (enc_utf8 && ScreenLinesUC[off] != 0)
7326 bytes[utfc_char2bytes(off, bytes)] = NUL;
7327 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7328 {
7329 bytes[0] = ScreenLines[off];
7330 bytes[1] = ScreenLines2[off];
7331 bytes[2] = NUL;
7332 }
7333 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7334 {
7335 bytes[1] = ScreenLines[off + 1];
7336 bytes[2] = NUL;
7337 }
7338#endif
7339 }
7340}
7341
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007342#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007343static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007344
7345/*
7346 * Return TRUE if composing characters for screen posn "off" differs from
7347 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007348 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007349 */
7350 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007351screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007352{
7353 int i;
7354
7355 for (i = 0; i < Screen_mco; ++i)
7356 {
7357 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7358 return TRUE;
7359 if (u8cc[i] == 0)
7360 break;
7361 }
7362 return FALSE;
7363}
7364#endif
7365
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366/*
7367 * Put string '*text' on the screen at position 'row' and 'col', with
7368 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7369 * Note: only outputs within one row, message is truncated at screen boundary!
7370 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7371 */
7372 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007373screen_puts(
7374 char_u *text,
7375 int row,
7376 int col,
7377 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378{
7379 screen_puts_len(text, -1, row, col, attr);
7380}
7381
7382/*
7383 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7384 * a NUL.
7385 */
7386 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007387screen_puts_len(
7388 char_u *text,
7389 int textlen,
7390 int row,
7391 int col,
7392 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
7394 unsigned off;
7395 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007396 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 int c;
7398#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007399 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 int mbyte_blen = 1;
7401 int mbyte_cells = 1;
7402 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007403 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 int clear_next_cell = FALSE;
7405# ifdef FEAT_ARABIC
7406 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007407 int pc, nc, nc1;
7408 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409# endif
7410#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007411#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7412 int force_redraw_this;
7413 int force_redraw_next = FALSE;
7414#endif
7415 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416
7417 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7418 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007419 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420
Bram Moolenaarc236c162008-07-13 17:41:49 +00007421#ifdef FEAT_MBYTE
7422 /* When drawing over the right halve of a double-wide char clear out the
7423 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007424 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007425# ifdef FEAT_GUI
7426 && !gui.in_use
7427# endif
7428 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007429 {
7430 ScreenLines[off - 1] = ' ';
7431 ScreenAttrs[off - 1] = 0;
7432 if (enc_utf8)
7433 {
7434 ScreenLinesUC[off - 1] = 0;
7435 ScreenLinesC[0][off - 1] = 0;
7436 }
7437 /* redraw the previous cell, make it empty */
7438 screen_char(off - 1, row, col - 1);
7439 /* force the cell at "col" to be redrawn */
7440 force_redraw_next = TRUE;
7441 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007442#endif
7443
Bram Moolenaar367329b2007-08-30 11:53:22 +00007444#ifdef FEAT_MBYTE
7445 max_off = LineOffset[row] + screen_Columns;
7446#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007447 while (col < screen_Columns
7448 && (len < 0 || (int)(ptr - text) < len)
7449 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 {
7451 c = *ptr;
7452#ifdef FEAT_MBYTE
7453 /* check if this is the first byte of a multibyte */
7454 if (has_mbyte)
7455 {
7456 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007457 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007459 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7461 mbyte_cells = 1;
7462 else if (enc_dbcs != 0)
7463 mbyte_cells = mbyte_blen;
7464 else /* enc_utf8 */
7465 {
7466 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007467 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 (int)((text + len) - ptr));
7469 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007470 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007472# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 /* Non-BMP character: display as ? or fullwidth ?. */
7474 if (u8c >= 0x10000)
7475 {
7476 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7477 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007478 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481# ifdef FEAT_ARABIC
7482 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7483 {
7484 /* Do Arabic shaping. */
7485 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7486 {
7487 /* Past end of string to be displayed. */
7488 nc = NUL;
7489 nc1 = NUL;
7490 }
7491 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007492 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007493 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7494 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007495 nc1 = pcc[0];
7496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 pc = prev_c;
7498 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007499 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 }
7501 else
7502 prev_c = u8c;
7503# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007504 if (col + mbyte_cells > screen_Columns)
7505 {
7506 /* Only 1 cell left, but character requires 2 cells:
7507 * display a '>' in the last column to avoid wrapping. */
7508 c = '>';
7509 mbyte_cells = 1;
7510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 }
7512 }
7513#endif
7514
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007515#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7516 force_redraw_this = force_redraw_next;
7517 force_redraw_next = FALSE;
7518#endif
7519
7520 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521#ifdef FEAT_MBYTE
7522 || (mbyte_cells == 2
7523 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7524 || (enc_dbcs == DBCS_JPNU
7525 && c == 0x8e
7526 && ScreenLines2[off] != ptr[1])
7527 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007528 && (ScreenLinesUC[off] !=
7529 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7530 || (ScreenLinesUC[off] != 0
7531 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532#endif
7533 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007534 || exmode_active;
7535
7536 if (need_redraw
7537#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7538 || force_redraw_this
7539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 )
7541 {
7542#if defined(FEAT_GUI) || defined(UNIX)
7543 /* The bold trick makes a single row of pixels appear in the next
7544 * character. When a bold character is removed, the next
7545 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007546 * and for some xterms. */
7547 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548# ifdef FEAT_GUI
7549 gui.in_use
7550# endif
7551# if defined(FEAT_GUI) && defined(UNIX)
7552 ||
7553# endif
7554# ifdef UNIX
7555 term_is_xterm
7556# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007557 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007559 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007561 if (n > HL_ALL)
7562 n = syn_attr2attr(n);
7563 if (n & HL_BOLD)
7564 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 }
7566#endif
7567#ifdef FEAT_MBYTE
7568 /* When at the end of the text and overwriting a two-cell
7569 * character with a one-cell character, need to clear the next
7570 * cell. Also when overwriting the left halve of a two-cell char
7571 * with the right halve of a two-cell char. Do this only once
7572 * (mb_off2cells() may return 2 on the right halve). */
7573 if (clear_next_cell)
7574 clear_next_cell = FALSE;
7575 else if (has_mbyte
7576 && (len < 0 ? ptr[mbyte_blen] == NUL
7577 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007578 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007580 && (*mb_off2cells)(off, max_off) == 1
7581 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 clear_next_cell = TRUE;
7583
7584 /* Make sure we never leave a second byte of a double-byte behind,
7585 * it confuses mb_off2cells(). */
7586 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007587 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007589 && (*mb_off2cells)(off, max_off) == 1
7590 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 ScreenLines[off + mbyte_blen] = 0;
7592#endif
7593 ScreenLines[off] = c;
7594 ScreenAttrs[off] = attr;
7595#ifdef FEAT_MBYTE
7596 if (enc_utf8)
7597 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007598 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 ScreenLinesUC[off] = 0;
7600 else
7601 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007602 int i;
7603
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007605 for (i = 0; i < Screen_mco; ++i)
7606 {
7607 ScreenLinesC[i][off] = u8cc[i];
7608 if (u8cc[i] == 0)
7609 break;
7610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 }
7612 if (mbyte_cells == 2)
7613 {
7614 ScreenLines[off + 1] = 0;
7615 ScreenAttrs[off + 1] = attr;
7616 }
7617 screen_char(off, row, col);
7618 }
7619 else if (mbyte_cells == 2)
7620 {
7621 ScreenLines[off + 1] = ptr[1];
7622 ScreenAttrs[off + 1] = attr;
7623 screen_char_2(off, row, col);
7624 }
7625 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7626 {
7627 ScreenLines2[off] = ptr[1];
7628 screen_char(off, row, col);
7629 }
7630 else
7631#endif
7632 screen_char(off, row, col);
7633 }
7634#ifdef FEAT_MBYTE
7635 if (has_mbyte)
7636 {
7637 off += mbyte_cells;
7638 col += mbyte_cells;
7639 ptr += mbyte_blen;
7640 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007641 {
7642 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007644 len = -1;
7645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 }
7647 else
7648#endif
7649 {
7650 ++off;
7651 ++col;
7652 ++ptr;
7653 }
7654 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007655
7656#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7657 /* If we detected the next character needs to be redrawn, but the text
7658 * doesn't extend up to there, update the character here. */
7659 if (force_redraw_next && col < screen_Columns)
7660 {
7661# ifdef FEAT_MBYTE
7662 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7663 screen_char_2(off, row, col);
7664 else
7665# endif
7666 screen_char(off, row, col);
7667 }
7668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669}
7670
7671#ifdef FEAT_SEARCH_EXTRA
7672/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007673 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 */
7675 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007676start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677{
7678 if (p_hls && !no_hlsearch)
7679 {
7680 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007681 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007682# ifdef FEAT_RELTIME
7683 /* Set the time limit to 'redrawtime'. */
7684 profile_setlimit(p_rdt, &search_hl.tm);
7685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
7687}
7688
7689/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007690 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 */
7692 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007693end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
7695 if (search_hl.rm.regprog != NULL)
7696 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007697 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 search_hl.rm.regprog = NULL;
7699 }
7700}
7701
7702/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007703 * Init for calling prepare_search_hl().
7704 */
7705 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007706init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007707{
7708 matchitem_T *cur;
7709
7710 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7711 * match */
7712 cur = wp->w_match_head;
7713 while (cur != NULL)
7714 {
7715 cur->hl.rm = cur->match;
7716 if (cur->hlg_id == 0)
7717 cur->hl.attr = 0;
7718 else
7719 cur->hl.attr = syn_id2attr(cur->hlg_id);
7720 cur->hl.buf = wp->w_buffer;
7721 cur->hl.lnum = 0;
7722 cur->hl.first_lnum = 0;
7723# ifdef FEAT_RELTIME
7724 /* Set the time limit to 'redrawtime'. */
7725 profile_setlimit(p_rdt, &(cur->hl.tm));
7726# endif
7727 cur = cur->next;
7728 }
7729 search_hl.buf = wp->w_buffer;
7730 search_hl.lnum = 0;
7731 search_hl.first_lnum = 0;
7732 /* time limit is set at the toplevel, for all windows */
7733}
7734
7735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 * Advance to the match in window "wp" line "lnum" or past it.
7737 */
7738 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007739prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007741 matchitem_T *cur; /* points to the match list */
7742 match_T *shl; /* points to search_hl or a match */
7743 int shl_flag; /* flag to indicate whether search_hl
7744 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007745 int pos_inprogress; /* marks that position match search is
7746 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 int n;
7748
7749 /*
7750 * When using a multi-line pattern, start searching at the top
7751 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007752 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007754 cur = wp->w_match_head;
7755 shl_flag = FALSE;
7756 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007758 if (shl_flag == FALSE)
7759 {
7760 shl = &search_hl;
7761 shl_flag = TRUE;
7762 }
7763 else
7764 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 if (shl->rm.regprog != NULL
7766 && shl->lnum == 0
7767 && re_multiline(shl->rm.regprog))
7768 {
7769 if (shl->first_lnum == 0)
7770 {
7771# ifdef FEAT_FOLDING
7772 for (shl->first_lnum = lnum;
7773 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7774 if (hasFoldingWin(wp, shl->first_lnum - 1,
7775 NULL, NULL, TRUE, NULL))
7776 break;
7777# else
7778 shl->first_lnum = wp->w_topline;
7779# endif
7780 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007781 if (cur != NULL)
7782 cur->pos.cur = 0;
7783 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007785 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7786 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007788 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7789 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007790 pos_inprogress = cur == NULL || cur->pos.cur == 0
7791 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 if (shl->lnum != 0)
7793 {
7794 shl->first_lnum = shl->lnum
7795 + shl->rm.endpos[0].lnum
7796 - shl->rm.startpos[0].lnum;
7797 n = shl->rm.endpos[0].col;
7798 }
7799 else
7800 {
7801 ++shl->first_lnum;
7802 n = 0;
7803 }
7804 }
7805 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007806 if (shl != &search_hl && cur != NULL)
7807 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809}
7810
7811/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007812 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 * Uses shl->buf.
7814 * Sets shl->lnum and shl->rm contents.
7815 * Note: Assumes a previous match is always before "lnum", unless
7816 * shl->lnum is zero.
7817 * Careful: Any pointers for buffer lines will become invalid.
7818 */
7819 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007820next_search_hl(
7821 win_T *win,
7822 match_T *shl, /* points to search_hl or a match */
7823 linenr_T lnum,
7824 colnr_T mincol, /* minimal column for a match */
7825 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826{
7827 linenr_T l;
7828 colnr_T matchcol;
7829 long nmatched;
7830
7831 if (shl->lnum != 0)
7832 {
7833 /* Check for three situations:
7834 * 1. If the "lnum" is below a previous match, start a new search.
7835 * 2. If the previous match includes "mincol", use it.
7836 * 3. Continue after the previous match.
7837 */
7838 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7839 if (lnum > l)
7840 shl->lnum = 0;
7841 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7842 return;
7843 }
7844
7845 /*
7846 * Repeat searching for a match until one is found that includes "mincol"
7847 * or none is found in this line.
7848 */
7849 called_emsg = FALSE;
7850 for (;;)
7851 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007852#ifdef FEAT_RELTIME
7853 /* Stop searching after passing the time limit. */
7854 if (profile_passed_limit(&(shl->tm)))
7855 {
7856 shl->lnum = 0; /* no match found in time */
7857 break;
7858 }
7859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 /* Three situations:
7861 * 1. No useful previous match: search from start of line.
7862 * 2. Not Vi compatible or empty match: continue at next character.
7863 * Break the loop if this is beyond the end of the line.
7864 * 3. Vi compatible searching: continue at end of previous match.
7865 */
7866 if (shl->lnum == 0)
7867 matchcol = 0;
7868 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7869 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007870 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007872 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007873
7874 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007875 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007876 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007878 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 shl->lnum = 0;
7880 break;
7881 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007882#ifdef FEAT_MBYTE
7883 if (has_mbyte)
7884 matchcol += mb_ptr2len(ml);
7885 else
7886#endif
7887 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 }
7889 else
7890 matchcol = shl->rm.endpos[0].col;
7891
7892 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007893 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007895 /* Remember whether shl->rm is using a copy of the regprog in
7896 * cur->match. */
7897 int regprog_is_copy = (shl != &search_hl && cur != NULL
7898 && shl == &cur->hl
7899 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007900 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007901
Bram Moolenaarb3414592014-06-17 17:48:32 +02007902 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7903 matchcol,
7904#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007905 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007906#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007907 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007908#endif
7909 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007910 /* Copy the regprog, in case it got freed and recompiled. */
7911 if (regprog_is_copy)
7912 cur->match.regprog = cur->hl.rm.regprog;
7913
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007914 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007915 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007916 /* Error while handling regexp: stop using this regexp. */
7917 if (shl == &search_hl)
7918 {
7919 /* don't free regprog in the match list, it's a copy */
7920 vim_regfree(shl->rm.regprog);
7921 SET_NO_HLSEARCH(TRUE);
7922 }
7923 shl->rm.regprog = NULL;
7924 shl->lnum = 0;
7925 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7926 message */
7927 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007928 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007929 }
7930 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007931 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007932 else
7933 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 if (nmatched == 0)
7935 {
7936 shl->lnum = 0; /* no match found */
7937 break;
7938 }
7939 if (shl->rm.startpos[0].lnum > 0
7940 || shl->rm.startpos[0].col >= mincol
7941 || nmatched > 1
7942 || shl->rm.endpos[0].col > mincol)
7943 {
7944 shl->lnum += shl->rm.startpos[0].lnum;
7945 break; /* useful match found */
7946 }
7947 }
7948}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949
Bram Moolenaar85077472016-10-16 14:35:48 +02007950/*
7951 * If there is a match fill "shl" and return one.
7952 * Return zero otherwise.
7953 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007954 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007955next_search_hl_pos(
7956 match_T *shl, /* points to a match */
7957 linenr_T lnum,
7958 posmatch_T *posmatch, /* match positions */
7959 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007960{
7961 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007962 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007963
Bram Moolenaarb3414592014-06-17 17:48:32 +02007964 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7965 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007966 llpos_T *pos = &posmatch->pos[i];
7967
7968 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007969 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007970 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007971 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007972 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007973 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007974 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007975 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007976 /* if this match comes before the one at "found" then swap
7977 * them */
7978 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007979 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007980 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981
Bram Moolenaar85077472016-10-16 14:35:48 +02007982 *pos = posmatch->pos[found];
7983 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007984 }
7985 }
7986 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007987 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007988 }
7989 }
7990 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007991 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007992 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007993 colnr_T start = posmatch->pos[found].col == 0
7994 ? 0 : posmatch->pos[found].col - 1;
7995 colnr_T end = posmatch->pos[found].col == 0
7996 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007997
Bram Moolenaar85077472016-10-16 14:35:48 +02007998 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007999 shl->rm.startpos[0].lnum = 0;
8000 shl->rm.startpos[0].col = start;
8001 shl->rm.endpos[0].lnum = 0;
8002 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008003 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008004 posmatch->cur = found + 1;
8005 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008006 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008007 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008008}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008009#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008010
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008012screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013{
8014 attrentry_T *aep = NULL;
8015
8016 screen_attr = attr;
8017 if (full_screen
8018#ifdef WIN3264
8019 && termcap_active
8020#endif
8021 )
8022 {
8023#ifdef FEAT_GUI
8024 if (gui.in_use)
8025 {
8026 char buf[20];
8027
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008028 /* The GUI handles this internally. */
8029 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 OUT_STR(buf);
8031 }
8032 else
8033#endif
8034 {
8035 if (attr > HL_ALL) /* special HL attr. */
8036 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008037 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 aep = syn_cterm_attr2entry(attr);
8039 else
8040 aep = syn_term_attr2entry(attr);
8041 if (aep == NULL) /* did ":syntax clear" */
8042 attr = 0;
8043 else
8044 attr = aep->ae_attr;
8045 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008046 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008048 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008049#ifdef FEAT_TERMGUICOLORS
8050 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008051 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008052#endif
8053 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008054#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008055 )
8056#endif
8057 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008058 /* If the Normal FG color has BOLD attribute and the new HL
8059 * has a FG color defined, clear BOLD. */
8060 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008061 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008063 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008064 out_str(T_UCS);
8065 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008066 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8067 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008069 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008071 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008073 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008074 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075
8076 /*
8077 * Output the color or start string after bold etc., in case the
8078 * bold etc. override the color setting.
8079 */
8080 if (aep != NULL)
8081 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008082#ifdef FEAT_TERMGUICOLORS
8083 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008085 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008086 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008087 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008088 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 }
8090 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008093 if (t_colors > 1)
8094 {
8095 if (aep->ae_u.cterm.fg_color)
8096 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8097 if (aep->ae_u.cterm.bg_color)
8098 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8099 }
8100 else
8101 {
8102 if (aep->ae_u.term.start != NULL)
8103 out_str(aep->ae_u.term.start);
8104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 }
8106 }
8107 }
8108 }
8109}
8110
8111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008112screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113{
8114 int do_ME = FALSE; /* output T_ME code */
8115
8116 if (screen_attr != 0
8117#ifdef WIN3264
8118 && termcap_active
8119#endif
8120 )
8121 {
8122#ifdef FEAT_GUI
8123 if (gui.in_use)
8124 {
8125 char buf[20];
8126
8127 /* use internal GUI code */
8128 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8129 OUT_STR(buf);
8130 }
8131 else
8132#endif
8133 {
8134 if (screen_attr > HL_ALL) /* special HL attr. */
8135 {
8136 attrentry_T *aep;
8137
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008138 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {
8140 /*
8141 * Assume that t_me restores the original colors!
8142 */
8143 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008144 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008145#ifdef FEAT_TERMGUICOLORS
8146 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008147 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8148 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008149#endif
8150 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008151#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008152 )
8153#endif
8154 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 do_ME = TRUE;
8156 }
8157 else
8158 {
8159 aep = syn_term_attr2entry(screen_attr);
8160 if (aep != NULL && aep->ae_u.term.stop != NULL)
8161 {
8162 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8163 do_ME = TRUE;
8164 else
8165 out_str(aep->ae_u.term.stop);
8166 }
8167 }
8168 if (aep == NULL) /* did ":syntax clear" */
8169 screen_attr = 0;
8170 else
8171 screen_attr = aep->ae_attr;
8172 }
8173
8174 /*
8175 * Often all ending-codes are equal to T_ME. Avoid outputting the
8176 * same sequence several times.
8177 */
8178 if (screen_attr & HL_STANDOUT)
8179 {
8180 if (STRCMP(T_SE, T_ME) == 0)
8181 do_ME = TRUE;
8182 else
8183 out_str(T_SE);
8184 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008185 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008186 {
8187 if (STRCMP(T_UCE, T_ME) == 0)
8188 do_ME = TRUE;
8189 else
8190 out_str(T_UCE);
8191 }
8192 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008193 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {
8195 if (STRCMP(T_UE, T_ME) == 0)
8196 do_ME = TRUE;
8197 else
8198 out_str(T_UE);
8199 }
8200 if (screen_attr & HL_ITALIC)
8201 {
8202 if (STRCMP(T_CZR, T_ME) == 0)
8203 do_ME = TRUE;
8204 else
8205 out_str(T_CZR);
8206 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008207 if (screen_attr & HL_STRIKETHROUGH)
8208 {
8209 if (STRCMP(T_STE, T_ME) == 0)
8210 do_ME = TRUE;
8211 else
8212 out_str(T_STE);
8213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8215 out_str(T_ME);
8216
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008217#ifdef FEAT_TERMGUICOLORS
8218 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008220 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008221 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008222 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008223 term_bg_rgb_color(cterm_normal_bg_gui_color);
8224 }
8225 else
8226#endif
8227 {
8228 if (t_colors > 1)
8229 {
8230 /* set Normal cterm colors */
8231 if (cterm_normal_fg_color != 0)
8232 term_fg_color(cterm_normal_fg_color - 1);
8233 if (cterm_normal_bg_color != 0)
8234 term_bg_color(cterm_normal_bg_color - 1);
8235 if (cterm_normal_fg_bold)
8236 out_str(T_MD);
8237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 }
8239 }
8240 }
8241 screen_attr = 0;
8242}
8243
8244/*
8245 * Reset the colors for a cterm. Used when leaving Vim.
8246 * The machine specific code may override this again.
8247 */
8248 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008249reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008251 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {
8253 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008254#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008255 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8256 || cterm_normal_bg_gui_color != INVALCOLOR)
8257 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008258#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {
8262 out_str(T_OP);
8263 screen_attr = -1;
8264 }
8265 if (cterm_normal_fg_bold)
8266 {
8267 out_str(T_ME);
8268 screen_attr = -1;
8269 }
8270 }
8271}
8272
8273/*
8274 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8275 * using the attributes from ScreenAttrs["off"].
8276 */
8277 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008278screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279{
8280 int attr;
8281
8282 /* Check for illegal values, just in case (could happen just after
8283 * resizing). */
8284 if (row >= screen_Rows || col >= screen_Columns)
8285 return;
8286
Bram Moolenaar494838a2015-02-10 19:20:37 +01008287 /* Outputting a character in the last cell on the screen may scroll the
8288 * screen up. Only do it when the "xn" termcap property is set, otherwise
8289 * mark the character invalid (update it when scrolled up). */
8290 if (*T_XN == NUL
8291 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292#ifdef FEAT_RIGHTLEFT
8293 /* account for first command-line character in rightleft mode */
8294 && !cmdmsg_rl
8295#endif
8296 )
8297 {
8298 ScreenAttrs[off] = (sattr_T)-1;
8299 return;
8300 }
8301
8302 /*
8303 * Stop highlighting first, so it's easier to move the cursor.
8304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 if (screen_char_attr != 0)
8306 attr = screen_char_attr;
8307 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 attr = ScreenAttrs[off];
8309 if (screen_attr != attr)
8310 screen_stop_highlight();
8311
8312 windgoto(row, col);
8313
8314 if (screen_attr != attr)
8315 screen_start_highlight(attr);
8316
8317#ifdef FEAT_MBYTE
8318 if (enc_utf8 && ScreenLinesUC[off] != 0)
8319 {
8320 char_u buf[MB_MAXBYTES + 1];
8321
Bram Moolenaarcb070082016-04-02 22:14:51 +02008322 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008323 {
8324 if (*p_ambw == 'd'
8325# ifdef FEAT_GUI
8326 && !gui.in_use
8327# endif
8328 )
8329 {
8330 /* Clear the two screen cells. If the character is actually
8331 * single width it won't change the second cell. */
8332 out_str((char_u *)" ");
8333 term_windgoto(row, col);
8334 }
8335 /* not sure where the cursor is after drawing the ambiguous width
8336 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008337 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008338 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008339 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008341
8342 /* Convert the UTF-8 character to bytes and write it. */
8343 buf[utfc_char2bytes(off, buf)] = NUL;
8344 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 }
8346 else
8347#endif
8348 {
8349#ifdef FEAT_MBYTE
8350 out_flush_check();
8351#endif
8352 out_char(ScreenLines[off]);
8353#ifdef FEAT_MBYTE
8354 /* double-byte character in single-width cell */
8355 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8356 out_char(ScreenLines2[off]);
8357#endif
8358 }
8359
8360 screen_cur_col++;
8361}
8362
8363#ifdef FEAT_MBYTE
8364
8365/*
8366 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8367 * on the screen at position 'row' and 'col'.
8368 * The attributes of the first byte is used for all. This is required to
8369 * output the two bytes of a double-byte character with nothing in between.
8370 */
8371 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008372screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
8374 /* Check for illegal values (could be wrong when screen was resized). */
8375 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8376 return;
8377
8378 /* Outputting the last character on the screen may scrollup the screen.
8379 * Don't to it! Mark the character invalid (update it when scrolled up) */
8380 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8381 {
8382 ScreenAttrs[off] = (sattr_T)-1;
8383 return;
8384 }
8385
8386 /* Output the first byte normally (positions the cursor), then write the
8387 * second byte directly. */
8388 screen_char(off, row, col);
8389 out_char(ScreenLines[off + 1]);
8390 ++screen_cur_col;
8391}
8392#endif
8393
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394/*
8395 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8396 * This uses the contents of ScreenLines[] and doesn't change it.
8397 */
8398 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008399screen_draw_rectangle(
8400 int row,
8401 int col,
8402 int height,
8403 int width,
8404 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405{
8406 int r, c;
8407 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008408#ifdef FEAT_MBYTE
8409 int max_off;
8410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008412 /* Can't use ScreenLines unless initialized */
8413 if (ScreenLines == NULL)
8414 return;
8415
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 if (invert)
8417 screen_char_attr = HL_INVERSE;
8418 for (r = row; r < row + height; ++r)
8419 {
8420 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008421#ifdef FEAT_MBYTE
8422 max_off = off + screen_Columns;
8423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 for (c = col; c < col + width; ++c)
8425 {
8426#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008427 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {
8429 screen_char_2(off + c, r, c);
8430 ++c;
8431 }
8432 else
8433#endif
8434 {
8435 screen_char(off + c, r, c);
8436#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008437 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 ++c;
8439#endif
8440 }
8441 }
8442 }
8443 screen_char_attr = 0;
8444}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446/*
8447 * Redraw the characters for a vertically split window.
8448 */
8449 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008450redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451{
8452 int col;
8453 int width;
8454
8455# ifdef FEAT_CLIPBOARD
8456 clip_may_clear_selection(row, end - 1);
8457# endif
8458
8459 if (wp == NULL)
8460 {
8461 col = 0;
8462 width = Columns;
8463 }
8464 else
8465 {
8466 col = wp->w_wincol;
8467 width = wp->w_width;
8468 }
8469 screen_draw_rectangle(row, col, end - row, width, FALSE);
8470}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008472 static void
8473space_to_screenline(int off, int attr)
8474{
8475 ScreenLines[off] = ' ';
8476 ScreenAttrs[off] = attr;
8477# ifdef FEAT_MBYTE
8478 if (enc_utf8)
8479 ScreenLinesUC[off] = 0;
8480# endif
8481}
8482
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483/*
8484 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8485 * with character 'c1' in first column followed by 'c2' in the other columns.
8486 * Use attributes 'attr'.
8487 */
8488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008489screen_fill(
8490 int start_row,
8491 int end_row,
8492 int start_col,
8493 int end_col,
8494 int c1,
8495 int c2,
8496 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497{
8498 int row;
8499 int col;
8500 int off;
8501 int end_off;
8502 int did_delete;
8503 int c;
8504 int norm_term;
8505#if defined(FEAT_GUI) || defined(UNIX)
8506 int force_next = FALSE;
8507#endif
8508
8509 if (end_row > screen_Rows) /* safety check */
8510 end_row = screen_Rows;
8511 if (end_col > screen_Columns) /* safety check */
8512 end_col = screen_Columns;
8513 if (ScreenLines == NULL
8514 || start_row >= end_row
8515 || start_col >= end_col) /* nothing to do */
8516 return;
8517
8518 /* it's a "normal" terminal when not in a GUI or cterm */
8519 norm_term = (
8520#ifdef FEAT_GUI
8521 !gui.in_use &&
8522#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008523 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 for (row = start_row; row < end_row; ++row)
8525 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008526#ifdef FEAT_MBYTE
8527 if (has_mbyte
8528# ifdef FEAT_GUI
8529 && !gui.in_use
8530# endif
8531 )
8532 {
8533 /* When drawing over the right halve of a double-wide char clear
8534 * out the left halve. When drawing over the left halve of a
8535 * double wide-char clear out the right halve. Only needed in a
8536 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008537 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008538 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008539 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008540 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008541 }
8542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 /*
8544 * Try to use delete-line termcap code, when no attributes or in a
8545 * "normal" terminal, where a bold/italic space is just a
8546 * space.
8547 */
8548 did_delete = FALSE;
8549 if (c2 == ' '
8550 && end_col == Columns
8551 && can_clear(T_CE)
8552 && (attr == 0
8553 || (norm_term
8554 && attr <= HL_ALL
8555 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8556 {
8557 /*
8558 * check if we really need to clear something
8559 */
8560 col = start_col;
8561 if (c1 != ' ') /* don't clear first char */
8562 ++col;
8563
8564 off = LineOffset[row] + col;
8565 end_off = LineOffset[row] + end_col;
8566
8567 /* skip blanks (used often, keep it fast!) */
8568#ifdef FEAT_MBYTE
8569 if (enc_utf8)
8570 while (off < end_off && ScreenLines[off] == ' '
8571 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8572 ++off;
8573 else
8574#endif
8575 while (off < end_off && ScreenLines[off] == ' '
8576 && ScreenAttrs[off] == 0)
8577 ++off;
8578 if (off < end_off) /* something to be cleared */
8579 {
8580 col = off - LineOffset[row];
8581 screen_stop_highlight();
8582 term_windgoto(row, col);/* clear rest of this screen line */
8583 out_str(T_CE);
8584 screen_start(); /* don't know where cursor is now */
8585 col = end_col - col;
8586 while (col--) /* clear chars in ScreenLines */
8587 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008588 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 ++off;
8590 }
8591 }
8592 did_delete = TRUE; /* the chars are cleared now */
8593 }
8594
8595 off = LineOffset[row] + start_col;
8596 c = c1;
8597 for (col = start_col; col < end_col; ++col)
8598 {
8599 if (ScreenLines[off] != c
8600#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008601 || (enc_utf8 && (int)ScreenLinesUC[off]
8602 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603#endif
8604 || ScreenAttrs[off] != attr
8605#if defined(FEAT_GUI) || defined(UNIX)
8606 || force_next
8607#endif
8608 )
8609 {
8610#if defined(FEAT_GUI) || defined(UNIX)
8611 /* The bold trick may make a single row of pixels appear in
8612 * the next character. When a bold character is removed, the
8613 * next character should be redrawn too. This happens for our
8614 * own GUI and for some xterms. */
8615 if (
8616# ifdef FEAT_GUI
8617 gui.in_use
8618# endif
8619# if defined(FEAT_GUI) && defined(UNIX)
8620 ||
8621# endif
8622# ifdef UNIX
8623 term_is_xterm
8624# endif
8625 )
8626 {
8627 if (ScreenLines[off] != ' '
8628 && (ScreenAttrs[off] > HL_ALL
8629 || ScreenAttrs[off] & HL_BOLD))
8630 force_next = TRUE;
8631 else
8632 force_next = FALSE;
8633 }
8634#endif
8635 ScreenLines[off] = c;
8636#ifdef FEAT_MBYTE
8637 if (enc_utf8)
8638 {
8639 if (c >= 0x80)
8640 {
8641 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008642 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 }
8644 else
8645 ScreenLinesUC[off] = 0;
8646 }
8647#endif
8648 ScreenAttrs[off] = attr;
8649 if (!did_delete || c != ' ')
8650 screen_char(off, row, col);
8651 }
8652 ++off;
8653 if (col == start_col)
8654 {
8655 if (did_delete)
8656 break;
8657 c = c2;
8658 }
8659 }
8660 if (end_col == Columns)
8661 LineWraps[row] = FALSE;
8662 if (row == Rows - 1) /* overwritten the command line */
8663 {
8664 redraw_cmdline = TRUE;
8665 if (c1 == ' ' && c2 == ' ')
8666 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008667 if (start_col == 0)
8668 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 }
8670 }
8671}
8672
8673/*
8674 * Check if there should be a delay. Used before clearing or redrawing the
8675 * screen or the command line.
8676 */
8677 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008678check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679{
8680 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8681 && !did_wait_return
8682 && emsg_silent == 0)
8683 {
8684 out_flush();
8685 ui_delay(1000L, TRUE);
8686 emsg_on_display = FALSE;
8687 if (check_msg_scroll)
8688 msg_scroll = FALSE;
8689 }
8690}
8691
8692/*
8693 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008694 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 * Returns TRUE if there is a valid screen to write to.
8696 * Returns FALSE when starting up and screen not initialized yet.
8697 */
8698 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008699screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008701 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 return (ScreenLines != NULL);
8703}
8704
8705/*
8706 * Resize the shell to Rows and Columns.
8707 * Allocate ScreenLines[] and associated items.
8708 *
8709 * There may be some time between setting Rows and Columns and (re)allocating
8710 * ScreenLines[]. This happens when starting up and when (manually) changing
8711 * the shell size. Always use screen_Rows and screen_Columns to access items
8712 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8713 * final size of the shell is needed.
8714 */
8715 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008716screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717{
8718 int new_row, old_row;
8719#ifdef FEAT_GUI
8720 int old_Rows;
8721#endif
8722 win_T *wp;
8723 int outofmem = FALSE;
8724 int len;
8725 schar_T *new_ScreenLines;
8726#ifdef FEAT_MBYTE
8727 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008728 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008730 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731#endif
8732 sattr_T *new_ScreenAttrs;
8733 unsigned *new_LineOffset;
8734 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008735 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008736 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008738 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008739#ifdef FEAT_AUTOCMD
8740 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008742retry:
8743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 /*
8745 * Allocation of the screen buffers is done only when the size changes and
8746 * when Rows and Columns have been set and we have started doing full
8747 * screen stuff.
8748 */
8749 if ((ScreenLines != NULL
8750 && Rows == screen_Rows
8751 && Columns == screen_Columns
8752#ifdef FEAT_MBYTE
8753 && enc_utf8 == (ScreenLinesUC != NULL)
8754 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008755 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756#endif
8757 )
8758 || Rows == 0
8759 || Columns == 0
8760 || (!full_screen && ScreenLines == NULL))
8761 return;
8762
8763 /*
8764 * It's possible that we produce an out-of-memory message below, which
8765 * will cause this function to be called again. To break the loop, just
8766 * return here.
8767 */
8768 if (entered)
8769 return;
8770 entered = TRUE;
8771
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008772 /*
8773 * Note that the window sizes are updated before reallocating the arrays,
8774 * thus we must not redraw here!
8775 */
8776 ++RedrawingDisabled;
8777
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 win_new_shellsize(); /* fit the windows in the new sized shell */
8779
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 comp_col(); /* recompute columns for shown command and ruler */
8781
8782 /*
8783 * We're changing the size of the screen.
8784 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8785 * - Move lines from the old arrays into the new arrays, clear extra
8786 * lines (unless the screen is going to be cleared).
8787 * - Free the old arrays.
8788 *
8789 * If anything fails, make ScreenLines NULL, so we don't do anything!
8790 * Continuing with the old ScreenLines may result in a crash, because the
8791 * size is wrong.
8792 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008793 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008795#ifdef FEAT_AUTOCMD
8796 if (aucmd_win != NULL)
8797 win_free_lsize(aucmd_win);
8798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799
8800 new_ScreenLines = (schar_T *)lalloc((long_u)(
8801 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8802#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008803 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 if (enc_utf8)
8805 {
8806 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8807 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008808 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008809 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8811 }
8812 if (enc_dbcs == DBCS_JPNU)
8813 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8814 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8815#endif
8816 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8817 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8818 new_LineOffset = (unsigned *)lalloc((long_u)(
8819 Rows * sizeof(unsigned)), FALSE);
8820 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008821 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008823 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 {
8825 if (win_alloc_lines(wp) == FAIL)
8826 {
8827 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008828 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 }
8830 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008831#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008832 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8833 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008834 outofmem = TRUE;
8835#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008836give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008838#ifdef FEAT_MBYTE
8839 for (i = 0; i < p_mco; ++i)
8840 if (new_ScreenLinesC[i] == NULL)
8841 break;
8842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 if (new_ScreenLines == NULL
8844#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008845 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8847#endif
8848 || new_ScreenAttrs == NULL
8849 || new_LineOffset == NULL
8850 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008851 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 || outofmem)
8853 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008854 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008855 {
8856 /* guess the size */
8857 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8858
8859 /* Remember we did this to avoid getting outofmem messages over
8860 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008861 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 vim_free(new_ScreenLines);
8864 new_ScreenLines = NULL;
8865#ifdef FEAT_MBYTE
8866 vim_free(new_ScreenLinesUC);
8867 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008868 for (i = 0; i < p_mco; ++i)
8869 {
8870 vim_free(new_ScreenLinesC[i]);
8871 new_ScreenLinesC[i] = NULL;
8872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 vim_free(new_ScreenLines2);
8874 new_ScreenLines2 = NULL;
8875#endif
8876 vim_free(new_ScreenAttrs);
8877 new_ScreenAttrs = NULL;
8878 vim_free(new_LineOffset);
8879 new_LineOffset = NULL;
8880 vim_free(new_LineWraps);
8881 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008882 vim_free(new_TabPageIdxs);
8883 new_TabPageIdxs = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 }
8885 else
8886 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008887 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008888
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 for (new_row = 0; new_row < Rows; ++new_row)
8890 {
8891 new_LineOffset[new_row] = new_row * Columns;
8892 new_LineWraps[new_row] = FALSE;
8893
8894 /*
8895 * If the screen is not going to be cleared, copy as much as
8896 * possible from the old screen to the new one and clear the rest
8897 * (used when resizing the window at the "--more--" prompt or when
8898 * executing an external command, for the GUI).
8899 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008900 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
8902 (void)vim_memset(new_ScreenLines + new_row * Columns,
8903 ' ', (size_t)Columns * sizeof(schar_T));
8904#ifdef FEAT_MBYTE
8905 if (enc_utf8)
8906 {
8907 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8908 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008909 for (i = 0; i < p_mco; ++i)
8910 (void)vim_memset(new_ScreenLinesC[i]
8911 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 0, (size_t)Columns * sizeof(u8char_T));
8913 }
8914 if (enc_dbcs == DBCS_JPNU)
8915 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8916 0, (size_t)Columns * sizeof(schar_T));
8917#endif
8918 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8919 0, (size_t)Columns * sizeof(sattr_T));
8920 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008921 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 {
8923 if (screen_Columns < Columns)
8924 len = screen_Columns;
8925 else
8926 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008927#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008928 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008929 * may be invalid now. Also when p_mco changes. */
8930 if (!(enc_utf8 && ScreenLinesUC == NULL)
8931 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008932#endif
8933 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8934 ScreenLines + LineOffset[old_row],
8935 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008937 if (enc_utf8 && ScreenLinesUC != NULL
8938 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 {
8940 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8941 ScreenLinesUC + LineOffset[old_row],
8942 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008943 for (i = 0; i < p_mco; ++i)
8944 mch_memmove(new_ScreenLinesC[i]
8945 + new_LineOffset[new_row],
8946 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 (size_t)len * sizeof(u8char_T));
8948 }
8949 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8950 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8951 ScreenLines2 + LineOffset[old_row],
8952 (size_t)len * sizeof(schar_T));
8953#endif
8954 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8955 ScreenAttrs + LineOffset[old_row],
8956 (size_t)len * sizeof(sattr_T));
8957 }
8958 }
8959 }
8960 /* Use the last line of the screen for the current line. */
8961 current_ScreenLine = new_ScreenLines + Rows * Columns;
8962 }
8963
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008964 free_screenlines();
8965
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 ScreenLines = new_ScreenLines;
8967#ifdef FEAT_MBYTE
8968 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008969 for (i = 0; i < p_mco; ++i)
8970 ScreenLinesC[i] = new_ScreenLinesC[i];
8971 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 ScreenLines2 = new_ScreenLines2;
8973#endif
8974 ScreenAttrs = new_ScreenAttrs;
8975 LineOffset = new_LineOffset;
8976 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008977 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
8979 /* It's important that screen_Rows and screen_Columns reflect the actual
8980 * size of ScreenLines[]. Set them before calling anything. */
8981#ifdef FEAT_GUI
8982 old_Rows = screen_Rows;
8983#endif
8984 screen_Rows = Rows;
8985 screen_Columns = Columns;
8986
8987 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008988 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 screenclear2();
8990
8991#ifdef FEAT_GUI
8992 else if (gui.in_use
8993 && !gui.starting
8994 && ScreenLines != NULL
8995 && old_Rows != Rows)
8996 {
8997 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8998 /*
8999 * Adjust the position of the cursor, for when executing an external
9000 * command.
9001 */
9002 if (msg_row >= Rows) /* Rows got smaller */
9003 msg_row = Rows - 1; /* put cursor at last row */
9004 else if (Rows > old_Rows) /* Rows got bigger */
9005 msg_row += Rows - old_Rows; /* put cursor in same place */
9006 if (msg_col >= Columns) /* Columns got smaller */
9007 msg_col = Columns - 1; /* put cursor at last column */
9008 }
9009#endif
9010
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009012 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009013
9014#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009015 /*
9016 * Do not apply autocommands more than 3 times to avoid an endless loop
9017 * in case applying autocommands always changes Rows or Columns.
9018 */
9019 if (starting == 0 && ++retry_count <= 3)
9020 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009021 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009022 /* In rare cases, autocommands may have altered Rows or Columns,
9023 * jump back to check if we need to allocate the screen again. */
9024 goto retry;
9025 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027}
9028
9029 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009030free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009031{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009032#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009033 int i;
9034
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009035 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009036 for (i = 0; i < Screen_mco; ++i)
9037 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009038 vim_free(ScreenLines2);
9039#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009040 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009041 vim_free(ScreenAttrs);
9042 vim_free(LineOffset);
9043 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009044 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009045}
9046
9047 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009048screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049{
9050 check_for_delay(FALSE);
9051 screenalloc(FALSE); /* allocate screen buffers if size changed */
9052 screenclear2(); /* clear the screen */
9053}
9054
9055 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009056screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057{
9058 int i;
9059
9060 if (starting == NO_SCREEN || ScreenLines == NULL
9061#ifdef FEAT_GUI
9062 || (gui.in_use && gui.starting)
9063#endif
9064 )
9065 return;
9066
9067#ifdef FEAT_GUI
9068 if (!gui.in_use)
9069#endif
9070 screen_attr = -1; /* force setting the Normal colors */
9071 screen_stop_highlight(); /* don't want highlighting here */
9072
9073#ifdef FEAT_CLIPBOARD
9074 /* disable selection without redrawing it */
9075 clip_scroll_selection(9999);
9076#endif
9077
9078 /* blank out ScreenLines */
9079 for (i = 0; i < Rows; ++i)
9080 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009081 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 LineWraps[i] = FALSE;
9083 }
9084
9085 if (can_clear(T_CL))
9086 {
9087 out_str(T_CL); /* clear the display */
9088 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009089 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 }
9091 else
9092 {
9093 /* can't clear the screen, mark all chars with invalid attributes */
9094 for (i = 0; i < Rows; ++i)
9095 lineinvalid(LineOffset[i], (int)Columns);
9096 clear_cmdline = TRUE;
9097 }
9098
9099 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9100
9101 win_rest_invalid(firstwin);
9102 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009103 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 if (must_redraw == CLEAR) /* no need to clear again */
9105 must_redraw = NOT_VALID;
9106 compute_cmdrow();
9107 msg_row = cmdline_row; /* put cursor on last line for messages */
9108 msg_col = 0;
9109 screen_start(); /* don't know where cursor is now */
9110 msg_scrolled = 0; /* can't scroll back */
9111 msg_didany = FALSE;
9112 msg_didout = FALSE;
9113}
9114
9115/*
9116 * Clear one line in ScreenLines.
9117 */
9118 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009119lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
9121 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9122#ifdef FEAT_MBYTE
9123 if (enc_utf8)
9124 (void)vim_memset(ScreenLinesUC + off, 0,
9125 (size_t)width * sizeof(u8char_T));
9126#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009127 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128}
9129
9130/*
9131 * Mark one line in ScreenLines invalid by setting the attributes to an
9132 * invalid value.
9133 */
9134 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009135lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
9137 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9138}
9139
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140/*
9141 * Copy part of a Screenline for vertically split window "wp".
9142 */
9143 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009144linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 unsigned off_to = LineOffset[to] + wp->w_wincol;
9147 unsigned off_from = LineOffset[from] + wp->w_wincol;
9148
9149 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9150 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009151#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 if (enc_utf8)
9153 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009154 int i;
9155
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9157 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009158 for (i = 0; i < p_mco; ++i)
9159 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9160 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 }
9162 if (enc_dbcs == DBCS_JPNU)
9163 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9164 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9167 wp->w_width * sizeof(sattr_T));
9168}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169
9170/*
9171 * Return TRUE if clearing with term string "p" would work.
9172 * It can't work when the string is empty or it won't set the right background.
9173 */
9174 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009175can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176{
9177 return (*p != NUL && (t_colors <= 1
9178#ifdef FEAT_GUI
9179 || gui.in_use
9180#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009181#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009182 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009183 || (!p_tgc && cterm_normal_bg_color == 0)
9184#else
9185 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009186#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009187 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188}
9189
9190/*
9191 * Reset cursor position. Use whenever cursor was moved because of outputting
9192 * something directly to the screen (shell commands) or a terminal control
9193 * code.
9194 */
9195 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009196screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197{
9198 screen_cur_row = screen_cur_col = 9999;
9199}
9200
9201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 * Move the cursor to position "row","col" in the screen.
9203 * This tries to find the most efficient way to move, minimizing the number of
9204 * characters sent to the terminal.
9205 */
9206 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009207windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009209 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 int i;
9211 int plan;
9212 int cost;
9213 int wouldbe_col;
9214 int noinvcurs;
9215 char_u *bs;
9216 int goto_cost;
9217 int attr;
9218
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009219#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9221
9222#define PLAN_LE 1
9223#define PLAN_CR 2
9224#define PLAN_NL 3
9225#define PLAN_WRITE 4
9226 /* Can't use ScreenLines unless initialized */
9227 if (ScreenLines == NULL)
9228 return;
9229
9230 if (col != screen_cur_col || row != screen_cur_row)
9231 {
9232 /* Check for valid position. */
9233 if (row < 0) /* window without text lines? */
9234 row = 0;
9235 if (row >= screen_Rows)
9236 row = screen_Rows - 1;
9237 if (col >= screen_Columns)
9238 col = screen_Columns - 1;
9239
9240 /* check if no cursor movement is allowed in highlight mode */
9241 if (screen_attr && *T_MS == NUL)
9242 noinvcurs = HIGHL_COST;
9243 else
9244 noinvcurs = 0;
9245 goto_cost = GOTO_COST + noinvcurs;
9246
9247 /*
9248 * Plan how to do the positioning:
9249 * 1. Use CR to move it to column 0, same row.
9250 * 2. Use T_LE to move it a few columns to the left.
9251 * 3. Use NL to move a few lines down, column 0.
9252 * 4. Move a few columns to the right with T_ND or by writing chars.
9253 *
9254 * Don't do this if the cursor went beyond the last column, the cursor
9255 * position is unknown then (some terminals wrap, some don't )
9256 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009257 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 * characters to move the cursor to the right.
9259 */
9260 if (row >= screen_cur_row && screen_cur_col < Columns)
9261 {
9262 /*
9263 * If the cursor is in the same row, bigger col, we can use CR
9264 * or T_LE.
9265 */
9266 bs = NULL; /* init for GCC */
9267 attr = screen_attr;
9268 if (row == screen_cur_row && col < screen_cur_col)
9269 {
9270 /* "le" is preferred over "bc", because "bc" is obsolete */
9271 if (*T_LE)
9272 bs = T_LE; /* "cursor left" */
9273 else
9274 bs = T_BC; /* "backspace character (old) */
9275 if (*bs)
9276 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9277 else
9278 cost = 999;
9279 if (col + 1 < cost) /* using CR is less characters */
9280 {
9281 plan = PLAN_CR;
9282 wouldbe_col = 0;
9283 cost = 1; /* CR is just one character */
9284 }
9285 else
9286 {
9287 plan = PLAN_LE;
9288 wouldbe_col = col;
9289 }
9290 if (noinvcurs) /* will stop highlighting */
9291 {
9292 cost += noinvcurs;
9293 attr = 0;
9294 }
9295 }
9296
9297 /*
9298 * If the cursor is above where we want to be, we can use CR LF.
9299 */
9300 else if (row > screen_cur_row)
9301 {
9302 plan = PLAN_NL;
9303 wouldbe_col = 0;
9304 cost = (row - screen_cur_row) * 2; /* CR LF */
9305 if (noinvcurs) /* will stop highlighting */
9306 {
9307 cost += noinvcurs;
9308 attr = 0;
9309 }
9310 }
9311
9312 /*
9313 * If the cursor is in the same row, smaller col, just use write.
9314 */
9315 else
9316 {
9317 plan = PLAN_WRITE;
9318 wouldbe_col = screen_cur_col;
9319 cost = 0;
9320 }
9321
9322 /*
9323 * Check if any characters that need to be written have the
9324 * correct attributes. Also avoid UTF-8 characters.
9325 */
9326 i = col - wouldbe_col;
9327 if (i > 0)
9328 cost += i;
9329 if (cost < goto_cost && i > 0)
9330 {
9331 /*
9332 * Check if the attributes are correct without additionally
9333 * stopping highlighting.
9334 */
9335 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9336 while (i && *p++ == attr)
9337 --i;
9338 if (i != 0)
9339 {
9340 /*
9341 * Try if it works when highlighting is stopped here.
9342 */
9343 if (*--p == 0)
9344 {
9345 cost += noinvcurs;
9346 while (i && *p++ == 0)
9347 --i;
9348 }
9349 if (i != 0)
9350 cost = 999; /* different attributes, don't do it */
9351 }
9352#ifdef FEAT_MBYTE
9353 if (enc_utf8)
9354 {
9355 /* Don't use an UTF-8 char for positioning, it's slow. */
9356 for (i = wouldbe_col; i < col; ++i)
9357 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9358 {
9359 cost = 999;
9360 break;
9361 }
9362 }
9363#endif
9364 }
9365
9366 /*
9367 * We can do it without term_windgoto()!
9368 */
9369 if (cost < goto_cost)
9370 {
9371 if (plan == PLAN_LE)
9372 {
9373 if (noinvcurs)
9374 screen_stop_highlight();
9375 while (screen_cur_col > col)
9376 {
9377 out_str(bs);
9378 --screen_cur_col;
9379 }
9380 }
9381 else if (plan == PLAN_CR)
9382 {
9383 if (noinvcurs)
9384 screen_stop_highlight();
9385 out_char('\r');
9386 screen_cur_col = 0;
9387 }
9388 else if (plan == PLAN_NL)
9389 {
9390 if (noinvcurs)
9391 screen_stop_highlight();
9392 while (screen_cur_row < row)
9393 {
9394 out_char('\n');
9395 ++screen_cur_row;
9396 }
9397 screen_cur_col = 0;
9398 }
9399
9400 i = col - screen_cur_col;
9401 if (i > 0)
9402 {
9403 /*
9404 * Use cursor-right if it's one character only. Avoids
9405 * removing a line of pixels from the last bold char, when
9406 * using the bold trick in the GUI.
9407 */
9408 if (T_ND[0] != NUL && T_ND[1] == NUL)
9409 {
9410 while (i-- > 0)
9411 out_char(*T_ND);
9412 }
9413 else
9414 {
9415 int off;
9416
9417 off = LineOffset[row] + screen_cur_col;
9418 while (i-- > 0)
9419 {
9420 if (ScreenAttrs[off] != screen_attr)
9421 screen_stop_highlight();
9422#ifdef FEAT_MBYTE
9423 out_flush_check();
9424#endif
9425 out_char(ScreenLines[off]);
9426#ifdef FEAT_MBYTE
9427 if (enc_dbcs == DBCS_JPNU
9428 && ScreenLines[off] == 0x8e)
9429 out_char(ScreenLines2[off]);
9430#endif
9431 ++off;
9432 }
9433 }
9434 }
9435 }
9436 }
9437 else
9438 cost = 999;
9439
9440 if (cost >= goto_cost)
9441 {
9442 if (noinvcurs)
9443 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009444 if (row == screen_cur_row && (col > screen_cur_col)
9445 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 term_cursor_right(col - screen_cur_col);
9447 else
9448 term_windgoto(row, col);
9449 }
9450 screen_cur_row = row;
9451 screen_cur_col = col;
9452 }
9453}
9454
9455/*
9456 * Set cursor to its position in the current window.
9457 */
9458 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009459setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460{
9461 if (redrawing())
9462 {
9463 validate_cursor();
9464 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009465 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009467 /* With 'rightleft' set and the cursor on a double-wide
9468 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009469 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009471 (has_mbyte
9472 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9473 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474# endif
9475 1)) :
9476#endif
9477 curwin->w_wcol));
9478 }
9479}
9480
9481
9482/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009483 * Insert 'line_count' lines at 'row' in window 'wp'.
9484 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9485 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 * scrolling.
9487 * Returns FAIL if the lines are not inserted, OK for success.
9488 */
9489 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009490win_ins_lines(
9491 win_T *wp,
9492 int row,
9493 int line_count,
9494 int invalid,
9495 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
9497 int did_delete;
9498 int nextrow;
9499 int lastrow;
9500 int retval;
9501
9502 if (invalid)
9503 wp->w_lines_valid = 0;
9504
9505 if (wp->w_height < 5)
9506 return FAIL;
9507
9508 if (line_count > wp->w_height - row)
9509 line_count = wp->w_height - row;
9510
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009511 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 if (retval != MAYBE)
9513 return retval;
9514
9515 /*
9516 * If there is a next window or a status line, we first try to delete the
9517 * lines at the bottom to avoid messing what is after the window.
9518 * If this fails and there are following windows, don't do anything to avoid
9519 * messing up those windows, better just redraw.
9520 */
9521 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 if (wp->w_next != NULL || wp->w_status_height)
9523 {
9524 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009525 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 did_delete = TRUE;
9527 else if (wp->w_next)
9528 return FAIL;
9529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 /*
9531 * if no lines deleted, blank the lines that will end up below the window
9532 */
9533 if (!did_delete)
9534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009537 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 lastrow = nextrow + line_count;
9539 if (lastrow > Rows)
9540 lastrow = Rows;
9541 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009542 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 ' ', ' ', 0);
9544 }
9545
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009546 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 == FAIL)
9548 {
9549 /* deletion will have messed up other windows */
9550 if (did_delete)
9551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 win_rest_invalid(W_NEXT(wp));
9554 }
9555 return FAIL;
9556 }
9557
9558 return OK;
9559}
9560
9561/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009562 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9564 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9565 * scrolling
9566 * Return OK for success, FAIL if the lines are not deleted.
9567 */
9568 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009569win_del_lines(
9570 win_T *wp,
9571 int row,
9572 int line_count,
9573 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009574 int mayclear,
9575 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576{
9577 int retval;
9578
9579 if (invalid)
9580 wp->w_lines_valid = 0;
9581
9582 if (line_count > wp->w_height - row)
9583 line_count = wp->w_height - row;
9584
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009585 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 if (retval != MAYBE)
9587 return retval;
9588
9589 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009590 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 return FAIL;
9592
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 /*
9594 * If there are windows or status lines below, try to put them at the
9595 * correct place. If we can't do that, they have to be redrawn.
9596 */
9597 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9598 {
9599 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009600 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 {
9602 wp->w_redr_status = TRUE;
9603 win_rest_invalid(wp->w_next);
9604 }
9605 }
9606 /*
9607 * If this is the last window and there is no status line, redraw the
9608 * command line later.
9609 */
9610 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 redraw_cmdline = TRUE;
9612 return OK;
9613}
9614
9615/*
9616 * Common code for win_ins_lines() and win_del_lines().
9617 * Returns OK or FAIL when the work has been done.
9618 * Returns MAYBE when not finished yet.
9619 */
9620 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009621win_do_lines(
9622 win_T *wp,
9623 int row,
9624 int line_count,
9625 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009626 int del,
9627 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628{
9629 int retval;
9630
9631 if (!redrawing() || line_count <= 0)
9632 return FAIL;
9633
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009634 /* When inserting lines would result in loss of command output, just redraw
9635 * the lines. */
9636 if (no_win_do_lines_ins && !del)
9637 return FAIL;
9638
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009640 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009642 if (!no_win_do_lines_ins)
9643 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 return FAIL;
9645 }
9646
9647 /*
9648 * Delete all remaining lines
9649 */
9650 if (row + line_count >= wp->w_height)
9651 {
9652 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009653 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 ' ', ' ', 0);
9655 return OK;
9656 }
9657
9658 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009659 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009661 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009663 if (!no_win_do_lines_ins)
9664 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665
9666 /*
9667 * If the terminal can set a scroll region, use that.
9668 * Always do this in a vertically split window. This will redraw from
9669 * ScreenLines[] when t_CV isn't defined. That's faster than using
9670 * win_line().
9671 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009672 * a character in the lower right corner of the scroll region may cause a
9673 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009675 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 scroll_region_set(wp, row);
9679 if (del)
9680 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009681 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 else
9683 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009684 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 scroll_region_reset();
9687 return retval;
9688 }
9689
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9691 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
9693 return MAYBE;
9694}
9695
9696/*
9697 * window 'wp' and everything after it is messed up, mark it for redraw
9698 */
9699 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009700win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 {
9704 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 wp->w_redr_status = TRUE;
9706 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 }
9708 redraw_cmdline = TRUE;
9709}
9710
9711/*
9712 * The rest of the routines in this file perform screen manipulations. The
9713 * given operation is performed physically on the screen. The corresponding
9714 * change is also made to the internal screen image. In this way, the editor
9715 * anticipates the effect of editing changes on the appearance of the screen.
9716 * That way, when we call screenupdate a complete redraw isn't usually
9717 * necessary. Another advantage is that we can keep adding code to anticipate
9718 * screen changes, and in the meantime, everything still works.
9719 */
9720
9721/*
9722 * types for inserting or deleting lines
9723 */
9724#define USE_T_CAL 1
9725#define USE_T_CDL 2
9726#define USE_T_AL 3
9727#define USE_T_CE 4
9728#define USE_T_DL 5
9729#define USE_T_SR 6
9730#define USE_NL 7
9731#define USE_T_CD 8
9732#define USE_REDRAW 9
9733
9734/*
9735 * insert lines on the screen and update ScreenLines[]
9736 * 'end' is the line after the scrolled part. Normally it is Rows.
9737 * When scrolling region used 'off' is the offset from the top for the region.
9738 * 'row' and 'end' are relative to the start of the region.
9739 *
9740 * return FAIL for failure, OK for success.
9741 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009742 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009743screen_ins_lines(
9744 int off,
9745 int row,
9746 int line_count,
9747 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009748 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009749 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750{
9751 int i;
9752 int j;
9753 unsigned temp;
9754 int cursor_row;
9755 int type;
9756 int result_empty;
9757 int can_ce = can_clear(T_CE);
9758
9759 /*
9760 * FAIL if
9761 * - there is no valid screen
9762 * - the screen has to be redrawn completely
9763 * - the line count is less than one
9764 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009765 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009767 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9768#ifdef FEAT_CLIPBOARD
9769 || (clip_star.state != SELECT_CLEARED
9770 && redrawing_for_callback > 0)
9771#endif
9772 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 return FAIL;
9774
9775 /*
9776 * There are seven ways to insert lines:
9777 * 0. When in a vertically split window and t_CV isn't set, redraw the
9778 * characters from ScreenLines[].
9779 * 1. Use T_CD (clear to end of display) if it exists and the result of
9780 * the insert is just empty lines
9781 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9782 * present or line_count > 1. It looks better if we do all the inserts
9783 * at once.
9784 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9785 * insert is just empty lines and T_CE is not present or line_count >
9786 * 1.
9787 * 4. Use T_AL (insert line) if it exists.
9788 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9789 * just empty lines.
9790 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9791 * just empty lines.
9792 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9793 * the 'da' flag is not set or we have clear line capability.
9794 * 8. redraw the characters from ScreenLines[].
9795 *
9796 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9797 * the scrollbar for the window. It does have insert line, use that if it
9798 * exists.
9799 */
9800 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9802 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009803 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 type = USE_T_CD;
9805 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9806 type = USE_T_CAL;
9807 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9808 type = USE_T_CDL;
9809 else if (*T_AL != NUL)
9810 type = USE_T_AL;
9811 else if (can_ce && result_empty)
9812 type = USE_T_CE;
9813 else if (*T_DL != NUL && result_empty)
9814 type = USE_T_DL;
9815 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9816 type = USE_T_SR;
9817 else
9818 return FAIL;
9819
9820 /*
9821 * For clearing the lines screen_del_lines() is used. This will also take
9822 * care of t_db if necessary.
9823 */
9824 if (type == USE_T_CD || type == USE_T_CDL ||
9825 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009826 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827
9828 /*
9829 * If text is retained below the screen, first clear or delete as many
9830 * lines at the bottom of the window as are about to be inserted so that
9831 * the deleted lines won't later surface during a screen_del_lines.
9832 */
9833 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009834 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835
9836#ifdef FEAT_CLIPBOARD
9837 /* Remove a modeless selection when inserting lines halfway the screen
9838 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009839 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009840 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 else
9842 clip_scroll_selection(-line_count);
9843#endif
9844
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845#ifdef FEAT_GUI
9846 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9847 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009848 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849#endif
9850
9851 if (*T_CCS != NUL) /* cursor relative to region */
9852 cursor_row = row;
9853 else
9854 cursor_row = row + off;
9855
9856 /*
9857 * Shift LineOffset[] line_count down to reflect the inserted lines.
9858 * Clear the inserted lines in ScreenLines[].
9859 */
9860 row += off;
9861 end += off;
9862 for (i = 0; i < line_count; ++i)
9863 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 if (wp != NULL && wp->w_width != Columns)
9865 {
9866 /* need to copy part of a line */
9867 j = end - 1 - i;
9868 while ((j -= line_count) >= row)
9869 linecopy(j + line_count, j, wp);
9870 j += line_count;
9871 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009872 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9873 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 else
9875 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9876 LineWraps[j] = FALSE;
9877 }
9878 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 {
9880 j = end - 1 - i;
9881 temp = LineOffset[j];
9882 while ((j -= line_count) >= row)
9883 {
9884 LineOffset[j + line_count] = LineOffset[j];
9885 LineWraps[j + line_count] = LineWraps[j];
9886 }
9887 LineOffset[j + line_count] = temp;
9888 LineWraps[j + line_count] = FALSE;
9889 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009890 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 else
9892 lineinvalid(temp, (int)Columns);
9893 }
9894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895
9896 screen_stop_highlight();
9897 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009898 if (clear_attr != 0)
9899 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 /* redraw the characters */
9902 if (type == USE_REDRAW)
9903 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009904 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 {
9906 term_append_lines(line_count);
9907 screen_start(); /* don't know where cursor is now */
9908 }
9909 else
9910 {
9911 for (i = 0; i < line_count; i++)
9912 {
9913 if (type == USE_T_AL)
9914 {
9915 if (i && cursor_row != 0)
9916 windgoto(cursor_row, 0);
9917 out_str(T_AL);
9918 }
9919 else /* type == USE_T_SR */
9920 out_str(T_SR);
9921 screen_start(); /* don't know where cursor is now */
9922 }
9923 }
9924
9925 /*
9926 * With scroll-reverse and 'da' flag set we need to clear the lines that
9927 * have been scrolled down into the region.
9928 */
9929 if (type == USE_T_SR && *T_DA)
9930 {
9931 for (i = 0; i < line_count; ++i)
9932 {
9933 windgoto(off + i, 0);
9934 out_str(T_CE);
9935 screen_start(); /* don't know where cursor is now */
9936 }
9937 }
9938
9939#ifdef FEAT_GUI
9940 gui_can_update_cursor();
9941 if (gui.in_use)
9942 out_flush(); /* always flush after a scroll */
9943#endif
9944 return OK;
9945}
9946
9947/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009948 * Delete lines on the screen and update ScreenLines[].
9949 * "end" is the line after the scrolled part. Normally it is Rows.
9950 * When scrolling region used "off" is the offset from the top for the region.
9951 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 *
9953 * Return OK for success, FAIL if the lines are not deleted.
9954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009956screen_del_lines(
9957 int off,
9958 int row,
9959 int line_count,
9960 int end,
9961 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009962 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009963 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
9965 int j;
9966 int i;
9967 unsigned temp;
9968 int cursor_row;
9969 int cursor_end;
9970 int result_empty; /* result is empty until end of region */
9971 int can_delete; /* deleting line codes can be used */
9972 int type;
9973
9974 /*
9975 * FAIL if
9976 * - there is no valid screen
9977 * - the screen has to be redrawn completely
9978 * - the line count is less than one
9979 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009980 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009982 if (!screen_valid(TRUE) || line_count <= 0
9983 || (!force && line_count > p_ttyscroll)
9984#ifdef FEAT_CLIPBOARD
9985 || (clip_star.state != SELECT_CLEARED
9986 && redrawing_for_callback > 0)
9987#endif
9988 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 return FAIL;
9990
9991 /*
9992 * Check if the rest of the current region will become empty.
9993 */
9994 result_empty = row + line_count >= end;
9995
9996 /*
9997 * We can delete lines only when 'db' flag not set or when 'ce' option
9998 * available.
9999 */
10000 can_delete = (*T_DB == NUL || can_clear(T_CE));
10001
10002 /*
10003 * There are six ways to delete lines:
10004 * 0. When in a vertically split window and t_CV isn't set, redraw the
10005 * characters from ScreenLines[].
10006 * 1. Use T_CD if it exists and the result is empty.
10007 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10008 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10009 * none of the other ways work.
10010 * 4. Use T_CE (erase line) if the result is empty.
10011 * 5. Use T_DL (delete line) if it exists.
10012 * 6. redraw the characters from ScreenLines[].
10013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10015 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010016 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 type = USE_T_CD;
10018#if defined(__BEOS__) && defined(BEOS_DR8)
10019 /*
10020 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10021 * its internal termcap... this works okay for tests which test *T_DB !=
10022 * NUL. It has the disadvantage that the user cannot use any :set t_*
10023 * command to get T_DB (back) to empty_option, only :set term=... will do
10024 * the trick...
10025 * Anyway, this hack will hopefully go away with the next OS release.
10026 * (Olaf Seibert)
10027 */
10028 else if (row == 0 && T_DB == empty_option
10029 && (line_count == 1 || *T_CDL == NUL))
10030#else
10031 else if (row == 0 && (
10032#ifndef AMIGA
10033 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10034 * up, so use delete-line command */
10035 line_count == 1 ||
10036#endif
10037 *T_CDL == NUL))
10038#endif
10039 type = USE_NL;
10040 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10041 type = USE_T_CDL;
10042 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010043 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 type = USE_T_CE;
10045 else if (*T_DL != NUL && can_delete)
10046 type = USE_T_DL;
10047 else if (*T_CDL != NUL && can_delete)
10048 type = USE_T_CDL;
10049 else
10050 return FAIL;
10051
10052#ifdef FEAT_CLIPBOARD
10053 /* Remove a modeless selection when deleting lines halfway the screen or
10054 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010055 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010056 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 else
10058 clip_scroll_selection(line_count);
10059#endif
10060
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061#ifdef FEAT_GUI
10062 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10063 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010064 gui_dont_update_cursor(gui.cursor_row >= row + off
10065 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066#endif
10067
10068 if (*T_CCS != NUL) /* cursor relative to region */
10069 {
10070 cursor_row = row;
10071 cursor_end = end;
10072 }
10073 else
10074 {
10075 cursor_row = row + off;
10076 cursor_end = end + off;
10077 }
10078
10079 /*
10080 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10081 * Clear the inserted lines in ScreenLines[].
10082 */
10083 row += off;
10084 end += off;
10085 for (i = 0; i < line_count; ++i)
10086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 if (wp != NULL && wp->w_width != Columns)
10088 {
10089 /* need to copy part of a line */
10090 j = row + i;
10091 while ((j += line_count) <= end - 1)
10092 linecopy(j - line_count, j, wp);
10093 j -= line_count;
10094 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010095 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10096 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 else
10098 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10099 LineWraps[j] = FALSE;
10100 }
10101 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 {
10103 /* whole width, moving the line pointers is faster */
10104 j = row + i;
10105 temp = LineOffset[j];
10106 while ((j += line_count) <= end - 1)
10107 {
10108 LineOffset[j - line_count] = LineOffset[j];
10109 LineWraps[j - line_count] = LineWraps[j];
10110 }
10111 LineOffset[j - line_count] = temp;
10112 LineWraps[j - line_count] = FALSE;
10113 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010114 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 else
10116 lineinvalid(temp, (int)Columns);
10117 }
10118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010120 if (screen_attr != clear_attr)
10121 screen_stop_highlight();
10122 if (clear_attr != 0)
10123 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 /* redraw the characters */
10126 if (type == USE_REDRAW)
10127 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010128 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 {
10130 windgoto(cursor_row, 0);
10131 out_str(T_CD);
10132 screen_start(); /* don't know where cursor is now */
10133 }
10134 else if (type == USE_T_CDL)
10135 {
10136 windgoto(cursor_row, 0);
10137 term_delete_lines(line_count);
10138 screen_start(); /* don't know where cursor is now */
10139 }
10140 /*
10141 * Deleting lines at top of the screen or scroll region: Just scroll
10142 * the whole screen (scroll region) up by outputting newlines on the
10143 * last line.
10144 */
10145 else if (type == USE_NL)
10146 {
10147 windgoto(cursor_end - 1, 0);
10148 for (i = line_count; --i >= 0; )
10149 out_char('\n'); /* cursor will remain on same line */
10150 }
10151 else
10152 {
10153 for (i = line_count; --i >= 0; )
10154 {
10155 if (type == USE_T_DL)
10156 {
10157 windgoto(cursor_row, 0);
10158 out_str(T_DL); /* delete a line */
10159 }
10160 else /* type == USE_T_CE */
10161 {
10162 windgoto(cursor_row + i, 0);
10163 out_str(T_CE); /* erase a line */
10164 }
10165 screen_start(); /* don't know where cursor is now */
10166 }
10167 }
10168
10169 /*
10170 * If the 'db' flag is set, we need to clear the lines that have been
10171 * scrolled up at the bottom of the region.
10172 */
10173 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10174 {
10175 for (i = line_count; i > 0; --i)
10176 {
10177 windgoto(cursor_end - i, 0);
10178 out_str(T_CE); /* erase a line */
10179 screen_start(); /* don't know where cursor is now */
10180 }
10181 }
10182
10183#ifdef FEAT_GUI
10184 gui_can_update_cursor();
10185 if (gui.in_use)
10186 out_flush(); /* always flush after a scroll */
10187#endif
10188
10189 return OK;
10190}
10191
10192/*
10193 * show the current mode and ruler
10194 *
10195 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10196 * If clear_cmdline is FALSE there may be a message there that needs to be
10197 * cleared only if a mode is shown.
10198 * Return the length of the message (0 if no message).
10199 */
10200 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010201showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202{
10203 int need_clear;
10204 int length = 0;
10205 int do_mode;
10206 int attr;
10207 int nwr_save;
10208#ifdef FEAT_INS_EXPAND
10209 int sub_attr;
10210#endif
10211
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010212 do_mode = ((p_smd && msg_silent == 0)
10213 && ((State & INSERT)
10214 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010215 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 if (do_mode || Recording)
10217 {
10218 /*
10219 * Don't show mode right now, when not redrawing or inside a mapping.
10220 * Call char_avail() only when we are going to show something, because
10221 * it takes a bit of time.
10222 */
10223 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10224 {
10225 redraw_cmdline = TRUE; /* show mode later */
10226 return 0;
10227 }
10228
10229 nwr_save = need_wait_return;
10230
10231 /* wait a bit before overwriting an important message */
10232 check_for_delay(FALSE);
10233
10234 /* if the cmdline is more than one line high, erase top lines */
10235 need_clear = clear_cmdline;
10236 if (clear_cmdline && cmdline_row < Rows - 1)
10237 msg_clr_cmdline(); /* will reset clear_cmdline */
10238
10239 /* Position on the last line in the window, column 0 */
10240 msg_pos_mode();
10241 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010242 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 if (do_mode)
10244 {
10245 MSG_PUTS_ATTR("--", attr);
10246#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010247 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010248# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010249 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010250# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010251 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010252# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010253 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010254# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 MSG_PUTS_ATTR(" IM", attr);
10256# else
10257 MSG_PUTS_ATTR(" XIM", attr);
10258# endif
10259#endif
10260#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10261 if (gui.in_use)
10262 {
10263 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010264 {
10265 /* HANGUL */
10266 if (enc_utf8)
10267 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10268 else
10269 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 }
10272#endif
10273#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010274 /* CTRL-X in Insert mode */
10275 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 {
10277 /* These messages can get long, avoid a wrap in a narrow
10278 * window. Prefer showing edit_submode_extra. */
10279 length = (Rows - msg_row) * Columns - 3;
10280 if (edit_submode_extra != NULL)
10281 length -= vim_strsize(edit_submode_extra);
10282 if (length > 0)
10283 {
10284 if (edit_submode_pre != NULL)
10285 length -= vim_strsize(edit_submode_pre);
10286 if (length - vim_strsize(edit_submode) > 0)
10287 {
10288 if (edit_submode_pre != NULL)
10289 msg_puts_attr(edit_submode_pre, attr);
10290 msg_puts_attr(edit_submode, attr);
10291 }
10292 if (edit_submode_extra != NULL)
10293 {
10294 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10295 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010296 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 else
10298 sub_attr = attr;
10299 msg_puts_attr(edit_submode_extra, sub_attr);
10300 }
10301 }
10302 length = 0;
10303 }
10304 else
10305#endif
10306 {
10307#ifdef FEAT_VREPLACE
10308 if (State & VREPLACE_FLAG)
10309 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10310 else
10311#endif
10312 if (State & REPLACE_FLAG)
10313 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10314 else if (State & INSERT)
10315 {
10316#ifdef FEAT_RIGHTLEFT
10317 if (p_ri)
10318 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10319#endif
10320 MSG_PUTS_ATTR(_(" INSERT"), attr);
10321 }
10322 else if (restart_edit == 'I')
10323 MSG_PUTS_ATTR(_(" (insert)"), attr);
10324 else if (restart_edit == 'R')
10325 MSG_PUTS_ATTR(_(" (replace)"), attr);
10326 else if (restart_edit == 'V')
10327 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10328#ifdef FEAT_RIGHTLEFT
10329 if (p_hkmap)
10330 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10331# ifdef FEAT_FKMAP
10332 if (p_fkmap)
10333 MSG_PUTS_ATTR(farsi_text_5, attr);
10334# endif
10335#endif
10336#ifdef FEAT_KEYMAP
10337 if (State & LANGMAP)
10338 {
10339# ifdef FEAT_ARABIC
10340 if (curwin->w_p_arab)
10341 MSG_PUTS_ATTR(_(" Arabic"), attr);
10342 else
10343# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010344 if (get_keymap_str(curwin, (char_u *)" (%s)",
10345 NameBuff, MAXPATHL))
10346 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 }
10348#endif
10349 if ((State & INSERT) && p_paste)
10350 MSG_PUTS_ATTR(_(" (paste)"), attr);
10351
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 if (VIsual_active)
10353 {
10354 char *p;
10355
10356 /* Don't concatenate separate words to avoid translation
10357 * problems. */
10358 switch ((VIsual_select ? 4 : 0)
10359 + (VIsual_mode == Ctrl_V) * 2
10360 + (VIsual_mode == 'V'))
10361 {
10362 case 0: p = N_(" VISUAL"); break;
10363 case 1: p = N_(" VISUAL LINE"); break;
10364 case 2: p = N_(" VISUAL BLOCK"); break;
10365 case 4: p = N_(" SELECT"); break;
10366 case 5: p = N_(" SELECT LINE"); break;
10367 default: p = N_(" SELECT BLOCK"); break;
10368 }
10369 MSG_PUTS_ATTR(_(p), attr);
10370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 MSG_PUTS_ATTR(" --", attr);
10372 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010373
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 need_clear = TRUE;
10375 }
10376 if (Recording
10377#ifdef FEAT_INS_EXPAND
10378 && edit_submode == NULL /* otherwise it gets too long */
10379#endif
10380 )
10381 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010382 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 need_clear = TRUE;
10384 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010385
10386 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 if (need_clear || clear_cmdline)
10388 msg_clr_eos();
10389 msg_didout = FALSE; /* overwrite this message */
10390 length = msg_col;
10391 msg_col = 0;
10392 need_wait_return = nwr_save; /* never ask for hit-return for this */
10393 }
10394 else if (clear_cmdline && msg_silent == 0)
10395 /* Clear the whole command line. Will reset "clear_cmdline". */
10396 msg_clr_cmdline();
10397
10398#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 /* In Visual mode the size of the selected area must be redrawn. */
10400 if (VIsual_active)
10401 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402
10403 /* If the last window has no status line, the ruler is after the mode
10404 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010405 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 win_redr_ruler(lastwin, TRUE);
10407#endif
10408 redraw_cmdline = FALSE;
10409 clear_cmdline = FALSE;
10410
10411 return length;
10412}
10413
10414/*
10415 * Position for a mode message.
10416 */
10417 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010418msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419{
10420 msg_col = 0;
10421 msg_row = Rows - 1;
10422}
10423
10424/*
10425 * Delete mode message. Used when ESC is typed which is expected to end
10426 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010427 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 */
10429 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010430unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431{
10432 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010433 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 */
10435 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10436 redraw_cmdline = TRUE; /* delete mode later */
10437 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010438 clearmode();
10439}
10440
10441/*
10442 * Clear the mode message.
10443 */
10444 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010445clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010446{
10447 msg_pos_mode();
10448 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010449 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010450 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451}
10452
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010453 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010454recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010455{
10456 MSG_PUTS_ATTR(_("recording"), attr);
10457 if (!shortmess(SHM_RECORDING))
10458 {
10459 char_u s[4];
10460 sprintf((char *)s, " @%c", Recording);
10461 MSG_PUTS_ATTR(s, attr);
10462 }
10463}
10464
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010465/*
10466 * Draw the tab pages line at the top of the Vim window.
10467 */
10468 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010469draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010470{
10471 int tabcount = 0;
10472 tabpage_T *tp;
10473 int tabwidth;
10474 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010475 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010476 int attr;
10477 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010478 win_T *cwp;
10479 int wincount;
10480 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010481 int c;
10482 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010483 int attr_sel = HL_ATTR(HLF_TPS);
10484 int attr_nosel = HL_ATTR(HLF_TP);
10485 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010486 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010487 int room;
10488 int use_sep_chars = (t_colors < 8
10489#ifdef FEAT_GUI
10490 && !gui.in_use
10491#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010492#ifdef FEAT_TERMGUICOLORS
10493 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010494#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010495 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010496
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010497 if (ScreenLines == NULL)
10498 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010499 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010500
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010501#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010502 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010503 if (gui_use_tabline())
10504 {
10505 gui_update_tabline();
10506 return;
10507 }
10508#endif
10509
10510 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010511 return;
10512
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010513#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010514
10515 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10516 for (scol = 0; scol < Columns; ++scol)
10517 TabPageIdxs[scol] = 0;
10518
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010519 /* Use the 'tabline' option if it's set. */
10520 if (*p_tal != NUL)
10521 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010522 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010523
10524 /* Check for an error. If there is one we would loop in redrawing the
10525 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010526 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010527 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010528 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010529 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010530 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010531 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010532 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010533 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010534#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010535 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010536 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010537 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010538
Bram Moolenaar238a5642006-02-21 22:12:05 +000010539 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10540 if (tabwidth < 6)
10541 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010542
Bram Moolenaar238a5642006-02-21 22:12:05 +000010543 attr = attr_nosel;
10544 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010545 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010546 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10547 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010548 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010549 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010550
Bram Moolenaar238a5642006-02-21 22:12:05 +000010551 if (tp->tp_topframe == topframe)
10552 attr = attr_sel;
10553 if (use_sep_chars && col > 0)
10554 screen_putchar('|', 0, col++, attr);
10555
10556 if (tp->tp_topframe != topframe)
10557 attr = attr_nosel;
10558
10559 screen_putchar(' ', 0, col++, attr);
10560
10561 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010562 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010563 cwp = curwin;
10564 wp = firstwin;
10565 }
10566 else
10567 {
10568 cwp = tp->tp_curwin;
10569 wp = tp->tp_firstwin;
10570 }
10571
10572 modified = FALSE;
10573 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10574 if (bufIsChanged(wp->w_buffer))
10575 modified = TRUE;
10576 if (modified || wincount > 1)
10577 {
10578 if (wincount > 1)
10579 {
10580 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010581 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010582 if (col + len >= Columns - 3)
10583 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010584 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010585#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010586 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010587#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010588 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010589#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010590 );
10591 col += len;
10592 }
10593 if (modified)
10594 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10595 screen_putchar(' ', 0, col++, attr);
10596 }
10597
10598 room = scol - col + tabwidth - 1;
10599 if (room > 0)
10600 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010601 /* Get buffer name in NameBuff[] */
10602 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010603 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010604 len = vim_strsize(NameBuff);
10605 p = NameBuff;
10606#ifdef FEAT_MBYTE
10607 if (has_mbyte)
10608 while (len > room)
10609 {
10610 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010611 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010612 }
10613 else
10614#endif
10615 if (len > room)
10616 {
10617 p += len - room;
10618 len = room;
10619 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010620 if (len > Columns - col - 1)
10621 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010622
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010623 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010624 col += len;
10625 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010626 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627
10628 /* Store the tab page number in TabPageIdxs[], so that
10629 * jump_to_mouse() knows where each one is. */
10630 ++tabcount;
10631 while (scol < col)
10632 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010633 }
10634
Bram Moolenaar238a5642006-02-21 22:12:05 +000010635 if (use_sep_chars)
10636 c = '_';
10637 else
10638 c = ' ';
10639 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010640
10641 /* Put an "X" for closing the current tab if there are several. */
10642 if (first_tabpage->tp_next != NULL)
10643 {
10644 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10645 TabPageIdxs[Columns - 1] = -999;
10646 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010647 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010648
10649 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10650 * set. */
10651 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010652}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010653
10654/*
10655 * Get buffer name for "buf" into NameBuff[].
10656 * Takes care of special buffer names and translates special characters.
10657 */
10658 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010659get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010660{
10661 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010662 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010663 else
10664 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10665 trans_characters(NameBuff, MAXPATHL);
10666}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010667
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668/*
10669 * Get the character to use in a status line. Get its attributes in "*attr".
10670 */
10671 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010672fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673{
10674 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010675
10676#ifdef FEAT_TERMINAL
10677 if (bt_terminal(wp->w_buffer))
10678 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010679 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010680 {
10681 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010682 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010683 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010684 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010685 {
10686 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010687 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010688 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010689 }
10690 else
10691#endif
10692 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010694 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 fill = fill_stl;
10696 }
10697 else
10698 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010699 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 fill = fill_stlnc;
10701 }
10702 /* Use fill when there is highlighting, and highlighting of current
10703 * window differs, or the fillchars differ, or this is not the
10704 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010705 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010706 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 || (fill_stl != fill_stlnc)))
10708 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010709 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 return '^';
10711 return '=';
10712}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714/*
10715 * Get the character to use in a separator between vertically split windows.
10716 * Get its attributes in "*attr".
10717 */
10718 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010719fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010721 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 if (*attr == 0 && fill_vert == ' ')
10723 return '|';
10724 else
10725 return fill_vert;
10726}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727
10728/*
10729 * Return TRUE if redrawing should currently be done.
10730 */
10731 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010732redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010734#ifdef FEAT_EVAL
10735 if (disable_redraw_for_testing)
10736 return 0;
10737 else
10738#endif
10739 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10741}
10742
10743/*
10744 * Return TRUE if printing messages should currently be done.
10745 */
10746 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010747messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748{
10749 return (!(p_lz && char_avail() && !KeyTyped));
10750}
10751
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010752#ifdef FEAT_MENU
10753/*
10754 * Draw the window toolbar.
10755 */
10756 static void
10757redraw_win_toolbar(win_T *wp)
10758{
10759 vimmenu_T *menu;
10760 int item_idx = 0;
10761 int item_count = 0;
10762 int col = 0;
10763 int next_col;
10764 int off = (int)(current_ScreenLine - ScreenLines);
10765 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10766 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10767
10768 vim_free(wp->w_winbar_items);
10769 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10770 ++item_count;
10771 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10772 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10773
10774 /* TODO: use fewer spaces if there is not enough room */
10775 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010776 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010777 {
10778 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010779 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010780 break;
10781 if (col > 1)
10782 {
10783 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010784 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010785 break;
10786 }
10787
10788 wp->w_winbar_items[item_idx].wb_startcol = col;
10789 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010790 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010791 break;
10792
10793 next_col = text_to_screenline(wp, menu->name, col);
10794 while (col < next_col)
10795 {
10796 ScreenAttrs[off + col] = button_attr;
10797 ++col;
10798 }
10799 wp->w_winbar_items[item_idx].wb_endcol = col;
10800 wp->w_winbar_items[item_idx].wb_menu = menu;
10801 ++item_idx;
10802
Bram Moolenaar02631462017-09-22 15:20:32 +020010803 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010804 break;
10805 space_to_screenline(off + col, button_attr);
10806 ++col;
10807 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010808 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010809 {
10810 space_to_screenline(off + col, fill_attr);
10811 ++col;
10812 }
10813 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10814
Bram Moolenaar02631462017-09-22 15:20:32 +020010815 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10816 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010817}
10818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819/*
10820 * Show current status info in ruler and various other places
10821 * If always is FALSE, only show ruler if position has changed.
10822 */
10823 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010824showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825{
10826 if (!always && !redrawing())
10827 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010828#ifdef FEAT_INS_EXPAND
10829 if (pum_visible())
10830 {
10831 /* Don't redraw right now, do it later. */
10832 curwin->w_redr_status = TRUE;
10833 return;
10834 }
10835#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010836#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010837 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010838 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010839 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 else
10842#endif
10843#ifdef FEAT_CMDL_INFO
10844 win_redr_ruler(curwin, always);
10845#endif
10846
10847#ifdef FEAT_TITLE
10848 if (need_maketitle
10849# ifdef FEAT_STL_OPT
10850 || (p_icon && (stl_syntax & STL_IN_ICON))
10851 || (p_title && (stl_syntax & STL_IN_TITLE))
10852# endif
10853 )
10854 maketitle();
10855#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010856 /* Redraw the tab pages line if needed. */
10857 if (redraw_tabline)
10858 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859}
10860
10861#ifdef FEAT_CMDL_INFO
10862 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010863win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010865#define RULER_BUF_LEN 70
10866 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 int row;
10868 int fillchar;
10869 int attr;
10870 int empty_line = FALSE;
10871 colnr_T virtcol;
10872 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010873 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 int this_ru_col;
10876 int off = 0;
10877 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878
10879 /* If 'ruler' off or redrawing disabled, don't do anything */
10880 if (!p_ru)
10881 return;
10882
10883 /*
10884 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10885 * after deleting lines, before cursor.lnum is corrected.
10886 */
10887 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10888 return;
10889
10890#ifdef FEAT_INS_EXPAND
10891 /* Don't draw the ruler while doing insert-completion, it might overwrite
10892 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 if (edit_submode != NULL)
10895 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010896 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10897 if (pum_visible())
10898 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899#endif
10900
10901#ifdef FEAT_STL_OPT
10902 if (*p_ruf)
10903 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010904 int save_called_emsg = called_emsg;
10905
10906 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010908 if (called_emsg)
10909 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010910 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010911 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 return;
10913 }
10914#endif
10915
10916 /*
10917 * Check if not in Insert mode and the line is empty (will show "0-1").
10918 */
10919 if (!(State & INSERT)
10920 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10921 empty_line = TRUE;
10922
10923 /*
10924 * Only draw the ruler when something changed.
10925 */
10926 validate_virtcol_win(wp);
10927 if ( redraw_cmdline
10928 || always
10929 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10930 || wp->w_cursor.col != wp->w_ru_cursor.col
10931 || wp->w_virtcol != wp->w_ru_virtcol
10932#ifdef FEAT_VIRTUALEDIT
10933 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10934#endif
10935 || wp->w_topline != wp->w_ru_topline
10936 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10937#ifdef FEAT_DIFF
10938 || wp->w_topfill != wp->w_ru_topfill
10939#endif
10940 || empty_line != wp->w_ru_empty)
10941 {
10942 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 if (wp->w_status_height)
10944 {
10945 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010946 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010947 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010948 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 }
10950 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 {
10952 row = Rows - 1;
10953 fillchar = ' ';
10954 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 width = Columns;
10956 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 }
10958
10959 /* In list mode virtcol needs to be recomputed */
10960 virtcol = wp->w_virtcol;
10961 if (wp->w_p_list && lcs_tab1 == NUL)
10962 {
10963 wp->w_p_list = FALSE;
10964 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10965 wp->w_p_list = TRUE;
10966 }
10967
10968 /*
10969 * Some sprintfs return the length, some return a pointer.
10970 * To avoid portability problems we use strlen() here.
10971 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010972 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10974 ? 0L
10975 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010976 len = STRLEN(buffer);
10977 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10979 (int)virtcol + 1);
10980
10981 /*
10982 * Add a "50%" if there is room for it.
10983 * On the last line, don't print in the last column (scrolls the
10984 * screen up on some terminals).
10985 */
10986 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010987 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 this_ru_col = ru_col - (Columns - width);
10992 if (this_ru_col < 0)
10993 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 /* Never use more than half the window/screen width, leave the other
10995 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010996 if (this_ru_col < (width + 1) / 2)
10997 this_ru_col = (width + 1) / 2;
10998 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011000 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011001 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 {
11003#ifdef FEAT_MBYTE
11004 if (has_mbyte)
11005 i += (*mb_char2bytes)(fillchar, buffer + i);
11006 else
11007#endif
11008 buffer[i++] = fillchar;
11009 ++o;
11010 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011011 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 }
11013 /* Truncate at window boundary. */
11014#ifdef FEAT_MBYTE
11015 if (has_mbyte)
11016 {
11017 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011018 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 {
11020 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011021 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 {
11023 buffer[i] = NUL;
11024 break;
11025 }
11026 }
11027 }
11028 else
11029#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011030 if (this_ru_col + (int)STRLEN(buffer) > width)
11031 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032
Bram Moolenaar4033c552017-09-16 20:54:51 +020011033 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 i = redraw_cmdline;
11035 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011036 this_ru_col + off + (int)STRLEN(buffer),
11037 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 fillchar, fillchar, attr);
11039 /* don't redraw the cmdline because of showing the ruler */
11040 redraw_cmdline = i;
11041 wp->w_ru_cursor = wp->w_cursor;
11042 wp->w_ru_virtcol = wp->w_virtcol;
11043 wp->w_ru_empty = empty_line;
11044 wp->w_ru_topline = wp->w_topline;
11045 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11046#ifdef FEAT_DIFF
11047 wp->w_ru_topfill = wp->w_topfill;
11048#endif
11049 }
11050}
11051#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011052
11053#if defined(FEAT_LINEBREAK) || defined(PROTO)
11054/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011055 * Return the width of the 'number' and 'relativenumber' column.
11056 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011057 * Otherwise it depends on 'numberwidth' and the line count.
11058 */
11059 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011060number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011061{
11062 int n;
11063 linenr_T lnum;
11064
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011065 if (wp->w_p_rnu && !wp->w_p_nu)
11066 /* cursor line shows "0" */
11067 lnum = wp->w_height;
11068 else
11069 /* cursor line shows absolute line number */
11070 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011071
Bram Moolenaar6b314672015-03-20 15:42:10 +010011072 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011073 return wp->w_nrwidth_width;
11074 wp->w_nrwidth_line_count = lnum;
11075
11076 n = 0;
11077 do
11078 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011079 lnum /= 10;
11080 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011081 } while (lnum > 0);
11082
11083 /* 'numberwidth' gives the minimal width plus one */
11084 if (n < wp->w_p_nuw - 1)
11085 n = wp->w_p_nuw - 1;
11086
11087 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011088 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011089 return n;
11090}
11091#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011092
11093/*
11094 * Return the current cursor column. This is the actual position on the
11095 * screen. First column is 0.
11096 */
11097 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011098screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011099{
11100 return screen_cur_col;
11101}
11102
11103/*
11104 * Return the current cursor row. This is the actual position on the screen.
11105 * First row is 0.
11106 */
11107 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011108screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011109{
11110 return screen_cur_row;
11111}