blob: 9256fbd1e6f85bc68e50b853055354dbf755ff0a [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
477#else
478 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100479#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200480
481 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100482}
483
484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 * Changed something in the current window, at buffer line "lnum", that
486 * requires that line and possibly other lines to be redrawn.
487 * Used when entering/leaving Insert mode with the cursor on a folded line.
488 * Used to remove the "$" from a change command.
489 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
490 * may become invalid and the whole window will have to be redrawn.
491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100493redrawWinline(
494 linenr_T lnum,
495 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496{
497#ifdef FEAT_FOLDING
498 int i;
499#endif
500
501 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
502 curwin->w_redraw_top = lnum;
503 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
504 curwin->w_redraw_bot = lnum;
505 redraw_later(VALID);
506
507#ifdef FEAT_FOLDING
508 if (invalid)
509 {
510 /* A w_lines[] entry for this lnum has become invalid. */
511 i = find_wl_entry(curwin, lnum);
512 if (i >= 0)
513 curwin->w_lines[i].wl_valid = FALSE;
514 }
515#endif
516}
517
518/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200519 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 */
521 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100522update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523{
524 redraw_curbuf_later(type);
525 update_screen(type);
526}
527
528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 * Based on the current value of curwin->w_topline, transfer a screenfull
530 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200531 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200533 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200534update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200536 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 win_T *wp;
538 static int did_intro = FALSE;
539#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
540 int did_one;
541#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200542#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200543 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200544 int gui_cursor_col;
545 int gui_cursor_row;
546#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200547 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000549 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200551 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200553 if (type == VALID_NO_UPDATE)
554 {
555 no_update = TRUE;
556 type = 0;
557 }
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 if (must_redraw)
560 {
561 if (type < must_redraw) /* use maximal type */
562 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000563
564 /* must_redraw is reset here, so that when we run into some weird
565 * reason to redraw while busy redrawing (e.g., asynchronous
566 * scrolling), or update_topline() in win_update() will cause a
567 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 must_redraw = 0;
569 }
570
571 /* Need to update w_lines[]. */
572 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
573 type = NOT_VALID;
574
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000575 /* Postpone the redrawing when it's not needed and when being called
576 * recursively. */
577 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {
579 redraw_later(type); /* remember type for next time */
580 must_redraw = type;
581 if (type > INVERTED_ALL)
582 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200583 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 }
585
586 updating_screen = TRUE;
587#ifdef FEAT_SYN_HL
588 ++display_tick; /* let syntax code know we're in a next round of
589 * display updating */
590#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200591 if (no_update)
592 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594 /*
595 * if the screen was scrolled up when displaying a message, scroll it down
596 */
597 if (msg_scrolled)
598 {
599 clear_cmdline = TRUE;
600 if (msg_scrolled > Rows - 5) /* clearing is faster */
601 type = CLEAR;
602 else if (type != CLEAR)
603 {
604 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200605 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
606 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 type = CLEAR;
608 FOR_ALL_WINDOWS(wp)
609 {
610 if (W_WINROW(wp) < msg_scrolled)
611 {
612 if (W_WINROW(wp) + wp->w_height > msg_scrolled
613 && wp->w_redr_type < REDRAW_TOP
614 && wp->w_lines_valid > 0
615 && wp->w_topline == wp->w_lines[0].wl_lnum)
616 {
617 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
618 wp->w_redr_type = REDRAW_TOP;
619 }
620 else
621 {
622 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200623 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
624 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 }
627 }
628 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200629 if (!no_update)
630 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000631 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
633 msg_scrolled = 0;
634 need_wait_return = FALSE;
635 }
636
637 /* reset cmdline_row now (may have been changed temporarily) */
638 compute_cmdrow();
639
640 /* Check for changed highlighting */
641 if (need_highlight_changed)
642 highlight_changed();
643
644 if (type == CLEAR) /* first clear screen */
645 {
646 screenclear(); /* will reset clear_cmdline */
647 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200648 /* must_redraw may be set indirectly, avoid another redraw later */
649 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 }
651
652 if (clear_cmdline) /* going to clear cmdline (done below) */
653 check_for_delay(FALSE);
654
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000655#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200656 /* Force redraw when width of 'number' or 'relativenumber' column
657 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000658 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200659 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
660 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000661 curwin->w_redr_type = NOT_VALID;
662#endif
663
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 /*
665 * Only start redrawing if there is really something to do.
666 */
667 if (type == INVERTED)
668 update_curswant();
669 if (curwin->w_redr_type < type
670 && !((type == VALID
671 && curwin->w_lines[0].wl_valid
672#ifdef FEAT_DIFF
673 && curwin->w_topfill == curwin->w_old_topfill
674 && curwin->w_botfill == curwin->w_old_botfill
675#endif
676 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000678 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
680 && curwin->w_old_visual_mode == VIsual_mode
681 && (curwin->w_valid & VALID_VIRTCOL)
682 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 ))
684 curwin->w_redr_type = type;
685
Bram Moolenaar5a305422006-04-28 22:38:25 +0000686 /* Redraw the tab pages line if needed. */
687 if (redraw_tabline || type >= NOT_VALID)
688 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000689
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690#ifdef FEAT_SYN_HL
691 /*
692 * Correct stored syntax highlighting info for changes in each displayed
693 * buffer. Each buffer must only be done once.
694 */
695 FOR_ALL_WINDOWS(wp)
696 {
697 if (wp->w_buffer->b_mod_set)
698 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 win_T *wwp;
700
701 /* Check if we already did this buffer. */
702 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
703 if (wwp->w_buffer == wp->w_buffer)
704 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200705 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 syn_stack_apply_changes(wp->w_buffer);
707 }
708 }
709#endif
710
711 /*
712 * Go from top to bottom through the windows, redrawing the ones that need
713 * it.
714 */
715#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
716 did_one = FALSE;
717#endif
718#ifdef FEAT_SEARCH_EXTRA
719 search_hl.rm.regprog = NULL;
720#endif
721 FOR_ALL_WINDOWS(wp)
722 {
723 if (wp->w_redr_type != 0)
724 {
725 cursor_off();
726#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
727 if (!did_one)
728 {
729 did_one = TRUE;
730# ifdef FEAT_SEARCH_EXTRA
731 start_search_hl();
732# endif
733# ifdef FEAT_CLIPBOARD
734 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200735 if (clip_star.available && clip_isautosel_star())
736 clip_update_selection(&clip_star);
737 if (clip_plus.available && clip_isautosel_plus())
738 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739# endif
740#ifdef FEAT_GUI
741 /* Remove the cursor before starting to do anything, because
742 * scrolling may make it difficult to redraw the text under
743 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200744 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200745 {
746 gui_cursor_col = gui.cursor_col;
747 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200749 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751#endif
752 }
753#endif
754 win_update(wp);
755 }
756
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 /* redraw status line after the window to minimize cursor movement */
758 if (wp->w_redr_status)
759 {
760 cursor_off();
761 win_redr_status(wp);
762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 }
764#if defined(FEAT_SEARCH_EXTRA)
765 end_search_hl();
766#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100767#ifdef FEAT_INS_EXPAND
768 /* May need to redraw the popup menu. */
769 if (pum_visible())
770 pum_redraw();
771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 /* Reset b_mod_set flags. Going through all windows is probably faster
774 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200775 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
778 updating_screen = FALSE;
779#ifdef FEAT_GUI
780 gui_may_resize_shell();
781#endif
782
783 /* Clear or redraw the command line. Done last, because scrolling may
784 * mess up the command line. */
785 if (clear_cmdline || redraw_cmdline)
786 showmode();
787
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200788 if (no_update)
789 --no_win_do_lines_ins;
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200792 if (!did_intro)
793 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 did_intro = TRUE;
795
796#ifdef FEAT_GUI
797 /* Redraw the cursor and update the scrollbars when all screen updating is
798 * done. */
799 if (gui.in_use)
800 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200801 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200802 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100803 mch_disable_flush();
804 out_flush(); /* required before updating the cursor */
805 mch_enable_flush();
806
Bram Moolenaar144445d2016-07-08 21:41:54 +0200807 /* Put the GUI position where the cursor was, gui_update_cursor()
808 * uses that. */
809 gui.col = gui_cursor_col;
810 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200811# ifdef FEAT_MBYTE
812 gui.col = mb_fix_col(gui.col, gui.row);
813# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100815 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200816 screen_cur_col = gui.col;
817 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200818 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100819 else
820 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 gui_update_scrollbars(FALSE);
822 }
823#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200824 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825}
826
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100827#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
828/*
829 * Prepare for updating one or more windows.
830 * Caller must check for "updating_screen" already set to avoid recursiveness.
831 */
832 static void
833update_prepare(void)
834{
835 cursor_off();
836 updating_screen = TRUE;
837#ifdef FEAT_GUI
838 /* Remove the cursor before starting to do anything, because scrolling may
839 * make it difficult to redraw the text under it. */
840 if (gui.in_use)
841 gui_undraw_cursor();
842#endif
843#ifdef FEAT_SEARCH_EXTRA
844 start_search_hl();
845#endif
846}
847
848/*
849 * Finish updating one or more windows.
850 */
851 static void
852update_finish(void)
853{
854 if (redraw_cmdline)
855 showmode();
856
857# ifdef FEAT_SEARCH_EXTRA
858 end_search_hl();
859# endif
860
861 updating_screen = FALSE;
862
863# ifdef FEAT_GUI
864 gui_may_resize_shell();
865
866 /* Redraw the cursor and update the scrollbars when all screen updating is
867 * done. */
868 if (gui.in_use)
869 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100870 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100871 gui_update_scrollbars(FALSE);
872 }
873# endif
874}
875#endif
876
Bram Moolenaar860cae12010-06-05 23:22:07 +0200877#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200878/*
879 * Return TRUE if the cursor line in window "wp" may be concealed, according
880 * to the 'concealcursor' option.
881 */
882 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100883conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200884{
885 int c;
886
887 if (*wp->w_p_cocu == NUL)
888 return FALSE;
889 if (get_real_state() & VISUAL)
890 c = 'v';
891 else if (State & INSERT)
892 c = 'i';
893 else if (State & NORMAL)
894 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200895 else if (State & CMDLINE)
896 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200897 else
898 return FALSE;
899 return vim_strchr(wp->w_p_cocu, c) != NULL;
900}
901
902/*
903 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
904 */
905 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100906conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200907{
908 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
909 {
910 need_cursor_line_redraw = TRUE;
911 /* Need to recompute cursor column, e.g., when starting Visual mode
912 * without concealing. */
913 curs_columns(TRUE);
914 }
915}
916
Bram Moolenaar860cae12010-06-05 23:22:07 +0200917 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100918update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200919{
920 int row;
921 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200922#ifdef SYN_TIME_LIMIT
923 proftime_T syntax_tm;
924#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200925
Bram Moolenaar908be432016-05-24 10:51:30 +0200926 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100927 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200928 return;
929
Bram Moolenaar860cae12010-06-05 23:22:07 +0200930 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200931 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200932 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200933#ifdef SYN_TIME_LIMIT
934 /* Set the time limit to 'redrawtime'. */
935 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200936 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200937#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100938 update_prepare();
939
Bram Moolenaar860cae12010-06-05 23:22:07 +0200940 row = 0;
941 for (j = 0; j < wp->w_lines_valid; ++j)
942 {
943 if (lnum == wp->w_lines[j].wl_lnum)
944 {
945 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200946# ifdef FEAT_SEARCH_EXTRA
947 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948 start_search_hl();
949 prepare_search_hl(wp, lnum);
950# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200951 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200952# if defined(FEAT_SEARCH_EXTRA)
953 end_search_hl();
954# endif
955 break;
956 }
957 row += wp->w_lines[j].wl_size;
958 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100959
960 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200961
962#ifdef SYN_TIME_LIMIT
963 syn_set_timeout(NULL);
964#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200965 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200966 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968#endif
969
970#if defined(FEAT_SIGNS) || defined(PROTO)
971 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100972update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973{
974 win_T *wp;
975 int doit = FALSE;
976
977# ifdef FEAT_FOLDING
978 win_foldinfo.fi_level = 0;
979# endif
980
981 /* update/delete a specific mark */
982 FOR_ALL_WINDOWS(wp)
983 {
984 if (buf != NULL && lnum > 0)
985 {
986 if (wp->w_buffer == buf && lnum >= wp->w_topline
987 && lnum < wp->w_botline)
988 {
989 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
990 wp->w_redraw_top = lnum;
991 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
992 wp->w_redraw_bot = lnum;
993 redraw_win_later(wp, VALID);
994 }
995 }
996 else
997 redraw_win_later(wp, VALID);
998 if (wp->w_redr_type != 0)
999 doit = TRUE;
1000 }
1001
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001002 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001003 * happening (recursive call), messages on the screen or still starting up.
1004 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001005 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001006 || State == ASKMORE || State == HITRETURN
1007 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001008#ifdef FEAT_GUI
1009 || gui.starting
1010#endif
1011 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 return;
1013
1014 /* update all windows that need updating */
1015 update_prepare();
1016
Bram Moolenaar29323592016-07-24 22:04:11 +02001017 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 {
1019 if (wp->w_redr_type != 0)
1020 win_update(wp);
1021 if (wp->w_redr_status)
1022 win_redr_status(wp);
1023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024
1025 update_finish();
1026}
1027#endif
1028
1029
1030#if defined(FEAT_GUI) || defined(PROTO)
1031/*
1032 * Update a single window, its status line and maybe the command line msg.
1033 * Used for the GUI scrollbar.
1034 */
1035 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001036updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001038 /* return if already busy updating */
1039 if (updating_screen)
1040 return;
1041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 update_prepare();
1043
1044#ifdef FEAT_CLIPBOARD
1045 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001046 if (clip_star.available && clip_isautosel_star())
1047 clip_update_selection(&clip_star);
1048 if (clip_plus.available && clip_isautosel_plus())
1049 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001053
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001054 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001055 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001056 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 if (wp->w_redr_status
1059# ifdef FEAT_CMDL_INFO
1060 || p_ru
1061# endif
1062# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001063 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064# endif
1065 )
1066 win_redr_status(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
1068 update_finish();
1069}
1070#endif
1071
1072/*
1073 * Update a single window.
1074 *
1075 * This may cause the windows below it also to be redrawn (when clearing the
1076 * screen or scrolling lines).
1077 *
1078 * How the window is redrawn depends on wp->w_redr_type. Each type also
1079 * implies the one below it.
1080 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001081 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1083 * INVERTED redraw the changed part of the Visual area
1084 * INVERTED_ALL redraw the whole Visual area
1085 * VALID 1. scroll up/down to adjust for a changed w_topline
1086 * 2. update lines at the top when scrolled down
1087 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001088 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 * b_mod_top and b_mod_bot.
1090 * - if wp->w_redraw_top non-zero, redraw lines between
1091 * wp->w_redraw_top and wp->w_redr_bot.
1092 * - continue redrawing when syntax status is invalid.
1093 * 4. if scrolled up, update lines at the bottom.
1094 * This results in three areas that may need updating:
1095 * top: from first row to top_end (when scrolled down)
1096 * mid: from mid_start to mid_end (update inversion or changed text)
1097 * bot: from bot_start to last row (when scrolled up)
1098 */
1099 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001100win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
1102 buf_T *buf = wp->w_buffer;
1103 int type;
1104 int top_end = 0; /* Below last row of the top area that needs
1105 updating. 0 when no top area updating. */
1106 int mid_start = 999;/* first row of the mid area that needs
1107 updating. 999 when no mid area updating. */
1108 int mid_end = 0; /* Below last row of the mid area that needs
1109 updating. 0 when no mid area updating. */
1110 int bot_start = 999;/* first row of the bot area that needs
1111 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 int scrolled_down = FALSE; /* TRUE when scrolled down when
1113 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001115 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 int top_to_mod = FALSE; /* redraw above mod_top */
1117#endif
1118
1119 int row; /* current window row to display */
1120 linenr_T lnum; /* current buffer lnum to display */
1121 int idx; /* current index in w_lines[] */
1122 int srow; /* starting row of the current line */
1123
1124 int eof = FALSE; /* if TRUE, we hit the end of the file */
1125 int didline = FALSE; /* if TRUE, we finished the last line */
1126 int i;
1127 long j;
1128 static int recursive = FALSE; /* being called recursively */
1129 int old_botline = wp->w_botline;
1130#ifdef FEAT_FOLDING
1131 long fold_count;
1132#endif
1133#ifdef FEAT_SYN_HL
1134 /* remember what happened to the previous line, to know if
1135 * check_visual_highlight() can be used */
1136#define DID_NONE 1 /* didn't update a line */
1137#define DID_LINE 2 /* updated a normal line */
1138#define DID_FOLD 3 /* updated a folded line */
1139 int did_update = DID_NONE;
1140 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1141#endif
1142 linenr_T mod_top = 0;
1143 linenr_T mod_bot = 0;
1144#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1145 int save_got_int;
1146#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001147#ifdef SYN_TIME_LIMIT
1148 proftime_T syntax_tm;
1149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
1151 type = wp->w_redr_type;
1152
1153 if (type == NOT_VALID)
1154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 wp->w_lines_valid = 0;
1157 }
1158
1159 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001160 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 {
1162 wp->w_redr_type = 0;
1163 return;
1164 }
1165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 /* Window is zero-width: Only need to draw the separator. */
1167 if (wp->w_width == 0)
1168 {
1169 /* draw the vertical separator right of this window */
1170 draw_vsep_win(wp, 0);
1171 wp->w_redr_type = 0;
1172 return;
1173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001175#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001176 /* If this window contains a terminal, redraw works completely differently.
1177 */
1178 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001179 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001180 wp->w_redr_type = 0;
1181 return;
1182 }
1183#endif
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001186 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187#endif
1188
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001189#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001190 /* Force redraw when width of 'number' or 'relativenumber' column
1191 * changes. */
1192 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001193 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001194 {
1195 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001196 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001197 }
1198 else
1199#endif
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1202 {
1203 /*
1204 * When there are both inserted/deleted lines and specific lines to be
1205 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1206 * everything (only happens when redrawing is off for while).
1207 */
1208 type = NOT_VALID;
1209 }
1210 else
1211 {
1212 /*
1213 * Set mod_top to the first line that needs displaying because of
1214 * changes. Set mod_bot to the first line after the changes.
1215 */
1216 mod_top = wp->w_redraw_top;
1217 if (wp->w_redraw_bot != 0)
1218 mod_bot = wp->w_redraw_bot + 1;
1219 else
1220 mod_bot = 0;
1221 wp->w_redraw_top = 0; /* reset for next time */
1222 wp->w_redraw_bot = 0;
1223 if (buf->b_mod_set)
1224 {
1225 if (mod_top == 0 || mod_top > buf->b_mod_top)
1226 {
1227 mod_top = buf->b_mod_top;
1228#ifdef FEAT_SYN_HL
1229 /* Need to redraw lines above the change that may be included
1230 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001231 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001233 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (mod_top < 1)
1235 mod_top = 1;
1236 }
1237#endif
1238 }
1239 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1240 mod_bot = buf->b_mod_bot;
1241
1242#ifdef FEAT_SEARCH_EXTRA
1243 /* When 'hlsearch' is on and using a multi-line search pattern, a
1244 * change in one line may make the Search highlighting in a
1245 * previous line invalid. Simple solution: redraw all visible
1246 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001247 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001249 if (search_hl.rm.regprog != NULL
1250 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001252 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001253 {
1254 cur = wp->w_match_head;
1255 while (cur != NULL)
1256 {
1257 if (cur->match.regprog != NULL
1258 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001259 {
1260 top_to_mod = TRUE;
1261 break;
1262 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001263 cur = cur->next;
1264 }
1265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266#endif
1267 }
1268#ifdef FEAT_FOLDING
1269 if (mod_top != 0 && hasAnyFolding(wp))
1270 {
1271 linenr_T lnumt, lnumb;
1272
1273 /*
1274 * A change in a line can cause lines above it to become folded or
1275 * unfolded. Find the top most buffer line that may be affected.
1276 * If the line was previously folded and displayed, get the first
1277 * line of that fold. If the line is folded now, get the first
1278 * folded line. Use the minimum of these two.
1279 */
1280
1281 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1282 * the line below it. If there is no valid entry, use w_topline.
1283 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1284 * to this line. If there is no valid entry, use MAXLNUM. */
1285 lnumt = wp->w_topline;
1286 lnumb = MAXLNUM;
1287 for (i = 0; i < wp->w_lines_valid; ++i)
1288 if (wp->w_lines[i].wl_valid)
1289 {
1290 if (wp->w_lines[i].wl_lastlnum < mod_top)
1291 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1292 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1293 {
1294 lnumb = wp->w_lines[i].wl_lnum;
1295 /* When there is a fold column it might need updating
1296 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001297 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 ++lnumb;
1299 }
1300 }
1301
1302 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1303 if (mod_top > lnumt)
1304 mod_top = lnumt;
1305
1306 /* Now do the same for the bottom line (one above mod_bot). */
1307 --mod_bot;
1308 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1309 ++mod_bot;
1310 if (mod_bot < lnumb)
1311 mod_bot = lnumb;
1312 }
1313#endif
1314
1315 /* When a change starts above w_topline and the end is below
1316 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001317 * If the end of the change is above w_topline: do like no change was
1318 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 if (mod_top != 0 && mod_top < wp->w_topline)
1320 {
1321 if (mod_bot > wp->w_topline)
1322 mod_top = wp->w_topline;
1323#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001324 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 top_end = 1;
1326#endif
1327 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001328
1329 /* When line numbers are displayed need to redraw all lines below
1330 * inserted/deleted lines. */
1331 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1332 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 }
1334
1335 /*
1336 * When only displaying the lines at the top, set top_end. Used when
1337 * window has scrolled down for msg_scrolled.
1338 */
1339 if (type == REDRAW_TOP)
1340 {
1341 j = 0;
1342 for (i = 0; i < wp->w_lines_valid; ++i)
1343 {
1344 j += wp->w_lines[i].wl_size;
1345 if (j >= wp->w_upd_rows)
1346 {
1347 top_end = j;
1348 break;
1349 }
1350 }
1351 if (top_end == 0)
1352 /* not found (cannot happen?): redraw everything */
1353 type = NOT_VALID;
1354 else
1355 /* top area defined, the rest is VALID */
1356 type = VALID;
1357 }
1358
Bram Moolenaar367329b2007-08-30 11:53:22 +00001359 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001360 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1361 * non-zero and thus not FALSE) will indicate that screenclear() was not
1362 * called. */
1363 if (screen_cleared)
1364 screen_cleared = MAYBE;
1365
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 /*
1367 * If there are no changes on the screen that require a complete redraw,
1368 * handle three cases:
1369 * 1: we are off the top of the screen by a few lines: scroll down
1370 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1371 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1372 * w_lines[] that needs updating.
1373 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001374 if ((type == VALID || type == SOME_VALID
1375 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#ifdef FEAT_DIFF
1377 && !wp->w_botfill && !wp->w_old_botfill
1378#endif
1379 )
1380 {
1381 if (mod_top != 0 && wp->w_topline == mod_top)
1382 {
1383 /*
1384 * w_topline is the first changed line, the scrolling will be done
1385 * further down.
1386 */
1387 }
1388 else if (wp->w_lines[0].wl_valid
1389 && (wp->w_topline < wp->w_lines[0].wl_lnum
1390#ifdef FEAT_DIFF
1391 || (wp->w_topline == wp->w_lines[0].wl_lnum
1392 && wp->w_topfill > wp->w_old_topfill)
1393#endif
1394 ))
1395 {
1396 /*
1397 * New topline is above old topline: May scroll down.
1398 */
1399#ifdef FEAT_FOLDING
1400 if (hasAnyFolding(wp))
1401 {
1402 linenr_T ln;
1403
1404 /* count the number of lines we are off, counting a sequence
1405 * of folded lines as one */
1406 j = 0;
1407 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1408 {
1409 ++j;
1410 if (j >= wp->w_height - 2)
1411 break;
1412 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1413 }
1414 }
1415 else
1416#endif
1417 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1418 if (j < wp->w_height - 2) /* not too far off */
1419 {
1420 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1421#ifdef FEAT_DIFF
1422 /* insert extra lines for previously invisible filler lines */
1423 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1424 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1425 - wp->w_old_topfill;
1426#endif
1427 if (i < wp->w_height - 2) /* less than a screen off */
1428 {
1429 /*
1430 * Try to insert the correct number of lines.
1431 * If not the last window, delete the lines at the bottom.
1432 * win_ins_lines may fail when the terminal can't do it.
1433 */
1434 if (i > 0)
1435 check_for_delay(FALSE);
1436 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1437 {
1438 if (wp->w_lines_valid != 0)
1439 {
1440 /* Need to update rows that are new, stop at the
1441 * first one that scrolled down. */
1442 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
1445 /* Move the entries that were scrolled, disable
1446 * the entries for the lines to be redrawn. */
1447 if ((wp->w_lines_valid += j) > wp->w_height)
1448 wp->w_lines_valid = wp->w_height;
1449 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1450 wp->w_lines[idx] = wp->w_lines[idx - j];
1451 while (idx >= 0)
1452 wp->w_lines[idx--].wl_valid = FALSE;
1453 }
1454 }
1455 else
1456 mid_start = 0; /* redraw all lines */
1457 }
1458 else
1459 mid_start = 0; /* redraw all lines */
1460 }
1461 else
1462 mid_start = 0; /* redraw all lines */
1463 }
1464 else
1465 {
1466 /*
1467 * New topline is at or below old topline: May scroll up.
1468 * When topline didn't change, find first entry in w_lines[] that
1469 * needs updating.
1470 */
1471
1472 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1473 j = -1;
1474 row = 0;
1475 for (i = 0; i < wp->w_lines_valid; i++)
1476 {
1477 if (wp->w_lines[i].wl_valid
1478 && wp->w_lines[i].wl_lnum == wp->w_topline)
1479 {
1480 j = i;
1481 break;
1482 }
1483 row += wp->w_lines[i].wl_size;
1484 }
1485 if (j == -1)
1486 {
1487 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1488 * lines */
1489 mid_start = 0;
1490 }
1491 else
1492 {
1493 /*
1494 * Try to delete the correct number of lines.
1495 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1496 */
1497#ifdef FEAT_DIFF
1498 /* If the topline didn't change, delete old filler lines,
1499 * otherwise delete filler lines of the new topline... */
1500 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1501 row += wp->w_old_topfill;
1502 else
1503 row += diff_check_fill(wp, wp->w_topline);
1504 /* ... but don't delete new filler lines. */
1505 row -= wp->w_topfill;
1506#endif
1507 if (row > 0)
1508 {
1509 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001510 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1511 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 bot_start = wp->w_height - row;
1513 else
1514 mid_start = 0; /* redraw all lines */
1515 }
1516 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1517 {
1518 /*
1519 * Skip the lines (below the deleted lines) that are still
1520 * valid and don't need redrawing. Copy their info
1521 * upwards, to compensate for the deleted lines. Set
1522 * bot_start to the first row that needs redrawing.
1523 */
1524 bot_start = 0;
1525 idx = 0;
1526 for (;;)
1527 {
1528 wp->w_lines[idx] = wp->w_lines[j];
1529 /* stop at line that didn't fit, unless it is still
1530 * valid (no lines deleted) */
1531 if (row > 0 && bot_start + row
1532 + (int)wp->w_lines[j].wl_size > wp->w_height)
1533 {
1534 wp->w_lines_valid = idx + 1;
1535 break;
1536 }
1537 bot_start += wp->w_lines[idx++].wl_size;
1538
1539 /* stop at the last valid entry in w_lines[].wl_size */
1540 if (++j >= wp->w_lines_valid)
1541 {
1542 wp->w_lines_valid = idx;
1543 break;
1544 }
1545 }
1546#ifdef FEAT_DIFF
1547 /* Correct the first entry for filler lines at the top
1548 * when it won't get updated below. */
1549 if (wp->w_p_diff && bot_start > 0)
1550 wp->w_lines[0].wl_size =
1551 plines_win_nofill(wp, wp->w_topline, TRUE)
1552 + wp->w_topfill;
1553#endif
1554 }
1555 }
1556 }
1557
1558 /* When starting redraw in the first line, redraw all lines. When
1559 * there is only one window it's probably faster to clear the screen
1560 * first. */
1561 if (mid_start == 0)
1562 {
1563 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001564 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001565 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001566 /* Clear the screen when it was not done by win_del_lines() or
1567 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1568 * then. */
1569 if (screen_cleared != TRUE)
1570 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001571 /* The screen was cleared, redraw the tab pages line. */
1572 if (redraw_tabline)
1573 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001576
1577 /* When win_del_lines() or win_ins_lines() caused the screen to be
1578 * cleared (only happens for the first window) or when screenclear()
1579 * was called directly above, "must_redraw" will have been set to
1580 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1581 if (screen_cleared == TRUE)
1582 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 }
1584 else
1585 {
1586 /* Not VALID or INVERTED: redraw all lines. */
1587 mid_start = 0;
1588 mid_end = wp->w_height;
1589 }
1590
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001591 if (type == SOME_VALID)
1592 {
1593 /* SOME_VALID: redraw all lines. */
1594 mid_start = 0;
1595 mid_end = wp->w_height;
1596 type = NOT_VALID;
1597 }
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 /* check if we are updating or removing the inverted part */
1600 if ((VIsual_active && buf == curwin->w_buffer)
1601 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1602 {
1603 linenr_T from, to;
1604
1605 if (VIsual_active)
1606 {
1607 if (VIsual_active
1608 && (VIsual_mode != wp->w_old_visual_mode
1609 || type == INVERTED_ALL))
1610 {
1611 /*
1612 * If the type of Visual selection changed, redraw the whole
1613 * selection. Also when the ownership of the X selection is
1614 * gained or lost.
1615 */
1616 if (curwin->w_cursor.lnum < VIsual.lnum)
1617 {
1618 from = curwin->w_cursor.lnum;
1619 to = VIsual.lnum;
1620 }
1621 else
1622 {
1623 from = VIsual.lnum;
1624 to = curwin->w_cursor.lnum;
1625 }
1626 /* redraw more when the cursor moved as well */
1627 if (wp->w_old_cursor_lnum < from)
1628 from = wp->w_old_cursor_lnum;
1629 if (wp->w_old_cursor_lnum > to)
1630 to = wp->w_old_cursor_lnum;
1631 if (wp->w_old_visual_lnum < from)
1632 from = wp->w_old_visual_lnum;
1633 if (wp->w_old_visual_lnum > to)
1634 to = wp->w_old_visual_lnum;
1635 }
1636 else
1637 {
1638 /*
1639 * Find the line numbers that need to be updated: The lines
1640 * between the old cursor position and the current cursor
1641 * position. Also check if the Visual position changed.
1642 */
1643 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1644 {
1645 from = curwin->w_cursor.lnum;
1646 to = wp->w_old_cursor_lnum;
1647 }
1648 else
1649 {
1650 from = wp->w_old_cursor_lnum;
1651 to = curwin->w_cursor.lnum;
1652 if (from == 0) /* Visual mode just started */
1653 from = to;
1654 }
1655
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001656 if (VIsual.lnum != wp->w_old_visual_lnum
1657 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 {
1659 if (wp->w_old_visual_lnum < from
1660 && wp->w_old_visual_lnum != 0)
1661 from = wp->w_old_visual_lnum;
1662 if (wp->w_old_visual_lnum > to)
1663 to = wp->w_old_visual_lnum;
1664 if (VIsual.lnum < from)
1665 from = VIsual.lnum;
1666 if (VIsual.lnum > to)
1667 to = VIsual.lnum;
1668 }
1669 }
1670
1671 /*
1672 * If in block mode and changed column or curwin->w_curswant:
1673 * update all lines.
1674 * First compute the actual start and end column.
1675 */
1676 if (VIsual_mode == Ctrl_V)
1677 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001678 colnr_T fromc, toc;
1679#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1680 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
Bram Moolenaar404406a2014-10-09 13:24:43 +02001682 if (curwin->w_p_lbr)
1683 ve_flags = VE_ALL;
1684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001686#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1687 ve_flags = save_ve_flags;
1688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 ++toc;
1690 if (curwin->w_curswant == MAXCOL)
1691 toc = MAXCOL;
1692
1693 if (fromc != wp->w_old_cursor_fcol
1694 || toc != wp->w_old_cursor_lcol)
1695 {
1696 if (from > VIsual.lnum)
1697 from = VIsual.lnum;
1698 if (to < VIsual.lnum)
1699 to = VIsual.lnum;
1700 }
1701 wp->w_old_cursor_fcol = fromc;
1702 wp->w_old_cursor_lcol = toc;
1703 }
1704 }
1705 else
1706 {
1707 /* Use the line numbers of the old Visual area. */
1708 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1709 {
1710 from = wp->w_old_cursor_lnum;
1711 to = wp->w_old_visual_lnum;
1712 }
1713 else
1714 {
1715 from = wp->w_old_visual_lnum;
1716 to = wp->w_old_cursor_lnum;
1717 }
1718 }
1719
1720 /*
1721 * There is no need to update lines above the top of the window.
1722 */
1723 if (from < wp->w_topline)
1724 from = wp->w_topline;
1725
1726 /*
1727 * If we know the value of w_botline, use it to restrict the update to
1728 * the lines that are visible in the window.
1729 */
1730 if (wp->w_valid & VALID_BOTLINE)
1731 {
1732 if (from >= wp->w_botline)
1733 from = wp->w_botline - 1;
1734 if (to >= wp->w_botline)
1735 to = wp->w_botline - 1;
1736 }
1737
1738 /*
1739 * Find the minimal part to be updated.
1740 * Watch out for scrolling that made entries in w_lines[] invalid.
1741 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1742 * top_end; need to redraw from top_end to the "to" line.
1743 * A middle mouse click with a Visual selection may change the text
1744 * above the Visual area and reset wl_valid, do count these for
1745 * mid_end (in srow).
1746 */
1747 if (mid_start > 0)
1748 {
1749 lnum = wp->w_topline;
1750 idx = 0;
1751 srow = 0;
1752 if (scrolled_down)
1753 mid_start = top_end;
1754 else
1755 mid_start = 0;
1756 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1757 {
1758 if (wp->w_lines[idx].wl_valid)
1759 mid_start += wp->w_lines[idx].wl_size;
1760 else if (!scrolled_down)
1761 srow += wp->w_lines[idx].wl_size;
1762 ++idx;
1763# ifdef FEAT_FOLDING
1764 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1765 lnum = wp->w_lines[idx].wl_lnum;
1766 else
1767# endif
1768 ++lnum;
1769 }
1770 srow += mid_start;
1771 mid_end = wp->w_height;
1772 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1773 {
1774 if (wp->w_lines[idx].wl_valid
1775 && wp->w_lines[idx].wl_lnum >= to + 1)
1776 {
1777 /* Only update until first row of this line */
1778 mid_end = srow;
1779 break;
1780 }
1781 srow += wp->w_lines[idx].wl_size;
1782 }
1783 }
1784 }
1785
1786 if (VIsual_active && buf == curwin->w_buffer)
1787 {
1788 wp->w_old_visual_mode = VIsual_mode;
1789 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1790 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001791 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 wp->w_old_curswant = curwin->w_curswant;
1793 }
1794 else
1795 {
1796 wp->w_old_visual_mode = 0;
1797 wp->w_old_cursor_lnum = 0;
1798 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001799 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1803 /* reset got_int, otherwise regexp won't work */
1804 save_got_int = got_int;
1805 got_int = 0;
1806#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001807#ifdef SYN_TIME_LIMIT
1808 /* Set the time limit to 'redrawtime'. */
1809 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001810 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812#ifdef FEAT_FOLDING
1813 win_foldinfo.fi_level = 0;
1814#endif
1815
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001816#ifdef FEAT_MENU
1817 /*
1818 * Draw the window toolbar, if there is one.
1819 * TODO: only when needed.
1820 */
1821 if (winbar_height(wp) > 0)
1822 redraw_win_toolbar(wp);
1823#endif
1824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 /*
1826 * Update all the window rows.
1827 */
1828 idx = 0; /* first entry in w_lines[].wl_size */
1829 row = 0;
1830 srow = 0;
1831 lnum = wp->w_topline; /* first line shown in window */
1832 for (;;)
1833 {
1834 /* stop updating when reached the end of the window (check for _past_
1835 * the end of the window is at the end of the loop) */
1836 if (row == wp->w_height)
1837 {
1838 didline = TRUE;
1839 break;
1840 }
1841
1842 /* stop updating when hit the end of the file */
1843 if (lnum > buf->b_ml.ml_line_count)
1844 {
1845 eof = TRUE;
1846 break;
1847 }
1848
1849 /* Remember the starting row of the line that is going to be dealt
1850 * with. It is used further down when the line doesn't fit. */
1851 srow = row;
1852
1853 /*
1854 * Update a line when it is in an area that needs updating, when it
1855 * has changes or w_lines[idx] is invalid.
1856 * bot_start may be halfway a wrapped line after using
1857 * win_del_lines(), check if the current line includes it.
1858 * When syntax folding is being used, the saved syntax states will
1859 * already have been updated, we can't see where the syntax state is
1860 * the same again, just update until the end of the window.
1861 */
1862 if (row < top_end
1863 || (row >= mid_start && row < mid_end)
1864#ifdef FEAT_SEARCH_EXTRA
1865 || top_to_mod
1866#endif
1867 || idx >= wp->w_lines_valid
1868 || (row + wp->w_lines[idx].wl_size > bot_start)
1869 || (mod_top != 0
1870 && (lnum == mod_top
1871 || (lnum >= mod_top
1872 && (lnum < mod_bot
1873#ifdef FEAT_SYN_HL
1874 || did_update == DID_FOLD
1875 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001876 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 && (
1878# ifdef FEAT_FOLDING
1879 (foldmethodIsSyntax(wp)
1880 && hasAnyFolding(wp)) ||
1881# endif
1882 syntax_check_changed(lnum)))
1883#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001884#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001885 /* match in fixed position might need redraw
1886 * if lines were inserted or deleted */
1887 || (wp->w_match_head != NULL
1888 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 )))))
1891 {
1892#ifdef FEAT_SEARCH_EXTRA
1893 if (lnum == mod_top)
1894 top_to_mod = FALSE;
1895#endif
1896
1897 /*
1898 * When at start of changed lines: May scroll following lines
1899 * up or down to minimize redrawing.
1900 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001901 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 */
1903 if (lnum == mod_top
1904 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001905 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
1907 int old_rows = 0;
1908 int new_rows = 0;
1909 int xtra_rows;
1910 linenr_T l;
1911
1912 /* Count the old number of window rows, using w_lines[], which
1913 * should still contain the sizes for the lines as they are
1914 * currently displayed. */
1915 for (i = idx; i < wp->w_lines_valid; ++i)
1916 {
1917 /* Only valid lines have a meaningful wl_lnum. Invalid
1918 * lines are part of the changed area. */
1919 if (wp->w_lines[i].wl_valid
1920 && wp->w_lines[i].wl_lnum == mod_bot)
1921 break;
1922 old_rows += wp->w_lines[i].wl_size;
1923#ifdef FEAT_FOLDING
1924 if (wp->w_lines[i].wl_valid
1925 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1926 {
1927 /* Must have found the last valid entry above mod_bot.
1928 * Add following invalid entries. */
1929 ++i;
1930 while (i < wp->w_lines_valid
1931 && !wp->w_lines[i].wl_valid)
1932 old_rows += wp->w_lines[i++].wl_size;
1933 break;
1934 }
1935#endif
1936 }
1937
1938 if (i >= wp->w_lines_valid)
1939 {
1940 /* We can't find a valid line below the changed lines,
1941 * need to redraw until the end of the window.
1942 * Inserting/deleting lines has no use. */
1943 bot_start = 0;
1944 }
1945 else
1946 {
1947 /* Able to count old number of rows: Count new window
1948 * rows, and may insert/delete lines */
1949 j = idx;
1950 for (l = lnum; l < mod_bot; ++l)
1951 {
1952#ifdef FEAT_FOLDING
1953 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1954 ++new_rows;
1955 else
1956#endif
1957#ifdef FEAT_DIFF
1958 if (l == wp->w_topline)
1959 new_rows += plines_win_nofill(wp, l, TRUE)
1960 + wp->w_topfill;
1961 else
1962#endif
1963 new_rows += plines_win(wp, l, TRUE);
1964 ++j;
1965 if (new_rows > wp->w_height - row - 2)
1966 {
1967 /* it's getting too much, must redraw the rest */
1968 new_rows = 9999;
1969 break;
1970 }
1971 }
1972 xtra_rows = new_rows - old_rows;
1973 if (xtra_rows < 0)
1974 {
1975 /* May scroll text up. If there is not enough
1976 * remaining text or scrolling fails, must redraw the
1977 * rest. If scrolling works, must redraw the text
1978 * below the scrolled text. */
1979 if (row - xtra_rows >= wp->w_height - 2)
1980 mod_bot = MAXLNUM;
1981 else
1982 {
1983 check_for_delay(FALSE);
1984 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001985 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 mod_bot = MAXLNUM;
1987 else
1988 bot_start = wp->w_height + xtra_rows;
1989 }
1990 }
1991 else if (xtra_rows > 0)
1992 {
1993 /* May scroll text down. If there is not enough
1994 * remaining text of scrolling fails, must redraw the
1995 * rest. */
1996 if (row + xtra_rows >= wp->w_height - 2)
1997 mod_bot = MAXLNUM;
1998 else
1999 {
2000 check_for_delay(FALSE);
2001 if (win_ins_lines(wp, row + old_rows,
2002 xtra_rows, FALSE, FALSE) == FAIL)
2003 mod_bot = MAXLNUM;
2004 else if (top_end > row + old_rows)
2005 /* Scrolled the part at the top that requires
2006 * updating down. */
2007 top_end += xtra_rows;
2008 }
2009 }
2010
2011 /* When not updating the rest, may need to move w_lines[]
2012 * entries. */
2013 if (mod_bot != MAXLNUM && i != j)
2014 {
2015 if (j < i)
2016 {
2017 int x = row + new_rows;
2018
2019 /* move entries in w_lines[] upwards */
2020 for (;;)
2021 {
2022 /* stop at last valid entry in w_lines[] */
2023 if (i >= wp->w_lines_valid)
2024 {
2025 wp->w_lines_valid = j;
2026 break;
2027 }
2028 wp->w_lines[j] = wp->w_lines[i];
2029 /* stop at a line that won't fit */
2030 if (x + (int)wp->w_lines[j].wl_size
2031 > wp->w_height)
2032 {
2033 wp->w_lines_valid = j + 1;
2034 break;
2035 }
2036 x += wp->w_lines[j++].wl_size;
2037 ++i;
2038 }
2039 if (bot_start > x)
2040 bot_start = x;
2041 }
2042 else /* j > i */
2043 {
2044 /* move entries in w_lines[] downwards */
2045 j -= i;
2046 wp->w_lines_valid += j;
2047 if (wp->w_lines_valid > wp->w_height)
2048 wp->w_lines_valid = wp->w_height;
2049 for (i = wp->w_lines_valid; i - j >= idx; --i)
2050 wp->w_lines[i] = wp->w_lines[i - j];
2051
2052 /* The w_lines[] entries for inserted lines are
2053 * now invalid, but wl_size may be used above.
2054 * Reset to zero. */
2055 while (i >= idx)
2056 {
2057 wp->w_lines[i].wl_size = 0;
2058 wp->w_lines[i--].wl_valid = FALSE;
2059 }
2060 }
2061 }
2062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064
2065#ifdef FEAT_FOLDING
2066 /*
2067 * When lines are folded, display one line for all of them.
2068 * Otherwise, display normally (can be several display lines when
2069 * 'wrap' is on).
2070 */
2071 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2072 if (fold_count != 0)
2073 {
2074 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2075 ++row;
2076 --fold_count;
2077 wp->w_lines[idx].wl_folded = TRUE;
2078 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2079# ifdef FEAT_SYN_HL
2080 did_update = DID_FOLD;
2081# endif
2082 }
2083 else
2084#endif
2085 if (idx < wp->w_lines_valid
2086 && wp->w_lines[idx].wl_valid
2087 && wp->w_lines[idx].wl_lnum == lnum
2088 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002089 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 && srow + wp->w_lines[idx].wl_size > wp->w_height
2091#ifdef FEAT_DIFF
2092 && diff_check_fill(wp, lnum) == 0
2093#endif
2094 )
2095 {
2096 /* This line is not going to fit. Don't draw anything here,
2097 * will draw "@ " lines below. */
2098 row = wp->w_height + 1;
2099 }
2100 else
2101 {
2102#ifdef FEAT_SEARCH_EXTRA
2103 prepare_search_hl(wp, lnum);
2104#endif
2105#ifdef FEAT_SYN_HL
2106 /* Let the syntax stuff know we skipped a few lines. */
2107 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002108 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 syntax_end_parsing(syntax_last_parsed + 1);
2110#endif
2111
2112 /*
2113 * Display one line.
2114 */
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002115 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117#ifdef FEAT_FOLDING
2118 wp->w_lines[idx].wl_folded = FALSE;
2119 wp->w_lines[idx].wl_lastlnum = lnum;
2120#endif
2121#ifdef FEAT_SYN_HL
2122 did_update = DID_LINE;
2123 syntax_last_parsed = lnum;
2124#endif
2125 }
2126
2127 wp->w_lines[idx].wl_lnum = lnum;
2128 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002129
2130 /* Past end of the window or end of the screen. Note that after
2131 * resizing wp->w_height may be end up too big. That's a problem
2132 * elsewhere, but prevent a crash here. */
2133 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 {
2135 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002136 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2138 ++idx;
2139 break;
2140 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002141 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 wp->w_lines[idx].wl_size = row - srow;
2143 ++idx;
2144#ifdef FEAT_FOLDING
2145 lnum += fold_count + 1;
2146#else
2147 ++lnum;
2148#endif
2149 }
2150 else
2151 {
2152 /* This line does not need updating, advance to the next one */
2153 row += wp->w_lines[idx++].wl_size;
2154 if (row > wp->w_height) /* past end of screen */
2155 break;
2156#ifdef FEAT_FOLDING
2157 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2158#else
2159 ++lnum;
2160#endif
2161#ifdef FEAT_SYN_HL
2162 did_update = DID_NONE;
2163#endif
2164 }
2165
2166 if (lnum > buf->b_ml.ml_line_count)
2167 {
2168 eof = TRUE;
2169 break;
2170 }
2171 }
2172 /*
2173 * End of loop over all window lines.
2174 */
2175
2176
2177 if (idx > wp->w_lines_valid)
2178 wp->w_lines_valid = idx;
2179
2180#ifdef FEAT_SYN_HL
2181 /*
2182 * Let the syntax stuff know we stop parsing here.
2183 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002184 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 syntax_end_parsing(syntax_last_parsed + 1);
2186#endif
2187
2188 /*
2189 * If we didn't hit the end of the file, and we didn't finish the last
2190 * line we were working on, then the line didn't fit.
2191 */
2192 wp->w_empty_rows = 0;
2193#ifdef FEAT_DIFF
2194 wp->w_filler_rows = 0;
2195#endif
2196 if (!eof && !didline)
2197 {
2198 if (lnum == wp->w_topline)
2199 {
2200 /*
2201 * Single line that does not fit!
2202 * Don't overwrite it, it can be edited.
2203 */
2204 wp->w_botline = lnum + 1;
2205 }
2206#ifdef FEAT_DIFF
2207 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2208 {
2209 /* Window ends in filler lines. */
2210 wp->w_botline = lnum;
2211 wp->w_filler_rows = wp->w_height - srow;
2212 }
2213#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002214 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2215 {
2216 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2217
2218 /*
2219 * Last line isn't finished: Display "@@@" in the last screen line.
2220 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002221 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002222 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002223 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002224 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002225 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002226 set_empty_rows(wp, srow);
2227 wp->w_botline = lnum;
2228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2230 {
2231 /*
2232 * Last line isn't finished: Display "@@@" at the end.
2233 */
2234 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2235 W_WINROW(wp) + wp->w_height,
2236 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002237 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 set_empty_rows(wp, srow);
2239 wp->w_botline = lnum;
2240 }
2241 else
2242 {
2243 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2244 wp->w_botline = lnum;
2245 }
2246 }
2247 else
2248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 if (eof) /* we hit the end of the file */
2251 {
2252 wp->w_botline = buf->b_ml.ml_line_count + 1;
2253#ifdef FEAT_DIFF
2254 j = diff_check_fill(wp, wp->w_botline);
2255 if (j > 0 && !wp->w_botfill)
2256 {
2257 /*
2258 * Display filler lines at the end of the file
2259 */
2260 if (char2cells(fill_diff) > 1)
2261 i = '-';
2262 else
2263 i = fill_diff;
2264 if (row + j > wp->w_height)
2265 j = wp->w_height - row;
2266 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2267 row += j;
2268 }
2269#endif
2270 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002271 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 wp->w_botline = lnum;
2273
2274 /* make sure the rest of the screen is blank */
2275 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002276 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 }
2278
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002279#ifdef SYN_TIME_LIMIT
2280 syn_set_timeout(NULL);
2281#endif
2282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 /* Reset the type of redrawing required, the window has been updated. */
2284 wp->w_redr_type = 0;
2285#ifdef FEAT_DIFF
2286 wp->w_old_topfill = wp->w_topfill;
2287 wp->w_old_botfill = wp->w_botfill;
2288#endif
2289
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002290 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 /*
2293 * There is a trick with w_botline. If we invalidate it on each
2294 * change that might modify it, this will cause a lot of expensive
2295 * calls to plines() in update_topline() each time. Therefore the
2296 * value of w_botline is often approximated, and this value is used to
2297 * compute the value of w_topline. If the value of w_botline was
2298 * wrong, check that the value of w_topline is correct (cursor is on
2299 * the visible part of the text). If it's not, we need to redraw
2300 * again. Mostly this just means scrolling up a few lines, so it
2301 * doesn't look too bad. Only do this for the current window (where
2302 * changes are relevant).
2303 */
2304 wp->w_valid |= VALID_BOTLINE;
2305 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2306 {
2307 recursive = TRUE;
2308 curwin->w_valid &= ~VALID_TOPLINE;
2309 update_topline(); /* may invalidate w_botline again */
2310 if (must_redraw != 0)
2311 {
2312 /* Don't update for changes in buffer again. */
2313 i = curbuf->b_mod_set;
2314 curbuf->b_mod_set = FALSE;
2315 win_update(curwin);
2316 must_redraw = 0;
2317 curbuf->b_mod_set = i;
2318 }
2319 recursive = FALSE;
2320 }
2321 }
2322
2323#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2324 /* restore got_int, unless CTRL-C was hit while redrawing */
2325 if (!got_int)
2326 got_int = save_got_int;
2327#endif
2328}
2329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330/*
2331 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2332 * as the filler character.
2333 */
2334 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002335win_draw_end(
2336 win_T *wp,
2337 int c1,
2338 int c2,
2339 int row,
2340 int endrow,
2341 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2344 int n = 0;
2345# define FDC_OFF n
2346#else
2347# define FDC_OFF 0
2348#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002349#ifdef FEAT_FOLDING
2350 int fdc = compute_foldcolumn(wp, 0);
2351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353#ifdef FEAT_RIGHTLEFT
2354 if (wp->w_p_rl)
2355 {
2356 /* No check for cmdline window: should never be right-left. */
2357# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002358 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360 if (n > 0)
2361 {
2362 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002363 if (n > wp->w_width)
2364 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2366 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002367 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369# endif
2370# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002371 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
2373 int nn = n + 2;
2374
2375 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002376 if (nn > wp->w_width)
2377 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2379 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002380 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 n = nn;
2382 }
2383# endif
2384 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002385 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002386 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2388 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002389 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
2391 else
2392#endif
2393 {
2394#ifdef FEAT_CMDWIN
2395 if (cmdwin_type != 0 && wp == curwin)
2396 {
2397 /* draw the cmdline character in the leftmost column */
2398 n = 1;
2399 if (n > wp->w_width)
2400 n = wp->w_width;
2401 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002402 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002403 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
2405#endif
2406#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002407 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002409 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
2411 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002412 if (nn > wp->w_width)
2413 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002415 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002416 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 n = nn;
2418 }
2419#endif
2420#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002421 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
2423 int nn = n + 2;
2424
2425 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002426 if (nn > wp->w_width)
2427 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002429 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002430 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 n = nn;
2432 }
2433#endif
2434 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002435 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002436 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
2438 set_empty_rows(wp, row);
2439}
2440
Bram Moolenaar1a384422010-07-14 19:53:30 +02002441#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002442static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002443
2444/*
2445 * Advance **color_cols and return TRUE when there are columns to draw.
2446 */
2447 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002448advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002449{
2450 while (**color_cols >= 0 && vcol > **color_cols)
2451 ++*color_cols;
2452 return (**color_cols >= 0);
2453}
2454#endif
2455
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002456#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2457/*
2458 * Copy "text" to ScreenLines using "attr".
2459 * Returns the next screen column.
2460 */
2461 static int
2462text_to_screenline(win_T *wp, char_u *text, int col)
2463{
2464 int off = (int)(current_ScreenLine - ScreenLines);
2465
2466#ifdef FEAT_MBYTE
2467 if (has_mbyte)
2468 {
2469 int cells;
2470 int u8c, u8cc[MAX_MCO];
2471 int i;
2472 int idx;
2473 int c_len;
2474 char_u *p;
2475# ifdef FEAT_ARABIC
2476 int prev_c = 0; /* previous Arabic character */
2477 int prev_c1 = 0; /* first composing char for prev_c */
2478# endif
2479
2480# ifdef FEAT_RIGHTLEFT
2481 if (wp->w_p_rl)
2482 idx = off;
2483 else
2484# endif
2485 idx = off + col;
2486
2487 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2488 for (p = text; *p != NUL; )
2489 {
2490 cells = (*mb_ptr2cells)(p);
2491 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002492 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002493# ifdef FEAT_RIGHTLEFT
2494 - (wp->w_p_rl ? col : 0)
2495# endif
2496 )
2497 break;
2498 ScreenLines[idx] = *p;
2499 if (enc_utf8)
2500 {
2501 u8c = utfc_ptr2char(p, u8cc);
2502 if (*p < 0x80 && u8cc[0] == 0)
2503 {
2504 ScreenLinesUC[idx] = 0;
2505#ifdef FEAT_ARABIC
2506 prev_c = u8c;
2507#endif
2508 }
2509 else
2510 {
2511#ifdef FEAT_ARABIC
2512 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2513 {
2514 /* Do Arabic shaping. */
2515 int pc, pc1, nc;
2516 int pcc[MAX_MCO];
2517 int firstbyte = *p;
2518
2519 /* The idea of what is the previous and next
2520 * character depends on 'rightleft'. */
2521 if (wp->w_p_rl)
2522 {
2523 pc = prev_c;
2524 pc1 = prev_c1;
2525 nc = utf_ptr2char(p + c_len);
2526 prev_c1 = u8cc[0];
2527 }
2528 else
2529 {
2530 pc = utfc_ptr2char(p + c_len, pcc);
2531 nc = prev_c;
2532 pc1 = pcc[0];
2533 }
2534 prev_c = u8c;
2535
2536 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2537 pc, pc1, nc);
2538 ScreenLines[idx] = firstbyte;
2539 }
2540 else
2541 prev_c = u8c;
2542#endif
2543 /* Non-BMP character: display as ? or fullwidth ?. */
2544#ifdef UNICODE16
2545 if (u8c >= 0x10000)
2546 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2547 else
2548#endif
2549 ScreenLinesUC[idx] = u8c;
2550 for (i = 0; i < Screen_mco; ++i)
2551 {
2552 ScreenLinesC[i][idx] = u8cc[i];
2553 if (u8cc[i] == 0)
2554 break;
2555 }
2556 }
2557 if (cells > 1)
2558 ScreenLines[idx + 1] = 0;
2559 }
2560 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2561 /* double-byte single width character */
2562 ScreenLines2[idx] = p[1];
2563 else if (cells > 1)
2564 /* double-width character */
2565 ScreenLines[idx + 1] = p[1];
2566 col += cells;
2567 idx += cells;
2568 p += c_len;
2569 }
2570 }
2571 else
2572#endif
2573 {
2574 int len = (int)STRLEN(text);
2575
Bram Moolenaar02631462017-09-22 15:20:32 +02002576 if (len > wp->w_width - col)
2577 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002578 if (len > 0)
2579 {
2580#ifdef FEAT_RIGHTLEFT
2581 if (wp->w_p_rl)
2582 STRNCPY(current_ScreenLine, text, len);
2583 else
2584#endif
2585 STRNCPY(current_ScreenLine + col, text, len);
2586 col += len;
2587 }
2588 }
2589 return col;
2590}
2591#endif
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593#ifdef FEAT_FOLDING
2594/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002595 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2596 * space is available for window "wp", minus "col".
2597 */
2598 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002599compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002600{
2601 int fdc = wp->w_p_fdc;
2602 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002603 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002604
2605 if (fdc > wwidth - (col + wmw))
2606 fdc = wwidth - (col + wmw);
2607 return fdc;
2608}
2609
2610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 * Display one folded line.
2612 */
2613 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002614fold_line(
2615 win_T *wp,
2616 long fold_count,
2617 foldinfo_T *foldinfo,
2618 linenr_T lnum,
2619 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002621 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 pos_T *top, *bot;
2623 linenr_T lnume = lnum + fold_count - 1;
2624 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002625 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 int col;
2628 int txtcol;
2629 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002630 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
2632 /* Build the fold line:
2633 * 1. Add the cmdwin_type for the command-line window
2634 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002635 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 * 4. Compose the text
2637 * 5. Add the text
2638 * 6. set highlighting for the Visual area an other text
2639 */
2640 col = 0;
2641
2642 /*
2643 * 1. Add the cmdwin_type for the command-line window
2644 * Ignores 'rightleft', this window is never right-left.
2645 */
2646#ifdef FEAT_CMDWIN
2647 if (cmdwin_type != 0 && wp == curwin)
2648 {
2649 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002650 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651#ifdef FEAT_MBYTE
2652 if (enc_utf8)
2653 ScreenLinesUC[off] = 0;
2654#endif
2655 ++col;
2656 }
2657#endif
2658
2659 /*
2660 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002661 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002663 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (fdc > 0)
2665 {
2666 fill_foldcolumn(buf, wp, TRUE, lnum);
2667#ifdef FEAT_RIGHTLEFT
2668 if (wp->w_p_rl)
2669 {
2670 int i;
2671
Bram Moolenaar02631462017-09-22 15:20:32 +02002672 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002673 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 /* reverse the fold column */
2675 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002676 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678 else
2679#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002680 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 col += fdc;
2682 }
2683
2684#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002685# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2686 for (ri = 0; ri < l; ++ri) \
Bram Moolenaar02631462017-09-22 15:20:32 +02002687 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002688 else \
2689 for (ri = 0; ri < l; ++ri) \
2690 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002692# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2693 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694#endif
2695
Bram Moolenaar64486672010-05-16 15:46:46 +02002696 /* Set all attributes of the 'number' or 'relativenumber' column and the
2697 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002698 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
2700#ifdef FEAT_SIGNS
2701 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002702 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002704 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 if (len > 0)
2706 {
2707 if (len > 2)
2708 len = 2;
2709# ifdef FEAT_RIGHTLEFT
2710 if (wp->w_p_rl)
2711 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002712 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002713 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 else
2715# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002716 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 col += len;
2718 }
2719 }
2720#endif
2721
2722 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002723 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002725 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002727 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (len > 0)
2729 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002730 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002731 long num;
2732 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002733
2734 if (len > w + 1)
2735 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002736
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002737 if (wp->w_p_nu && !wp->w_p_rnu)
2738 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002739 num = (long)lnum;
2740 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002741 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002742 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002743 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002744 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002745 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002746 /* 'number' + 'relativenumber': cursor line shows absolute
2747 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002748 num = lnum;
2749 fmt = "%-*ld ";
2750 }
2751 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002752
Bram Moolenaar700e7342013-01-30 12:31:36 +01002753 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754#ifdef FEAT_RIGHTLEFT
2755 if (wp->w_p_rl)
2756 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002757 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002758 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 else
2760#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002761 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 col += len;
2763 }
2764 }
2765
2766 /*
2767 * 4. Compose the folded-line string with 'foldtext', if set.
2768 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002769 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
2771 txtcol = col; /* remember where text starts */
2772
2773 /*
2774 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2775 * Right-left text is put in columns 0 - number-col, normal text is put
2776 * in columns number-col - window-width.
2777 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002778 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
2780 /* Fill the rest of the line with the fold filler */
2781#ifdef FEAT_RIGHTLEFT
2782 if (wp->w_p_rl)
2783 col -= txtcol;
2784#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002785 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786#ifdef FEAT_RIGHTLEFT
2787 - (wp->w_p_rl ? txtcol : 0)
2788#endif
2789 )
2790 {
2791#ifdef FEAT_MBYTE
2792 if (enc_utf8)
2793 {
2794 if (fill_fold >= 0x80)
2795 {
2796 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002797 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002798 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
2800 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002801 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002803 ScreenLines[off + col] = fill_fold;
2804 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002805 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002807 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002809 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
2811
2812 if (text != buf)
2813 vim_free(text);
2814
2815 /*
2816 * 6. set highlighting for the Visual area an other text.
2817 * If all folded lines are in the Visual area, highlight the line.
2818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2820 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002821 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 {
2823 /* Visual is after curwin->w_cursor */
2824 top = &curwin->w_cursor;
2825 bot = &VIsual;
2826 }
2827 else
2828 {
2829 /* Visual is before curwin->w_cursor */
2830 top = &VIsual;
2831 bot = &curwin->w_cursor;
2832 }
2833 if (lnum >= top->lnum
2834 && lnume <= bot->lnum
2835 && (VIsual_mode != 'v'
2836 || ((lnum > top->lnum
2837 || (lnum == top->lnum
2838 && top->col == 0))
2839 && (lnume < bot->lnum
2840 || (lnume == bot->lnum
2841 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002842 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
2844 if (VIsual_mode == Ctrl_V)
2845 {
2846 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002847 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002849 if (wp->w_old_cursor_lcol != MAXCOL
2850 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002851 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 len = wp->w_old_cursor_lcol;
2853 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002854 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002855 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002856 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
2858 }
2859 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002862 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002867#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002868 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2869 if (wp->w_p_cc_cols)
2870 {
2871 int i = 0;
2872 int j = wp->w_p_cc_cols[i];
2873 int old_txtcol = txtcol;
2874
2875 while (j > -1)
2876 {
2877 txtcol += j;
2878 if (wp->w_p_wrap)
2879 txtcol -= wp->w_skipcol;
2880 else
2881 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002882 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002883 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002884 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002885 txtcol = old_txtcol;
2886 j = wp->w_p_cc_cols[++i];
2887 }
2888 }
2889
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002890 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002891 if (wp->w_p_cuc)
2892 {
2893 txtcol += wp->w_virtcol;
2894 if (wp->w_p_wrap)
2895 txtcol -= wp->w_skipcol;
2896 else
2897 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002898 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002899 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002900 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002901 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
Bram Moolenaar02631462017-09-22 15:20:32 +02002904 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2905 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
2907 /*
2908 * Update w_cline_height and w_cline_folded if the cursor line was
2909 * updated (saves a call to plines() later).
2910 */
2911 if (wp == curwin
2912 && lnum <= curwin->w_cursor.lnum
2913 && lnume >= curwin->w_cursor.lnum)
2914 {
2915 curwin->w_cline_row = row;
2916 curwin->w_cline_height = 1;
2917 curwin->w_cline_folded = TRUE;
2918 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2919 }
2920}
2921
2922/*
2923 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2924 */
2925 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002926copy_text_attr(
2927 int off,
2928 char_u *buf,
2929 int len,
2930 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002932 int i;
2933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 mch_memmove(ScreenLines + off, buf, (size_t)len);
2935# ifdef FEAT_MBYTE
2936 if (enc_utf8)
2937 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2938# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002939 for (i = 0; i < len; ++i)
2940 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941}
2942
2943/*
2944 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002945 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 */
2947 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002948fill_foldcolumn(
2949 char_u *p,
2950 win_T *wp,
2951 int closed, /* TRUE of FALSE */
2952 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953{
2954 int i = 0;
2955 int level;
2956 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002957 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002958 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002961 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963 level = win_foldinfo.fi_level;
2964 if (level > 0)
2965 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002966 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002967 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002968
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 /* If the column is too narrow, we start at the lowest level that
2970 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002971 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 if (first_level < 1)
2973 first_level = 1;
2974
Bram Moolenaar1c934292015-01-27 16:39:29 +01002975 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 {
2977 if (win_foldinfo.fi_lnum == lnum
2978 && first_level + i >= win_foldinfo.fi_low_level)
2979 p[i] = '-';
2980 else if (first_level == 1)
2981 p[i] = '|';
2982 else if (first_level + i <= 9)
2983 p[i] = '0' + first_level + i;
2984 else
2985 p[i] = '>';
2986 if (first_level + i == level)
2987 break;
2988 }
2989 }
2990 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002991 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992}
2993#endif /* FEAT_FOLDING */
2994
2995/*
2996 * Display line "lnum" of window 'wp' on the screen.
2997 * Start at row "startrow", stop when "endrow" is reached.
2998 * wp->w_virtcol needs to be valid.
2999 *
3000 * Return the number of last row the line occupies.
3001 */
3002 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003003win_line(
3004 win_T *wp,
3005 linenr_T lnum,
3006 int startrow,
3007 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003008 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003010 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3012 int c = 0; /* init for GCC */
3013 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003014#ifdef FEAT_LINEBREAK
3015 long vcol_sbr = -1; /* virtual column after showbreak */
3016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 long vcol_prev = -1; /* "vcol" of previous character */
3018 char_u *line; /* current line */
3019 char_u *ptr; /* current position in "line" */
3020 int row; /* row in the window, excl w_winrow */
3021 int screen_row; /* row on the screen, incl w_winrow */
3022
3023 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3024 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003025 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003026 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 int c_extra = NUL; /* extra chars, all the same */
3028 int extra_attr = 0; /* attributes when n_extra != 0 */
3029 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3030 displaying lcs_eol at end-of-line */
3031 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3032 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3033
3034 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3035 int saved_n_extra = 0;
3036 char_u *saved_p_extra = NULL;
3037 int saved_c_extra = 0;
3038 int saved_char_attr = 0;
3039
3040 int n_attr = 0; /* chars with special attr */
3041 int saved_attr2 = 0; /* char_attr saved for n_attr */
3042 int n_attr3 = 0; /* chars with overruling special attr */
3043 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3044
3045 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3046
3047 int fromcol, tocol; /* start/end of inverting */
3048 int fromcol_prev = -2; /* start of inverting after cursor */
3049 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003051 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 pos_T pos;
3053 long v;
3054
3055 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003056 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3058 in this line */
3059 int attr = 0; /* attributes for area highlighting */
3060 int area_attr = 0; /* attributes desired by highlighting */
3061 int search_attr = 0; /* attributes desired by 'hlsearch' */
3062#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003063 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 int syntax_attr = 0; /* attributes desired by syntax */
3065 int has_syntax = FALSE; /* this buffer has syntax highl. */
3066 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003067 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003068 int draw_color_col = FALSE; /* highlight colorcolumn */
3069 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003070#endif
3071#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003072 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003073# define SPWORDLEN 150
3074 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003075 int nextlinecol = 0; /* column where nextline[] starts */
3076 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003077 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003078 int spell_attr = 0; /* attributes desired by spelling */
3079 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003080 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3081 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003082 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003083 static int cap_col = -1; /* column to check for Cap word */
3084 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003085 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086#endif
3087 int extra_check; /* has syntax or linebreak */
3088#ifdef FEAT_MBYTE
3089 int multi_attr = 0; /* attributes desired by multibyte */
3090 int mb_l = 1; /* multi-byte byte length */
3091 int mb_c = 0; /* decoded multi-byte character */
3092 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003093 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094#endif
3095#ifdef FEAT_DIFF
3096 int filler_lines; /* nr of filler lines to be drawn */
3097 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003098 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 int change_start = MAXCOL; /* first col of changed area */
3100 int change_end = -1; /* last col of changed area */
3101#endif
3102 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3103#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003104 int need_showbreak = FALSE; /* overlong line, skipping first x
3105 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003107#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003108 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003110 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111#endif
3112#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003113 matchitem_T *cur; /* points to the match list */
3114 match_T *shl; /* points to search_hl or a match */
3115 int shl_flag; /* flag to indicate whether search_hl
3116 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003117 int pos_inprogress; /* marks that position match search is
3118 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003119 int prevcol_hl_flag; /* flag to indicate whether prevcol
3120 equals startcol of search_hl or one
3121 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122#endif
3123#ifdef FEAT_ARABIC
3124 int prev_c = 0; /* previous Arabic character */
3125 int prev_c1 = 0; /* first composing char for prev_c */
3126#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003127#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003128 int did_line_attr = 0;
3129#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003130#ifdef FEAT_TERMINAL
3131 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003132 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
3135 /* draw_state: items that are drawn in sequence: */
3136#define WL_START 0 /* nothing done yet */
3137#ifdef FEAT_CMDWIN
3138# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3139#else
3140# define WL_CMDLINE WL_START
3141#endif
3142#ifdef FEAT_FOLDING
3143# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3144#else
3145# define WL_FOLD WL_CMDLINE
3146#endif
3147#ifdef FEAT_SIGNS
3148# define WL_SIGN WL_FOLD + 1 /* column for signs */
3149#else
3150# define WL_SIGN WL_FOLD /* column for signs */
3151#endif
3152#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003153#ifdef FEAT_LINEBREAK
3154# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003156# define WL_BRI WL_NR
3157#endif
3158#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3159# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3160#else
3161# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162#endif
3163#define WL_LINE WL_SBR + 1 /* text in the line */
3164 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003165#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 int feedback_col = 0;
3167 int feedback_old_attr = -1;
3168#endif
3169
Bram Moolenaar860cae12010-06-05 23:22:07 +02003170#ifdef FEAT_CONCEAL
3171 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003172 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003173 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003174 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003175 int is_concealing = FALSE;
3176 int boguscols = 0; /* nonexistent columns added to force
3177 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003178 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003179 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003180 int match_conc = 0; /* cchar for match functions */
3181 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003182 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003183# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003184# define FIX_FOR_BOGUSCOLS \
3185 { \
3186 n_extra += vcol_off; \
3187 vcol -= vcol_off; \
3188 vcol_off = 0; \
3189 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003190 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003191 boguscols = 0; \
3192 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003193#else
3194# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197 if (startrow > endrow) /* past the end already! */
3198 return startrow;
3199
3200 row = startrow;
3201 screen_row = row + W_WINROW(wp);
3202
3203 /*
3204 * To speed up the loop below, set extra_check when there is linebreak,
3205 * trailing white space and/or syntax processing to be done.
3206 */
3207#ifdef FEAT_LINEBREAK
3208 extra_check = wp->w_p_lbr;
3209#else
3210 extra_check = 0;
3211#endif
3212#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003213 if (syntax_present(wp) && !wp->w_s->b_syn_error
3214# ifdef SYN_TIME_LIMIT
3215 && !wp->w_s->b_syn_slow
3216# endif
3217 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
3219 /* Prepare for syntax highlighting in this line. When there is an
3220 * error, stop syntax highlighting. */
3221 save_did_emsg = did_emsg;
3222 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003223 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003225 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 else
3227 {
3228 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003229#ifdef SYN_TIME_LIMIT
3230 if (!wp->w_s->b_syn_slow)
3231#endif
3232 {
3233 has_syntax = TRUE;
3234 extra_check = TRUE;
3235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
3237 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003238
3239 /* Check for columns to display for 'colorcolumn'. */
3240 color_cols = wp->w_p_cc_cols;
3241 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003242 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003243#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003244
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003245#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003246 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003247 {
3248 extra_check = TRUE;
3249 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003250 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003251 }
3252#endif
3253
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003254#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003255 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003256 && *wp->w_s->b_p_spl != NUL
3257 && wp->w_s->b_langp.ga_len > 0
3258 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003259 {
3260 /* Prepare for spell checking. */
3261 has_spell = TRUE;
3262 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003263
3264 /* Get the start of the next line, so that words that wrap to the next
3265 * line are found too: "et<line-break>al.".
3266 * Trick: skip a few chars for C/shell/Vim comments */
3267 nextline[SPWORDLEN] = NUL;
3268 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3269 {
3270 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3271 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3272 }
3273
3274 /* When a word wrapped from the previous line the start of the current
3275 * line is valid. */
3276 if (lnum == checked_lnum)
3277 cur_checked_col = checked_col;
3278 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003279
3280 /* When there was a sentence end in the previous line may require a
3281 * word starting with capital in this line. In line 1 always check
3282 * the first word. */
3283 if (lnum != capcol_lnum)
3284 cap_col = -1;
3285 if (lnum == 1)
3286 cap_col = 0;
3287 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289#endif
3290
3291 /*
3292 * handle visual active in this window
3293 */
3294 fromcol = -10;
3295 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3297 {
3298 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003299 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 {
3301 top = &curwin->w_cursor;
3302 bot = &VIsual;
3303 }
3304 else /* Visual is before curwin->w_cursor */
3305 {
3306 top = &VIsual;
3307 bot = &curwin->w_cursor;
3308 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003309 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 if (VIsual_mode == Ctrl_V) /* block mode */
3311 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003312 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
3314 fromcol = wp->w_old_cursor_fcol;
3315 tocol = wp->w_old_cursor_lcol;
3316 }
3317 }
3318 else /* non-block mode */
3319 {
3320 if (lnum > top->lnum && lnum <= bot->lnum)
3321 fromcol = 0;
3322 else if (lnum == top->lnum)
3323 {
3324 if (VIsual_mode == 'V') /* linewise */
3325 fromcol = 0;
3326 else
3327 {
3328 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3329 if (gchar_pos(top) == NUL)
3330 tocol = fromcol + 1;
3331 }
3332 }
3333 if (VIsual_mode != 'V' && lnum == bot->lnum)
3334 {
3335 if (*p_sel == 'e' && bot->col == 0
3336#ifdef FEAT_VIRTUALEDIT
3337 && bot->coladd == 0
3338#endif
3339 )
3340 {
3341 fromcol = -10;
3342 tocol = MAXCOL;
3343 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003344 else if (bot->col == MAXCOL)
3345 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 else
3347 {
3348 pos = *bot;
3349 if (*p_sel == 'e')
3350 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3351 else
3352 {
3353 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3354 ++tocol;
3355 }
3356 }
3357 }
3358 }
3359
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 /* Check if the character under the cursor should not be inverted */
3361 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003362#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 )
3366 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367
3368 /* if inverting in this line set area_highlighting */
3369 if (fromcol >= 0)
3370 {
3371 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003372 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003374 if ((clip_star.available && !clip_star.owned
3375 && clip_isautosel_star())
3376 || (clip_plus.available && !clip_plus.owned
3377 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003378 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#endif
3380 }
3381 }
3382
3383 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003384 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003386 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 && wp == curwin
3388 && lnum >= curwin->w_cursor.lnum
3389 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3390 {
3391 if (lnum == curwin->w_cursor.lnum)
3392 getvcol(curwin, &(curwin->w_cursor),
3393 (colnr_T *)&fromcol, NULL, NULL);
3394 else
3395 fromcol = 0;
3396 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3397 {
3398 pos.lnum = lnum;
3399 pos.col = search_match_endcol;
3400 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3401 }
3402 else
3403 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003404 /* do at least one character; happens when past end of line */
3405 if (fromcol == tocol)
3406 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003408 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 }
3410
3411#ifdef FEAT_DIFF
3412 filler_lines = diff_check(wp, lnum);
3413 if (filler_lines < 0)
3414 {
3415 if (filler_lines == -1)
3416 {
3417 if (diff_find_change(wp, lnum, &change_start, &change_end))
3418 diff_hlf = HLF_ADD; /* added line */
3419 else if (change_start == 0)
3420 diff_hlf = HLF_TXD; /* changed text */
3421 else
3422 diff_hlf = HLF_CHD; /* changed line */
3423 }
3424 else
3425 diff_hlf = HLF_ADD; /* added line */
3426 filler_lines = 0;
3427 area_highlighting = TRUE;
3428 }
3429 if (lnum == wp->w_topline)
3430 filler_lines = wp->w_topfill;
3431 filler_todo = filler_lines;
3432#endif
3433
3434#ifdef LINE_ATTR
3435# ifdef FEAT_SIGNS
3436 /* If this line has a sign with line highlighting set line_attr. */
3437 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3438 if (v != 0)
3439 line_attr = sign_get_attr((int)v, TRUE);
3440# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003441# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003443 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003444 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445# endif
3446 if (line_attr != 0)
3447 area_highlighting = TRUE;
3448#endif
3449
3450 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3451 ptr = line;
3452
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003453#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003454 if (has_spell)
3455 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003456 /* For checking first word with a capital skip white space. */
3457 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003458 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003459
Bram Moolenaar30abd282005-06-22 22:35:10 +00003460 /* To be able to spell-check over line boundaries copy the end of the
3461 * current line into nextline[]. Above the start of the next line was
3462 * copied to nextline[SPWORDLEN]. */
3463 if (nextline[SPWORDLEN] == NUL)
3464 {
3465 /* No next line or it is empty. */
3466 nextlinecol = MAXCOL;
3467 nextline_idx = 0;
3468 }
3469 else
3470 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003471 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003472 if (v < SPWORDLEN)
3473 {
3474 /* Short line, use it completely and append the start of the
3475 * next line. */
3476 nextlinecol = 0;
3477 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003478 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003479 nextline_idx = v + 1;
3480 }
3481 else
3482 {
3483 /* Long line, use only the last SPWORDLEN bytes. */
3484 nextlinecol = v - SPWORDLEN;
3485 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3486 nextline_idx = SPWORDLEN + 1;
3487 }
3488 }
3489 }
3490#endif
3491
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003492 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003494 if (lcs_space || lcs_trail)
3495 extra_check = TRUE;
3496 /* find start of trailing whitespace */
3497 if (lcs_trail)
3498 {
3499 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003500 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003501 --trailcol;
3502 trailcol += (colnr_T) (ptr - line);
3503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505
3506 /*
3507 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3508 * first character to be displayed.
3509 */
3510 if (wp->w_p_wrap)
3511 v = wp->w_skipcol;
3512 else
3513 v = wp->w_leftcol;
3514 if (v > 0)
3515 {
3516#ifdef FEAT_MBYTE
3517 char_u *prev_ptr = ptr;
3518#endif
3519 while (vcol < v && *ptr != NUL)
3520 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003521 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 vcol += c;
3523#ifdef FEAT_MBYTE
3524 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003526 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003529 /* When:
3530 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003531 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003532 * - 'virtualedit' is set, or
3533 * - the visual mode is active,
3534 * the end of the line may be before the start of the displayed part.
3535 */
3536 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003537#ifdef FEAT_SYN_HL
3538 wp->w_p_cuc || draw_color_col ||
3539#endif
3540#ifdef FEAT_VIRTUALEDIT
3541 virtual_active() ||
3542#endif
3543 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003544 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
3548 /* Handle a character that's not completely on the screen: Put ptr at
3549 * that character but skip the first few screen characters. */
3550 if (vcol > v)
3551 {
3552 vcol -= c;
3553#ifdef FEAT_MBYTE
3554 ptr = prev_ptr;
3555#else
3556 --ptr;
3557#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003558 /* If the character fits on the screen, don't need to skip it.
3559 * Except for a TAB. */
3560 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003561#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003562 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003563#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003564 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003565 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567
3568 /*
3569 * Adjust for when the inverted text is before the screen,
3570 * and when the start of the inverted text is before the screen.
3571 */
3572 if (tocol <= vcol)
3573 fromcol = 0;
3574 else if (fromcol >= 0 && fromcol < vcol)
3575 fromcol = vcol;
3576
3577#ifdef FEAT_LINEBREAK
3578 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3579 if (wp->w_p_wrap)
3580 need_showbreak = TRUE;
3581#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003582#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003583 /* When spell checking a word we need to figure out the start of the
3584 * word and if it's badly spelled or not. */
3585 if (has_spell)
3586 {
3587 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003588 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003589 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003590
3591 pos = wp->w_cursor;
3592 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003593 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003594 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003595
3596 /* spell_move_to() may call ml_get() and make "line" invalid */
3597 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3598 ptr = line + linecol;
3599
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003600 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003601 {
3602 /* no bad word found at line start, don't check until end of a
3603 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003604 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003605 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003606 }
3607 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003608 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003609 /* bad word found, use attributes until end of word */
3610 word_end = wp->w_cursor.col + len + 1;
3611
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003612 /* Turn index into actual attributes. */
3613 if (spell_hlf != HLF_COUNT)
3614 spell_attr = highlight_attr[spell_hlf];
3615 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003616 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003617
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003618# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003619 /* Need to restart syntax highlighting for this line. */
3620 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003621 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003622# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003623 }
3624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626
3627 /*
3628 * Correct highlighting for cursor that can't be disabled.
3629 * Avoids having to check this for each character.
3630 */
3631 if (fromcol >= 0)
3632 {
3633 if (noinvcur)
3634 {
3635 if ((colnr_T)fromcol == wp->w_virtcol)
3636 {
3637 /* highlighting starts at cursor, let it start just after the
3638 * cursor */
3639 fromcol_prev = fromcol;
3640 fromcol = -1;
3641 }
3642 else if ((colnr_T)fromcol < wp->w_virtcol)
3643 /* restart highlighting after the cursor */
3644 fromcol_prev = wp->w_virtcol;
3645 }
3646 if (fromcol >= tocol)
3647 fromcol = -1;
3648 }
3649
3650#ifdef FEAT_SEARCH_EXTRA
3651 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003652 * Handle highlighting the last used search pattern and matches.
3653 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003655 cur = wp->w_match_head;
3656 shl_flag = FALSE;
3657 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003659 if (shl_flag == FALSE)
3660 {
3661 shl = &search_hl;
3662 shl_flag = TRUE;
3663 }
3664 else
3665 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003666 shl->startcol = MAXCOL;
3667 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003669 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003670 v = (long)(ptr - line);
3671 if (cur != NULL)
3672 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003673 next_search_hl(wp, shl, lnum, (colnr_T)v,
3674 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003675
3676 /* Need to get the line again, a multi-line regexp may have made it
3677 * invalid. */
3678 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3679 ptr = line + v;
3680
3681 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003683 if (shl->lnum == lnum)
3684 shl->startcol = shl->rm.startpos[0].col;
3685 else
3686 shl->startcol = 0;
3687 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3688 - shl->rm.startpos[0].lnum)
3689 shl->endcol = shl->rm.endpos[0].col;
3690 else
3691 shl->endcol = MAXCOL;
3692 /* Highlight one character for an empty match. */
3693 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003696 if (has_mbyte && line[shl->endcol] != NUL)
3697 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3698 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003700 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003702 if ((long)shl->startcol < v) /* match at leftcol */
3703 {
3704 shl->attr_cur = shl->attr;
3705 search_attr = shl->attr;
3706 }
3707 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003709 if (shl != &search_hl && cur != NULL)
3710 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712#endif
3713
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003714#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003715 /* Cursor line highlighting for 'cursorline' in the current window. Not
3716 * when Visual mode is active, because it's not clear what is selected
3717 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003718 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003719 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003720 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003721 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003722 area_highlighting = TRUE;
3723 }
3724#endif
3725
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003726 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 col = 0;
3728#ifdef FEAT_RIGHTLEFT
3729 if (wp->w_p_rl)
3730 {
3731 /* Rightleft window: process the text in the normal direction, but put
3732 * it in current_ScreenLine[] from right to left. Start at the
3733 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003734 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 off += col;
3736 }
3737#endif
3738
3739 /*
3740 * Repeat for the whole displayed line.
3741 */
3742 for (;;)
3743 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003744#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003745 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 /* Skip this quickly when working on the text. */
3748 if (draw_state != WL_LINE)
3749 {
3750#ifdef FEAT_CMDWIN
3751 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3752 {
3753 draw_state = WL_CMDLINE;
3754 if (cmdwin_type != 0 && wp == curwin)
3755 {
3756 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003758 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003759 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761 }
3762#endif
3763
3764#ifdef FEAT_FOLDING
3765 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3766 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003767 int fdc = compute_foldcolumn(wp, 0);
3768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003770 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003772 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003773 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003774 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003775 p_extra_free = alloc(12 + 1);
3776
3777 if (p_extra_free != NULL)
3778 {
3779 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3780 n_extra = fdc;
3781 p_extra_free[n_extra] = NUL;
3782 p_extra = p_extra_free;
3783 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003784 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
3787 }
3788#endif
3789
3790#ifdef FEAT_SIGNS
3791 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3792 {
3793 draw_state = WL_SIGN;
3794 /* Show the sign column when there are any signs in this
3795 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003796 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003798 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003800 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801# endif
3802
3803 /* Draw two cells with the sign value or blank. */
3804 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003805 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 n_extra = 2;
3807
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003808 if (row == startrow
3809#ifdef FEAT_DIFF
3810 + filler_lines && filler_todo <= 0
3811#endif
3812 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 {
3814 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3815 SIGN_TEXT);
3816# ifdef FEAT_SIGN_ICONS
3817 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3818 SIGN_ICON);
3819 if (gui.in_use && icon_sign != 0)
3820 {
3821 /* Use the image in this position. */
3822 c_extra = SIGN_BYTE;
3823# ifdef FEAT_NETBEANS_INTG
3824 if (buf_signcount(wp->w_buffer, lnum) > 1)
3825 c_extra = MULTISIGN_BYTE;
3826# endif
3827 char_attr = icon_sign;
3828 }
3829 else
3830# endif
3831 if (text_sign != 0)
3832 {
3833 p_extra = sign_get_text(text_sign);
3834 if (p_extra != NULL)
3835 {
3836 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003837 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839 char_attr = sign_get_attr(text_sign, FALSE);
3840 }
3841 }
3842 }
3843 }
3844#endif
3845
3846 if (draw_state == WL_NR - 1 && n_extra == 0)
3847 {
3848 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003849 /* Display the absolute or relative line number. After the
3850 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3851 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 && (row == startrow
3853#ifdef FEAT_DIFF
3854 + filler_lines
3855#endif
3856 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3857 {
3858 /* Draw the line number (empty space after wrapping). */
3859 if (row == startrow
3860#ifdef FEAT_DIFF
3861 + filler_lines
3862#endif
3863 )
3864 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003865 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003866 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003867
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003868 if (wp->w_p_nu && !wp->w_p_rnu)
3869 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003870 num = (long)lnum;
3871 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003872 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003873 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003874 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003875 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003876 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003877 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003878 num = lnum;
3879 fmt = "%-*ld ";
3880 }
3881 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003882
Bram Moolenaar700e7342013-01-30 12:31:36 +01003883 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003884 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 if (wp->w_skipcol > 0)
3886 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3887 *p_extra = '-';
3888#ifdef FEAT_RIGHTLEFT
3889 if (wp->w_p_rl) /* reverse line numbers */
3890 rl_mirror(extra);
3891#endif
3892 p_extra = extra;
3893 c_extra = NUL;
3894 }
3895 else
3896 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003897 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003898 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003899#ifdef FEAT_SYN_HL
3900 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003901 * the current line differently.
3902 * TODO: Can we use CursorLine instead of CursorLineNr
3903 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003904 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003905 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003906 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909 }
3910
Bram Moolenaar597a4222014-06-25 14:39:50 +02003911#ifdef FEAT_LINEBREAK
3912 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3913 && n_extra == 0 && *p_sbr != NUL)
3914 /* draw indent after showbreak value */
3915 draw_state = WL_BRI;
3916 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3917 /* After the showbreak, draw the breakindent */
3918 draw_state = WL_BRI - 1;
3919
3920 /* draw 'breakindent': indent wrapped text accordingly */
3921 if (draw_state == WL_BRI - 1 && n_extra == 0)
3922 {
3923 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003924 /* if need_showbreak is set, breakindent also applies */
3925 if (wp->w_p_bri && n_extra == 0
3926 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003927# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003928 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003929# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003930 )
3931 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003932 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003933# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003934 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003935 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003936 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003937# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003938 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3939 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003940 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003941# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003942 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003943# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003944 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003945 c_extra = ' ';
3946 n_extra = get_breakindent_win(wp,
3947 ml_get_buf(wp->w_buffer, lnum, FALSE));
3948 /* Correct end of highlighted area for 'breakindent',
3949 * required when 'linebreak' is also set. */
3950 if (tocol == vcol)
3951 tocol += n_extra;
3952 }
3953 }
3954#endif
3955
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3957 if (draw_state == WL_SBR - 1 && n_extra == 0)
3958 {
3959 draw_state = WL_SBR;
3960# ifdef FEAT_DIFF
3961 if (filler_todo > 0)
3962 {
3963 /* Draw "deleted" diff line(s). */
3964 if (char2cells(fill_diff) > 1)
3965 c_extra = '-';
3966 else
3967 c_extra = fill_diff;
3968# ifdef FEAT_RIGHTLEFT
3969 if (wp->w_p_rl)
3970 n_extra = col + 1;
3971 else
3972# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02003973 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003974 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
3976# endif
3977# ifdef FEAT_LINEBREAK
3978 if (*p_sbr != NUL && need_showbreak)
3979 {
3980 /* Draw 'showbreak' at the start of each broken line. */
3981 p_extra = p_sbr;
3982 c_extra = NUL;
3983 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003984 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003986 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 /* Correct end of highlighted area for 'showbreak',
3988 * required when 'linebreak' is also set. */
3989 if (tocol == vcol)
3990 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003991#ifdef FEAT_SYN_HL
3992 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003993 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003994 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003995 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998# endif
3999 }
4000#endif
4001
4002 if (draw_state == WL_LINE - 1 && n_extra == 0)
4003 {
4004 draw_state = WL_LINE;
4005 if (saved_n_extra)
4006 {
4007 /* Continue item from end of wrapped line. */
4008 n_extra = saved_n_extra;
4009 c_extra = saved_c_extra;
4010 p_extra = saved_p_extra;
4011 char_attr = saved_char_attr;
4012 }
4013 else
4014 char_attr = 0;
4015 }
4016 }
4017
4018 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004019 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021#ifdef FEAT_DIFF
4022 && filler_todo <= 0
4023#endif
4024 )
4025 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004026 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004027 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004028 /* Pretend we have finished updating the window. Except when
4029 * 'cursorcolumn' is set. */
4030#ifdef FEAT_SYN_HL
4031 if (wp->w_p_cuc)
4032 row = wp->w_cline_row + wp->w_cline_height;
4033 else
4034#endif
4035 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 break;
4037 }
4038
4039 if (draw_state == WL_LINE && area_highlighting)
4040 {
4041 /* handle Visual or match highlighting in this line */
4042 if (vcol == fromcol
4043#ifdef FEAT_MBYTE
4044 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4045 && (*mb_ptr2cells)(ptr) > 1)
4046#endif
4047 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004048 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 && vcol < tocol))
4050 area_attr = attr; /* start highlighting */
4051 else if (area_attr != 0
4052 && (vcol == tocol
4053 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
4056#ifdef FEAT_SEARCH_EXTRA
4057 if (!n_extra)
4058 {
4059 /*
4060 * Check for start/end of search pattern match.
4061 * After end, check for start/end of next match.
4062 * When another match, have to check for start again.
4063 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004064 * Do this for 'search_hl' and the match list (ordered by
4065 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004067 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004068 cur = wp->w_match_head;
4069 shl_flag = FALSE;
4070 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004072 if (shl_flag == FALSE
4073 && ((cur != NULL
4074 && cur->priority > SEARCH_HL_PRIORITY)
4075 || cur == NULL))
4076 {
4077 shl = &search_hl;
4078 shl_flag = TRUE;
4079 }
4080 else
4081 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004082 if (cur != NULL)
4083 cur->pos.cur = 0;
4084 pos_inprogress = TRUE;
4085 while (shl->rm.regprog != NULL
4086 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004088 if (shl->startcol != MAXCOL
4089 && v >= (long)shl->startcol
4090 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004092#ifdef FEAT_MBYTE
4093 int tmp_col = v + MB_PTR2LEN(ptr);
4094
4095 if (shl->endcol < tmp_col)
4096 shl->endcol = tmp_col;
4097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004099#ifdef FEAT_CONCEAL
4100 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4101 == cur->hlg_id)
4102 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004103 has_match_conc =
4104 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004105 match_conc = cur->conceal_char;
4106 }
4107 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004108 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004111 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 {
4113 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004114 next_search_hl(wp, shl, lnum, (colnr_T)v,
4115 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004116 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004117 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
4119 /* Need to get the line again, a multi-line regexp
4120 * may have made it invalid. */
4121 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4122 ptr = line + v;
4123
4124 if (shl->lnum == lnum)
4125 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004126 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004128 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004130 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004132 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
4134 /* highlight empty match, try again after
4135 * it */
4136#ifdef FEAT_MBYTE
4137 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004138 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004139 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 else
4141#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004142 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144
4145 /* Loop to check if the match starts at the
4146 * current position */
4147 continue;
4148 }
4149 }
4150 break;
4151 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004152 if (shl != &search_hl && cur != NULL)
4153 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004155
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004156 /* Use attributes from match with highest priority among
4157 * 'search_hl' and the match list. */
4158 search_attr = search_hl.attr_cur;
4159 cur = wp->w_match_head;
4160 shl_flag = FALSE;
4161 while (cur != NULL || shl_flag == FALSE)
4162 {
4163 if (shl_flag == FALSE
4164 && ((cur != NULL
4165 && cur->priority > SEARCH_HL_PRIORITY)
4166 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004167 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004168 shl = &search_hl;
4169 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004170 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004171 else
4172 shl = &cur->hl;
4173 if (shl->attr_cur != 0)
4174 search_attr = shl->attr_cur;
4175 if (shl != &search_hl && cur != NULL)
4176 cur = cur->next;
4177 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004178 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004179 if (*ptr == NUL && (did_line_attr >= 1
4180 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004181 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183#endif
4184
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004186 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004188 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4189 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004191 if (diff_hlf == HLF_TXD && ptr - line > change_end
4192 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004194 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004195 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004196 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004199
4200 /* Decide which of the highlight attributes to use. */
4201 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004202#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004203 if (area_attr != 0)
4204 char_attr = hl_combine_attr(line_attr, area_attr);
4205 else if (search_attr != 0)
4206 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004207 /* Use line_attr when not in the Visual or 'incsearch' area
4208 * (area_attr may be 0 when "noinvcur" is set). */
4209 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004210 || vcol < fromcol || vcol_prev < fromcol_prev
4211 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004212 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004213#else
4214 if (area_attr != 0)
4215 char_attr = area_attr;
4216 else if (search_attr != 0)
4217 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004218#endif
4219 else
4220 {
4221 attr_pri = FALSE;
4222#ifdef FEAT_SYN_HL
4223 if (has_syntax)
4224 char_attr = syntax_attr;
4225 else
4226#endif
4227 char_attr = 0;
4228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 }
4230
4231 /*
4232 * Get the next character to put on the screen.
4233 */
4234 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004235 * The "p_extra" points to the extra stuff that is inserted to
4236 * represent special characters (non-printable stuff) and other
4237 * things. When all characters are the same, c_extra is used.
4238 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4239 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4241 */
4242 if (n_extra > 0)
4243 {
4244 if (c_extra != NUL)
4245 {
4246 c = c_extra;
4247#ifdef FEAT_MBYTE
4248 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004249 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 {
4251 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004252 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004253 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
4255 else
4256 mb_utf8 = FALSE;
4257#endif
4258 }
4259 else
4260 {
4261 c = *p_extra;
4262#ifdef FEAT_MBYTE
4263 if (has_mbyte)
4264 {
4265 mb_c = c;
4266 if (enc_utf8)
4267 {
4268 /* If the UTF-8 character is more than one byte:
4269 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004270 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 mb_utf8 = FALSE;
4272 if (mb_l > n_extra)
4273 mb_l = 1;
4274 else if (mb_l > 1)
4275 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004276 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004278 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 }
4281 else
4282 {
4283 /* if this is a DBCS character, put it in "mb_c" */
4284 mb_l = MB_BYTE2LEN(c);
4285 if (mb_l >= n_extra)
4286 mb_l = 1;
4287 else if (mb_l > 1)
4288 mb_c = (c << 8) + p_extra[1];
4289 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004290 if (mb_l == 0) /* at the NUL at end-of-line */
4291 mb_l = 1;
4292
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 /* If a double-width char doesn't fit display a '>' in the
4294 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004295 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296# ifdef FEAT_RIGHTLEFT
4297 wp->w_p_rl ? (col <= 0) :
4298# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004299 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 && (*mb_char2cells)(mb_c) == 2)
4301 {
4302 c = '>';
4303 mb_c = c;
4304 mb_l = 1;
4305 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004306 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 /* put the pointer back to output the double-width
4308 * character at the start of the next line. */
4309 ++n_extra;
4310 --p_extra;
4311 }
4312 else
4313 {
4314 n_extra -= mb_l - 1;
4315 p_extra += mb_l - 1;
4316 }
4317 }
4318#endif
4319 ++p_extra;
4320 }
4321 --n_extra;
4322 }
4323 else
4324 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004325#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004326 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004327#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004328
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004329 if (p_extra_free != NULL)
4330 {
4331 vim_free(p_extra_free);
4332 p_extra_free = NULL;
4333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 /*
4335 * Get a character from the line itself.
4336 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004337 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004338#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004339 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341#ifdef FEAT_MBYTE
4342 if (has_mbyte)
4343 {
4344 mb_c = c;
4345 if (enc_utf8)
4346 {
4347 /* If the UTF-8 character is more than one byte: Decode it
4348 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004349 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 mb_utf8 = FALSE;
4351 if (mb_l > 1)
4352 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004353 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 /* Overlong encoded ASCII or ASCII with composing char
4355 * is displayed normally, except a NUL. */
4356 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004357 {
4358 c = mb_c;
4359# ifdef FEAT_LINEBREAK
4360 c0 = mb_c;
4361# endif
4362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004364
4365 /* At start of the line we can have a composing char.
4366 * Draw it as a space with a composing char. */
4367 if (utf_iscomposing(mb_c))
4368 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004369 int i;
4370
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004371 for (i = Screen_mco - 1; i > 0; --i)
4372 u8cc[i] = u8cc[i - 1];
4373 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004374 mb_c = ' ';
4375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377
4378 if ((mb_l == 1 && c >= 0x80)
4379 || (mb_l >= 1 && mb_c == 0)
4380 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004381# ifdef UNICODE16
4382 || mb_c >= 0x10000
4383# endif
4384 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
4386 /*
4387 * Illegal UTF-8 byte: display as <xx>.
4388 * Non-BMP character : display as ? or fullwidth ?.
4389 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004390# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
4394 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004395# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 if (wp->w_p_rl) /* reverse */
4397 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004400# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 else if (utf_char2cells(mb_c) != 2)
4402 STRCPY(extra, "?");
4403 else
4404 /* 0xff1f in UTF-8: full-width '?' */
4405 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
4408 p_extra = extra;
4409 c = *p_extra;
4410 mb_c = mb_ptr2char_adv(&p_extra);
4411 mb_utf8 = (c >= 0x80);
4412 n_extra = (int)STRLEN(p_extra);
4413 c_extra = NUL;
4414 if (area_attr == 0 && search_attr == 0)
4415 {
4416 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004417 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 saved_attr2 = char_attr; /* save current attr */
4419 }
4420 }
4421 else if (mb_l == 0) /* at the NUL at end-of-line */
4422 mb_l = 1;
4423#ifdef FEAT_ARABIC
4424 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4425 {
4426 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004427 int pc, pc1, nc;
4428 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429
4430 /* The idea of what is the previous and next
4431 * character depends on 'rightleft'. */
4432 if (wp->w_p_rl)
4433 {
4434 pc = prev_c;
4435 pc1 = prev_c1;
4436 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004437 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439 else
4440 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004441 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004443 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445 prev_c = mb_c;
4446
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004447 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 else
4450 prev_c = mb_c;
4451#endif
4452 }
4453 else /* enc_dbcs */
4454 {
4455 mb_l = MB_BYTE2LEN(c);
4456 if (mb_l == 0) /* at the NUL at end-of-line */
4457 mb_l = 1;
4458 else if (mb_l > 1)
4459 {
4460 /* We assume a second byte below 32 is illegal.
4461 * Hopefully this is OK for all double-byte encodings!
4462 */
4463 if (ptr[1] >= 32)
4464 mb_c = (c << 8) + ptr[1];
4465 else
4466 {
4467 if (ptr[1] == NUL)
4468 {
4469 /* head byte at end of line */
4470 mb_l = 1;
4471 transchar_nonprint(extra, c);
4472 }
4473 else
4474 {
4475 /* illegal tail byte */
4476 mb_l = 2;
4477 STRCPY(extra, "XX");
4478 }
4479 p_extra = extra;
4480 n_extra = (int)STRLEN(extra) - 1;
4481 c_extra = NUL;
4482 c = *p_extra++;
4483 if (area_attr == 0 && search_attr == 0)
4484 {
4485 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004486 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 saved_attr2 = char_attr; /* save current attr */
4488 }
4489 mb_c = c;
4490 }
4491 }
4492 }
4493 /* If a double-width char doesn't fit display a '>' in the
4494 * last column; the character is displayed at the start of the
4495 * next line. */
4496 if ((
4497# ifdef FEAT_RIGHTLEFT
4498 wp->w_p_rl ? (col <= 0) :
4499# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004500 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 && (*mb_char2cells)(mb_c) == 2)
4502 {
4503 c = '>';
4504 mb_c = c;
4505 mb_utf8 = FALSE;
4506 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004507 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 /* Put pointer back so that the character will be
4509 * displayed at the start of the next line. */
4510 --ptr;
4511 }
4512 else if (*ptr != NUL)
4513 ptr += mb_l - 1;
4514
4515 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004516 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004517 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004518 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004521 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 c = ' ';
4523 if (area_attr == 0 && search_attr == 0)
4524 {
4525 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004526 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 saved_attr2 = char_attr; /* save current attr */
4528 }
4529 mb_c = c;
4530 mb_utf8 = FALSE;
4531 mb_l = 1;
4532 }
4533
4534 }
4535#endif
4536 ++ptr;
4537
4538 if (extra_check)
4539 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004540#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004541 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004542#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004543
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004544#ifdef FEAT_TERMINAL
4545 if (get_term_attr)
4546 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004547 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004548
4549 if (!attr_pri)
4550 char_attr = syntax_attr;
4551 else
4552 char_attr = hl_combine_attr(syntax_attr, char_attr);
4553 }
4554#endif
4555
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004556#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 /* Get syntax attribute, unless still at the start of the line
4558 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004559 v = (long)(ptr - line);
4560 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 {
4562 /* Get the syntax attribute for the character. If there
4563 * is an error, disable syntax highlighting. */
4564 save_did_emsg = did_emsg;
4565 did_emsg = FALSE;
4566
Bram Moolenaar217ad922005-03-20 22:37:15 +00004567 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004568# ifdef FEAT_SPELL
4569 has_spell ? &can_spell :
4570# endif
4571 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
4573 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004574 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004575 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004576 has_syntax = FALSE;
4577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 else
4579 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004580#ifdef SYN_TIME_LIMIT
4581 if (wp->w_s->b_syn_slow)
4582 has_syntax = FALSE;
4583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584
4585 /* Need to get the line again, a multi-line regexp may
4586 * have made it invalid. */
4587 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4588 ptr = line + v;
4589
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004590 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004592 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004593 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004594# ifdef FEAT_CONCEAL
4595 /* no concealing past the end of the line, it interferes
4596 * with line highlighting */
4597 if (c == NUL)
4598 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004599 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004600 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004603#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004604
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004605#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004606 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004607 * Only do this when there is no syntax highlighting, the
4608 * @Spell cluster is not used or the current syntax item
4609 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004610 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004611 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004612 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004613# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004614 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004615 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004616# endif
4617 if (c != 0 && (
4618# ifdef FEAT_SYN_HL
4619 !has_syntax ||
4620# endif
4621 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004622 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004623 char_u *prev_ptr, *p;
4624 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004625 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004626# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004627 if (has_mbyte)
4628 {
4629 prev_ptr = ptr - mb_l;
4630 v -= mb_l - 1;
4631 }
4632 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004633# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004634 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004635
4636 /* Use nextline[] if possible, it has the start of the
4637 * next line concatenated. */
4638 if ((prev_ptr - line) - nextlinecol >= 0)
4639 p = nextline + (prev_ptr - line) - nextlinecol;
4640 else
4641 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004642 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004643 len = spell_check(wp, p, &spell_hlf, &cap_col,
4644 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004645 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004646
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004647 /* In Insert mode only highlight a word that
4648 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004649 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004650 && (State & INSERT) != 0
4651 && wp->w_cursor.lnum == lnum
4652 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004653 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004654 && wp->w_cursor.col < (colnr_T)word_end)
4655 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004656 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004657 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004658 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004659
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004660 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004661 && (p - nextline) + len > nextline_idx)
4662 {
4663 /* Remember that the good word continues at the
4664 * start of the next line. */
4665 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004666 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004667 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004668
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004669 /* Turn index into actual attributes. */
4670 if (spell_hlf != HLF_COUNT)
4671 spell_attr = highlight_attr[spell_hlf];
4672
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004673 if (cap_col > 0)
4674 {
4675 if (p != prev_ptr
4676 && (p - nextline) + cap_col >= nextline_idx)
4677 {
4678 /* Remember that the word in the next line
4679 * must start with a capital. */
4680 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004681 cap_col = (int)((p - nextline) + cap_col
4682 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004683 }
4684 else
4685 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004686 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004687 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004688 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004689 }
4690 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004691 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004692 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004693 char_attr = hl_combine_attr(char_attr, spell_attr);
4694 else
4695 char_attr = hl_combine_attr(spell_attr, char_attr);
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697#endif
4698#ifdef FEAT_LINEBREAK
4699 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004700 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004702 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004703 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004705# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004706 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004707# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004708 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004710 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004712 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004713
Bram Moolenaar597a4222014-06-25 14:39:50 +02004714 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004715 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004716 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004717 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaara3650912014-11-19 13:21:57 +01004718 n_extra = (int)wp->w_buffer->b_p_ts
4719 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4720
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004721# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004722 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004723# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004725# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004726 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004727 {
4728#ifdef FEAT_CONCEAL
4729 if (c == TAB)
4730 /* See "Tab alignment" below. */
4731 FIX_FOR_BOGUSCOLS;
4732#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004733 if (!wp->w_p_list)
4734 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
4737#endif
4738
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004739 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4740 */
4741 if (wp->w_p_list
4742 && (((c == 160
4743#ifdef FEAT_MBYTE
4744 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4745#endif
4746 ) && lcs_nbsp)
4747 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4748 {
4749 c = (c == ' ') ? lcs_space : lcs_nbsp;
4750 if (area_attr == 0 && search_attr == 0)
4751 {
4752 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004753 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004754 saved_attr2 = char_attr; /* save current attr */
4755 }
4756#ifdef FEAT_MBYTE
4757 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004758 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004759 {
4760 mb_utf8 = TRUE;
4761 u8cc[0] = 0;
4762 c = 0xc0;
4763 }
4764 else
4765 mb_utf8 = FALSE;
4766#endif
4767 }
4768
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4770 {
4771 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004772 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
4774 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004775 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 saved_attr2 = char_attr; /* save current attr */
4777 }
4778#ifdef FEAT_MBYTE
4779 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004780 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 {
4782 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004783 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004784 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
4786 else
4787 mb_utf8 = FALSE;
4788#endif
4789 }
4790 }
4791
4792 /*
4793 * Handling of non-printable characters.
4794 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004795 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 {
4797 /*
4798 * when getting a character from the file, we may have to
4799 * turn it into something else on the way to putting it
4800 * into "ScreenLines".
4801 */
4802 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4803 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004804 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004805 long vcol_adjusted = vcol; /* removed showbreak length */
4806#ifdef FEAT_LINEBREAK
4807 /* only adjust the tab_len, when at the first column
4808 * after the showbreak value was drawn */
4809 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4810 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004813 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004814 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4815
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004816#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004817 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004818#endif
4819 /* tab amount depends on current column */
4820 n_extra = tab_len;
4821#ifdef FEAT_LINEBREAK
4822 else
4823 {
4824 char_u *p;
4825 int len = n_extra;
4826 int i;
4827 int saved_nextra = n_extra;
4828
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004829#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004830 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004831 /* there are characters to conceal */
4832 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004833 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4834 */
4835 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4836 && n_extra > tab_len)
4837 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004838#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004839
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004840 /* if n_extra > 0, it gives the number of chars, to
4841 * use for a tab, else we need to calculate the width
4842 * for a tab */
4843#ifdef FEAT_MBYTE
4844 len = (tab_len * mb_char2len(lcs_tab2));
4845 if (n_extra > 0)
4846 len += n_extra - tab_len;
4847#endif
4848 c = lcs_tab1;
4849 p = alloc((unsigned)(len + 1));
4850 vim_memset(p, ' ', len);
4851 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004852 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004853 p_extra_free = p;
4854 for (i = 0; i < tab_len; i++)
4855 {
4856#ifdef FEAT_MBYTE
4857 mb_char2bytes(lcs_tab2, p);
4858 p += mb_char2len(lcs_tab2);
4859 n_extra += mb_char2len(lcs_tab2)
4860 - (saved_nextra > 0 ? 1 : 0);
4861#else
4862 p[i] = lcs_tab2;
4863#endif
4864 }
4865 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004866#ifdef FEAT_CONCEAL
4867 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4868 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004869 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004870 n_extra -= vcol_off;
4871#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004872 }
4873#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004874#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004875 {
4876 int vc_saved = vcol_off;
4877
4878 /* Tab alignment should be identical regardless of
4879 * 'conceallevel' value. So tab compensates of all
4880 * previous concealed characters, and thus resets
4881 * vcol_off and boguscols accumulated so far in the
4882 * line. Note that the tab can be longer than
4883 * 'tabstop' when there are concealed characters. */
4884 FIX_FOR_BOGUSCOLS;
4885
4886 /* Make sure, the highlighting for the tab char will be
4887 * correctly set further below (effectively reverts the
4888 * FIX_FOR_BOGSUCOLS macro */
4889 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004890 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004891 tab_len += vc_saved;
4892 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894#ifdef FEAT_MBYTE
4895 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4896#endif
4897 if (wp->w_p_list)
4898 {
4899 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004900#ifdef FEAT_LINEBREAK
4901 if (wp->w_p_lbr)
4902 c_extra = NUL; /* using p_extra from above */
4903 else
4904#endif
4905 c_extra = lcs_tab2;
4906 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004907 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 saved_attr2 = char_attr; /* save current attr */
4909#ifdef FEAT_MBYTE
4910 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004911 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
4913 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004914 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004915 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917#endif
4918 }
4919 else
4920 {
4921 c_extra = ' ';
4922 c = ' ';
4923 }
4924 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004925 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004926 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004927 || ((fromcol >= 0 || fromcol_prev >= 0)
4928 && tocol > vcol
4929 && VIsual_mode != Ctrl_V
4930 && (
4931# ifdef FEAT_RIGHTLEFT
4932 wp->w_p_rl ? (col >= 0) :
4933# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004934 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004935 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004936 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004937 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004938 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004940 /* Display a '$' after the line or highlight an extra
4941 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4943 /* For a diff line the highlighting continues after the
4944 * "$". */
4945 if (
4946# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004947 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948# ifdef LINE_ATTR
4949 &&
4950# endif
4951# endif
4952# ifdef LINE_ATTR
4953 line_attr == 0
4954# endif
4955 )
4956#endif
4957 {
4958#ifdef FEAT_VIRTUALEDIT
4959 /* In virtualedit, visual selections may extend
4960 * beyond end of line. */
4961 if (area_highlighting && virtual_active()
4962 && tocol != MAXCOL && vcol < tocol)
4963 n_extra = 0;
4964 else
4965#endif
4966 {
4967 p_extra = at_end_str;
4968 n_extra = 1;
4969 c_extra = NUL;
4970 }
4971 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004972 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004973 c = lcs_eol;
4974 else
4975 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 lcs_eol_one = -1;
4977 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004978 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004980 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 n_attr = 1;
4982 }
4983#ifdef FEAT_MBYTE
4984 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004985 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
4987 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004988 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004989 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 else
4992 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4993#endif
4994 }
4995 else if (c != NUL)
4996 {
4997 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004998 if (n_extra == 0)
4999 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000#ifdef FEAT_RIGHTLEFT
5001 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5002 rl_mirror(p_extra); /* reverse "<12>" */
5003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005005#ifdef FEAT_LINEBREAK
5006 if (wp->w_p_lbr)
5007 {
5008 char_u *p;
5009
5010 c = *p_extra;
5011 p = alloc((unsigned)n_extra + 1);
5012 vim_memset(p, ' ', n_extra);
5013 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5014 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005015 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005016 p_extra_free = p_extra = p;
5017 }
5018 else
5019#endif
5020 {
5021 n_extra = byte2cells(c) - 1;
5022 c = *p_extra++;
5023 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005024 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 {
5026 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005027 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 saved_attr2 = char_attr; /* save current attr */
5029 }
5030#ifdef FEAT_MBYTE
5031 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5032#endif
5033 }
5034#ifdef FEAT_VIRTUALEDIT
5035 else if (VIsual_active
5036 && (VIsual_mode == Ctrl_V
5037 || VIsual_mode == 'v')
5038 && virtual_active()
5039 && tocol != MAXCOL
5040 && vcol < tocol
5041 && (
5042# ifdef FEAT_RIGHTLEFT
5043 wp->w_p_rl ? (col >= 0) :
5044# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005045 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
5047 c = ' ';
5048 --ptr; /* put it back at the NUL */
5049 }
5050#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005051#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 else if ((
5053# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005054 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005056# ifdef FEAT_TERMINAL
5057 term_attr != 0 ||
5058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 ) && (
5061# ifdef FEAT_RIGHTLEFT
5062 wp->w_p_rl ? (col >= 0) :
5063# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005064 (col
5065# ifdef FEAT_CONCEAL
5066 - boguscols
5067# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005068 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 /* Highlight until the right side of the window */
5071 c = ' ';
5072 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005073
5074 /* Remember we do the char for line highlighting. */
5075 ++did_line_attr;
5076
5077 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005078 if (line_attr != 0 && char_attr == search_attr
5079 && (did_line_attr > 1
5080 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005081 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082# ifdef FEAT_DIFF
5083 if (diff_hlf == HLF_TXD)
5084 {
5085 diff_hlf = HLF_CHD;
5086 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005087 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005088 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005089 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5090 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005091 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005095# ifdef FEAT_TERMINAL
5096 if (term_attr != 0)
5097 {
5098 char_attr = term_attr;
5099 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5100 char_attr = hl_combine_attr(char_attr,
5101 HL_ATTR(HLF_CUL));
5102 }
5103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105#endif
5106 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005107
5108#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005109 if ( wp->w_p_cole > 0
5110 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005111 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005112 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005113 && !(lnum_in_visual_area
5114 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005115 {
5116 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005117 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005118 && (syn_get_sub_char() != NUL || match_conc
5119 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005120 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005121 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005122 /* First time at this concealed item: display one
5123 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005124 if (match_conc)
5125 c = match_conc;
5126 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005127 c = syn_get_sub_char();
5128 else if (lcs_conceal != NUL)
5129 c = lcs_conceal;
5130 else
5131 c = ' ';
5132
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005133 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005134
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005135 if (n_extra > 0)
5136 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005137 vcol += n_extra;
5138 if (wp->w_p_wrap && n_extra > 0)
5139 {
5140# ifdef FEAT_RIGHTLEFT
5141 if (wp->w_p_rl)
5142 {
5143 col -= n_extra;
5144 boguscols -= n_extra;
5145 }
5146 else
5147# endif
5148 {
5149 boguscols += n_extra;
5150 col += n_extra;
5151 }
5152 }
5153 n_extra = 0;
5154 n_attr = 0;
5155 }
5156 else if (n_skip == 0)
5157 {
5158 is_concealing = TRUE;
5159 n_skip = 1;
5160 }
5161# ifdef FEAT_MBYTE
5162 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005163 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005164 {
5165 mb_utf8 = TRUE;
5166 u8cc[0] = 0;
5167 c = 0xc0;
5168 }
5169 else
5170 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5171# endif
5172 }
5173 else
5174 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005175 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005176 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005177 }
5178#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005181#ifdef FEAT_CONCEAL
5182 /* In the cursor line and we may be concealing characters: correct
5183 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005184 if (!did_wcol && draw_state == WL_LINE
5185 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005186 && conceal_cursor_line(wp)
5187 && (int)wp->w_virtcol <= vcol + n_skip)
5188 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005189# ifdef FEAT_RIGHTLEFT
5190 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005191 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005192 else
5193# endif
5194 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005195 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005196 did_wcol = TRUE;
5197 }
5198#endif
5199
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 /* Don't override visual selection highlighting. */
5201 if (n_attr > 0
5202 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005203 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 char_attr = extra_attr;
5205
Bram Moolenaar81695252004-12-29 20:58:21 +00005206#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 /* XIM don't send preedit_start and preedit_end, but they send
5208 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5209 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005210 if (p_imst == IM_ON_THE_SPOT
5211 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005212 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 && (State & INSERT)
5214 && !p_imdisable
5215 && im_is_preediting()
5216 && draw_state == WL_LINE)
5217 {
5218 colnr_T tcol;
5219
5220 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005221 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 else
5223 tcol = preedit_end_col;
5224 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5225 {
5226 if (feedback_old_attr < 0)
5227 {
5228 feedback_col = 0;
5229 feedback_old_attr = char_attr;
5230 }
5231 char_attr = im_get_feedback_attr(feedback_col);
5232 if (char_attr < 0)
5233 char_attr = feedback_old_attr;
5234 feedback_col++;
5235 }
5236 else if (feedback_old_attr >= 0)
5237 {
5238 char_attr = feedback_old_attr;
5239 feedback_old_attr = -1;
5240 feedback_col = 0;
5241 }
5242 }
5243#endif
5244 /*
5245 * Handle the case where we are in column 0 but not on the first
5246 * character of the line and the user wants us to show us a
5247 * special character (via 'listchars' option "precedes:<char>".
5248 */
5249 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005250 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5252#ifdef FEAT_DIFF
5253 && filler_todo <= 0
5254#endif
5255 && draw_state > WL_NR
5256 && c != NUL)
5257 {
5258 c = lcs_prec;
5259 lcs_prec_todo = NUL;
5260#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005261 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5262 {
5263 /* Double-width character being overwritten by the "precedes"
5264 * character, need to fill up half the character. */
5265 c_extra = MB_FILLER_CHAR;
5266 n_extra = 1;
5267 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005268 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005271 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
5273 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005274 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005275 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 }
5277 else
5278 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5279#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005280 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {
5282 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005283 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 n_attr3 = 1;
5285 }
5286 }
5287
5288 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005289 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005291 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005292#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005293 || did_line_attr == 1
5294#endif
5295 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005297#ifdef FEAT_SEARCH_EXTRA
5298 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005299
5300 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005301 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005302 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005303#endif
5304
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005305 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 * highlight match at end of line. If it's beyond the last
5307 * char on the screen, just overwrite that one (tricky!) Not
5308 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005309#ifdef FEAT_SEARCH_EXTRA
5310 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005311 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005312 prevcol_hl_flag = TRUE;
5313 else
5314 {
5315 cur = wp->w_match_head;
5316 while (cur != NULL)
5317 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005318 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005319 {
5320 prevcol_hl_flag = TRUE;
5321 break;
5322 }
5323 cur = cur->next;
5324 }
5325 }
5326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005328 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005329 && (VIsual_mode != Ctrl_V
5330 || lnum == VIsual.lnum
5331 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005332 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333#ifdef FEAT_SEARCH_EXTRA
5334 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005335 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005336# ifdef FEAT_SYN_HL
5337 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5338 && !(wp == curwin && VIsual_active))
5339# endif
5340# ifdef FEAT_DIFF
5341 && diff_hlf == (hlf_T)0
5342# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005343# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005344 && did_line_attr <= 1
5345# endif
5346 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347#endif
5348 ))
5349 {
5350 int n = 0;
5351
5352#ifdef FEAT_RIGHTLEFT
5353 if (wp->w_p_rl)
5354 {
5355 if (col < 0)
5356 n = 1;
5357 }
5358 else
5359#endif
5360 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005361 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 n = -1;
5363 }
5364 if (n != 0)
5365 {
5366 /* At the window boundary, highlight the last character
5367 * instead (better than nothing). */
5368 off += n;
5369 col += n;
5370 }
5371 else
5372 {
5373 /* Add a blank character to highlight. */
5374 ScreenLines[off] = ' ';
5375#ifdef FEAT_MBYTE
5376 if (enc_utf8)
5377 ScreenLinesUC[off] = 0;
5378#endif
5379 }
5380#ifdef FEAT_SEARCH_EXTRA
5381 if (area_attr == 0)
5382 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005383 /* Use attributes from match with highest priority among
5384 * 'search_hl' and the match list. */
5385 char_attr = search_hl.attr;
5386 cur = wp->w_match_head;
5387 shl_flag = FALSE;
5388 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005389 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005390 if (shl_flag == FALSE
5391 && ((cur != NULL
5392 && cur->priority > SEARCH_HL_PRIORITY)
5393 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005394 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005395 shl = &search_hl;
5396 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005397 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005398 else
5399 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005400 if ((ptr - line) - 1 == (long)shl->startcol
5401 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005402 char_attr = shl->attr;
5403 if (shl != &search_hl && cur != NULL)
5404 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
5407#endif
5408 ScreenAttrs[off] = char_attr;
5409#ifdef FEAT_RIGHTLEFT
5410 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005413 --off;
5414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 else
5416#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005419 ++off;
5420 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005421 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005422#ifdef FEAT_SYN_HL
5423 eol_hl_off = 1;
5424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427
Bram Moolenaar91170f82006-05-05 21:15:17 +00005428 /*
5429 * At end of the text line.
5430 */
5431 if (c == NUL)
5432 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005433#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005434 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5435 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005436 {
5437 /* highlight last char after line */
5438 --col;
5439 --off;
5440 --vcol;
5441 }
5442
Bram Moolenaar1a384422010-07-14 19:53:30 +02005443 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005444 if (wp->w_p_wrap)
5445 v = wp->w_skipcol;
5446 else
5447 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005448
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005449 /* check if line ends before left margin */
5450 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005451 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005452#ifdef FEAT_CONCEAL
5453 /* Get rid of the boguscols now, we want to draw until the right
5454 * edge for 'cursorcolumn'. */
5455 col -= boguscols;
5456 boguscols = 0;
5457#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005458
5459 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005460 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005461
5462 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005463 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5464 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005465 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005466 && lnum != wp->w_cursor.lnum)
5467 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005468# ifdef FEAT_RIGHTLEFT
5469 && !wp->w_p_rl
5470# endif
5471 )
5472 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005473 int rightmost_vcol = 0;
5474 int i;
5475
5476 if (wp->w_p_cuc)
5477 rightmost_vcol = wp->w_virtcol;
5478 if (draw_color_col)
5479 /* determine rightmost colorcolumn to possibly draw */
5480 for (i = 0; color_cols[i] >= 0; ++i)
5481 if (rightmost_vcol < color_cols[i])
5482 rightmost_vcol = color_cols[i];
5483
Bram Moolenaar02631462017-09-22 15:20:32 +02005484 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005485 {
5486 ScreenLines[off] = ' ';
5487#ifdef FEAT_MBYTE
5488 if (enc_utf8)
5489 ScreenLinesUC[off] = 0;
5490#endif
5491 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005492 if (draw_color_col)
5493 draw_color_col = advance_color_col(VCOL_HLC,
5494 &color_cols);
5495
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005496 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005497 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005498 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005499 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005500 else
5501 ScreenAttrs[off++] = 0;
5502
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005503 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005504 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005505
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005506 ++vcol;
5507 }
5508 }
5509#endif
5510
Bram Moolenaar53f81742017-09-22 14:35:51 +02005511 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005512 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 row++;
5514
5515 /*
5516 * Update w_cline_height and w_cline_folded if the cursor line was
5517 * updated (saves a call to plines() later).
5518 */
5519 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5520 {
5521 curwin->w_cline_row = startrow;
5522 curwin->w_cline_height = row - startrow;
5523#ifdef FEAT_FOLDING
5524 curwin->w_cline_folded = FALSE;
5525#endif
5526 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5527 }
5528
5529 break;
5530 }
5531
5532 /* line continues beyond line end */
5533 if (lcs_ext
5534 && !wp->w_p_wrap
5535#ifdef FEAT_DIFF
5536 && filler_todo <= 0
5537#endif
5538 && (
5539#ifdef FEAT_RIGHTLEFT
5540 wp->w_p_rl ? col == 0 :
5541#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005542 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005544 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5546 {
5547 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005548 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549#ifdef FEAT_MBYTE
5550 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005551 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 {
5553 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005554 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005555 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 }
5557 else
5558 mb_utf8 = FALSE;
5559#endif
5560 }
5561
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005562#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005563 /* advance to the next 'colorcolumn' */
5564 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005565 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005566
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005567 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005568 * highlight the cursor position itself.
5569 * Also highlight the 'colorcolumn' if it is different than
5570 * 'cursorcolumn' */
5571 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005572 if (draw_state == WL_LINE && !lnum_in_visual_area
5573 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005574 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005575 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005576 && lnum != wp->w_cursor.lnum)
5577 {
5578 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005579 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005580 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005581 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005582 {
5583 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005584 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005585 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005586 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005587#endif
5588
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 /*
5590 * Store character to be displayed.
5591 * Skip characters that are left of the screen for 'nowrap'.
5592 */
5593 vcol_prev = vcol;
5594 if (draw_state < WL_LINE || n_skip <= 0)
5595 {
5596 /*
5597 * Store the character.
5598 */
5599#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5600 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5601 {
5602 /* A double-wide character is: put first halve in left cell. */
5603 --off;
5604 --col;
5605 }
5606#endif
5607 ScreenLines[off] = c;
5608#ifdef FEAT_MBYTE
5609 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005610 {
5611 if ((mb_c & 0xff00) == 0x8e00)
5612 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 else if (enc_utf8)
5616 {
5617 if (mb_utf8)
5618 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005619 int i;
5620
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005622 if ((c & 0xff) == 0)
5623 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005624 for (i = 0; i < Screen_mco; ++i)
5625 {
5626 ScreenLinesC[i][off] = u8cc[i];
5627 if (u8cc[i] == 0)
5628 break;
5629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
5631 else
5632 ScreenLinesUC[off] = 0;
5633 }
5634 if (multi_attr)
5635 {
5636 ScreenAttrs[off] = multi_attr;
5637 multi_attr = 0;
5638 }
5639 else
5640#endif
5641 ScreenAttrs[off] = char_attr;
5642
5643#ifdef FEAT_MBYTE
5644 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5645 {
5646 /* Need to fill two screen columns. */
5647 ++off;
5648 ++col;
5649 if (enc_utf8)
5650 /* UTF-8: Put a 0 in the second screen char. */
5651 ScreenLines[off] = 0;
5652 else
5653 /* DBCS: Put second byte in the second screen char. */
5654 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005655 if (draw_state > WL_NR
5656#ifdef FEAT_DIFF
5657 && filler_todo <= 0
5658#endif
5659 )
5660 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 /* When "tocol" is halfway a character, set it to the end of
5662 * the character, otherwise highlighting won't stop. */
5663 if (tocol == vcol)
5664 ++tocol;
5665#ifdef FEAT_RIGHTLEFT
5666 if (wp->w_p_rl)
5667 {
5668 /* now it's time to backup one cell */
5669 --off;
5670 --col;
5671 }
5672#endif
5673 }
5674#endif
5675#ifdef FEAT_RIGHTLEFT
5676 if (wp->w_p_rl)
5677 {
5678 --off;
5679 --col;
5680 }
5681 else
5682#endif
5683 {
5684 ++off;
5685 ++col;
5686 }
5687 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005688#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005689 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005690 {
5691 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005692 ++vcol_off;
5693 if (n_extra > 0)
5694 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005695 if (wp->w_p_wrap)
5696 {
5697 /*
5698 * Special voodoo required if 'wrap' is on.
5699 *
5700 * Advance the column indicator to force the line
5701 * drawing to wrap early. This will make the line
5702 * take up the same screen space when parts are concealed,
5703 * so that cursor line computations aren't messed up.
5704 *
5705 * To avoid the fictitious advance of 'col' causing
5706 * trailing junk to be written out of the screen line
5707 * we are building, 'boguscols' keeps track of the number
5708 * of bad columns we have advanced.
5709 */
5710 if (n_extra > 0)
5711 {
5712 vcol += n_extra;
5713# ifdef FEAT_RIGHTLEFT
5714 if (wp->w_p_rl)
5715 {
5716 col -= n_extra;
5717 boguscols -= n_extra;
5718 }
5719 else
5720# endif
5721 {
5722 col += n_extra;
5723 boguscols += n_extra;
5724 }
5725 n_extra = 0;
5726 n_attr = 0;
5727 }
5728
5729
5730# ifdef FEAT_MBYTE
5731 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5732 {
5733 /* Need to fill two screen columns. */
5734# ifdef FEAT_RIGHTLEFT
5735 if (wp->w_p_rl)
5736 {
5737 --boguscols;
5738 --col;
5739 }
5740 else
5741# endif
5742 {
5743 ++boguscols;
5744 ++col;
5745 }
5746 }
5747# endif
5748
5749# ifdef FEAT_RIGHTLEFT
5750 if (wp->w_p_rl)
5751 {
5752 --boguscols;
5753 --col;
5754 }
5755 else
5756# endif
5757 {
5758 ++boguscols;
5759 ++col;
5760 }
5761 }
5762 else
5763 {
5764 if (n_extra > 0)
5765 {
5766 vcol += n_extra;
5767 n_extra = 0;
5768 n_attr = 0;
5769 }
5770 }
5771
5772 }
5773#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 else
5775 --n_skip;
5776
Bram Moolenaar64486672010-05-16 15:46:46 +02005777 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5778 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005779 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780#ifdef FEAT_DIFF
5781 && filler_todo <= 0
5782#endif
5783 )
5784 ++vcol;
5785
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005786#ifdef FEAT_SYN_HL
5787 if (vcol_save_attr >= 0)
5788 char_attr = vcol_save_attr;
5789#endif
5790
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 /* restore attributes after "predeces" in 'listchars' */
5792 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5793 char_attr = saved_attr3;
5794
5795 /* restore attributes after last 'listchars' or 'number' char */
5796 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5797 char_attr = saved_attr2;
5798
5799 /*
5800 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005801 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 */
5803 if ((
5804#ifdef FEAT_RIGHTLEFT
5805 wp->w_p_rl ? (col < 0) :
5806#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005807 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 && (*ptr != NUL
5809#ifdef FEAT_DIFF
5810 || filler_todo > 0
5811#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005812 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5814 )
5815 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005816#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005817 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005818 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005819 boguscols = 0;
5820#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005821 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005822 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 ++row;
5825 ++screen_row;
5826
5827 /* When not wrapping and finished diff lines, or when displayed
5828 * '$' and highlighting until last column, break here. */
5829 if ((!wp->w_p_wrap
5830#ifdef FEAT_DIFF
5831 && filler_todo <= 0
5832#endif
5833 ) || lcs_eol_one == -1)
5834 break;
5835
5836 /* When the window is too narrow draw all "@" lines. */
5837 if (draw_state != WL_LINE
5838#ifdef FEAT_DIFF
5839 && filler_todo <= 0
5840#endif
5841 )
5842 {
5843 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 row = endrow;
5846 }
5847
5848 /* When line got too long for screen break here. */
5849 if (row == endrow)
5850 {
5851 ++row;
5852 break;
5853 }
5854
5855 if (screen_cur_row == screen_row - 1
5856#ifdef FEAT_DIFF
5857 && filler_todo <= 0
5858#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005859 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 {
5861 /* Remember that the line wraps, used for modeless copy. */
5862 LineWraps[screen_row - 1] = TRUE;
5863
5864 /*
5865 * Special trick to make copy/paste of wrapped lines work with
5866 * xterm/screen: write an extra character beyond the end of
5867 * the line. This will work with all terminal types
5868 * (regardless of the xn,am settings).
5869 * Only do this on a fast tty.
5870 * Only do this if the cursor is on the current line
5871 * (something has been written in it).
5872 * Don't do this for the GUI.
5873 * Don't do this for double-width characters.
5874 * Don't do this for a window not at the right screen border.
5875 */
5876 if (p_tf
5877#ifdef FEAT_GUI
5878 && !gui.in_use
5879#endif
5880#ifdef FEAT_MBYTE
5881 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005882 && ((*mb_off2cells)(LineOffset[screen_row],
5883 LineOffset[screen_row] + screen_Columns)
5884 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005886 + (int)Columns - 2,
5887 LineOffset[screen_row] + screen_Columns)
5888 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889#endif
5890 )
5891 {
5892 /* First make sure we are at the end of the screen line,
5893 * then output the same character again to let the
5894 * terminal know about the wrap. If the terminal doesn't
5895 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005896 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 screen_char(LineOffset[screen_row - 1]
5898 + (unsigned)Columns - 1,
5899 screen_row - 1, (int)(Columns - 1));
5900
5901#ifdef FEAT_MBYTE
5902 /* When there is a multi-byte character, just output a
5903 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005904 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5905 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 out_char(' ');
5907 else
5908#endif
5909 out_char(ScreenLines[LineOffset[screen_row - 1]
5910 + (Columns - 1)]);
5911 /* force a redraw of the first char on the next line */
5912 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5913 screen_start(); /* don't know where cursor is now */
5914 }
5915 }
5916
5917 col = 0;
5918 off = (unsigned)(current_ScreenLine - ScreenLines);
5919#ifdef FEAT_RIGHTLEFT
5920 if (wp->w_p_rl)
5921 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005922 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 off += col;
5924 }
5925#endif
5926
5927 /* reset the drawing state for the start of a wrapped line */
5928 draw_state = WL_START;
5929 saved_n_extra = n_extra;
5930 saved_p_extra = p_extra;
5931 saved_c_extra = c_extra;
5932 saved_char_attr = char_attr;
5933 n_extra = 0;
5934 lcs_prec_todo = lcs_prec;
5935#ifdef FEAT_LINEBREAK
5936# ifdef FEAT_DIFF
5937 if (filler_todo <= 0)
5938# endif
5939 need_showbreak = TRUE;
5940#endif
5941#ifdef FEAT_DIFF
5942 --filler_todo;
5943 /* When the filler lines are actually below the last line of the
5944 * file, don't draw the line itself, break here. */
5945 if (filler_todo == 0 && wp->w_botfill)
5946 break;
5947#endif
5948 }
5949
5950 } /* for every character in the line */
5951
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005952#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005953 /* After an empty line check first word for capital. */
5954 if (*skipwhite(line) == NUL)
5955 {
5956 capcol_lnum = lnum + 1;
5957 cap_col = 0;
5958 }
5959#endif
5960
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005961 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 return row;
5963}
5964
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005965#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005966static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005967
5968/*
5969 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005970 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005971 */
5972 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005973comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005974{
5975 int i;
5976
5977 for (i = 0; i < Screen_mco; ++i)
5978 {
5979 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5980 return TRUE;
5981 if (ScreenLinesC[i][off_from] == 0)
5982 break;
5983 }
5984 return FALSE;
5985}
5986#endif
5987
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988/*
5989 * Check whether the given character needs redrawing:
5990 * - the (first byte of the) character is different
5991 * - the attributes are different
5992 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005993 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 */
5995 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005996char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
5998 if (cols > 0
5999 && ((ScreenLines[off_from] != ScreenLines[off_to]
6000 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6001
6002#ifdef FEAT_MBYTE
6003 || (enc_dbcs != 0
6004 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6005 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6006 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6007 : (cols > 1 && ScreenLines[off_from + 1]
6008 != ScreenLines[off_to + 1])))
6009 || (enc_utf8
6010 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6011 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006012 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006013 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6014 && ScreenLines[off_from + 1]
6015 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016#endif
6017 ))
6018 return TRUE;
6019 return FALSE;
6020}
6021
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006022#if defined(FEAT_TERMINAL) || defined(PROTO)
6023/*
6024 * Return the index in ScreenLines[] for the current screen line.
6025 */
6026 int
6027screen_get_current_line_off()
6028{
6029 return (int)(current_ScreenLine - ScreenLines);
6030}
6031#endif
6032
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033/*
6034 * Move one "cooked" screen line to the screen, but only the characters that
6035 * have actually changed. Handle insert/delete character.
6036 * "coloff" gives the first column on the screen for this line.
6037 * "endcol" gives the columns where valid characters are.
6038 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6039 * needs to be cleared, negative otherwise.
6040 * "rlflag" is TRUE in a rightleft window:
6041 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6042 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6043 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006044 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006045screen_line(
6046 int row,
6047 int coloff,
6048 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006049 int clear_width,
6050 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
6052 unsigned off_from;
6053 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006054#ifdef FEAT_MBYTE
6055 unsigned max_off_from;
6056 unsigned max_off_to;
6057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 int force = FALSE; /* force update rest of the line */
6061 int redraw_this /* bool: does character need redraw? */
6062#ifdef FEAT_GUI
6063 = TRUE /* For GUI when while-loop empty */
6064#endif
6065 ;
6066 int redraw_next; /* redraw_this for next character */
6067#ifdef FEAT_MBYTE
6068 int clear_next = FALSE;
6069 int char_cells; /* 1: normal char */
6070 /* 2: occupies two display cells */
6071# define CHAR_CELLS char_cells
6072#else
6073# define CHAR_CELLS 1
6074#endif
6075
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006076 /* Check for illegal row and col, just in case. */
6077 if (row >= Rows)
6078 row = Rows - 1;
6079 if (endcol > Columns)
6080 endcol = Columns;
6081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082# ifdef FEAT_CLIPBOARD
6083 clip_may_clear_selection(row, row);
6084# endif
6085
6086 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6087 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006088#ifdef FEAT_MBYTE
6089 max_off_from = off_from + screen_Columns;
6090 max_off_to = LineOffset[row] + screen_Columns;
6091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092
6093#ifdef FEAT_RIGHTLEFT
6094 if (rlflag)
6095 {
6096 /* Clear rest first, because it's left of the text. */
6097 if (clear_width > 0)
6098 {
6099 while (col <= endcol && ScreenLines[off_to] == ' '
6100 && ScreenAttrs[off_to] == 0
6101# ifdef FEAT_MBYTE
6102 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6103# endif
6104 )
6105 {
6106 ++off_to;
6107 ++col;
6108 }
6109 if (col <= endcol)
6110 screen_fill(row, row + 1, col + coloff,
6111 endcol + coloff + 1, ' ', ' ', 0);
6112 }
6113 col = endcol + 1;
6114 off_to = LineOffset[row] + col + coloff;
6115 off_from += col;
6116 endcol = (clear_width > 0 ? clear_width : -clear_width);
6117 }
6118#endif /* FEAT_RIGHTLEFT */
6119
6120 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6121
6122 while (col < endcol)
6123 {
6124#ifdef FEAT_MBYTE
6125 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006126 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 else
6128 char_cells = 1;
6129#endif
6130
6131 redraw_this = redraw_next;
6132 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6133 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6134
6135#ifdef FEAT_GUI
6136 /* If the next character was bold, then redraw the current character to
6137 * remove any pixels that might have spilt over into us. This only
6138 * happens in the GUI.
6139 */
6140 if (redraw_next && gui.in_use)
6141 {
6142 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006143 if (hl > HL_ALL)
6144 hl = syn_attr2attr(hl);
6145 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 redraw_this = TRUE;
6147 }
6148#endif
6149
6150 if (redraw_this)
6151 {
6152 /*
6153 * Special handling when 'xs' termcap flag set (hpterm):
6154 * Attributes for characters are stored at the position where the
6155 * cursor is when writing the highlighting code. The
6156 * start-highlighting code must be written with the cursor on the
6157 * first highlighted character. The stop-highlighting code must
6158 * be written with the cursor just after the last highlighted
6159 * character.
6160 * Overwriting a character doesn't remove it's highlighting. Need
6161 * to clear the rest of the line, and force redrawing it
6162 * completely.
6163 */
6164 if ( p_wiv
6165 && !force
6166#ifdef FEAT_GUI
6167 && !gui.in_use
6168#endif
6169 && ScreenAttrs[off_to] != 0
6170 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6171 {
6172 /*
6173 * Need to remove highlighting attributes here.
6174 */
6175 windgoto(row, col + coloff);
6176 out_str(T_CE); /* clear rest of this screen line */
6177 screen_start(); /* don't know where cursor is now */
6178 force = TRUE; /* force redraw of rest of the line */
6179 redraw_next = TRUE; /* or else next char would miss out */
6180
6181 /*
6182 * If the previous character was highlighted, need to stop
6183 * highlighting at this character.
6184 */
6185 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6186 {
6187 screen_attr = ScreenAttrs[off_to - 1];
6188 term_windgoto(row, col + coloff);
6189 screen_stop_highlight();
6190 }
6191 else
6192 screen_attr = 0; /* highlighting has stopped */
6193 }
6194#ifdef FEAT_MBYTE
6195 if (enc_dbcs != 0)
6196 {
6197 /* Check if overwriting a double-byte with a single-byte or
6198 * the other way around requires another character to be
6199 * redrawn. For UTF-8 this isn't needed, because comparing
6200 * ScreenLinesUC[] is sufficient. */
6201 if (char_cells == 1
6202 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006203 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 {
6205 /* Writing a single-cell character over a double-cell
6206 * character: need to redraw the next cell. */
6207 ScreenLines[off_to + 1] = 0;
6208 redraw_next = TRUE;
6209 }
6210 else if (char_cells == 2
6211 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006212 && (*mb_off2cells)(off_to, max_off_to) == 1
6213 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
6215 /* Writing the second half of a double-cell character over
6216 * a double-cell character: need to redraw the second
6217 * cell. */
6218 ScreenLines[off_to + 2] = 0;
6219 redraw_next = TRUE;
6220 }
6221
6222 if (enc_dbcs == DBCS_JPNU)
6223 ScreenLines2[off_to] = ScreenLines2[off_from];
6224 }
6225 /* When writing a single-width character over a double-width
6226 * character and at the end of the redrawn text, need to clear out
6227 * the right halve of the old character.
6228 * Also required when writing the right halve of a double-width
6229 * char over the left halve of an existing one. */
6230 if (has_mbyte && col + char_cells == endcol
6231 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006232 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006234 && (*mb_off2cells)(off_to, max_off_to) == 1
6235 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 clear_next = TRUE;
6237#endif
6238
6239 ScreenLines[off_to] = ScreenLines[off_from];
6240#ifdef FEAT_MBYTE
6241 if (enc_utf8)
6242 {
6243 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6244 if (ScreenLinesUC[off_from] != 0)
6245 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006246 int i;
6247
6248 for (i = 0; i < Screen_mco; ++i)
6249 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251 }
6252 if (char_cells == 2)
6253 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6254#endif
6255
6256#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006257 /* The bold trick makes a single column of pixels appear in the
6258 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 * character should be redrawn too. This happens for our own GUI
6260 * and for some xterms. */
6261 if (
6262# ifdef FEAT_GUI
6263 gui.in_use
6264# endif
6265# if defined(FEAT_GUI) && defined(UNIX)
6266 ||
6267# endif
6268# ifdef UNIX
6269 term_is_xterm
6270# endif
6271 )
6272 {
6273 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006274 if (hl > HL_ALL)
6275 hl = syn_attr2attr(hl);
6276 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 redraw_next = TRUE;
6278 }
6279#endif
6280 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6281#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006282 /* For simplicity set the attributes of second half of a
6283 * double-wide character equal to the first half. */
6284 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006286
6287 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 else
6290#endif
6291 screen_char(off_to, row, col + coloff);
6292 }
6293 else if ( p_wiv
6294#ifdef FEAT_GUI
6295 && !gui.in_use
6296#endif
6297 && col + coloff > 0)
6298 {
6299 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6300 {
6301 /*
6302 * Don't output stop-highlight when moving the cursor, it will
6303 * stop the highlighting when it should continue.
6304 */
6305 screen_attr = 0;
6306 }
6307 else if (screen_attr != 0)
6308 screen_stop_highlight();
6309 }
6310
6311 off_to += CHAR_CELLS;
6312 off_from += CHAR_CELLS;
6313 col += CHAR_CELLS;
6314 }
6315
6316#ifdef FEAT_MBYTE
6317 if (clear_next)
6318 {
6319 /* Clear the second half of a double-wide character of which the left
6320 * half was overwritten with a single-wide character. */
6321 ScreenLines[off_to] = ' ';
6322 if (enc_utf8)
6323 ScreenLinesUC[off_to] = 0;
6324 screen_char(off_to, row, col + coloff);
6325 }
6326#endif
6327
6328 if (clear_width > 0
6329#ifdef FEAT_RIGHTLEFT
6330 && !rlflag
6331#endif
6332 )
6333 {
6334#ifdef FEAT_GUI
6335 int startCol = col;
6336#endif
6337
6338 /* blank out the rest of the line */
6339 while (col < clear_width && ScreenLines[off_to] == ' '
6340 && ScreenAttrs[off_to] == 0
6341#ifdef FEAT_MBYTE
6342 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6343#endif
6344 )
6345 {
6346 ++off_to;
6347 ++col;
6348 }
6349 if (col < clear_width)
6350 {
6351#ifdef FEAT_GUI
6352 /*
6353 * In the GUI, clearing the rest of the line may leave pixels
6354 * behind if the first character cleared was bold. Some bold
6355 * fonts spill over the left. In this case we redraw the previous
6356 * character too. If we didn't skip any blanks above, then we
6357 * only redraw if the character wasn't already redrawn anyway.
6358 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006359 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 {
6361 hl = ScreenAttrs[off_to];
6362 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006363 {
6364 int prev_cells = 1;
6365# ifdef FEAT_MBYTE
6366 if (enc_utf8)
6367 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6368 * that its width is 2. */
6369 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6370 else if (enc_dbcs != 0)
6371 {
6372 /* find previous character by counting from first
6373 * column and get its width. */
6374 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006375 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006376
6377 while (off < off_to)
6378 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006379 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006380 off += prev_cells;
6381 }
6382 }
6383
6384 if (enc_dbcs != 0 && prev_cells > 1)
6385 screen_char_2(off_to - prev_cells, row,
6386 col + coloff - prev_cells);
6387 else
6388# endif
6389 screen_char(off_to - prev_cells, row,
6390 col + coloff - prev_cells);
6391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 }
6393#endif
6394 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6395 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 off_to += clear_width - col;
6397 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399 }
6400
6401 if (clear_width > 0)
6402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 /* For a window that's left of another, draw the separator char. */
6404 if (col + coloff < Columns)
6405 {
6406 int c;
6407
6408 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006409 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006410#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006411 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6412 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 || ScreenAttrs[off_to] != hl)
6415 {
6416 ScreenLines[off_to] = c;
6417 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006418#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 if (enc_utf8)
6420 {
6421 if (c >= 0x80)
6422 {
6423 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006424 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 }
6426 else
6427 ScreenLinesUC[off_to] = 0;
6428 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 screen_char(off_to, row, col + coloff);
6431 }
6432 }
6433 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 LineWraps[row] = FALSE;
6435 }
6436}
6437
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006438#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006440 * Mirror text "str" for right-left displaying.
6441 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006443 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006444rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
6446 char_u *p1, *p2;
6447 int t;
6448
6449 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6450 {
6451 t = *p1;
6452 *p1 = *p2;
6453 *p2 = t;
6454 }
6455}
6456#endif
6457
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458/*
6459 * mark all status lines for redraw; used after first :cd
6460 */
6461 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006462status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
6464 win_T *wp;
6465
Bram Moolenaar29323592016-07-24 22:04:11 +02006466 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (wp->w_status_height)
6468 {
6469 wp->w_redr_status = TRUE;
6470 redraw_later(VALID);
6471 }
6472}
6473
6474/*
6475 * mark all status lines of the current buffer for redraw
6476 */
6477 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006478status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
6480 win_T *wp;
6481
Bram Moolenaar29323592016-07-24 22:04:11 +02006482 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6484 {
6485 wp->w_redr_status = TRUE;
6486 redraw_later(VALID);
6487 }
6488}
6489
6490/*
6491 * Redraw all status lines that need to be redrawn.
6492 */
6493 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006494redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495{
6496 win_T *wp;
6497
Bram Moolenaar29323592016-07-24 22:04:11 +02006498 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 if (wp->w_redr_status)
6500 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006501 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006502 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
Bram Moolenaar4033c552017-09-16 20:54:51 +02006505#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506/*
6507 * Redraw all status lines at the bottom of frame "frp".
6508 */
6509 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006510win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511{
6512 if (frp->fr_layout == FR_LEAF)
6513 frp->fr_win->w_redr_status = TRUE;
6514 else if (frp->fr_layout == FR_ROW)
6515 {
6516 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6517 win_redraw_last_status(frp);
6518 }
6519 else /* frp->fr_layout == FR_COL */
6520 {
6521 frp = frp->fr_child;
6522 while (frp->fr_next != NULL)
6523 frp = frp->fr_next;
6524 win_redraw_last_status(frp);
6525 }
6526}
6527#endif
6528
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529/*
6530 * Draw the verticap separator right of window "wp" starting with line "row".
6531 */
6532 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006533draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534{
6535 int hl;
6536 int c;
6537
6538 if (wp->w_vsep_width)
6539 {
6540 /* draw the vertical separator right of this window */
6541 c = fillchar_vsep(&hl);
6542 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6543 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6544 c, ' ', hl);
6545 }
6546}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547
6548#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006549static int status_match_len(expand_T *xp, char_u *s);
6550static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551
6552/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006553 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 */
6555 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006556status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
6558 int len = 0;
6559
6560#ifdef FEAT_MENU
6561 int emenu = (xp->xp_context == EXPAND_MENUS
6562 || xp->xp_context == EXPAND_MENUNAMES);
6563
6564 /* Check for menu separators - replace with '|'. */
6565 if (emenu && menu_is_separator(s))
6566 return 1;
6567#endif
6568
6569 while (*s != NUL)
6570 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006571 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006572 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006573 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 }
6575
6576 return len;
6577}
6578
6579/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006580 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006581 * These are backslashes used for escaping. Do show backslashes in help tags.
6582 */
6583 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006584skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006585{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006586 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006587#ifdef FEAT_MENU
6588 || ((xp->xp_context == EXPAND_MENUS
6589 || xp->xp_context == EXPAND_MENUNAMES)
6590 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6591#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006592 )
6593 {
6594#ifndef BACKSLASH_IN_FILENAME
6595 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6596 return 2;
6597#endif
6598 return 1;
6599 }
6600 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006601}
6602
6603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 * Show wildchar matches in the status line.
6605 * Show at least the "match" item.
6606 * We start at item 'first_match' in the list and show all matches that fit.
6607 *
6608 * If inversion is possible we use it. Else '=' characters are used.
6609 */
6610 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006611win_redr_status_matches(
6612 expand_T *xp,
6613 int num_matches,
6614 char_u **matches, /* list of matches */
6615 int match,
6616 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617{
6618#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6619 int row;
6620 char_u *buf;
6621 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006622 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 int fillchar;
6624 int attr;
6625 int i;
6626 int highlight = TRUE;
6627 char_u *selstart = NULL;
6628 int selstart_col = 0;
6629 char_u *selend = NULL;
6630 static int first_match = 0;
6631 int add_left = FALSE;
6632 char_u *s;
6633#ifdef FEAT_MENU
6634 int emenu;
6635#endif
6636#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6637 int l;
6638#endif
6639
6640 if (matches == NULL) /* interrupted completion? */
6641 return;
6642
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006643#ifdef FEAT_MBYTE
6644 if (has_mbyte)
6645 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6646 else
6647#endif
6648 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 if (buf == NULL)
6650 return;
6651
6652 if (match == -1) /* don't show match but original text */
6653 {
6654 match = 0;
6655 highlight = FALSE;
6656 }
6657 /* count 1 for the ending ">" */
6658 clen = status_match_len(xp, L_MATCH(match)) + 3;
6659 if (match == 0)
6660 first_match = 0;
6661 else if (match < first_match)
6662 {
6663 /* jumping left, as far as we can go */
6664 first_match = match;
6665 add_left = TRUE;
6666 }
6667 else
6668 {
6669 /* check if match fits on the screen */
6670 for (i = first_match; i < match; ++i)
6671 clen += status_match_len(xp, L_MATCH(i)) + 2;
6672 if (first_match > 0)
6673 clen += 2;
6674 /* jumping right, put match at the left */
6675 if ((long)clen > Columns)
6676 {
6677 first_match = match;
6678 /* if showing the last match, we can add some on the left */
6679 clen = 2;
6680 for (i = match; i < num_matches; ++i)
6681 {
6682 clen += status_match_len(xp, L_MATCH(i)) + 2;
6683 if ((long)clen >= Columns)
6684 break;
6685 }
6686 if (i == num_matches)
6687 add_left = TRUE;
6688 }
6689 }
6690 if (add_left)
6691 while (first_match > 0)
6692 {
6693 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6694 if ((long)clen >= Columns)
6695 break;
6696 --first_match;
6697 }
6698
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006699 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
6701 if (first_match == 0)
6702 {
6703 *buf = NUL;
6704 len = 0;
6705 }
6706 else
6707 {
6708 STRCPY(buf, "< ");
6709 len = 2;
6710 }
6711 clen = len;
6712
6713 i = first_match;
6714 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6715 {
6716 if (i == match)
6717 {
6718 selstart = buf + len;
6719 selstart_col = clen;
6720 }
6721
6722 s = L_MATCH(i);
6723 /* Check for menu separators - replace with '|' */
6724#ifdef FEAT_MENU
6725 emenu = (xp->xp_context == EXPAND_MENUS
6726 || xp->xp_context == EXPAND_MENUNAMES);
6727 if (emenu && menu_is_separator(s))
6728 {
6729 STRCPY(buf + len, transchar('|'));
6730 l = (int)STRLEN(buf + len);
6731 len += l;
6732 clen += l;
6733 }
6734 else
6735#endif
6736 for ( ; *s != NUL; ++s)
6737 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006738 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 clen += ptr2cells(s);
6740#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006741 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {
6743 STRNCPY(buf + len, s, l);
6744 s += l - 1;
6745 len += l;
6746 }
6747 else
6748#endif
6749 {
6750 STRCPY(buf + len, transchar_byte(*s));
6751 len += (int)STRLEN(buf + len);
6752 }
6753 }
6754 if (i == match)
6755 selend = buf + len;
6756
6757 *(buf + len++) = ' ';
6758 *(buf + len++) = ' ';
6759 clen += 2;
6760 if (++i == num_matches)
6761 break;
6762 }
6763
6764 if (i != num_matches)
6765 {
6766 *(buf + len++) = '>';
6767 ++clen;
6768 }
6769
6770 buf[len] = NUL;
6771
6772 row = cmdline_row - 1;
6773 if (row >= 0)
6774 {
6775 if (wild_menu_showing == 0)
6776 {
6777 if (msg_scrolled > 0)
6778 {
6779 /* Put the wildmenu just above the command line. If there is
6780 * no room, scroll the screen one line up. */
6781 if (cmdline_row == Rows - 1)
6782 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006783 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 ++msg_scrolled;
6785 }
6786 else
6787 {
6788 ++cmdline_row;
6789 ++row;
6790 }
6791 wild_menu_showing = WM_SCROLLED;
6792 }
6793 else
6794 {
6795 /* Create status line if needed by setting 'laststatus' to 2.
6796 * Set 'winminheight' to zero to avoid that the window is
6797 * resized. */
6798 if (lastwin->w_status_height == 0)
6799 {
6800 save_p_ls = p_ls;
6801 save_p_wmh = p_wmh;
6802 p_ls = 2;
6803 p_wmh = 0;
6804 last_status(FALSE);
6805 }
6806 wild_menu_showing = WM_SHOWN;
6807 }
6808 }
6809
6810 screen_puts(buf, row, 0, attr);
6811 if (selstart != NULL && highlight)
6812 {
6813 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006814 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816
6817 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6818 }
6819
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 vim_free(buf);
6822}
6823#endif
6824
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825/*
6826 * Redraw the status line of window wp.
6827 *
6828 * If inversion is possible we use it. Else '=' characters are used.
6829 */
6830 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006831win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832{
6833 int row;
6834 char_u *p;
6835 int len;
6836 int fillchar;
6837 int attr;
6838 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006839 static int busy = FALSE;
6840
6841 /* It's possible to get here recursively when 'statusline' (indirectly)
6842 * invokes ":redrawstatus". Simply ignore the call then. */
6843 if (busy)
6844 return;
6845 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846
6847 wp->w_redr_status = FALSE;
6848 if (wp->w_status_height == 0)
6849 {
6850 /* no status line, can only be last window */
6851 redraw_cmdline = TRUE;
6852 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006853 else if (!redrawing()
6854#ifdef FEAT_INS_EXPAND
6855 /* don't update status line when popup menu is visible and may be
6856 * drawn over it */
6857 || pum_visible()
6858#endif
6859 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {
6861 /* Don't redraw right now, do it later. */
6862 wp->w_redr_status = TRUE;
6863 }
6864#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006865 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {
6867 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006868 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 }
6870#endif
6871 else
6872 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006873 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006875 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 p = NameBuff;
6877 len = (int)STRLEN(p);
6878
Bram Moolenaard85f2712017-07-28 21:51:57 +02006879 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880#ifdef FEAT_QUICKFIX
6881 || wp->w_p_pvw
6882#endif
6883 || bufIsChanged(wp->w_buffer)
6884 || wp->w_buffer->b_p_ro)
6885 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006886 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006888 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 len += (int)STRLEN(p + len);
6890 }
6891#ifdef FEAT_QUICKFIX
6892 if (wp->w_p_pvw)
6893 {
6894 STRCPY(p + len, _("[Preview]"));
6895 len += (int)STRLEN(p + len);
6896 }
6897#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006898 if (bufIsChanged(wp->w_buffer)
6899#ifdef FEAT_TERMINAL
6900 && !bt_terminal(wp->w_buffer)
6901#endif
6902 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 {
6904 STRCPY(p + len, "[+]");
6905 len += 3;
6906 }
6907 if (wp->w_buffer->b_p_ro)
6908 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006909 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006910 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 }
6912
Bram Moolenaar02631462017-09-22 15:20:32 +02006913 this_ru_col = ru_col - (Columns - wp->w_width);
6914 if (this_ru_col < (wp->w_width + 1) / 2)
6915 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 if (this_ru_col <= 1)
6917 {
6918 p = (char_u *)"<"; /* No room for file name! */
6919 len = 1;
6920 }
6921 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922#ifdef FEAT_MBYTE
6923 if (has_mbyte)
6924 {
6925 int clen = 0, i;
6926
6927 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006928 clen = mb_string2cells(p, -1);
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 /* Find first character that will fit.
6931 * Going from start to end is much faster for DBCS. */
6932 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006933 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 clen -= (*mb_ptr2cells)(p + i);
6935 len = clen;
6936 if (i > 0)
6937 {
6938 p = p + i - 1;
6939 *p = '<';
6940 ++len;
6941 }
6942
6943 }
6944 else
6945#endif
6946 if (len > this_ru_col - 1)
6947 {
6948 p += len - (this_ru_col - 1);
6949 *p = '<';
6950 len = this_ru_col - 1;
6951 }
6952
6953 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006954 screen_puts(p, row, wp->w_wincol, attr);
6955 screen_fill(row, row + 1, len + wp->w_wincol,
6956 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006958 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6960 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006961 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962
6963#ifdef FEAT_CMDL_INFO
6964 win_redr_ruler(wp, TRUE);
6965#endif
6966 }
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 /*
6969 * May need to draw the character below the vertical separator.
6970 */
6971 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6972 {
6973 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006974 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 else
6976 fillchar = fillchar_vsep(&attr);
6977 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6978 attr);
6979 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006980 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981}
6982
Bram Moolenaar238a5642006-02-21 22:12:05 +00006983#ifdef FEAT_STL_OPT
6984/*
6985 * Redraw the status line according to 'statusline' and take care of any
6986 * errors encountered.
6987 */
6988 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006989redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006990{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006991 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006992 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006993
6994 /* When called recursively return. This can happen when the statusline
6995 * contains an expression that triggers a redraw. */
6996 if (entered)
6997 return;
6998 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006999
Bram Moolenaara742e082016-04-05 21:10:38 +02007000 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007001 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007002 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007003 {
7004 /* When there is an error disable the statusline, otherwise the
7005 * display is messed up with errors and a redraw triggers the problem
7006 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007007 set_string_option_direct((char_u *)"statusline", -1,
7008 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007009 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007010 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007011 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007012 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007013}
7014#endif
7015
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016/*
7017 * Return TRUE if the status line of window "wp" is connected to the status
7018 * line of the window right of it. If not, then it's a vertical separator.
7019 * Only call if (wp->w_vsep_width != 0).
7020 */
7021 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007022stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
7024 frame_T *fr;
7025
7026 fr = wp->w_frame;
7027 while (fr->fr_parent != NULL)
7028 {
7029 if (fr->fr_parent->fr_layout == FR_COL)
7030 {
7031 if (fr->fr_next != NULL)
7032 break;
7033 }
7034 else
7035 {
7036 if (fr->fr_next != NULL)
7037 return TRUE;
7038 }
7039 fr = fr->fr_parent;
7040 }
7041 return FALSE;
7042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045/*
7046 * Get the value to show for the language mappings, active 'keymap'.
7047 */
7048 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007049get_keymap_str(
7050 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007051 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007052 char_u *buf, /* buffer for the result */
7053 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054{
7055 char_u *p;
7056
7057 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7058 return FALSE;
7059
7060 {
7061#ifdef FEAT_EVAL
7062 buf_T *old_curbuf = curbuf;
7063 win_T *old_curwin = curwin;
7064 char_u *s;
7065
7066 curbuf = wp->w_buffer;
7067 curwin = wp;
7068 STRCPY(buf, "b:keymap_name"); /* must be writable */
7069 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007070 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 --emsg_skip;
7072 curbuf = old_curbuf;
7073 curwin = old_curwin;
7074 if (p == NULL || *p == NUL)
7075#endif
7076 {
7077#ifdef FEAT_KEYMAP
7078 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7079 p = wp->w_buffer->b_p_keymap;
7080 else
7081#endif
7082 p = (char_u *)"lang";
7083 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007084 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 buf[0] = NUL;
7086#ifdef FEAT_EVAL
7087 vim_free(s);
7088#endif
7089 }
7090 return buf[0] != NUL;
7091}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
7093#if defined(FEAT_STL_OPT) || defined(PROTO)
7094/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007095 * Redraw the status line or ruler of window "wp".
7096 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 */
7098 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007099win_redr_custom(
7100 win_T *wp,
7101 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007103 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 int attr;
7105 int curattr;
7106 int row;
7107 int col = 0;
7108 int maxwidth;
7109 int width;
7110 int n;
7111 int len;
7112 int fillchar;
7113 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007114 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007116 struct stl_hlrec hltab[STL_MAX_ITEM];
7117 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007118 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007119 win_T *ewp;
7120 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121
Bram Moolenaar1d633412013-12-11 15:52:01 +01007122 /* There is a tiny chance that this gets called recursively: When
7123 * redrawing a status line triggers redrawing the ruler or tabline.
7124 * Avoid trouble by not allowing recursion. */
7125 if (entered)
7126 return;
7127 entered = TRUE;
7128
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007130 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007132 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007133 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007134 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007135 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007136 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007137 maxwidth = Columns;
7138# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007139 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007142 else
7143 {
7144 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007145 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007146 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007147
7148 if (draw_ruler)
7149 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007150 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007151 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007152 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007153 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007154 if (*++stl == '-')
7155 stl++;
7156 if (atoi((char *)stl))
7157 while (VIM_ISDIGIT(*stl))
7158 stl++;
7159 if (*stl++ != '(')
7160 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007161 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007162 col = ru_col - (Columns - wp->w_width);
7163 if (col < (wp->w_width + 1) / 2)
7164 col = (wp->w_width + 1) / 2;
7165 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007166 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007167 {
7168 row = Rows - 1;
7169 --maxwidth; /* writing in last column may cause scrolling */
7170 fillchar = ' ';
7171 attr = 0;
7172 }
7173
7174# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007175 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007176# endif
7177 }
7178 else
7179 {
7180 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007181 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007182 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007183 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007184# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007185 use_sandbox = was_set_insecurely((char_u *)"statusline",
7186 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007187# endif
7188 }
7189
Bram Moolenaar53f81742017-09-22 14:35:51 +02007190 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007191 }
7192
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007194 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195
Bram Moolenaar61452852011-02-01 18:01:11 +01007196 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7197 * the cursor away and back. */
7198 ewp = wp == NULL ? curwin : wp;
7199 p_crb_save = ewp->w_p_crb;
7200 ewp->w_p_crb = FALSE;
7201
Bram Moolenaar362f3562009-11-03 16:20:34 +00007202 /* Make a copy, because the statusline may include a function call that
7203 * might change the option value and free the memory. */
7204 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007205 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007206 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007207 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007208 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007209 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007211 /* Make all characters printable. */
7212 p = transstr(buf);
7213 if (p != NULL)
7214 {
7215 vim_strncpy(buf, p, sizeof(buf) - 1);
7216 vim_free(p);
7217 }
7218
7219 /* fill up with "fillchar" */
7220 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007221 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {
7223#ifdef FEAT_MBYTE
7224 len += (*mb_char2bytes)(fillchar, buf + len);
7225#else
7226 buf[len++] = fillchar;
7227#endif
7228 ++width;
7229 }
7230 buf[len] = NUL;
7231
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007232 /*
7233 * Draw each snippet with the specified highlighting.
7234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 curattr = attr;
7236 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007237 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007239 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 screen_puts_len(p, len, row, col, curattr);
7241 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007242 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007244 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007246 else if (hltab[n].userhl < 0)
7247 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007248#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007249 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7250 && wp->w_status_height != 0)
7251 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007252 else if (wp != NULL && bt_terminal(wp->w_buffer)
7253 && wp->w_status_height != 0)
7254 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007255#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007256 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007257 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007259 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007262
7263 if (wp == NULL)
7264 {
7265 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7266 col = 0;
7267 len = 0;
7268 p = buf;
7269 fillchar = 0;
7270 for (n = 0; tabtab[n].start != NULL; n++)
7271 {
7272 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7273 while (col < len)
7274 TabPageIdxs[col++] = fillchar;
7275 p = tabtab[n].start;
7276 fillchar = tabtab[n].userhl;
7277 }
7278 while (col < Columns)
7279 TabPageIdxs[col++] = fillchar;
7280 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007281
7282theend:
7283 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284}
7285
7286#endif /* FEAT_STL_OPT */
7287
7288/*
7289 * Output a single character directly to the screen and update ScreenLines.
7290 */
7291 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007292screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 char_u buf[MB_MAXBYTES + 1];
7295
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007296#ifdef FEAT_MBYTE
7297 if (has_mbyte)
7298 buf[(*mb_char2bytes)(c, buf)] = NUL;
7299 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007301 {
7302 buf[0] = c;
7303 buf[1] = NUL;
7304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 screen_puts(buf, row, col, attr);
7306}
7307
7308/*
7309 * Get a single character directly from ScreenLines into "bytes[]".
7310 * Also return its attribute in *attrp;
7311 */
7312 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007313screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314{
7315 unsigned off;
7316
7317 /* safety check */
7318 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7319 {
7320 off = LineOffset[row] + col;
7321 *attrp = ScreenAttrs[off];
7322 bytes[0] = ScreenLines[off];
7323 bytes[1] = NUL;
7324
7325#ifdef FEAT_MBYTE
7326 if (enc_utf8 && ScreenLinesUC[off] != 0)
7327 bytes[utfc_char2bytes(off, bytes)] = NUL;
7328 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7329 {
7330 bytes[0] = ScreenLines[off];
7331 bytes[1] = ScreenLines2[off];
7332 bytes[2] = NUL;
7333 }
7334 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7335 {
7336 bytes[1] = ScreenLines[off + 1];
7337 bytes[2] = NUL;
7338 }
7339#endif
7340 }
7341}
7342
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007343#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007344static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007345
7346/*
7347 * Return TRUE if composing characters for screen posn "off" differs from
7348 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007349 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007350 */
7351 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007352screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007353{
7354 int i;
7355
7356 for (i = 0; i < Screen_mco; ++i)
7357 {
7358 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7359 return TRUE;
7360 if (u8cc[i] == 0)
7361 break;
7362 }
7363 return FALSE;
7364}
7365#endif
7366
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367/*
7368 * Put string '*text' on the screen at position 'row' and 'col', with
7369 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7370 * Note: only outputs within one row, message is truncated at screen boundary!
7371 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7372 */
7373 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007374screen_puts(
7375 char_u *text,
7376 int row,
7377 int col,
7378 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
7380 screen_puts_len(text, -1, row, col, attr);
7381}
7382
7383/*
7384 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7385 * a NUL.
7386 */
7387 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007388screen_puts_len(
7389 char_u *text,
7390 int textlen,
7391 int row,
7392 int col,
7393 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394{
7395 unsigned off;
7396 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007397 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 int c;
7399#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007400 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 int mbyte_blen = 1;
7402 int mbyte_cells = 1;
7403 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007404 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 int clear_next_cell = FALSE;
7406# ifdef FEAT_ARABIC
7407 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007408 int pc, nc, nc1;
7409 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410# endif
7411#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007412#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7413 int force_redraw_this;
7414 int force_redraw_next = FALSE;
7415#endif
7416 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
7418 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7419 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007420 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421
Bram Moolenaarc236c162008-07-13 17:41:49 +00007422#ifdef FEAT_MBYTE
7423 /* When drawing over the right halve of a double-wide char clear out the
7424 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007425 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007426# ifdef FEAT_GUI
7427 && !gui.in_use
7428# endif
7429 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007430 {
7431 ScreenLines[off - 1] = ' ';
7432 ScreenAttrs[off - 1] = 0;
7433 if (enc_utf8)
7434 {
7435 ScreenLinesUC[off - 1] = 0;
7436 ScreenLinesC[0][off - 1] = 0;
7437 }
7438 /* redraw the previous cell, make it empty */
7439 screen_char(off - 1, row, col - 1);
7440 /* force the cell at "col" to be redrawn */
7441 force_redraw_next = TRUE;
7442 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007443#endif
7444
Bram Moolenaar367329b2007-08-30 11:53:22 +00007445#ifdef FEAT_MBYTE
7446 max_off = LineOffset[row] + screen_Columns;
7447#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007448 while (col < screen_Columns
7449 && (len < 0 || (int)(ptr - text) < len)
7450 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {
7452 c = *ptr;
7453#ifdef FEAT_MBYTE
7454 /* check if this is the first byte of a multibyte */
7455 if (has_mbyte)
7456 {
7457 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007458 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007460 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7462 mbyte_cells = 1;
7463 else if (enc_dbcs != 0)
7464 mbyte_cells = mbyte_blen;
7465 else /* enc_utf8 */
7466 {
7467 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007468 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 (int)((text + len) - ptr));
7470 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007471 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007473# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 /* Non-BMP character: display as ? or fullwidth ?. */
7475 if (u8c >= 0x10000)
7476 {
7477 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7478 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007479 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482# ifdef FEAT_ARABIC
7483 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7484 {
7485 /* Do Arabic shaping. */
7486 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7487 {
7488 /* Past end of string to be displayed. */
7489 nc = NUL;
7490 nc1 = NUL;
7491 }
7492 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007493 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007494 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7495 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007496 nc1 = pcc[0];
7497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 pc = prev_c;
7499 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007500 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 }
7502 else
7503 prev_c = u8c;
7504# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007505 if (col + mbyte_cells > screen_Columns)
7506 {
7507 /* Only 1 cell left, but character requires 2 cells:
7508 * display a '>' in the last column to avoid wrapping. */
7509 c = '>';
7510 mbyte_cells = 1;
7511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 }
7513 }
7514#endif
7515
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007516#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7517 force_redraw_this = force_redraw_next;
7518 force_redraw_next = FALSE;
7519#endif
7520
7521 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522#ifdef FEAT_MBYTE
7523 || (mbyte_cells == 2
7524 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7525 || (enc_dbcs == DBCS_JPNU
7526 && c == 0x8e
7527 && ScreenLines2[off] != ptr[1])
7528 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007529 && (ScreenLinesUC[off] !=
7530 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7531 || (ScreenLinesUC[off] != 0
7532 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533#endif
7534 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007535 || exmode_active;
7536
7537 if (need_redraw
7538#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7539 || force_redraw_this
7540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 )
7542 {
7543#if defined(FEAT_GUI) || defined(UNIX)
7544 /* The bold trick makes a single row of pixels appear in the next
7545 * character. When a bold character is removed, the next
7546 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007547 * and for some xterms. */
7548 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549# ifdef FEAT_GUI
7550 gui.in_use
7551# endif
7552# if defined(FEAT_GUI) && defined(UNIX)
7553 ||
7554# endif
7555# ifdef UNIX
7556 term_is_xterm
7557# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007558 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007560 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007562 if (n > HL_ALL)
7563 n = syn_attr2attr(n);
7564 if (n & HL_BOLD)
7565 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 }
7567#endif
7568#ifdef FEAT_MBYTE
7569 /* When at the end of the text and overwriting a two-cell
7570 * character with a one-cell character, need to clear the next
7571 * cell. Also when overwriting the left halve of a two-cell char
7572 * with the right halve of a two-cell char. Do this only once
7573 * (mb_off2cells() may return 2 on the right halve). */
7574 if (clear_next_cell)
7575 clear_next_cell = FALSE;
7576 else if (has_mbyte
7577 && (len < 0 ? ptr[mbyte_blen] == NUL
7578 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007579 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007581 && (*mb_off2cells)(off, max_off) == 1
7582 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 clear_next_cell = TRUE;
7584
7585 /* Make sure we never leave a second byte of a double-byte behind,
7586 * it confuses mb_off2cells(). */
7587 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007588 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007590 && (*mb_off2cells)(off, max_off) == 1
7591 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 ScreenLines[off + mbyte_blen] = 0;
7593#endif
7594 ScreenLines[off] = c;
7595 ScreenAttrs[off] = attr;
7596#ifdef FEAT_MBYTE
7597 if (enc_utf8)
7598 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007599 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 ScreenLinesUC[off] = 0;
7601 else
7602 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007603 int i;
7604
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007606 for (i = 0; i < Screen_mco; ++i)
7607 {
7608 ScreenLinesC[i][off] = u8cc[i];
7609 if (u8cc[i] == 0)
7610 break;
7611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
7613 if (mbyte_cells == 2)
7614 {
7615 ScreenLines[off + 1] = 0;
7616 ScreenAttrs[off + 1] = attr;
7617 }
7618 screen_char(off, row, col);
7619 }
7620 else if (mbyte_cells == 2)
7621 {
7622 ScreenLines[off + 1] = ptr[1];
7623 ScreenAttrs[off + 1] = attr;
7624 screen_char_2(off, row, col);
7625 }
7626 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7627 {
7628 ScreenLines2[off] = ptr[1];
7629 screen_char(off, row, col);
7630 }
7631 else
7632#endif
7633 screen_char(off, row, col);
7634 }
7635#ifdef FEAT_MBYTE
7636 if (has_mbyte)
7637 {
7638 off += mbyte_cells;
7639 col += mbyte_cells;
7640 ptr += mbyte_blen;
7641 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007642 {
7643 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007645 len = -1;
7646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 }
7648 else
7649#endif
7650 {
7651 ++off;
7652 ++col;
7653 ++ptr;
7654 }
7655 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007656
7657#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7658 /* If we detected the next character needs to be redrawn, but the text
7659 * doesn't extend up to there, update the character here. */
7660 if (force_redraw_next && col < screen_Columns)
7661 {
7662# ifdef FEAT_MBYTE
7663 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7664 screen_char_2(off, row, col);
7665 else
7666# endif
7667 screen_char(off, row, col);
7668 }
7669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670}
7671
7672#ifdef FEAT_SEARCH_EXTRA
7673/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007674 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 */
7676 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007677start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679 if (p_hls && !no_hlsearch)
7680 {
7681 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007682 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007683# ifdef FEAT_RELTIME
7684 /* Set the time limit to 'redrawtime'. */
7685 profile_setlimit(p_rdt, &search_hl.tm);
7686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 }
7688}
7689
7690/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007691 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 */
7693 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007694end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
7696 if (search_hl.rm.regprog != NULL)
7697 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007698 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 search_hl.rm.regprog = NULL;
7700 }
7701}
7702
7703/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007704 * Init for calling prepare_search_hl().
7705 */
7706 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007707init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007708{
7709 matchitem_T *cur;
7710
7711 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7712 * match */
7713 cur = wp->w_match_head;
7714 while (cur != NULL)
7715 {
7716 cur->hl.rm = cur->match;
7717 if (cur->hlg_id == 0)
7718 cur->hl.attr = 0;
7719 else
7720 cur->hl.attr = syn_id2attr(cur->hlg_id);
7721 cur->hl.buf = wp->w_buffer;
7722 cur->hl.lnum = 0;
7723 cur->hl.first_lnum = 0;
7724# ifdef FEAT_RELTIME
7725 /* Set the time limit to 'redrawtime'. */
7726 profile_setlimit(p_rdt, &(cur->hl.tm));
7727# endif
7728 cur = cur->next;
7729 }
7730 search_hl.buf = wp->w_buffer;
7731 search_hl.lnum = 0;
7732 search_hl.first_lnum = 0;
7733 /* time limit is set at the toplevel, for all windows */
7734}
7735
7736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 * Advance to the match in window "wp" line "lnum" or past it.
7738 */
7739 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007740prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007742 matchitem_T *cur; /* points to the match list */
7743 match_T *shl; /* points to search_hl or a match */
7744 int shl_flag; /* flag to indicate whether search_hl
7745 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007746 int pos_inprogress; /* marks that position match search is
7747 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 int n;
7749
7750 /*
7751 * When using a multi-line pattern, start searching at the top
7752 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007753 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007755 cur = wp->w_match_head;
7756 shl_flag = FALSE;
7757 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007759 if (shl_flag == FALSE)
7760 {
7761 shl = &search_hl;
7762 shl_flag = TRUE;
7763 }
7764 else
7765 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 if (shl->rm.regprog != NULL
7767 && shl->lnum == 0
7768 && re_multiline(shl->rm.regprog))
7769 {
7770 if (shl->first_lnum == 0)
7771 {
7772# ifdef FEAT_FOLDING
7773 for (shl->first_lnum = lnum;
7774 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7775 if (hasFoldingWin(wp, shl->first_lnum - 1,
7776 NULL, NULL, TRUE, NULL))
7777 break;
7778# else
7779 shl->first_lnum = wp->w_topline;
7780# endif
7781 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007782 if (cur != NULL)
7783 cur->pos.cur = 0;
7784 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007786 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7787 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007789 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7790 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007791 pos_inprogress = cur == NULL || cur->pos.cur == 0
7792 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 if (shl->lnum != 0)
7794 {
7795 shl->first_lnum = shl->lnum
7796 + shl->rm.endpos[0].lnum
7797 - shl->rm.startpos[0].lnum;
7798 n = shl->rm.endpos[0].col;
7799 }
7800 else
7801 {
7802 ++shl->first_lnum;
7803 n = 0;
7804 }
7805 }
7806 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007807 if (shl != &search_hl && cur != NULL)
7808 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 }
7810}
7811
7812/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007813 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 * Uses shl->buf.
7815 * Sets shl->lnum and shl->rm contents.
7816 * Note: Assumes a previous match is always before "lnum", unless
7817 * shl->lnum is zero.
7818 * Careful: Any pointers for buffer lines will become invalid.
7819 */
7820 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007821next_search_hl(
7822 win_T *win,
7823 match_T *shl, /* points to search_hl or a match */
7824 linenr_T lnum,
7825 colnr_T mincol, /* minimal column for a match */
7826 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
7828 linenr_T l;
7829 colnr_T matchcol;
7830 long nmatched;
7831
7832 if (shl->lnum != 0)
7833 {
7834 /* Check for three situations:
7835 * 1. If the "lnum" is below a previous match, start a new search.
7836 * 2. If the previous match includes "mincol", use it.
7837 * 3. Continue after the previous match.
7838 */
7839 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7840 if (lnum > l)
7841 shl->lnum = 0;
7842 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7843 return;
7844 }
7845
7846 /*
7847 * Repeat searching for a match until one is found that includes "mincol"
7848 * or none is found in this line.
7849 */
7850 called_emsg = FALSE;
7851 for (;;)
7852 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007853#ifdef FEAT_RELTIME
7854 /* Stop searching after passing the time limit. */
7855 if (profile_passed_limit(&(shl->tm)))
7856 {
7857 shl->lnum = 0; /* no match found in time */
7858 break;
7859 }
7860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 /* Three situations:
7862 * 1. No useful previous match: search from start of line.
7863 * 2. Not Vi compatible or empty match: continue at next character.
7864 * Break the loop if this is beyond the end of the line.
7865 * 3. Vi compatible searching: continue at end of previous match.
7866 */
7867 if (shl->lnum == 0)
7868 matchcol = 0;
7869 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7870 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007871 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007873 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007874
7875 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007876 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007877 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007879 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 shl->lnum = 0;
7881 break;
7882 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007883#ifdef FEAT_MBYTE
7884 if (has_mbyte)
7885 matchcol += mb_ptr2len(ml);
7886 else
7887#endif
7888 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890 else
7891 matchcol = shl->rm.endpos[0].col;
7892
7893 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007894 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007896 /* Remember whether shl->rm is using a copy of the regprog in
7897 * cur->match. */
7898 int regprog_is_copy = (shl != &search_hl && cur != NULL
7899 && shl == &cur->hl
7900 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007901 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007902
Bram Moolenaarb3414592014-06-17 17:48:32 +02007903 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7904 matchcol,
7905#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007906 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007907#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007908 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007909#endif
7910 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007911 /* Copy the regprog, in case it got freed and recompiled. */
7912 if (regprog_is_copy)
7913 cur->match.regprog = cur->hl.rm.regprog;
7914
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007915 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007916 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007917 /* Error while handling regexp: stop using this regexp. */
7918 if (shl == &search_hl)
7919 {
7920 /* don't free regprog in the match list, it's a copy */
7921 vim_regfree(shl->rm.regprog);
7922 SET_NO_HLSEARCH(TRUE);
7923 }
7924 shl->rm.regprog = NULL;
7925 shl->lnum = 0;
7926 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7927 message */
7928 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007929 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007930 }
7931 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007932 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007933 else
7934 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 if (nmatched == 0)
7936 {
7937 shl->lnum = 0; /* no match found */
7938 break;
7939 }
7940 if (shl->rm.startpos[0].lnum > 0
7941 || shl->rm.startpos[0].col >= mincol
7942 || nmatched > 1
7943 || shl->rm.endpos[0].col > mincol)
7944 {
7945 shl->lnum += shl->rm.startpos[0].lnum;
7946 break; /* useful match found */
7947 }
7948 }
7949}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950
Bram Moolenaar85077472016-10-16 14:35:48 +02007951/*
7952 * If there is a match fill "shl" and return one.
7953 * Return zero otherwise.
7954 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007955 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007956next_search_hl_pos(
7957 match_T *shl, /* points to a match */
7958 linenr_T lnum,
7959 posmatch_T *posmatch, /* match positions */
7960 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961{
7962 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007963 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007964
Bram Moolenaarb3414592014-06-17 17:48:32 +02007965 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7966 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007967 llpos_T *pos = &posmatch->pos[i];
7968
7969 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007970 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007971 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007972 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007973 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007974 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007975 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007976 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007977 /* if this match comes before the one at "found" then swap
7978 * them */
7979 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007980 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007981 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007982
Bram Moolenaar85077472016-10-16 14:35:48 +02007983 *pos = posmatch->pos[found];
7984 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007985 }
7986 }
7987 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007988 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007989 }
7990 }
7991 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007992 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007993 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007994 colnr_T start = posmatch->pos[found].col == 0
7995 ? 0 : posmatch->pos[found].col - 1;
7996 colnr_T end = posmatch->pos[found].col == 0
7997 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007998
Bram Moolenaar85077472016-10-16 14:35:48 +02007999 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008000 shl->rm.startpos[0].lnum = 0;
8001 shl->rm.startpos[0].col = start;
8002 shl->rm.endpos[0].lnum = 0;
8003 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008004 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008005 posmatch->cur = found + 1;
8006 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008007 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008008 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008009}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008010#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008011
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008013screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014{
8015 attrentry_T *aep = NULL;
8016
8017 screen_attr = attr;
8018 if (full_screen
8019#ifdef WIN3264
8020 && termcap_active
8021#endif
8022 )
8023 {
8024#ifdef FEAT_GUI
8025 if (gui.in_use)
8026 {
8027 char buf[20];
8028
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008029 /* The GUI handles this internally. */
8030 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 OUT_STR(buf);
8032 }
8033 else
8034#endif
8035 {
8036 if (attr > HL_ALL) /* special HL attr. */
8037 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008038 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 aep = syn_cterm_attr2entry(attr);
8040 else
8041 aep = syn_term_attr2entry(attr);
8042 if (aep == NULL) /* did ":syntax clear" */
8043 attr = 0;
8044 else
8045 attr = aep->ae_attr;
8046 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008047 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008049 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008050#ifdef FEAT_TERMGUICOLORS
8051 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008052 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008053#endif
8054 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008055#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008056 )
8057#endif
8058 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008059 /* If the Normal FG color has BOLD attribute and the new HL
8060 * has a FG color defined, clear BOLD. */
8061 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008062 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008064 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008065 out_str(T_UCS);
8066 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008067 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8068 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008070 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008072 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008074 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008075 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076
8077 /*
8078 * Output the color or start string after bold etc., in case the
8079 * bold etc. override the color setting.
8080 */
8081 if (aep != NULL)
8082 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008083#ifdef FEAT_TERMGUICOLORS
8084 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008086 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008087 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008088 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008089 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 }
8091 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008094 if (t_colors > 1)
8095 {
8096 if (aep->ae_u.cterm.fg_color)
8097 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8098 if (aep->ae_u.cterm.bg_color)
8099 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8100 }
8101 else
8102 {
8103 if (aep->ae_u.term.start != NULL)
8104 out_str(aep->ae_u.term.start);
8105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 }
8107 }
8108 }
8109 }
8110}
8111
8112 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008113screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114{
8115 int do_ME = FALSE; /* output T_ME code */
8116
8117 if (screen_attr != 0
8118#ifdef WIN3264
8119 && termcap_active
8120#endif
8121 )
8122 {
8123#ifdef FEAT_GUI
8124 if (gui.in_use)
8125 {
8126 char buf[20];
8127
8128 /* use internal GUI code */
8129 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8130 OUT_STR(buf);
8131 }
8132 else
8133#endif
8134 {
8135 if (screen_attr > HL_ALL) /* special HL attr. */
8136 {
8137 attrentry_T *aep;
8138
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008139 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {
8141 /*
8142 * Assume that t_me restores the original colors!
8143 */
8144 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008145 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008146#ifdef FEAT_TERMGUICOLORS
8147 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008148 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8149 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008150#endif
8151 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008152#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008153 )
8154#endif
8155 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 do_ME = TRUE;
8157 }
8158 else
8159 {
8160 aep = syn_term_attr2entry(screen_attr);
8161 if (aep != NULL && aep->ae_u.term.stop != NULL)
8162 {
8163 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8164 do_ME = TRUE;
8165 else
8166 out_str(aep->ae_u.term.stop);
8167 }
8168 }
8169 if (aep == NULL) /* did ":syntax clear" */
8170 screen_attr = 0;
8171 else
8172 screen_attr = aep->ae_attr;
8173 }
8174
8175 /*
8176 * Often all ending-codes are equal to T_ME. Avoid outputting the
8177 * same sequence several times.
8178 */
8179 if (screen_attr & HL_STANDOUT)
8180 {
8181 if (STRCMP(T_SE, T_ME) == 0)
8182 do_ME = TRUE;
8183 else
8184 out_str(T_SE);
8185 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008186 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008187 {
8188 if (STRCMP(T_UCE, T_ME) == 0)
8189 do_ME = TRUE;
8190 else
8191 out_str(T_UCE);
8192 }
8193 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008194 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {
8196 if (STRCMP(T_UE, T_ME) == 0)
8197 do_ME = TRUE;
8198 else
8199 out_str(T_UE);
8200 }
8201 if (screen_attr & HL_ITALIC)
8202 {
8203 if (STRCMP(T_CZR, T_ME) == 0)
8204 do_ME = TRUE;
8205 else
8206 out_str(T_CZR);
8207 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008208 if (screen_attr & HL_STRIKETHROUGH)
8209 {
8210 if (STRCMP(T_STE, T_ME) == 0)
8211 do_ME = TRUE;
8212 else
8213 out_str(T_STE);
8214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8216 out_str(T_ME);
8217
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008218#ifdef FEAT_TERMGUICOLORS
8219 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008221 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008222 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008223 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008224 term_bg_rgb_color(cterm_normal_bg_gui_color);
8225 }
8226 else
8227#endif
8228 {
8229 if (t_colors > 1)
8230 {
8231 /* set Normal cterm colors */
8232 if (cterm_normal_fg_color != 0)
8233 term_fg_color(cterm_normal_fg_color - 1);
8234 if (cterm_normal_bg_color != 0)
8235 term_bg_color(cterm_normal_bg_color - 1);
8236 if (cterm_normal_fg_bold)
8237 out_str(T_MD);
8238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 }
8240 }
8241 }
8242 screen_attr = 0;
8243}
8244
8245/*
8246 * Reset the colors for a cterm. Used when leaving Vim.
8247 * The machine specific code may override this again.
8248 */
8249 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008250reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008252 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {
8254 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008255#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008256 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8257 || cterm_normal_bg_gui_color != INVALCOLOR)
8258 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008259#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {
8263 out_str(T_OP);
8264 screen_attr = -1;
8265 }
8266 if (cterm_normal_fg_bold)
8267 {
8268 out_str(T_ME);
8269 screen_attr = -1;
8270 }
8271 }
8272}
8273
8274/*
8275 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8276 * using the attributes from ScreenAttrs["off"].
8277 */
8278 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008279screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280{
8281 int attr;
8282
8283 /* Check for illegal values, just in case (could happen just after
8284 * resizing). */
8285 if (row >= screen_Rows || col >= screen_Columns)
8286 return;
8287
Bram Moolenaar494838a2015-02-10 19:20:37 +01008288 /* Outputting a character in the last cell on the screen may scroll the
8289 * screen up. Only do it when the "xn" termcap property is set, otherwise
8290 * mark the character invalid (update it when scrolled up). */
8291 if (*T_XN == NUL
8292 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293#ifdef FEAT_RIGHTLEFT
8294 /* account for first command-line character in rightleft mode */
8295 && !cmdmsg_rl
8296#endif
8297 )
8298 {
8299 ScreenAttrs[off] = (sattr_T)-1;
8300 return;
8301 }
8302
8303 /*
8304 * Stop highlighting first, so it's easier to move the cursor.
8305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 if (screen_char_attr != 0)
8307 attr = screen_char_attr;
8308 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 attr = ScreenAttrs[off];
8310 if (screen_attr != attr)
8311 screen_stop_highlight();
8312
8313 windgoto(row, col);
8314
8315 if (screen_attr != attr)
8316 screen_start_highlight(attr);
8317
8318#ifdef FEAT_MBYTE
8319 if (enc_utf8 && ScreenLinesUC[off] != 0)
8320 {
8321 char_u buf[MB_MAXBYTES + 1];
8322
Bram Moolenaarcb070082016-04-02 22:14:51 +02008323 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008324 {
8325 if (*p_ambw == 'd'
8326# ifdef FEAT_GUI
8327 && !gui.in_use
8328# endif
8329 )
8330 {
8331 /* Clear the two screen cells. If the character is actually
8332 * single width it won't change the second cell. */
8333 out_str((char_u *)" ");
8334 term_windgoto(row, col);
8335 }
8336 /* not sure where the cursor is after drawing the ambiguous width
8337 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008338 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008339 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008340 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008342
8343 /* Convert the UTF-8 character to bytes and write it. */
8344 buf[utfc_char2bytes(off, buf)] = NUL;
8345 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 }
8347 else
8348#endif
8349 {
8350#ifdef FEAT_MBYTE
8351 out_flush_check();
8352#endif
8353 out_char(ScreenLines[off]);
8354#ifdef FEAT_MBYTE
8355 /* double-byte character in single-width cell */
8356 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8357 out_char(ScreenLines2[off]);
8358#endif
8359 }
8360
8361 screen_cur_col++;
8362}
8363
8364#ifdef FEAT_MBYTE
8365
8366/*
8367 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8368 * on the screen at position 'row' and 'col'.
8369 * The attributes of the first byte is used for all. This is required to
8370 * output the two bytes of a double-byte character with nothing in between.
8371 */
8372 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008373screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374{
8375 /* Check for illegal values (could be wrong when screen was resized). */
8376 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8377 return;
8378
8379 /* Outputting the last character on the screen may scrollup the screen.
8380 * Don't to it! Mark the character invalid (update it when scrolled up) */
8381 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8382 {
8383 ScreenAttrs[off] = (sattr_T)-1;
8384 return;
8385 }
8386
8387 /* Output the first byte normally (positions the cursor), then write the
8388 * second byte directly. */
8389 screen_char(off, row, col);
8390 out_char(ScreenLines[off + 1]);
8391 ++screen_cur_col;
8392}
8393#endif
8394
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395/*
8396 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8397 * This uses the contents of ScreenLines[] and doesn't change it.
8398 */
8399 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008400screen_draw_rectangle(
8401 int row,
8402 int col,
8403 int height,
8404 int width,
8405 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
8407 int r, c;
8408 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008409#ifdef FEAT_MBYTE
8410 int max_off;
8411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008413 /* Can't use ScreenLines unless initialized */
8414 if (ScreenLines == NULL)
8415 return;
8416
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 if (invert)
8418 screen_char_attr = HL_INVERSE;
8419 for (r = row; r < row + height; ++r)
8420 {
8421 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008422#ifdef FEAT_MBYTE
8423 max_off = off + screen_Columns;
8424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 for (c = col; c < col + width; ++c)
8426 {
8427#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008428 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {
8430 screen_char_2(off + c, r, c);
8431 ++c;
8432 }
8433 else
8434#endif
8435 {
8436 screen_char(off + c, r, c);
8437#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008438 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 ++c;
8440#endif
8441 }
8442 }
8443 }
8444 screen_char_attr = 0;
8445}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447/*
8448 * Redraw the characters for a vertically split window.
8449 */
8450 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008451redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
8453 int col;
8454 int width;
8455
8456# ifdef FEAT_CLIPBOARD
8457 clip_may_clear_selection(row, end - 1);
8458# endif
8459
8460 if (wp == NULL)
8461 {
8462 col = 0;
8463 width = Columns;
8464 }
8465 else
8466 {
8467 col = wp->w_wincol;
8468 width = wp->w_width;
8469 }
8470 screen_draw_rectangle(row, col, end - row, width, FALSE);
8471}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008473 static void
8474space_to_screenline(int off, int attr)
8475{
8476 ScreenLines[off] = ' ';
8477 ScreenAttrs[off] = attr;
8478# ifdef FEAT_MBYTE
8479 if (enc_utf8)
8480 ScreenLinesUC[off] = 0;
8481# endif
8482}
8483
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484/*
8485 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8486 * with character 'c1' in first column followed by 'c2' in the other columns.
8487 * Use attributes 'attr'.
8488 */
8489 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008490screen_fill(
8491 int start_row,
8492 int end_row,
8493 int start_col,
8494 int end_col,
8495 int c1,
8496 int c2,
8497 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498{
8499 int row;
8500 int col;
8501 int off;
8502 int end_off;
8503 int did_delete;
8504 int c;
8505 int norm_term;
8506#if defined(FEAT_GUI) || defined(UNIX)
8507 int force_next = FALSE;
8508#endif
8509
8510 if (end_row > screen_Rows) /* safety check */
8511 end_row = screen_Rows;
8512 if (end_col > screen_Columns) /* safety check */
8513 end_col = screen_Columns;
8514 if (ScreenLines == NULL
8515 || start_row >= end_row
8516 || start_col >= end_col) /* nothing to do */
8517 return;
8518
8519 /* it's a "normal" terminal when not in a GUI or cterm */
8520 norm_term = (
8521#ifdef FEAT_GUI
8522 !gui.in_use &&
8523#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008524 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 for (row = start_row; row < end_row; ++row)
8526 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008527#ifdef FEAT_MBYTE
8528 if (has_mbyte
8529# ifdef FEAT_GUI
8530 && !gui.in_use
8531# endif
8532 )
8533 {
8534 /* When drawing over the right halve of a double-wide char clear
8535 * out the left halve. When drawing over the left halve of a
8536 * double wide-char clear out the right halve. Only needed in a
8537 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008538 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008539 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008540 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008541 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008542 }
8543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 /*
8545 * Try to use delete-line termcap code, when no attributes or in a
8546 * "normal" terminal, where a bold/italic space is just a
8547 * space.
8548 */
8549 did_delete = FALSE;
8550 if (c2 == ' '
8551 && end_col == Columns
8552 && can_clear(T_CE)
8553 && (attr == 0
8554 || (norm_term
8555 && attr <= HL_ALL
8556 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8557 {
8558 /*
8559 * check if we really need to clear something
8560 */
8561 col = start_col;
8562 if (c1 != ' ') /* don't clear first char */
8563 ++col;
8564
8565 off = LineOffset[row] + col;
8566 end_off = LineOffset[row] + end_col;
8567
8568 /* skip blanks (used often, keep it fast!) */
8569#ifdef FEAT_MBYTE
8570 if (enc_utf8)
8571 while (off < end_off && ScreenLines[off] == ' '
8572 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8573 ++off;
8574 else
8575#endif
8576 while (off < end_off && ScreenLines[off] == ' '
8577 && ScreenAttrs[off] == 0)
8578 ++off;
8579 if (off < end_off) /* something to be cleared */
8580 {
8581 col = off - LineOffset[row];
8582 screen_stop_highlight();
8583 term_windgoto(row, col);/* clear rest of this screen line */
8584 out_str(T_CE);
8585 screen_start(); /* don't know where cursor is now */
8586 col = end_col - col;
8587 while (col--) /* clear chars in ScreenLines */
8588 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008589 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 ++off;
8591 }
8592 }
8593 did_delete = TRUE; /* the chars are cleared now */
8594 }
8595
8596 off = LineOffset[row] + start_col;
8597 c = c1;
8598 for (col = start_col; col < end_col; ++col)
8599 {
8600 if (ScreenLines[off] != c
8601#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008602 || (enc_utf8 && (int)ScreenLinesUC[off]
8603 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604#endif
8605 || ScreenAttrs[off] != attr
8606#if defined(FEAT_GUI) || defined(UNIX)
8607 || force_next
8608#endif
8609 )
8610 {
8611#if defined(FEAT_GUI) || defined(UNIX)
8612 /* The bold trick may make a single row of pixels appear in
8613 * the next character. When a bold character is removed, the
8614 * next character should be redrawn too. This happens for our
8615 * own GUI and for some xterms. */
8616 if (
8617# ifdef FEAT_GUI
8618 gui.in_use
8619# endif
8620# if defined(FEAT_GUI) && defined(UNIX)
8621 ||
8622# endif
8623# ifdef UNIX
8624 term_is_xterm
8625# endif
8626 )
8627 {
8628 if (ScreenLines[off] != ' '
8629 && (ScreenAttrs[off] > HL_ALL
8630 || ScreenAttrs[off] & HL_BOLD))
8631 force_next = TRUE;
8632 else
8633 force_next = FALSE;
8634 }
8635#endif
8636 ScreenLines[off] = c;
8637#ifdef FEAT_MBYTE
8638 if (enc_utf8)
8639 {
8640 if (c >= 0x80)
8641 {
8642 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008643 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 }
8645 else
8646 ScreenLinesUC[off] = 0;
8647 }
8648#endif
8649 ScreenAttrs[off] = attr;
8650 if (!did_delete || c != ' ')
8651 screen_char(off, row, col);
8652 }
8653 ++off;
8654 if (col == start_col)
8655 {
8656 if (did_delete)
8657 break;
8658 c = c2;
8659 }
8660 }
8661 if (end_col == Columns)
8662 LineWraps[row] = FALSE;
8663 if (row == Rows - 1) /* overwritten the command line */
8664 {
8665 redraw_cmdline = TRUE;
8666 if (c1 == ' ' && c2 == ' ')
8667 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008668 if (start_col == 0)
8669 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 }
8671 }
8672}
8673
8674/*
8675 * Check if there should be a delay. Used before clearing or redrawing the
8676 * screen or the command line.
8677 */
8678 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008679check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
8681 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8682 && !did_wait_return
8683 && emsg_silent == 0)
8684 {
8685 out_flush();
8686 ui_delay(1000L, TRUE);
8687 emsg_on_display = FALSE;
8688 if (check_msg_scroll)
8689 msg_scroll = FALSE;
8690 }
8691}
8692
8693/*
8694 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008695 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 * Returns TRUE if there is a valid screen to write to.
8697 * Returns FALSE when starting up and screen not initialized yet.
8698 */
8699 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008700screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008702 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 return (ScreenLines != NULL);
8704}
8705
8706/*
8707 * Resize the shell to Rows and Columns.
8708 * Allocate ScreenLines[] and associated items.
8709 *
8710 * There may be some time between setting Rows and Columns and (re)allocating
8711 * ScreenLines[]. This happens when starting up and when (manually) changing
8712 * the shell size. Always use screen_Rows and screen_Columns to access items
8713 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8714 * final size of the shell is needed.
8715 */
8716 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008717screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718{
8719 int new_row, old_row;
8720#ifdef FEAT_GUI
8721 int old_Rows;
8722#endif
8723 win_T *wp;
8724 int outofmem = FALSE;
8725 int len;
8726 schar_T *new_ScreenLines;
8727#ifdef FEAT_MBYTE
8728 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008729 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008731 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732#endif
8733 sattr_T *new_ScreenAttrs;
8734 unsigned *new_LineOffset;
8735 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008736 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008737 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008739 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008740#ifdef FEAT_AUTOCMD
8741 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008743retry:
8744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 /*
8746 * Allocation of the screen buffers is done only when the size changes and
8747 * when Rows and Columns have been set and we have started doing full
8748 * screen stuff.
8749 */
8750 if ((ScreenLines != NULL
8751 && Rows == screen_Rows
8752 && Columns == screen_Columns
8753#ifdef FEAT_MBYTE
8754 && enc_utf8 == (ScreenLinesUC != NULL)
8755 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008756 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757#endif
8758 )
8759 || Rows == 0
8760 || Columns == 0
8761 || (!full_screen && ScreenLines == NULL))
8762 return;
8763
8764 /*
8765 * It's possible that we produce an out-of-memory message below, which
8766 * will cause this function to be called again. To break the loop, just
8767 * return here.
8768 */
8769 if (entered)
8770 return;
8771 entered = TRUE;
8772
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008773 /*
8774 * Note that the window sizes are updated before reallocating the arrays,
8775 * thus we must not redraw here!
8776 */
8777 ++RedrawingDisabled;
8778
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 win_new_shellsize(); /* fit the windows in the new sized shell */
8780
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 comp_col(); /* recompute columns for shown command and ruler */
8782
8783 /*
8784 * We're changing the size of the screen.
8785 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8786 * - Move lines from the old arrays into the new arrays, clear extra
8787 * lines (unless the screen is going to be cleared).
8788 * - Free the old arrays.
8789 *
8790 * If anything fails, make ScreenLines NULL, so we don't do anything!
8791 * Continuing with the old ScreenLines may result in a crash, because the
8792 * size is wrong.
8793 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008794 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008796#ifdef FEAT_AUTOCMD
8797 if (aucmd_win != NULL)
8798 win_free_lsize(aucmd_win);
8799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800
8801 new_ScreenLines = (schar_T *)lalloc((long_u)(
8802 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8803#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008804 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 if (enc_utf8)
8806 {
8807 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8808 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008809 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008810 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8812 }
8813 if (enc_dbcs == DBCS_JPNU)
8814 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8815 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8816#endif
8817 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8818 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8819 new_LineOffset = (unsigned *)lalloc((long_u)(
8820 Rows * sizeof(unsigned)), FALSE);
8821 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008822 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008824 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 {
8826 if (win_alloc_lines(wp) == FAIL)
8827 {
8828 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008829 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 }
8831 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008832#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008833 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8834 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008835 outofmem = TRUE;
8836#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008837give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008839#ifdef FEAT_MBYTE
8840 for (i = 0; i < p_mco; ++i)
8841 if (new_ScreenLinesC[i] == NULL)
8842 break;
8843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 if (new_ScreenLines == NULL
8845#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008846 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8848#endif
8849 || new_ScreenAttrs == NULL
8850 || new_LineOffset == NULL
8851 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008852 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 || outofmem)
8854 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008855 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008856 {
8857 /* guess the size */
8858 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8859
8860 /* Remember we did this to avoid getting outofmem messages over
8861 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008862 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 vim_free(new_ScreenLines);
8865 new_ScreenLines = NULL;
8866#ifdef FEAT_MBYTE
8867 vim_free(new_ScreenLinesUC);
8868 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008869 for (i = 0; i < p_mco; ++i)
8870 {
8871 vim_free(new_ScreenLinesC[i]);
8872 new_ScreenLinesC[i] = NULL;
8873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 vim_free(new_ScreenLines2);
8875 new_ScreenLines2 = NULL;
8876#endif
8877 vim_free(new_ScreenAttrs);
8878 new_ScreenAttrs = NULL;
8879 vim_free(new_LineOffset);
8880 new_LineOffset = NULL;
8881 vim_free(new_LineWraps);
8882 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008883 vim_free(new_TabPageIdxs);
8884 new_TabPageIdxs = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 }
8886 else
8887 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008888 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008889
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 for (new_row = 0; new_row < Rows; ++new_row)
8891 {
8892 new_LineOffset[new_row] = new_row * Columns;
8893 new_LineWraps[new_row] = FALSE;
8894
8895 /*
8896 * If the screen is not going to be cleared, copy as much as
8897 * possible from the old screen to the new one and clear the rest
8898 * (used when resizing the window at the "--more--" prompt or when
8899 * executing an external command, for the GUI).
8900 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008901 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 {
8903 (void)vim_memset(new_ScreenLines + new_row * Columns,
8904 ' ', (size_t)Columns * sizeof(schar_T));
8905#ifdef FEAT_MBYTE
8906 if (enc_utf8)
8907 {
8908 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8909 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008910 for (i = 0; i < p_mco; ++i)
8911 (void)vim_memset(new_ScreenLinesC[i]
8912 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 0, (size_t)Columns * sizeof(u8char_T));
8914 }
8915 if (enc_dbcs == DBCS_JPNU)
8916 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8917 0, (size_t)Columns * sizeof(schar_T));
8918#endif
8919 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8920 0, (size_t)Columns * sizeof(sattr_T));
8921 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008922 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 {
8924 if (screen_Columns < Columns)
8925 len = screen_Columns;
8926 else
8927 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008928#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008929 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008930 * may be invalid now. Also when p_mco changes. */
8931 if (!(enc_utf8 && ScreenLinesUC == NULL)
8932 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008933#endif
8934 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8935 ScreenLines + LineOffset[old_row],
8936 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008938 if (enc_utf8 && ScreenLinesUC != NULL
8939 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 {
8941 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8942 ScreenLinesUC + LineOffset[old_row],
8943 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008944 for (i = 0; i < p_mco; ++i)
8945 mch_memmove(new_ScreenLinesC[i]
8946 + new_LineOffset[new_row],
8947 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 (size_t)len * sizeof(u8char_T));
8949 }
8950 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8951 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8952 ScreenLines2 + LineOffset[old_row],
8953 (size_t)len * sizeof(schar_T));
8954#endif
8955 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8956 ScreenAttrs + LineOffset[old_row],
8957 (size_t)len * sizeof(sattr_T));
8958 }
8959 }
8960 }
8961 /* Use the last line of the screen for the current line. */
8962 current_ScreenLine = new_ScreenLines + Rows * Columns;
8963 }
8964
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008965 free_screenlines();
8966
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 ScreenLines = new_ScreenLines;
8968#ifdef FEAT_MBYTE
8969 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008970 for (i = 0; i < p_mco; ++i)
8971 ScreenLinesC[i] = new_ScreenLinesC[i];
8972 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 ScreenLines2 = new_ScreenLines2;
8974#endif
8975 ScreenAttrs = new_ScreenAttrs;
8976 LineOffset = new_LineOffset;
8977 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008978 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
8980 /* It's important that screen_Rows and screen_Columns reflect the actual
8981 * size of ScreenLines[]. Set them before calling anything. */
8982#ifdef FEAT_GUI
8983 old_Rows = screen_Rows;
8984#endif
8985 screen_Rows = Rows;
8986 screen_Columns = Columns;
8987
8988 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008989 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 screenclear2();
8991
8992#ifdef FEAT_GUI
8993 else if (gui.in_use
8994 && !gui.starting
8995 && ScreenLines != NULL
8996 && old_Rows != Rows)
8997 {
8998 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8999 /*
9000 * Adjust the position of the cursor, for when executing an external
9001 * command.
9002 */
9003 if (msg_row >= Rows) /* Rows got smaller */
9004 msg_row = Rows - 1; /* put cursor at last row */
9005 else if (Rows > old_Rows) /* Rows got bigger */
9006 msg_row += Rows - old_Rows; /* put cursor in same place */
9007 if (msg_col >= Columns) /* Columns got smaller */
9008 msg_col = Columns - 1; /* put cursor at last column */
9009 }
9010#endif
9011
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009013 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009014
9015#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009016 /*
9017 * Do not apply autocommands more than 3 times to avoid an endless loop
9018 * in case applying autocommands always changes Rows or Columns.
9019 */
9020 if (starting == 0 && ++retry_count <= 3)
9021 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009022 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009023 /* In rare cases, autocommands may have altered Rows or Columns,
9024 * jump back to check if we need to allocate the screen again. */
9025 goto retry;
9026 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028}
9029
9030 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009031free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009032{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009033#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009034 int i;
9035
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009036 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009037 for (i = 0; i < Screen_mco; ++i)
9038 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009039 vim_free(ScreenLines2);
9040#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009041 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009042 vim_free(ScreenAttrs);
9043 vim_free(LineOffset);
9044 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009045 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009046}
9047
9048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009049screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050{
9051 check_for_delay(FALSE);
9052 screenalloc(FALSE); /* allocate screen buffers if size changed */
9053 screenclear2(); /* clear the screen */
9054}
9055
9056 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009057screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058{
9059 int i;
9060
9061 if (starting == NO_SCREEN || ScreenLines == NULL
9062#ifdef FEAT_GUI
9063 || (gui.in_use && gui.starting)
9064#endif
9065 )
9066 return;
9067
9068#ifdef FEAT_GUI
9069 if (!gui.in_use)
9070#endif
9071 screen_attr = -1; /* force setting the Normal colors */
9072 screen_stop_highlight(); /* don't want highlighting here */
9073
9074#ifdef FEAT_CLIPBOARD
9075 /* disable selection without redrawing it */
9076 clip_scroll_selection(9999);
9077#endif
9078
9079 /* blank out ScreenLines */
9080 for (i = 0; i < Rows; ++i)
9081 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009082 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 LineWraps[i] = FALSE;
9084 }
9085
9086 if (can_clear(T_CL))
9087 {
9088 out_str(T_CL); /* clear the display */
9089 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009090 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 }
9092 else
9093 {
9094 /* can't clear the screen, mark all chars with invalid attributes */
9095 for (i = 0; i < Rows; ++i)
9096 lineinvalid(LineOffset[i], (int)Columns);
9097 clear_cmdline = TRUE;
9098 }
9099
9100 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9101
9102 win_rest_invalid(firstwin);
9103 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009104 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 if (must_redraw == CLEAR) /* no need to clear again */
9106 must_redraw = NOT_VALID;
9107 compute_cmdrow();
9108 msg_row = cmdline_row; /* put cursor on last line for messages */
9109 msg_col = 0;
9110 screen_start(); /* don't know where cursor is now */
9111 msg_scrolled = 0; /* can't scroll back */
9112 msg_didany = FALSE;
9113 msg_didout = FALSE;
9114}
9115
9116/*
9117 * Clear one line in ScreenLines.
9118 */
9119 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009120lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121{
9122 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9123#ifdef FEAT_MBYTE
9124 if (enc_utf8)
9125 (void)vim_memset(ScreenLinesUC + off, 0,
9126 (size_t)width * sizeof(u8char_T));
9127#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009128 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129}
9130
9131/*
9132 * Mark one line in ScreenLines invalid by setting the attributes to an
9133 * invalid value.
9134 */
9135 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009136lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
9138 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9139}
9140
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141/*
9142 * Copy part of a Screenline for vertically split window "wp".
9143 */
9144 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009145linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
9147 unsigned off_to = LineOffset[to] + wp->w_wincol;
9148 unsigned off_from = LineOffset[from] + wp->w_wincol;
9149
9150 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9151 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009152#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 if (enc_utf8)
9154 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009155 int i;
9156
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9158 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009159 for (i = 0; i < p_mco; ++i)
9160 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9161 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 }
9163 if (enc_dbcs == DBCS_JPNU)
9164 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9165 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9168 wp->w_width * sizeof(sattr_T));
9169}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170
9171/*
9172 * Return TRUE if clearing with term string "p" would work.
9173 * It can't work when the string is empty or it won't set the right background.
9174 */
9175 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009176can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177{
9178 return (*p != NUL && (t_colors <= 1
9179#ifdef FEAT_GUI
9180 || gui.in_use
9181#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009182#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009183 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009184 || (!p_tgc && cterm_normal_bg_color == 0)
9185#else
9186 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009187#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009188 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189}
9190
9191/*
9192 * Reset cursor position. Use whenever cursor was moved because of outputting
9193 * something directly to the screen (shell commands) or a terminal control
9194 * code.
9195 */
9196 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009197screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
9199 screen_cur_row = screen_cur_col = 9999;
9200}
9201
9202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 * Move the cursor to position "row","col" in the screen.
9204 * This tries to find the most efficient way to move, minimizing the number of
9205 * characters sent to the terminal.
9206 */
9207 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009208windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009210 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 int i;
9212 int plan;
9213 int cost;
9214 int wouldbe_col;
9215 int noinvcurs;
9216 char_u *bs;
9217 int goto_cost;
9218 int attr;
9219
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009220#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9222
9223#define PLAN_LE 1
9224#define PLAN_CR 2
9225#define PLAN_NL 3
9226#define PLAN_WRITE 4
9227 /* Can't use ScreenLines unless initialized */
9228 if (ScreenLines == NULL)
9229 return;
9230
9231 if (col != screen_cur_col || row != screen_cur_row)
9232 {
9233 /* Check for valid position. */
9234 if (row < 0) /* window without text lines? */
9235 row = 0;
9236 if (row >= screen_Rows)
9237 row = screen_Rows - 1;
9238 if (col >= screen_Columns)
9239 col = screen_Columns - 1;
9240
9241 /* check if no cursor movement is allowed in highlight mode */
9242 if (screen_attr && *T_MS == NUL)
9243 noinvcurs = HIGHL_COST;
9244 else
9245 noinvcurs = 0;
9246 goto_cost = GOTO_COST + noinvcurs;
9247
9248 /*
9249 * Plan how to do the positioning:
9250 * 1. Use CR to move it to column 0, same row.
9251 * 2. Use T_LE to move it a few columns to the left.
9252 * 3. Use NL to move a few lines down, column 0.
9253 * 4. Move a few columns to the right with T_ND or by writing chars.
9254 *
9255 * Don't do this if the cursor went beyond the last column, the cursor
9256 * position is unknown then (some terminals wrap, some don't )
9257 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009258 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 * characters to move the cursor to the right.
9260 */
9261 if (row >= screen_cur_row && screen_cur_col < Columns)
9262 {
9263 /*
9264 * If the cursor is in the same row, bigger col, we can use CR
9265 * or T_LE.
9266 */
9267 bs = NULL; /* init for GCC */
9268 attr = screen_attr;
9269 if (row == screen_cur_row && col < screen_cur_col)
9270 {
9271 /* "le" is preferred over "bc", because "bc" is obsolete */
9272 if (*T_LE)
9273 bs = T_LE; /* "cursor left" */
9274 else
9275 bs = T_BC; /* "backspace character (old) */
9276 if (*bs)
9277 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9278 else
9279 cost = 999;
9280 if (col + 1 < cost) /* using CR is less characters */
9281 {
9282 plan = PLAN_CR;
9283 wouldbe_col = 0;
9284 cost = 1; /* CR is just one character */
9285 }
9286 else
9287 {
9288 plan = PLAN_LE;
9289 wouldbe_col = col;
9290 }
9291 if (noinvcurs) /* will stop highlighting */
9292 {
9293 cost += noinvcurs;
9294 attr = 0;
9295 }
9296 }
9297
9298 /*
9299 * If the cursor is above where we want to be, we can use CR LF.
9300 */
9301 else if (row > screen_cur_row)
9302 {
9303 plan = PLAN_NL;
9304 wouldbe_col = 0;
9305 cost = (row - screen_cur_row) * 2; /* CR LF */
9306 if (noinvcurs) /* will stop highlighting */
9307 {
9308 cost += noinvcurs;
9309 attr = 0;
9310 }
9311 }
9312
9313 /*
9314 * If the cursor is in the same row, smaller col, just use write.
9315 */
9316 else
9317 {
9318 plan = PLAN_WRITE;
9319 wouldbe_col = screen_cur_col;
9320 cost = 0;
9321 }
9322
9323 /*
9324 * Check if any characters that need to be written have the
9325 * correct attributes. Also avoid UTF-8 characters.
9326 */
9327 i = col - wouldbe_col;
9328 if (i > 0)
9329 cost += i;
9330 if (cost < goto_cost && i > 0)
9331 {
9332 /*
9333 * Check if the attributes are correct without additionally
9334 * stopping highlighting.
9335 */
9336 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9337 while (i && *p++ == attr)
9338 --i;
9339 if (i != 0)
9340 {
9341 /*
9342 * Try if it works when highlighting is stopped here.
9343 */
9344 if (*--p == 0)
9345 {
9346 cost += noinvcurs;
9347 while (i && *p++ == 0)
9348 --i;
9349 }
9350 if (i != 0)
9351 cost = 999; /* different attributes, don't do it */
9352 }
9353#ifdef FEAT_MBYTE
9354 if (enc_utf8)
9355 {
9356 /* Don't use an UTF-8 char for positioning, it's slow. */
9357 for (i = wouldbe_col; i < col; ++i)
9358 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9359 {
9360 cost = 999;
9361 break;
9362 }
9363 }
9364#endif
9365 }
9366
9367 /*
9368 * We can do it without term_windgoto()!
9369 */
9370 if (cost < goto_cost)
9371 {
9372 if (plan == PLAN_LE)
9373 {
9374 if (noinvcurs)
9375 screen_stop_highlight();
9376 while (screen_cur_col > col)
9377 {
9378 out_str(bs);
9379 --screen_cur_col;
9380 }
9381 }
9382 else if (plan == PLAN_CR)
9383 {
9384 if (noinvcurs)
9385 screen_stop_highlight();
9386 out_char('\r');
9387 screen_cur_col = 0;
9388 }
9389 else if (plan == PLAN_NL)
9390 {
9391 if (noinvcurs)
9392 screen_stop_highlight();
9393 while (screen_cur_row < row)
9394 {
9395 out_char('\n');
9396 ++screen_cur_row;
9397 }
9398 screen_cur_col = 0;
9399 }
9400
9401 i = col - screen_cur_col;
9402 if (i > 0)
9403 {
9404 /*
9405 * Use cursor-right if it's one character only. Avoids
9406 * removing a line of pixels from the last bold char, when
9407 * using the bold trick in the GUI.
9408 */
9409 if (T_ND[0] != NUL && T_ND[1] == NUL)
9410 {
9411 while (i-- > 0)
9412 out_char(*T_ND);
9413 }
9414 else
9415 {
9416 int off;
9417
9418 off = LineOffset[row] + screen_cur_col;
9419 while (i-- > 0)
9420 {
9421 if (ScreenAttrs[off] != screen_attr)
9422 screen_stop_highlight();
9423#ifdef FEAT_MBYTE
9424 out_flush_check();
9425#endif
9426 out_char(ScreenLines[off]);
9427#ifdef FEAT_MBYTE
9428 if (enc_dbcs == DBCS_JPNU
9429 && ScreenLines[off] == 0x8e)
9430 out_char(ScreenLines2[off]);
9431#endif
9432 ++off;
9433 }
9434 }
9435 }
9436 }
9437 }
9438 else
9439 cost = 999;
9440
9441 if (cost >= goto_cost)
9442 {
9443 if (noinvcurs)
9444 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009445 if (row == screen_cur_row && (col > screen_cur_col)
9446 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 term_cursor_right(col - screen_cur_col);
9448 else
9449 term_windgoto(row, col);
9450 }
9451 screen_cur_row = row;
9452 screen_cur_col = col;
9453 }
9454}
9455
9456/*
9457 * Set cursor to its position in the current window.
9458 */
9459 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009460setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
9462 if (redrawing())
9463 {
9464 validate_cursor();
9465 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009466 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009468 /* With 'rightleft' set and the cursor on a double-wide
9469 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009470 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009472 (has_mbyte
9473 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9474 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475# endif
9476 1)) :
9477#endif
9478 curwin->w_wcol));
9479 }
9480}
9481
9482
9483/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009484 * Insert 'line_count' lines at 'row' in window 'wp'.
9485 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9486 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 * scrolling.
9488 * Returns FAIL if the lines are not inserted, OK for success.
9489 */
9490 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009491win_ins_lines(
9492 win_T *wp,
9493 int row,
9494 int line_count,
9495 int invalid,
9496 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497{
9498 int did_delete;
9499 int nextrow;
9500 int lastrow;
9501 int retval;
9502
9503 if (invalid)
9504 wp->w_lines_valid = 0;
9505
9506 if (wp->w_height < 5)
9507 return FAIL;
9508
9509 if (line_count > wp->w_height - row)
9510 line_count = wp->w_height - row;
9511
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009512 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 if (retval != MAYBE)
9514 return retval;
9515
9516 /*
9517 * If there is a next window or a status line, we first try to delete the
9518 * lines at the bottom to avoid messing what is after the window.
9519 * If this fails and there are following windows, don't do anything to avoid
9520 * messing up those windows, better just redraw.
9521 */
9522 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 if (wp->w_next != NULL || wp->w_status_height)
9524 {
9525 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009526 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 did_delete = TRUE;
9528 else if (wp->w_next)
9529 return FAIL;
9530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 /*
9532 * if no lines deleted, blank the lines that will end up below the window
9533 */
9534 if (!did_delete)
9535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009538 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 lastrow = nextrow + line_count;
9540 if (lastrow > Rows)
9541 lastrow = Rows;
9542 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009543 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 ' ', ' ', 0);
9545 }
9546
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009547 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 == FAIL)
9549 {
9550 /* deletion will have messed up other windows */
9551 if (did_delete)
9552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 win_rest_invalid(W_NEXT(wp));
9555 }
9556 return FAIL;
9557 }
9558
9559 return OK;
9560}
9561
9562/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009563 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9565 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9566 * scrolling
9567 * Return OK for success, FAIL if the lines are not deleted.
9568 */
9569 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009570win_del_lines(
9571 win_T *wp,
9572 int row,
9573 int line_count,
9574 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009575 int mayclear,
9576 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
9578 int retval;
9579
9580 if (invalid)
9581 wp->w_lines_valid = 0;
9582
9583 if (line_count > wp->w_height - row)
9584 line_count = wp->w_height - row;
9585
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009586 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 if (retval != MAYBE)
9588 return retval;
9589
9590 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009591 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 return FAIL;
9593
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 /*
9595 * If there are windows or status lines below, try to put them at the
9596 * correct place. If we can't do that, they have to be redrawn.
9597 */
9598 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9599 {
9600 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009601 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 {
9603 wp->w_redr_status = TRUE;
9604 win_rest_invalid(wp->w_next);
9605 }
9606 }
9607 /*
9608 * If this is the last window and there is no status line, redraw the
9609 * command line later.
9610 */
9611 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 redraw_cmdline = TRUE;
9613 return OK;
9614}
9615
9616/*
9617 * Common code for win_ins_lines() and win_del_lines().
9618 * Returns OK or FAIL when the work has been done.
9619 * Returns MAYBE when not finished yet.
9620 */
9621 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009622win_do_lines(
9623 win_T *wp,
9624 int row,
9625 int line_count,
9626 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009627 int del,
9628 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629{
9630 int retval;
9631
9632 if (!redrawing() || line_count <= 0)
9633 return FAIL;
9634
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009635 /* When inserting lines would result in loss of command output, just redraw
9636 * the lines. */
9637 if (no_win_do_lines_ins && !del)
9638 return FAIL;
9639
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009641 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009643 if (!no_win_do_lines_ins)
9644 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 return FAIL;
9646 }
9647
9648 /*
9649 * Delete all remaining lines
9650 */
9651 if (row + line_count >= wp->w_height)
9652 {
9653 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009654 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 ' ', ' ', 0);
9656 return OK;
9657 }
9658
9659 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009660 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009662 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009664 if (!no_win_do_lines_ins)
9665 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666
9667 /*
9668 * If the terminal can set a scroll region, use that.
9669 * Always do this in a vertically split window. This will redraw from
9670 * ScreenLines[] when t_CV isn't defined. That's faster than using
9671 * win_line().
9672 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009673 * a character in the lower right corner of the scroll region may cause a
9674 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009676 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 scroll_region_set(wp, row);
9680 if (del)
9681 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009682 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 else
9684 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009685 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 scroll_region_reset();
9688 return retval;
9689 }
9690
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9692 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693
9694 return MAYBE;
9695}
9696
9697/*
9698 * window 'wp' and everything after it is messed up, mark it for redraw
9699 */
9700 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009701win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 {
9705 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 wp->w_redr_status = TRUE;
9707 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 }
9709 redraw_cmdline = TRUE;
9710}
9711
9712/*
9713 * The rest of the routines in this file perform screen manipulations. The
9714 * given operation is performed physically on the screen. The corresponding
9715 * change is also made to the internal screen image. In this way, the editor
9716 * anticipates the effect of editing changes on the appearance of the screen.
9717 * That way, when we call screenupdate a complete redraw isn't usually
9718 * necessary. Another advantage is that we can keep adding code to anticipate
9719 * screen changes, and in the meantime, everything still works.
9720 */
9721
9722/*
9723 * types for inserting or deleting lines
9724 */
9725#define USE_T_CAL 1
9726#define USE_T_CDL 2
9727#define USE_T_AL 3
9728#define USE_T_CE 4
9729#define USE_T_DL 5
9730#define USE_T_SR 6
9731#define USE_NL 7
9732#define USE_T_CD 8
9733#define USE_REDRAW 9
9734
9735/*
9736 * insert lines on the screen and update ScreenLines[]
9737 * 'end' is the line after the scrolled part. Normally it is Rows.
9738 * When scrolling region used 'off' is the offset from the top for the region.
9739 * 'row' and 'end' are relative to the start of the region.
9740 *
9741 * return FAIL for failure, OK for success.
9742 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009743 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009744screen_ins_lines(
9745 int off,
9746 int row,
9747 int line_count,
9748 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009749 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009750 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751{
9752 int i;
9753 int j;
9754 unsigned temp;
9755 int cursor_row;
9756 int type;
9757 int result_empty;
9758 int can_ce = can_clear(T_CE);
9759
9760 /*
9761 * FAIL if
9762 * - there is no valid screen
9763 * - the screen has to be redrawn completely
9764 * - the line count is less than one
9765 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009766 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009768 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9769#ifdef FEAT_CLIPBOARD
9770 || (clip_star.state != SELECT_CLEARED
9771 && redrawing_for_callback > 0)
9772#endif
9773 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 return FAIL;
9775
9776 /*
9777 * There are seven ways to insert lines:
9778 * 0. When in a vertically split window and t_CV isn't set, redraw the
9779 * characters from ScreenLines[].
9780 * 1. Use T_CD (clear to end of display) if it exists and the result of
9781 * the insert is just empty lines
9782 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9783 * present or line_count > 1. It looks better if we do all the inserts
9784 * at once.
9785 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9786 * insert is just empty lines and T_CE is not present or line_count >
9787 * 1.
9788 * 4. Use T_AL (insert line) if it exists.
9789 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9790 * just empty lines.
9791 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9792 * just empty lines.
9793 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9794 * the 'da' flag is not set or we have clear line capability.
9795 * 8. redraw the characters from ScreenLines[].
9796 *
9797 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9798 * the scrollbar for the window. It does have insert line, use that if it
9799 * exists.
9800 */
9801 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9803 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009804 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 type = USE_T_CD;
9806 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9807 type = USE_T_CAL;
9808 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9809 type = USE_T_CDL;
9810 else if (*T_AL != NUL)
9811 type = USE_T_AL;
9812 else if (can_ce && result_empty)
9813 type = USE_T_CE;
9814 else if (*T_DL != NUL && result_empty)
9815 type = USE_T_DL;
9816 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9817 type = USE_T_SR;
9818 else
9819 return FAIL;
9820
9821 /*
9822 * For clearing the lines screen_del_lines() is used. This will also take
9823 * care of t_db if necessary.
9824 */
9825 if (type == USE_T_CD || type == USE_T_CDL ||
9826 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009827 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828
9829 /*
9830 * If text is retained below the screen, first clear or delete as many
9831 * lines at the bottom of the window as are about to be inserted so that
9832 * the deleted lines won't later surface during a screen_del_lines.
9833 */
9834 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009835 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836
9837#ifdef FEAT_CLIPBOARD
9838 /* Remove a modeless selection when inserting lines halfway the screen
9839 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009840 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009841 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 else
9843 clip_scroll_selection(-line_count);
9844#endif
9845
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846#ifdef FEAT_GUI
9847 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9848 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009849 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850#endif
9851
9852 if (*T_CCS != NUL) /* cursor relative to region */
9853 cursor_row = row;
9854 else
9855 cursor_row = row + off;
9856
9857 /*
9858 * Shift LineOffset[] line_count down to reflect the inserted lines.
9859 * Clear the inserted lines in ScreenLines[].
9860 */
9861 row += off;
9862 end += off;
9863 for (i = 0; i < line_count; ++i)
9864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 if (wp != NULL && wp->w_width != Columns)
9866 {
9867 /* need to copy part of a line */
9868 j = end - 1 - i;
9869 while ((j -= line_count) >= row)
9870 linecopy(j + line_count, j, wp);
9871 j += line_count;
9872 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009873 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9874 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 else
9876 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9877 LineWraps[j] = FALSE;
9878 }
9879 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 {
9881 j = end - 1 - i;
9882 temp = LineOffset[j];
9883 while ((j -= line_count) >= row)
9884 {
9885 LineOffset[j + line_count] = LineOffset[j];
9886 LineWraps[j + line_count] = LineWraps[j];
9887 }
9888 LineOffset[j + line_count] = temp;
9889 LineWraps[j + line_count] = FALSE;
9890 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009891 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 else
9893 lineinvalid(temp, (int)Columns);
9894 }
9895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896
9897 screen_stop_highlight();
9898 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009899 if (clear_attr != 0)
9900 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 /* redraw the characters */
9903 if (type == USE_REDRAW)
9904 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009905 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 {
9907 term_append_lines(line_count);
9908 screen_start(); /* don't know where cursor is now */
9909 }
9910 else
9911 {
9912 for (i = 0; i < line_count; i++)
9913 {
9914 if (type == USE_T_AL)
9915 {
9916 if (i && cursor_row != 0)
9917 windgoto(cursor_row, 0);
9918 out_str(T_AL);
9919 }
9920 else /* type == USE_T_SR */
9921 out_str(T_SR);
9922 screen_start(); /* don't know where cursor is now */
9923 }
9924 }
9925
9926 /*
9927 * With scroll-reverse and 'da' flag set we need to clear the lines that
9928 * have been scrolled down into the region.
9929 */
9930 if (type == USE_T_SR && *T_DA)
9931 {
9932 for (i = 0; i < line_count; ++i)
9933 {
9934 windgoto(off + i, 0);
9935 out_str(T_CE);
9936 screen_start(); /* don't know where cursor is now */
9937 }
9938 }
9939
9940#ifdef FEAT_GUI
9941 gui_can_update_cursor();
9942 if (gui.in_use)
9943 out_flush(); /* always flush after a scroll */
9944#endif
9945 return OK;
9946}
9947
9948/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009949 * Delete lines on the screen and update ScreenLines[].
9950 * "end" is the line after the scrolled part. Normally it is Rows.
9951 * When scrolling region used "off" is the offset from the top for the region.
9952 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 *
9954 * Return OK for success, FAIL if the lines are not deleted.
9955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009957screen_del_lines(
9958 int off,
9959 int row,
9960 int line_count,
9961 int end,
9962 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009963 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009964 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965{
9966 int j;
9967 int i;
9968 unsigned temp;
9969 int cursor_row;
9970 int cursor_end;
9971 int result_empty; /* result is empty until end of region */
9972 int can_delete; /* deleting line codes can be used */
9973 int type;
9974
9975 /*
9976 * FAIL if
9977 * - there is no valid screen
9978 * - the screen has to be redrawn completely
9979 * - the line count is less than one
9980 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009981 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009983 if (!screen_valid(TRUE) || line_count <= 0
9984 || (!force && line_count > p_ttyscroll)
9985#ifdef FEAT_CLIPBOARD
9986 || (clip_star.state != SELECT_CLEARED
9987 && redrawing_for_callback > 0)
9988#endif
9989 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 return FAIL;
9991
9992 /*
9993 * Check if the rest of the current region will become empty.
9994 */
9995 result_empty = row + line_count >= end;
9996
9997 /*
9998 * We can delete lines only when 'db' flag not set or when 'ce' option
9999 * available.
10000 */
10001 can_delete = (*T_DB == NUL || can_clear(T_CE));
10002
10003 /*
10004 * There are six ways to delete lines:
10005 * 0. When in a vertically split window and t_CV isn't set, redraw the
10006 * characters from ScreenLines[].
10007 * 1. Use T_CD if it exists and the result is empty.
10008 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10009 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10010 * none of the other ways work.
10011 * 4. Use T_CE (erase line) if the result is empty.
10012 * 5. Use T_DL (delete line) if it exists.
10013 * 6. redraw the characters from ScreenLines[].
10014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10016 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010017 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 type = USE_T_CD;
10019#if defined(__BEOS__) && defined(BEOS_DR8)
10020 /*
10021 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10022 * its internal termcap... this works okay for tests which test *T_DB !=
10023 * NUL. It has the disadvantage that the user cannot use any :set t_*
10024 * command to get T_DB (back) to empty_option, only :set term=... will do
10025 * the trick...
10026 * Anyway, this hack will hopefully go away with the next OS release.
10027 * (Olaf Seibert)
10028 */
10029 else if (row == 0 && T_DB == empty_option
10030 && (line_count == 1 || *T_CDL == NUL))
10031#else
10032 else if (row == 0 && (
10033#ifndef AMIGA
10034 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10035 * up, so use delete-line command */
10036 line_count == 1 ||
10037#endif
10038 *T_CDL == NUL))
10039#endif
10040 type = USE_NL;
10041 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10042 type = USE_T_CDL;
10043 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010044 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 type = USE_T_CE;
10046 else if (*T_DL != NUL && can_delete)
10047 type = USE_T_DL;
10048 else if (*T_CDL != NUL && can_delete)
10049 type = USE_T_CDL;
10050 else
10051 return FAIL;
10052
10053#ifdef FEAT_CLIPBOARD
10054 /* Remove a modeless selection when deleting lines halfway the screen or
10055 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010056 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010057 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 else
10059 clip_scroll_selection(line_count);
10060#endif
10061
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062#ifdef FEAT_GUI
10063 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10064 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010065 gui_dont_update_cursor(gui.cursor_row >= row + off
10066 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067#endif
10068
10069 if (*T_CCS != NUL) /* cursor relative to region */
10070 {
10071 cursor_row = row;
10072 cursor_end = end;
10073 }
10074 else
10075 {
10076 cursor_row = row + off;
10077 cursor_end = end + off;
10078 }
10079
10080 /*
10081 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10082 * Clear the inserted lines in ScreenLines[].
10083 */
10084 row += off;
10085 end += off;
10086 for (i = 0; i < line_count; ++i)
10087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 if (wp != NULL && wp->w_width != Columns)
10089 {
10090 /* need to copy part of a line */
10091 j = row + i;
10092 while ((j += line_count) <= end - 1)
10093 linecopy(j - line_count, j, wp);
10094 j -= line_count;
10095 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010096 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10097 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 else
10099 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10100 LineWraps[j] = FALSE;
10101 }
10102 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 {
10104 /* whole width, moving the line pointers is faster */
10105 j = row + i;
10106 temp = LineOffset[j];
10107 while ((j += line_count) <= end - 1)
10108 {
10109 LineOffset[j - line_count] = LineOffset[j];
10110 LineWraps[j - line_count] = LineWraps[j];
10111 }
10112 LineOffset[j - line_count] = temp;
10113 LineWraps[j - line_count] = FALSE;
10114 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010115 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 else
10117 lineinvalid(temp, (int)Columns);
10118 }
10119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010121 if (screen_attr != clear_attr)
10122 screen_stop_highlight();
10123 if (clear_attr != 0)
10124 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 /* redraw the characters */
10127 if (type == USE_REDRAW)
10128 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010129 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 {
10131 windgoto(cursor_row, 0);
10132 out_str(T_CD);
10133 screen_start(); /* don't know where cursor is now */
10134 }
10135 else if (type == USE_T_CDL)
10136 {
10137 windgoto(cursor_row, 0);
10138 term_delete_lines(line_count);
10139 screen_start(); /* don't know where cursor is now */
10140 }
10141 /*
10142 * Deleting lines at top of the screen or scroll region: Just scroll
10143 * the whole screen (scroll region) up by outputting newlines on the
10144 * last line.
10145 */
10146 else if (type == USE_NL)
10147 {
10148 windgoto(cursor_end - 1, 0);
10149 for (i = line_count; --i >= 0; )
10150 out_char('\n'); /* cursor will remain on same line */
10151 }
10152 else
10153 {
10154 for (i = line_count; --i >= 0; )
10155 {
10156 if (type == USE_T_DL)
10157 {
10158 windgoto(cursor_row, 0);
10159 out_str(T_DL); /* delete a line */
10160 }
10161 else /* type == USE_T_CE */
10162 {
10163 windgoto(cursor_row + i, 0);
10164 out_str(T_CE); /* erase a line */
10165 }
10166 screen_start(); /* don't know where cursor is now */
10167 }
10168 }
10169
10170 /*
10171 * If the 'db' flag is set, we need to clear the lines that have been
10172 * scrolled up at the bottom of the region.
10173 */
10174 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10175 {
10176 for (i = line_count; i > 0; --i)
10177 {
10178 windgoto(cursor_end - i, 0);
10179 out_str(T_CE); /* erase a line */
10180 screen_start(); /* don't know where cursor is now */
10181 }
10182 }
10183
10184#ifdef FEAT_GUI
10185 gui_can_update_cursor();
10186 if (gui.in_use)
10187 out_flush(); /* always flush after a scroll */
10188#endif
10189
10190 return OK;
10191}
10192
10193/*
10194 * show the current mode and ruler
10195 *
10196 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10197 * If clear_cmdline is FALSE there may be a message there that needs to be
10198 * cleared only if a mode is shown.
10199 * Return the length of the message (0 if no message).
10200 */
10201 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010202showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203{
10204 int need_clear;
10205 int length = 0;
10206 int do_mode;
10207 int attr;
10208 int nwr_save;
10209#ifdef FEAT_INS_EXPAND
10210 int sub_attr;
10211#endif
10212
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010213 do_mode = ((p_smd && msg_silent == 0)
10214 && ((State & INSERT)
10215 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010216 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 if (do_mode || Recording)
10218 {
10219 /*
10220 * Don't show mode right now, when not redrawing or inside a mapping.
10221 * Call char_avail() only when we are going to show something, because
10222 * it takes a bit of time.
10223 */
10224 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10225 {
10226 redraw_cmdline = TRUE; /* show mode later */
10227 return 0;
10228 }
10229
10230 nwr_save = need_wait_return;
10231
10232 /* wait a bit before overwriting an important message */
10233 check_for_delay(FALSE);
10234
10235 /* if the cmdline is more than one line high, erase top lines */
10236 need_clear = clear_cmdline;
10237 if (clear_cmdline && cmdline_row < Rows - 1)
10238 msg_clr_cmdline(); /* will reset clear_cmdline */
10239
10240 /* Position on the last line in the window, column 0 */
10241 msg_pos_mode();
10242 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010243 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 if (do_mode)
10245 {
10246 MSG_PUTS_ATTR("--", attr);
10247#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010248 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010249# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010250 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010251# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010252 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010253# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010254 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010255# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 MSG_PUTS_ATTR(" IM", attr);
10257# else
10258 MSG_PUTS_ATTR(" XIM", attr);
10259# endif
10260#endif
10261#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10262 if (gui.in_use)
10263 {
10264 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010265 {
10266 /* HANGUL */
10267 if (enc_utf8)
10268 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10269 else
10270 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 }
10273#endif
10274#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010275 /* CTRL-X in Insert mode */
10276 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 {
10278 /* These messages can get long, avoid a wrap in a narrow
10279 * window. Prefer showing edit_submode_extra. */
10280 length = (Rows - msg_row) * Columns - 3;
10281 if (edit_submode_extra != NULL)
10282 length -= vim_strsize(edit_submode_extra);
10283 if (length > 0)
10284 {
10285 if (edit_submode_pre != NULL)
10286 length -= vim_strsize(edit_submode_pre);
10287 if (length - vim_strsize(edit_submode) > 0)
10288 {
10289 if (edit_submode_pre != NULL)
10290 msg_puts_attr(edit_submode_pre, attr);
10291 msg_puts_attr(edit_submode, attr);
10292 }
10293 if (edit_submode_extra != NULL)
10294 {
10295 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10296 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010297 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 else
10299 sub_attr = attr;
10300 msg_puts_attr(edit_submode_extra, sub_attr);
10301 }
10302 }
10303 length = 0;
10304 }
10305 else
10306#endif
10307 {
10308#ifdef FEAT_VREPLACE
10309 if (State & VREPLACE_FLAG)
10310 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10311 else
10312#endif
10313 if (State & REPLACE_FLAG)
10314 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10315 else if (State & INSERT)
10316 {
10317#ifdef FEAT_RIGHTLEFT
10318 if (p_ri)
10319 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10320#endif
10321 MSG_PUTS_ATTR(_(" INSERT"), attr);
10322 }
10323 else if (restart_edit == 'I')
10324 MSG_PUTS_ATTR(_(" (insert)"), attr);
10325 else if (restart_edit == 'R')
10326 MSG_PUTS_ATTR(_(" (replace)"), attr);
10327 else if (restart_edit == 'V')
10328 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10329#ifdef FEAT_RIGHTLEFT
10330 if (p_hkmap)
10331 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10332# ifdef FEAT_FKMAP
10333 if (p_fkmap)
10334 MSG_PUTS_ATTR(farsi_text_5, attr);
10335# endif
10336#endif
10337#ifdef FEAT_KEYMAP
10338 if (State & LANGMAP)
10339 {
10340# ifdef FEAT_ARABIC
10341 if (curwin->w_p_arab)
10342 MSG_PUTS_ATTR(_(" Arabic"), attr);
10343 else
10344# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010345 if (get_keymap_str(curwin, (char_u *)" (%s)",
10346 NameBuff, MAXPATHL))
10347 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 }
10349#endif
10350 if ((State & INSERT) && p_paste)
10351 MSG_PUTS_ATTR(_(" (paste)"), attr);
10352
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 if (VIsual_active)
10354 {
10355 char *p;
10356
10357 /* Don't concatenate separate words to avoid translation
10358 * problems. */
10359 switch ((VIsual_select ? 4 : 0)
10360 + (VIsual_mode == Ctrl_V) * 2
10361 + (VIsual_mode == 'V'))
10362 {
10363 case 0: p = N_(" VISUAL"); break;
10364 case 1: p = N_(" VISUAL LINE"); break;
10365 case 2: p = N_(" VISUAL BLOCK"); break;
10366 case 4: p = N_(" SELECT"); break;
10367 case 5: p = N_(" SELECT LINE"); break;
10368 default: p = N_(" SELECT BLOCK"); break;
10369 }
10370 MSG_PUTS_ATTR(_(p), attr);
10371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 MSG_PUTS_ATTR(" --", attr);
10373 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010374
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 need_clear = TRUE;
10376 }
10377 if (Recording
10378#ifdef FEAT_INS_EXPAND
10379 && edit_submode == NULL /* otherwise it gets too long */
10380#endif
10381 )
10382 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010383 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 need_clear = TRUE;
10385 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010386
10387 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 if (need_clear || clear_cmdline)
10389 msg_clr_eos();
10390 msg_didout = FALSE; /* overwrite this message */
10391 length = msg_col;
10392 msg_col = 0;
10393 need_wait_return = nwr_save; /* never ask for hit-return for this */
10394 }
10395 else if (clear_cmdline && msg_silent == 0)
10396 /* Clear the whole command line. Will reset "clear_cmdline". */
10397 msg_clr_cmdline();
10398
10399#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 /* In Visual mode the size of the selected area must be redrawn. */
10401 if (VIsual_active)
10402 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403
10404 /* If the last window has no status line, the ruler is after the mode
10405 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010406 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 win_redr_ruler(lastwin, TRUE);
10408#endif
10409 redraw_cmdline = FALSE;
10410 clear_cmdline = FALSE;
10411
10412 return length;
10413}
10414
10415/*
10416 * Position for a mode message.
10417 */
10418 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010419msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420{
10421 msg_col = 0;
10422 msg_row = Rows - 1;
10423}
10424
10425/*
10426 * Delete mode message. Used when ESC is typed which is expected to end
10427 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010428 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 */
10430 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010431unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
10433 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010434 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 */
10436 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10437 redraw_cmdline = TRUE; /* delete mode later */
10438 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010439 clearmode();
10440}
10441
10442/*
10443 * Clear the mode message.
10444 */
10445 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010446clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010447{
10448 msg_pos_mode();
10449 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010450 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010451 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452}
10453
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010454 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010455recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010456{
10457 MSG_PUTS_ATTR(_("recording"), attr);
10458 if (!shortmess(SHM_RECORDING))
10459 {
10460 char_u s[4];
10461 sprintf((char *)s, " @%c", Recording);
10462 MSG_PUTS_ATTR(s, attr);
10463 }
10464}
10465
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010466/*
10467 * Draw the tab pages line at the top of the Vim window.
10468 */
10469 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010470draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010471{
10472 int tabcount = 0;
10473 tabpage_T *tp;
10474 int tabwidth;
10475 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010476 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010477 int attr;
10478 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010479 win_T *cwp;
10480 int wincount;
10481 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010482 int c;
10483 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010484 int attr_sel = HL_ATTR(HLF_TPS);
10485 int attr_nosel = HL_ATTR(HLF_TP);
10486 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010487 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010488 int room;
10489 int use_sep_chars = (t_colors < 8
10490#ifdef FEAT_GUI
10491 && !gui.in_use
10492#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010493#ifdef FEAT_TERMGUICOLORS
10494 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010495#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010496 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010497
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010498 if (ScreenLines == NULL)
10499 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010500 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010501
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010502#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010503 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010504 if (gui_use_tabline())
10505 {
10506 gui_update_tabline();
10507 return;
10508 }
10509#endif
10510
10511 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010512 return;
10513
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010514#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010515
10516 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10517 for (scol = 0; scol < Columns; ++scol)
10518 TabPageIdxs[scol] = 0;
10519
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010520 /* Use the 'tabline' option if it's set. */
10521 if (*p_tal != NUL)
10522 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010523 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010524
10525 /* Check for an error. If there is one we would loop in redrawing the
10526 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010527 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010528 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010529 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010530 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010531 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010532 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010533 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010534 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010535#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010536 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010537 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010538 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010539
Bram Moolenaar238a5642006-02-21 22:12:05 +000010540 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10541 if (tabwidth < 6)
10542 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010543
Bram Moolenaar238a5642006-02-21 22:12:05 +000010544 attr = attr_nosel;
10545 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010546 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010547 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10548 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010549 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010550 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010551
Bram Moolenaar238a5642006-02-21 22:12:05 +000010552 if (tp->tp_topframe == topframe)
10553 attr = attr_sel;
10554 if (use_sep_chars && col > 0)
10555 screen_putchar('|', 0, col++, attr);
10556
10557 if (tp->tp_topframe != topframe)
10558 attr = attr_nosel;
10559
10560 screen_putchar(' ', 0, col++, attr);
10561
10562 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010563 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010564 cwp = curwin;
10565 wp = firstwin;
10566 }
10567 else
10568 {
10569 cwp = tp->tp_curwin;
10570 wp = tp->tp_firstwin;
10571 }
10572
10573 modified = FALSE;
10574 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10575 if (bufIsChanged(wp->w_buffer))
10576 modified = TRUE;
10577 if (modified || wincount > 1)
10578 {
10579 if (wincount > 1)
10580 {
10581 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010582 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010583 if (col + len >= Columns - 3)
10584 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010585 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010586#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010587 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010588#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010589 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010590#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010591 );
10592 col += len;
10593 }
10594 if (modified)
10595 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10596 screen_putchar(' ', 0, col++, attr);
10597 }
10598
10599 room = scol - col + tabwidth - 1;
10600 if (room > 0)
10601 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010602 /* Get buffer name in NameBuff[] */
10603 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010604 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010605 len = vim_strsize(NameBuff);
10606 p = NameBuff;
10607#ifdef FEAT_MBYTE
10608 if (has_mbyte)
10609 while (len > room)
10610 {
10611 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010612 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613 }
10614 else
10615#endif
10616 if (len > room)
10617 {
10618 p += len - room;
10619 len = room;
10620 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010621 if (len > Columns - col - 1)
10622 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010623
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010624 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010625 col += len;
10626 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010627 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010628
10629 /* Store the tab page number in TabPageIdxs[], so that
10630 * jump_to_mouse() knows where each one is. */
10631 ++tabcount;
10632 while (scol < col)
10633 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010634 }
10635
Bram Moolenaar238a5642006-02-21 22:12:05 +000010636 if (use_sep_chars)
10637 c = '_';
10638 else
10639 c = ' ';
10640 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010641
10642 /* Put an "X" for closing the current tab if there are several. */
10643 if (first_tabpage->tp_next != NULL)
10644 {
10645 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10646 TabPageIdxs[Columns - 1] = -999;
10647 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010648 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010649
10650 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10651 * set. */
10652 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010653}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010654
10655/*
10656 * Get buffer name for "buf" into NameBuff[].
10657 * Takes care of special buffer names and translates special characters.
10658 */
10659 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010660get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010661{
10662 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010663 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010664 else
10665 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10666 trans_characters(NameBuff, MAXPATHL);
10667}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010668
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669/*
10670 * Get the character to use in a status line. Get its attributes in "*attr".
10671 */
10672 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010673fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674{
10675 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010676
10677#ifdef FEAT_TERMINAL
10678 if (bt_terminal(wp->w_buffer))
10679 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010680 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010681 {
10682 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010683 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010684 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010685 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010686 {
10687 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010688 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010689 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010690 }
10691 else
10692#endif
10693 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010695 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 fill = fill_stl;
10697 }
10698 else
10699 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010700 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 fill = fill_stlnc;
10702 }
10703 /* Use fill when there is highlighting, and highlighting of current
10704 * window differs, or the fillchars differ, or this is not the
10705 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010706 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010707 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 || (fill_stl != fill_stlnc)))
10709 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010710 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 return '^';
10712 return '=';
10713}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715/*
10716 * Get the character to use in a separator between vertically split windows.
10717 * Get its attributes in "*attr".
10718 */
10719 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010720fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010722 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 if (*attr == 0 && fill_vert == ' ')
10724 return '|';
10725 else
10726 return fill_vert;
10727}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728
10729/*
10730 * Return TRUE if redrawing should currently be done.
10731 */
10732 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010733redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010735#ifdef FEAT_EVAL
10736 if (disable_redraw_for_testing)
10737 return 0;
10738 else
10739#endif
10740 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10742}
10743
10744/*
10745 * Return TRUE if printing messages should currently be done.
10746 */
10747 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010748messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749{
10750 return (!(p_lz && char_avail() && !KeyTyped));
10751}
10752
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010753#ifdef FEAT_MENU
10754/*
10755 * Draw the window toolbar.
10756 */
10757 static void
10758redraw_win_toolbar(win_T *wp)
10759{
10760 vimmenu_T *menu;
10761 int item_idx = 0;
10762 int item_count = 0;
10763 int col = 0;
10764 int next_col;
10765 int off = (int)(current_ScreenLine - ScreenLines);
10766 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10767 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10768
10769 vim_free(wp->w_winbar_items);
10770 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10771 ++item_count;
10772 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10773 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10774
10775 /* TODO: use fewer spaces if there is not enough room */
10776 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010777 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010778 {
10779 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010780 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010781 break;
10782 if (col > 1)
10783 {
10784 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010785 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010786 break;
10787 }
10788
10789 wp->w_winbar_items[item_idx].wb_startcol = col;
10790 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010791 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010792 break;
10793
10794 next_col = text_to_screenline(wp, menu->name, col);
10795 while (col < next_col)
10796 {
10797 ScreenAttrs[off + col] = button_attr;
10798 ++col;
10799 }
10800 wp->w_winbar_items[item_idx].wb_endcol = col;
10801 wp->w_winbar_items[item_idx].wb_menu = menu;
10802 ++item_idx;
10803
Bram Moolenaar02631462017-09-22 15:20:32 +020010804 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010805 break;
10806 space_to_screenline(off + col, button_attr);
10807 ++col;
10808 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010809 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010810 {
10811 space_to_screenline(off + col, fill_attr);
10812 ++col;
10813 }
10814 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10815
Bram Moolenaar02631462017-09-22 15:20:32 +020010816 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10817 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010818}
10819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820/*
10821 * Show current status info in ruler and various other places
10822 * If always is FALSE, only show ruler if position has changed.
10823 */
10824 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010825showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826{
10827 if (!always && !redrawing())
10828 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010829#ifdef FEAT_INS_EXPAND
10830 if (pum_visible())
10831 {
10832 /* Don't redraw right now, do it later. */
10833 curwin->w_redr_status = TRUE;
10834 return;
10835 }
10836#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010837#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010838 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010839 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010840 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 else
10843#endif
10844#ifdef FEAT_CMDL_INFO
10845 win_redr_ruler(curwin, always);
10846#endif
10847
10848#ifdef FEAT_TITLE
10849 if (need_maketitle
10850# ifdef FEAT_STL_OPT
10851 || (p_icon && (stl_syntax & STL_IN_ICON))
10852 || (p_title && (stl_syntax & STL_IN_TITLE))
10853# endif
10854 )
10855 maketitle();
10856#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010857 /* Redraw the tab pages line if needed. */
10858 if (redraw_tabline)
10859 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860}
10861
10862#ifdef FEAT_CMDL_INFO
10863 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010864win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010866#define RULER_BUF_LEN 70
10867 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 int row;
10869 int fillchar;
10870 int attr;
10871 int empty_line = FALSE;
10872 colnr_T virtcol;
10873 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010874 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 int this_ru_col;
10877 int off = 0;
10878 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879
10880 /* If 'ruler' off or redrawing disabled, don't do anything */
10881 if (!p_ru)
10882 return;
10883
10884 /*
10885 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10886 * after deleting lines, before cursor.lnum is corrected.
10887 */
10888 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10889 return;
10890
10891#ifdef FEAT_INS_EXPAND
10892 /* Don't draw the ruler while doing insert-completion, it might overwrite
10893 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 if (edit_submode != NULL)
10896 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010897 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10898 if (pum_visible())
10899 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900#endif
10901
10902#ifdef FEAT_STL_OPT
10903 if (*p_ruf)
10904 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010905 int save_called_emsg = called_emsg;
10906
10907 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010909 if (called_emsg)
10910 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010911 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010912 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 return;
10914 }
10915#endif
10916
10917 /*
10918 * Check if not in Insert mode and the line is empty (will show "0-1").
10919 */
10920 if (!(State & INSERT)
10921 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10922 empty_line = TRUE;
10923
10924 /*
10925 * Only draw the ruler when something changed.
10926 */
10927 validate_virtcol_win(wp);
10928 if ( redraw_cmdline
10929 || always
10930 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10931 || wp->w_cursor.col != wp->w_ru_cursor.col
10932 || wp->w_virtcol != wp->w_ru_virtcol
10933#ifdef FEAT_VIRTUALEDIT
10934 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10935#endif
10936 || wp->w_topline != wp->w_ru_topline
10937 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10938#ifdef FEAT_DIFF
10939 || wp->w_topfill != wp->w_ru_topfill
10940#endif
10941 || empty_line != wp->w_ru_empty)
10942 {
10943 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 if (wp->w_status_height)
10945 {
10946 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010947 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010948 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010949 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 }
10951 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 {
10953 row = Rows - 1;
10954 fillchar = ' ';
10955 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 width = Columns;
10957 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 }
10959
10960 /* In list mode virtcol needs to be recomputed */
10961 virtcol = wp->w_virtcol;
10962 if (wp->w_p_list && lcs_tab1 == NUL)
10963 {
10964 wp->w_p_list = FALSE;
10965 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10966 wp->w_p_list = TRUE;
10967 }
10968
10969 /*
10970 * Some sprintfs return the length, some return a pointer.
10971 * To avoid portability problems we use strlen() here.
10972 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010973 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10975 ? 0L
10976 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010977 len = STRLEN(buffer);
10978 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10980 (int)virtcol + 1);
10981
10982 /*
10983 * Add a "50%" if there is room for it.
10984 * On the last line, don't print in the last column (scrolls the
10985 * screen up on some terminals).
10986 */
10987 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010988 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 this_ru_col = ru_col - (Columns - width);
10993 if (this_ru_col < 0)
10994 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 /* Never use more than half the window/screen width, leave the other
10996 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010997 if (this_ru_col < (width + 1) / 2)
10998 this_ru_col = (width + 1) / 2;
10999 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011001 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011002 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 {
11004#ifdef FEAT_MBYTE
11005 if (has_mbyte)
11006 i += (*mb_char2bytes)(fillchar, buffer + i);
11007 else
11008#endif
11009 buffer[i++] = fillchar;
11010 ++o;
11011 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011012 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 }
11014 /* Truncate at window boundary. */
11015#ifdef FEAT_MBYTE
11016 if (has_mbyte)
11017 {
11018 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011019 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 {
11021 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011022 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 {
11024 buffer[i] = NUL;
11025 break;
11026 }
11027 }
11028 }
11029 else
11030#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011031 if (this_ru_col + (int)STRLEN(buffer) > width)
11032 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033
Bram Moolenaar4033c552017-09-16 20:54:51 +020011034 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 i = redraw_cmdline;
11036 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011037 this_ru_col + off + (int)STRLEN(buffer),
11038 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 fillchar, fillchar, attr);
11040 /* don't redraw the cmdline because of showing the ruler */
11041 redraw_cmdline = i;
11042 wp->w_ru_cursor = wp->w_cursor;
11043 wp->w_ru_virtcol = wp->w_virtcol;
11044 wp->w_ru_empty = empty_line;
11045 wp->w_ru_topline = wp->w_topline;
11046 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11047#ifdef FEAT_DIFF
11048 wp->w_ru_topfill = wp->w_topfill;
11049#endif
11050 }
11051}
11052#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011053
11054#if defined(FEAT_LINEBREAK) || defined(PROTO)
11055/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011056 * Return the width of the 'number' and 'relativenumber' column.
11057 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011058 * Otherwise it depends on 'numberwidth' and the line count.
11059 */
11060 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011061number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011062{
11063 int n;
11064 linenr_T lnum;
11065
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011066 if (wp->w_p_rnu && !wp->w_p_nu)
11067 /* cursor line shows "0" */
11068 lnum = wp->w_height;
11069 else
11070 /* cursor line shows absolute line number */
11071 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011072
Bram Moolenaar6b314672015-03-20 15:42:10 +010011073 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011074 return wp->w_nrwidth_width;
11075 wp->w_nrwidth_line_count = lnum;
11076
11077 n = 0;
11078 do
11079 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011080 lnum /= 10;
11081 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011082 } while (lnum > 0);
11083
11084 /* 'numberwidth' gives the minimal width plus one */
11085 if (n < wp->w_p_nuw - 1)
11086 n = wp->w_p_nuw - 1;
11087
11088 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011089 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011090 return n;
11091}
11092#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011093
11094/*
11095 * Return the current cursor column. This is the actual position on the
11096 * screen. First column is 0.
11097 */
11098 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011099screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011100{
11101 return screen_cur_col;
11102}
11103
11104/*
11105 * Return the current cursor row. This is the actual position on the screen.
11106 * First row is 0.
11107 */
11108 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011109screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011110{
11111 return screen_cur_row;
11112}