blob: 6ce478eccee915cb1aa9843f66d79e9f983ac0b5 [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
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
125static 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 +0000126#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
128static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
129static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200131static int win_line(win_T *, linenr_T, int, int, int nochange, proftime_T *syntax_tm);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100133#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200140# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static void start_search_hl(void);
142static void end_search_hl(void);
143static void init_search_hl(win_T *wp);
144static void prepare_search_hl(win_T *wp, linenr_T lnum);
145static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
146static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screen_start_highlight(int attr);
149static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200154static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100156#ifdef FEAT_WINDOWS
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 Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200160static 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 +0100161static void win_rest_invalid(win_T *wp);
162static void msg_pos_mode(void);
163static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000164#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200168static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100170#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100180#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181/* Ugly global: overrule attribute used by screen_char() */
182static int screen_char_attr = 0;
183#endif
184
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200185#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
186/* Can limit syntax highlight time to 'redrawtime'. */
187# define SYN_TIME_LIMIT 1
188#endif
189
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200190#ifdef FEAT_RIGHTLEFT
191# define HAS_RIGHTLEFT(x) x
192#else
193# define HAS_RIGHTLEFT(x) FALSE
194#endif
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
197 * Redraw the current window later, with update_screen(type).
198 * Set must_redraw only if not already set to a higher value.
199 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
200 */
201 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100202redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 redraw_win_later(curwin, type);
205}
206
207 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100208redraw_win_later(
209 win_T *wp,
210 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211{
212 if (wp->w_redr_type < type)
213 {
214 wp->w_redr_type = type;
215 if (type >= NOT_VALID)
216 wp->w_lines_valid = 0;
217 if (must_redraw < type) /* must_redraw is the maximum of all windows */
218 must_redraw = type;
219 }
220}
221
222/*
223 * Force a complete redraw later. Also resets the highlighting. To be used
224 * after executing a shell command that messes up the screen.
225 */
226 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100227redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000230#ifdef FEAT_GUI
231 if (gui.in_use)
232 /* Use a code that will reset gui.highlight_mask in
233 * gui_stop_highlight(). */
234 screen_attr = HL_ALL + 1;
235 else
236#endif
237 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200238 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239}
240
241/*
242 * Mark all windows to be redrawn later.
243 */
244 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100245redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
247 win_T *wp;
248
249 FOR_ALL_WINDOWS(wp)
250 {
251 redraw_win_later(wp, type);
252 }
253}
254
255/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000256 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 */
258 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100259redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 redraw_buf_later(curbuf, type);
262}
263
264 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100265redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
267 win_T *wp;
268
269 FOR_ALL_WINDOWS(wp)
270 {
271 if (wp->w_buffer == buf)
272 redraw_win_later(wp, type);
273 }
274}
275
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200276 void
277redraw_buf_and_status_later(buf_T *buf, int type)
278{
279 win_T *wp;
280
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200281#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200282 if (wild_menu_showing != 0)
283 /* Don't redraw while the command line completion is displayed, it
284 * would disappear. */
285 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200286#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200287 FOR_ALL_WINDOWS(wp)
288 {
289 if (wp->w_buffer == buf)
290 {
291 redraw_win_later(wp, type);
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200292#ifdef FEAT_WINDOWS
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200293 wp->w_redr_status = TRUE;
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200294#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200295 }
296 }
297}
298
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200300 * Redraw as soon as possible. When the command line is not scrolled redraw
301 * right away and restore what was on the command line.
302 * Return a code indicating what happened.
303 */
304 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100305redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306{
307 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200308 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309 int r;
310 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200311 schar_T *screenline; /* copy from ScreenLines[] */
312 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313#ifdef FEAT_MBYTE
314 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200315 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200317 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318#endif
319
320 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 return ret;
323
324 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200325 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200326 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200327 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200328 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200329 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenline == NULL || screenattr == NULL)
331 ret = 2;
332#ifdef FEAT_MBYTE
333 if (enc_utf8)
334 {
335 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200336 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenlineUC == NULL)
338 ret = 2;
339 for (i = 0; i < p_mco; ++i)
340 {
341 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200342 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenlineC[i] == NULL)
344 ret = 2;
345 }
346 }
347 if (enc_dbcs == DBCS_JPNU)
348 {
349 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 if (screenline2 == NULL)
352 ret = 2;
353 }
354#endif
355
356 if (ret != 2)
357 {
358 /* Save the text displayed in the command line area. */
359 for (r = 0; r < rows; ++r)
360 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 (size_t)cols * sizeof(schar_T));
364 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367#ifdef FEAT_MBYTE
368 if (enc_utf8)
369 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200370 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200371 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200372 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200373 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 mch_memmove(screenlineC[i] + r * cols,
375 ScreenLinesC[i] + LineOffset[cmdline_row + r],
376 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 }
378 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200379 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200381 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200382#endif
383 }
384
385 update_screen(0);
386 ret = 3;
387
388 if (must_redraw == 0)
389 {
390 int off = (int)(current_ScreenLine - ScreenLines);
391
392 /* Restore the text displayed in the command line area. */
393 for (r = 0; r < rows; ++r)
394 {
395 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200396 screenline + r * cols,
397 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200398 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200399 screenattr + r * cols,
400 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200401#ifdef FEAT_MBYTE
402 if (enc_utf8)
403 {
404 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200405 screenlineUC + r * cols,
406 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 for (i = 0; i < p_mco; ++i)
408 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenlineC[i] + r * cols,
410 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200411 }
412 if (enc_dbcs == DBCS_JPNU)
413 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200414 screenline2 + r * cols,
415 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200416#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200417 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200418 }
419 ret = 4;
420 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200421 }
422
423 vim_free(screenline);
424 vim_free(screenattr);
425#ifdef FEAT_MBYTE
426 if (enc_utf8)
427 {
428 vim_free(screenlineUC);
429 for (i = 0; i < p_mco; ++i)
430 vim_free(screenlineC[i]);
431 }
432 if (enc_dbcs == DBCS_JPNU)
433 vim_free(screenline2);
434#endif
435
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200436 /* Show the intro message when appropriate. */
437 maybe_intro_message();
438
439 setcursor();
440
Bram Moolenaar2951b772013-07-03 12:45:31 +0200441 return ret;
442}
443
444/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445 * Invoked after an asynchronous callback is called.
446 * If an echo command was used the cursor needs to be put back where
447 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200448 * If "call_update_screen" is FALSE don't call update_screen() when at the
449 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100450 */
451 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200452redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100453{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200454 ++redrawing_for_callback;
455
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100456 if (State == HITRETURN || State == ASKMORE)
457 ; /* do nothing */
458 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200459 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200460 /* Redrawing only works when the screen didn't scroll. Don't clear
461 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200462 if (msg_scrolled == 0
463#ifdef FEAT_WILDMENU
464 && wild_menu_showing == 0
465#endif
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200466 && call_update_screen)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200467 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200468 /* Redraw in the same position, so that the user can continue
469 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200470 redrawcmdline_ex(FALSE);
471 }
Bram Moolenaar144445d2016-07-08 21:41:54 +0200472 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100473 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200474 /* keep the command line if possible */
475 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100476 setcursor();
477 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100478 cursor_on();
479 out_flush();
480#ifdef FEAT_GUI
481 if (gui.in_use)
482 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200483 /* Don't update the cursor when it is blinking and off to avoid
484 * flicker. */
485 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200486 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100487 gui_mch_flush();
488 }
489#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200490
491 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100492}
493
494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 * Changed something in the current window, at buffer line "lnum", that
496 * requires that line and possibly other lines to be redrawn.
497 * Used when entering/leaving Insert mode with the cursor on a folded line.
498 * Used to remove the "$" from a change command.
499 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
500 * may become invalid and the whole window will have to be redrawn.
501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100503redrawWinline(
504 linenr_T lnum,
505 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506{
507#ifdef FEAT_FOLDING
508 int i;
509#endif
510
511 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
512 curwin->w_redraw_top = lnum;
513 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
514 curwin->w_redraw_bot = lnum;
515 redraw_later(VALID);
516
517#ifdef FEAT_FOLDING
518 if (invalid)
519 {
520 /* A w_lines[] entry for this lnum has become invalid. */
521 i = find_wl_entry(curwin, lnum);
522 if (i >= 0)
523 curwin->w_lines[i].wl_valid = FALSE;
524 }
525#endif
526}
527
528/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200529 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 */
531 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100532update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 redraw_curbuf_later(type);
535 update_screen(type);
536}
537
538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 * Based on the current value of curwin->w_topline, transfer a screenfull
540 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200541 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200543 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200544update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 win_T *wp;
548 static int did_intro = FALSE;
549#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
550 int did_one;
551#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200552#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200553 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200554 int gui_cursor_col;
555 int gui_cursor_row;
556#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200557 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000559 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200561 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200563 if (type == VALID_NO_UPDATE)
564 {
565 no_update = TRUE;
566 type = 0;
567 }
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (must_redraw)
570 {
571 if (type < must_redraw) /* use maximal type */
572 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000573
574 /* must_redraw is reset here, so that when we run into some weird
575 * reason to redraw while busy redrawing (e.g., asynchronous
576 * scrolling), or update_topline() in win_update() will cause a
577 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 must_redraw = 0;
579 }
580
581 /* Need to update w_lines[]. */
582 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
583 type = NOT_VALID;
584
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000585 /* Postpone the redrawing when it's not needed and when being called
586 * recursively. */
587 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 {
589 redraw_later(type); /* remember type for next time */
590 must_redraw = type;
591 if (type > INVERTED_ALL)
592 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200593 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 }
595
596 updating_screen = TRUE;
597#ifdef FEAT_SYN_HL
598 ++display_tick; /* let syntax code know we're in a next round of
599 * display updating */
600#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200601 if (no_update)
602 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603
604 /*
605 * if the screen was scrolled up when displaying a message, scroll it down
606 */
607 if (msg_scrolled)
608 {
609 clear_cmdline = TRUE;
610 if (msg_scrolled > Rows - 5) /* clearing is faster */
611 type = CLEAR;
612 else if (type != CLEAR)
613 {
614 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200615 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
616 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 type = CLEAR;
618 FOR_ALL_WINDOWS(wp)
619 {
620 if (W_WINROW(wp) < msg_scrolled)
621 {
622 if (W_WINROW(wp) + wp->w_height > msg_scrolled
623 && wp->w_redr_type < REDRAW_TOP
624 && wp->w_lines_valid > 0
625 && wp->w_topline == wp->w_lines[0].wl_lnum)
626 {
627 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
628 wp->w_redr_type = REDRAW_TOP;
629 }
630 else
631 {
632 wp->w_redr_type = NOT_VALID;
633#ifdef FEAT_WINDOWS
634 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
635 <= msg_scrolled)
636 wp->w_redr_status = TRUE;
637#endif
638 }
639 }
640 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200641 if (!no_update)
642 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000643#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000644 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
647 msg_scrolled = 0;
648 need_wait_return = FALSE;
649 }
650
651 /* reset cmdline_row now (may have been changed temporarily) */
652 compute_cmdrow();
653
654 /* Check for changed highlighting */
655 if (need_highlight_changed)
656 highlight_changed();
657
658 if (type == CLEAR) /* first clear screen */
659 {
660 screenclear(); /* will reset clear_cmdline */
661 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200662 /* must_redraw may be set indirectly, avoid another redraw later */
663 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665
666 if (clear_cmdline) /* going to clear cmdline (done below) */
667 check_for_delay(FALSE);
668
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000669#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200670 /* Force redraw when width of 'number' or 'relativenumber' column
671 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000672 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200673 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
674 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000675 curwin->w_redr_type = NOT_VALID;
676#endif
677
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 /*
679 * Only start redrawing if there is really something to do.
680 */
681 if (type == INVERTED)
682 update_curswant();
683 if (curwin->w_redr_type < type
684 && !((type == VALID
685 && curwin->w_lines[0].wl_valid
686#ifdef FEAT_DIFF
687 && curwin->w_topfill == curwin->w_old_topfill
688 && curwin->w_botfill == curwin->w_old_botfill
689#endif
690 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000692 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
694 && curwin->w_old_visual_mode == VIsual_mode
695 && (curwin->w_valid & VALID_VIRTCOL)
696 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 ))
698 curwin->w_redr_type = type;
699
Bram Moolenaar5a305422006-04-28 22:38:25 +0000700#ifdef FEAT_WINDOWS
701 /* Redraw the tab pages line if needed. */
702 if (redraw_tabline || type >= NOT_VALID)
703 draw_tabline();
704#endif
705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#ifdef FEAT_SYN_HL
707 /*
708 * Correct stored syntax highlighting info for changes in each displayed
709 * buffer. Each buffer must only be done once.
710 */
711 FOR_ALL_WINDOWS(wp)
712 {
713 if (wp->w_buffer->b_mod_set)
714 {
715# ifdef FEAT_WINDOWS
716 win_T *wwp;
717
718 /* Check if we already did this buffer. */
719 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
720 if (wwp->w_buffer == wp->w_buffer)
721 break;
722# endif
723 if (
724# ifdef FEAT_WINDOWS
725 wwp == wp &&
726# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200727 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 syn_stack_apply_changes(wp->w_buffer);
729 }
730 }
731#endif
732
733 /*
734 * Go from top to bottom through the windows, redrawing the ones that need
735 * it.
736 */
737#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
738 did_one = FALSE;
739#endif
740#ifdef FEAT_SEARCH_EXTRA
741 search_hl.rm.regprog = NULL;
742#endif
743 FOR_ALL_WINDOWS(wp)
744 {
745 if (wp->w_redr_type != 0)
746 {
747 cursor_off();
748#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
749 if (!did_one)
750 {
751 did_one = TRUE;
752# ifdef FEAT_SEARCH_EXTRA
753 start_search_hl();
754# endif
755# ifdef FEAT_CLIPBOARD
756 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200757 if (clip_star.available && clip_isautosel_star())
758 clip_update_selection(&clip_star);
759 if (clip_plus.available && clip_isautosel_plus())
760 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761# endif
762#ifdef FEAT_GUI
763 /* Remove the cursor before starting to do anything, because
764 * scrolling may make it difficult to redraw the text under
765 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200766 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200767 {
768 gui_cursor_col = gui.cursor_col;
769 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200771 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#endif
774 }
775#endif
776 win_update(wp);
777 }
778
779#ifdef FEAT_WINDOWS
780 /* redraw status line after the window to minimize cursor movement */
781 if (wp->w_redr_status)
782 {
783 cursor_off();
784 win_redr_status(wp);
785 }
786#endif
787 }
788#if defined(FEAT_SEARCH_EXTRA)
789 end_search_hl();
790#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100791#ifdef FEAT_INS_EXPAND
792 /* May need to redraw the popup menu. */
793 if (pum_visible())
794 pum_redraw();
795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
797#ifdef FEAT_WINDOWS
798 /* Reset b_mod_set flags. Going through all windows is probably faster
799 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200800 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 wp->w_buffer->b_mod_set = FALSE;
802#else
803 curbuf->b_mod_set = FALSE;
804#endif
805
806 updating_screen = FALSE;
807#ifdef FEAT_GUI
808 gui_may_resize_shell();
809#endif
810
811 /* Clear or redraw the command line. Done last, because scrolling may
812 * mess up the command line. */
813 if (clear_cmdline || redraw_cmdline)
814 showmode();
815
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200816 if (no_update)
817 --no_win_do_lines_ins;
818
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200820 if (!did_intro)
821 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 did_intro = TRUE;
823
824#ifdef FEAT_GUI
825 /* Redraw the cursor and update the scrollbars when all screen updating is
826 * done. */
827 if (gui.in_use)
828 {
829 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200830 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200831 {
832 /* Put the GUI position where the cursor was, gui_update_cursor()
833 * uses that. */
834 gui.col = gui_cursor_col;
835 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200836# ifdef FEAT_MBYTE
837 gui.col = mb_fix_col(gui.col, gui.row);
838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200840 screen_cur_col = gui.col;
841 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 gui_update_scrollbars(FALSE);
844 }
845#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200846 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847}
848
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100849#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
850/*
851 * Prepare for updating one or more windows.
852 * Caller must check for "updating_screen" already set to avoid recursiveness.
853 */
854 static void
855update_prepare(void)
856{
857 cursor_off();
858 updating_screen = TRUE;
859#ifdef FEAT_GUI
860 /* Remove the cursor before starting to do anything, because scrolling may
861 * make it difficult to redraw the text under it. */
862 if (gui.in_use)
863 gui_undraw_cursor();
864#endif
865#ifdef FEAT_SEARCH_EXTRA
866 start_search_hl();
867#endif
868}
869
870/*
871 * Finish updating one or more windows.
872 */
873 static void
874update_finish(void)
875{
876 if (redraw_cmdline)
877 showmode();
878
879# ifdef FEAT_SEARCH_EXTRA
880 end_search_hl();
881# endif
882
883 updating_screen = FALSE;
884
885# ifdef FEAT_GUI
886 gui_may_resize_shell();
887
888 /* Redraw the cursor and update the scrollbars when all screen updating is
889 * done. */
890 if (gui.in_use)
891 {
892 out_flush(); /* required before updating the cursor */
893 gui_update_cursor(FALSE, FALSE);
894 gui_update_scrollbars(FALSE);
895 }
896# endif
897}
898#endif
899
Bram Moolenaar860cae12010-06-05 23:22:07 +0200900#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200901/*
902 * Return TRUE if the cursor line in window "wp" may be concealed, according
903 * to the 'concealcursor' option.
904 */
905 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100906conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200907{
908 int c;
909
910 if (*wp->w_p_cocu == NUL)
911 return FALSE;
912 if (get_real_state() & VISUAL)
913 c = 'v';
914 else if (State & INSERT)
915 c = 'i';
916 else if (State & NORMAL)
917 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200918 else if (State & CMDLINE)
919 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200920 else
921 return FALSE;
922 return vim_strchr(wp->w_p_cocu, c) != NULL;
923}
924
925/*
926 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
927 */
928 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100929conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200930{
931 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
932 {
933 need_cursor_line_redraw = TRUE;
934 /* Need to recompute cursor column, e.g., when starting Visual mode
935 * without concealing. */
936 curs_columns(TRUE);
937 }
938}
939
Bram Moolenaar860cae12010-06-05 23:22:07 +0200940 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100941update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200942{
943 int row;
944 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200945#ifdef SYN_TIME_LIMIT
946 proftime_T syntax_tm;
947#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948
Bram Moolenaar908be432016-05-24 10:51:30 +0200949 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100950 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200951 return;
952
Bram Moolenaar860cae12010-06-05 23:22:07 +0200953 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200954 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200955 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200956#ifdef SYN_TIME_LIMIT
957 /* Set the time limit to 'redrawtime'. */
958 profile_setlimit(p_rdt, &syntax_tm);
959#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100960 update_prepare();
961
Bram Moolenaar860cae12010-06-05 23:22:07 +0200962 row = 0;
963 for (j = 0; j < wp->w_lines_valid; ++j)
964 {
965 if (lnum == wp->w_lines[j].wl_lnum)
966 {
967 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200968# ifdef FEAT_SEARCH_EXTRA
969 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200970 start_search_hl();
971 prepare_search_hl(wp, lnum);
972# endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200973 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE,
974#ifdef SYN_TIME_LIMIT
975 &syntax_tm
976#else
977 NULL
978#endif
979 );
Bram Moolenaar860cae12010-06-05 23:22:07 +0200980# if defined(FEAT_SEARCH_EXTRA)
981 end_search_hl();
982# endif
983 break;
984 }
985 row += wp->w_lines[j].wl_size;
986 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100987
988 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200989 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200990 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991}
992#endif
993
994#if defined(FEAT_SIGNS) || defined(PROTO)
995 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100996update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997{
998 win_T *wp;
999 int doit = FALSE;
1000
1001# ifdef FEAT_FOLDING
1002 win_foldinfo.fi_level = 0;
1003# endif
1004
1005 /* update/delete a specific mark */
1006 FOR_ALL_WINDOWS(wp)
1007 {
1008 if (buf != NULL && lnum > 0)
1009 {
1010 if (wp->w_buffer == buf && lnum >= wp->w_topline
1011 && lnum < wp->w_botline)
1012 {
1013 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1014 wp->w_redraw_top = lnum;
1015 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1016 wp->w_redraw_bot = lnum;
1017 redraw_win_later(wp, VALID);
1018 }
1019 }
1020 else
1021 redraw_win_later(wp, VALID);
1022 if (wp->w_redr_type != 0)
1023 doit = TRUE;
1024 }
1025
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001026 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001027 * happening (recursive call), messages on the screen or still starting up.
1028 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001029 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001030 || State == ASKMORE || State == HITRETURN
1031 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001032#ifdef FEAT_GUI
1033 || gui.starting
1034#endif
1035 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 return;
1037
1038 /* update all windows that need updating */
1039 update_prepare();
1040
1041# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001042 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 if (wp->w_redr_type != 0)
1045 win_update(wp);
1046 if (wp->w_redr_status)
1047 win_redr_status(wp);
1048 }
1049# else
1050 if (curwin->w_redr_type != 0)
1051 win_update(curwin);
1052# endif
1053
1054 update_finish();
1055}
1056#endif
1057
1058
1059#if defined(FEAT_GUI) || defined(PROTO)
1060/*
1061 * Update a single window, its status line and maybe the command line msg.
1062 * Used for the GUI scrollbar.
1063 */
1064 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001065updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001067 /* return if already busy updating */
1068 if (updating_screen)
1069 return;
1070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 update_prepare();
1072
1073#ifdef FEAT_CLIPBOARD
1074 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001075 if (clip_star.available && clip_isautosel_star())
1076 clip_update_selection(&clip_star);
1077 if (clip_plus.available && clip_isautosel_plus())
1078 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001084 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001085 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001086 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001087
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (wp->w_redr_status
1089# ifdef FEAT_CMDL_INFO
1090 || p_ru
1091# endif
1092# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001093 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094# endif
1095 )
1096 win_redr_status(wp);
1097#endif
1098
1099 update_finish();
1100}
1101#endif
1102
1103/*
1104 * Update a single window.
1105 *
1106 * This may cause the windows below it also to be redrawn (when clearing the
1107 * screen or scrolling lines).
1108 *
1109 * How the window is redrawn depends on wp->w_redr_type. Each type also
1110 * implies the one below it.
1111 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001112 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1114 * INVERTED redraw the changed part of the Visual area
1115 * INVERTED_ALL redraw the whole Visual area
1116 * VALID 1. scroll up/down to adjust for a changed w_topline
1117 * 2. update lines at the top when scrolled down
1118 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001119 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 * b_mod_top and b_mod_bot.
1121 * - if wp->w_redraw_top non-zero, redraw lines between
1122 * wp->w_redraw_top and wp->w_redr_bot.
1123 * - continue redrawing when syntax status is invalid.
1124 * 4. if scrolled up, update lines at the bottom.
1125 * This results in three areas that may need updating:
1126 * top: from first row to top_end (when scrolled down)
1127 * mid: from mid_start to mid_end (update inversion or changed text)
1128 * bot: from bot_start to last row (when scrolled up)
1129 */
1130 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001131win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
1133 buf_T *buf = wp->w_buffer;
1134 int type;
1135 int top_end = 0; /* Below last row of the top area that needs
1136 updating. 0 when no top area updating. */
1137 int mid_start = 999;/* first row of the mid area that needs
1138 updating. 999 when no mid area updating. */
1139 int mid_end = 0; /* Below last row of the mid area that needs
1140 updating. 0 when no mid area updating. */
1141 int bot_start = 999;/* first row of the bot area that needs
1142 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 int scrolled_down = FALSE; /* TRUE when scrolled down when
1144 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001146 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 int top_to_mod = FALSE; /* redraw above mod_top */
1148#endif
1149
1150 int row; /* current window row to display */
1151 linenr_T lnum; /* current buffer lnum to display */
1152 int idx; /* current index in w_lines[] */
1153 int srow; /* starting row of the current line */
1154
1155 int eof = FALSE; /* if TRUE, we hit the end of the file */
1156 int didline = FALSE; /* if TRUE, we finished the last line */
1157 int i;
1158 long j;
1159 static int recursive = FALSE; /* being called recursively */
1160 int old_botline = wp->w_botline;
1161#ifdef FEAT_FOLDING
1162 long fold_count;
1163#endif
1164#ifdef FEAT_SYN_HL
1165 /* remember what happened to the previous line, to know if
1166 * check_visual_highlight() can be used */
1167#define DID_NONE 1 /* didn't update a line */
1168#define DID_LINE 2 /* updated a normal line */
1169#define DID_FOLD 3 /* updated a folded line */
1170 int did_update = DID_NONE;
1171 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1172#endif
1173 linenr_T mod_top = 0;
1174 linenr_T mod_bot = 0;
1175#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1176 int save_got_int;
1177#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001178#ifdef SYN_TIME_LIMIT
1179 proftime_T syntax_tm;
1180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
1182 type = wp->w_redr_type;
1183
1184 if (type == NOT_VALID)
1185 {
1186#ifdef FEAT_WINDOWS
1187 wp->w_redr_status = TRUE;
1188#endif
1189 wp->w_lines_valid = 0;
1190 }
1191
1192 /* Window is zero-height: nothing to draw. */
1193 if (wp->w_height == 0)
1194 {
1195 wp->w_redr_type = 0;
1196 return;
1197 }
1198
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001199#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 /* Window is zero-width: Only need to draw the separator. */
1201 if (wp->w_width == 0)
1202 {
1203 /* draw the vertical separator right of this window */
1204 draw_vsep_win(wp, 0);
1205 wp->w_redr_type = 0;
1206 return;
1207 }
1208#endif
1209
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001210#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001211 /* If this window contains a terminal, redraw works completely differently.
1212 */
1213 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001214 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001215 wp->w_redr_type = 0;
1216 return;
1217 }
1218#endif
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001221 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#endif
1223
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001224#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001225 /* Force redraw when width of 'number' or 'relativenumber' column
1226 * changes. */
1227 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001228 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001229 {
1230 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001231 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001232 }
1233 else
1234#endif
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1237 {
1238 /*
1239 * When there are both inserted/deleted lines and specific lines to be
1240 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1241 * everything (only happens when redrawing is off for while).
1242 */
1243 type = NOT_VALID;
1244 }
1245 else
1246 {
1247 /*
1248 * Set mod_top to the first line that needs displaying because of
1249 * changes. Set mod_bot to the first line after the changes.
1250 */
1251 mod_top = wp->w_redraw_top;
1252 if (wp->w_redraw_bot != 0)
1253 mod_bot = wp->w_redraw_bot + 1;
1254 else
1255 mod_bot = 0;
1256 wp->w_redraw_top = 0; /* reset for next time */
1257 wp->w_redraw_bot = 0;
1258 if (buf->b_mod_set)
1259 {
1260 if (mod_top == 0 || mod_top > buf->b_mod_top)
1261 {
1262 mod_top = buf->b_mod_top;
1263#ifdef FEAT_SYN_HL
1264 /* Need to redraw lines above the change that may be included
1265 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001266 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001268 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (mod_top < 1)
1270 mod_top = 1;
1271 }
1272#endif
1273 }
1274 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1275 mod_bot = buf->b_mod_bot;
1276
1277#ifdef FEAT_SEARCH_EXTRA
1278 /* When 'hlsearch' is on and using a multi-line search pattern, a
1279 * change in one line may make the Search highlighting in a
1280 * previous line invalid. Simple solution: redraw all visible
1281 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001282 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001284 if (search_hl.rm.regprog != NULL
1285 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001287 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001288 {
1289 cur = wp->w_match_head;
1290 while (cur != NULL)
1291 {
1292 if (cur->match.regprog != NULL
1293 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001294 {
1295 top_to_mod = TRUE;
1296 break;
1297 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001298 cur = cur->next;
1299 }
1300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301#endif
1302 }
1303#ifdef FEAT_FOLDING
1304 if (mod_top != 0 && hasAnyFolding(wp))
1305 {
1306 linenr_T lnumt, lnumb;
1307
1308 /*
1309 * A change in a line can cause lines above it to become folded or
1310 * unfolded. Find the top most buffer line that may be affected.
1311 * If the line was previously folded and displayed, get the first
1312 * line of that fold. If the line is folded now, get the first
1313 * folded line. Use the minimum of these two.
1314 */
1315
1316 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1317 * the line below it. If there is no valid entry, use w_topline.
1318 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1319 * to this line. If there is no valid entry, use MAXLNUM. */
1320 lnumt = wp->w_topline;
1321 lnumb = MAXLNUM;
1322 for (i = 0; i < wp->w_lines_valid; ++i)
1323 if (wp->w_lines[i].wl_valid)
1324 {
1325 if (wp->w_lines[i].wl_lastlnum < mod_top)
1326 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1327 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1328 {
1329 lnumb = wp->w_lines[i].wl_lnum;
1330 /* When there is a fold column it might need updating
1331 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001332 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 ++lnumb;
1334 }
1335 }
1336
1337 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1338 if (mod_top > lnumt)
1339 mod_top = lnumt;
1340
1341 /* Now do the same for the bottom line (one above mod_bot). */
1342 --mod_bot;
1343 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1344 ++mod_bot;
1345 if (mod_bot < lnumb)
1346 mod_bot = lnumb;
1347 }
1348#endif
1349
1350 /* When a change starts above w_topline and the end is below
1351 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001352 * If the end of the change is above w_topline: do like no change was
1353 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (mod_top != 0 && mod_top < wp->w_topline)
1355 {
1356 if (mod_bot > wp->w_topline)
1357 mod_top = wp->w_topline;
1358#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001359 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 top_end = 1;
1361#endif
1362 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001363
1364 /* When line numbers are displayed need to redraw all lines below
1365 * inserted/deleted lines. */
1366 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1367 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369
1370 /*
1371 * When only displaying the lines at the top, set top_end. Used when
1372 * window has scrolled down for msg_scrolled.
1373 */
1374 if (type == REDRAW_TOP)
1375 {
1376 j = 0;
1377 for (i = 0; i < wp->w_lines_valid; ++i)
1378 {
1379 j += wp->w_lines[i].wl_size;
1380 if (j >= wp->w_upd_rows)
1381 {
1382 top_end = j;
1383 break;
1384 }
1385 }
1386 if (top_end == 0)
1387 /* not found (cannot happen?): redraw everything */
1388 type = NOT_VALID;
1389 else
1390 /* top area defined, the rest is VALID */
1391 type = VALID;
1392 }
1393
Bram Moolenaar367329b2007-08-30 11:53:22 +00001394 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001395 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1396 * non-zero and thus not FALSE) will indicate that screenclear() was not
1397 * called. */
1398 if (screen_cleared)
1399 screen_cleared = MAYBE;
1400
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 /*
1402 * If there are no changes on the screen that require a complete redraw,
1403 * handle three cases:
1404 * 1: we are off the top of the screen by a few lines: scroll down
1405 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1406 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1407 * w_lines[] that needs updating.
1408 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001409 if ((type == VALID || type == SOME_VALID
1410 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411#ifdef FEAT_DIFF
1412 && !wp->w_botfill && !wp->w_old_botfill
1413#endif
1414 )
1415 {
1416 if (mod_top != 0 && wp->w_topline == mod_top)
1417 {
1418 /*
1419 * w_topline is the first changed line, the scrolling will be done
1420 * further down.
1421 */
1422 }
1423 else if (wp->w_lines[0].wl_valid
1424 && (wp->w_topline < wp->w_lines[0].wl_lnum
1425#ifdef FEAT_DIFF
1426 || (wp->w_topline == wp->w_lines[0].wl_lnum
1427 && wp->w_topfill > wp->w_old_topfill)
1428#endif
1429 ))
1430 {
1431 /*
1432 * New topline is above old topline: May scroll down.
1433 */
1434#ifdef FEAT_FOLDING
1435 if (hasAnyFolding(wp))
1436 {
1437 linenr_T ln;
1438
1439 /* count the number of lines we are off, counting a sequence
1440 * of folded lines as one */
1441 j = 0;
1442 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1443 {
1444 ++j;
1445 if (j >= wp->w_height - 2)
1446 break;
1447 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1448 }
1449 }
1450 else
1451#endif
1452 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1453 if (j < wp->w_height - 2) /* not too far off */
1454 {
1455 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1456#ifdef FEAT_DIFF
1457 /* insert extra lines for previously invisible filler lines */
1458 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1459 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1460 - wp->w_old_topfill;
1461#endif
1462 if (i < wp->w_height - 2) /* less than a screen off */
1463 {
1464 /*
1465 * Try to insert the correct number of lines.
1466 * If not the last window, delete the lines at the bottom.
1467 * win_ins_lines may fail when the terminal can't do it.
1468 */
1469 if (i > 0)
1470 check_for_delay(FALSE);
1471 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1472 {
1473 if (wp->w_lines_valid != 0)
1474 {
1475 /* Need to update rows that are new, stop at the
1476 * first one that scrolled down. */
1477 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
1480 /* Move the entries that were scrolled, disable
1481 * the entries for the lines to be redrawn. */
1482 if ((wp->w_lines_valid += j) > wp->w_height)
1483 wp->w_lines_valid = wp->w_height;
1484 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1485 wp->w_lines[idx] = wp->w_lines[idx - j];
1486 while (idx >= 0)
1487 wp->w_lines[idx--].wl_valid = FALSE;
1488 }
1489 }
1490 else
1491 mid_start = 0; /* redraw all lines */
1492 }
1493 else
1494 mid_start = 0; /* redraw all lines */
1495 }
1496 else
1497 mid_start = 0; /* redraw all lines */
1498 }
1499 else
1500 {
1501 /*
1502 * New topline is at or below old topline: May scroll up.
1503 * When topline didn't change, find first entry in w_lines[] that
1504 * needs updating.
1505 */
1506
1507 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1508 j = -1;
1509 row = 0;
1510 for (i = 0; i < wp->w_lines_valid; i++)
1511 {
1512 if (wp->w_lines[i].wl_valid
1513 && wp->w_lines[i].wl_lnum == wp->w_topline)
1514 {
1515 j = i;
1516 break;
1517 }
1518 row += wp->w_lines[i].wl_size;
1519 }
1520 if (j == -1)
1521 {
1522 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1523 * lines */
1524 mid_start = 0;
1525 }
1526 else
1527 {
1528 /*
1529 * Try to delete the correct number of lines.
1530 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1531 */
1532#ifdef FEAT_DIFF
1533 /* If the topline didn't change, delete old filler lines,
1534 * otherwise delete filler lines of the new topline... */
1535 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1536 row += wp->w_old_topfill;
1537 else
1538 row += diff_check_fill(wp, wp->w_topline);
1539 /* ... but don't delete new filler lines. */
1540 row -= wp->w_topfill;
1541#endif
1542 if (row > 0)
1543 {
1544 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001545 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1546 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 bot_start = wp->w_height - row;
1548 else
1549 mid_start = 0; /* redraw all lines */
1550 }
1551 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1552 {
1553 /*
1554 * Skip the lines (below the deleted lines) that are still
1555 * valid and don't need redrawing. Copy their info
1556 * upwards, to compensate for the deleted lines. Set
1557 * bot_start to the first row that needs redrawing.
1558 */
1559 bot_start = 0;
1560 idx = 0;
1561 for (;;)
1562 {
1563 wp->w_lines[idx] = wp->w_lines[j];
1564 /* stop at line that didn't fit, unless it is still
1565 * valid (no lines deleted) */
1566 if (row > 0 && bot_start + row
1567 + (int)wp->w_lines[j].wl_size > wp->w_height)
1568 {
1569 wp->w_lines_valid = idx + 1;
1570 break;
1571 }
1572 bot_start += wp->w_lines[idx++].wl_size;
1573
1574 /* stop at the last valid entry in w_lines[].wl_size */
1575 if (++j >= wp->w_lines_valid)
1576 {
1577 wp->w_lines_valid = idx;
1578 break;
1579 }
1580 }
1581#ifdef FEAT_DIFF
1582 /* Correct the first entry for filler lines at the top
1583 * when it won't get updated below. */
1584 if (wp->w_p_diff && bot_start > 0)
1585 wp->w_lines[0].wl_size =
1586 plines_win_nofill(wp, wp->w_topline, TRUE)
1587 + wp->w_topfill;
1588#endif
1589 }
1590 }
1591 }
1592
1593 /* When starting redraw in the first line, redraw all lines. When
1594 * there is only one window it's probably faster to clear the screen
1595 * first. */
1596 if (mid_start == 0)
1597 {
1598 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001599 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001600 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001601 /* Clear the screen when it was not done by win_del_lines() or
1602 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1603 * then. */
1604 if (screen_cleared != TRUE)
1605 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001606#ifdef FEAT_WINDOWS
1607 /* The screen was cleared, redraw the tab pages line. */
1608 if (redraw_tabline)
1609 draw_tabline();
1610#endif
1611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001613
1614 /* When win_del_lines() or win_ins_lines() caused the screen to be
1615 * cleared (only happens for the first window) or when screenclear()
1616 * was called directly above, "must_redraw" will have been set to
1617 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1618 if (screen_cleared == TRUE)
1619 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621 else
1622 {
1623 /* Not VALID or INVERTED: redraw all lines. */
1624 mid_start = 0;
1625 mid_end = wp->w_height;
1626 }
1627
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001628 if (type == SOME_VALID)
1629 {
1630 /* SOME_VALID: redraw all lines. */
1631 mid_start = 0;
1632 mid_end = wp->w_height;
1633 type = NOT_VALID;
1634 }
1635
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 /* check if we are updating or removing the inverted part */
1637 if ((VIsual_active && buf == curwin->w_buffer)
1638 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1639 {
1640 linenr_T from, to;
1641
1642 if (VIsual_active)
1643 {
1644 if (VIsual_active
1645 && (VIsual_mode != wp->w_old_visual_mode
1646 || type == INVERTED_ALL))
1647 {
1648 /*
1649 * If the type of Visual selection changed, redraw the whole
1650 * selection. Also when the ownership of the X selection is
1651 * gained or lost.
1652 */
1653 if (curwin->w_cursor.lnum < VIsual.lnum)
1654 {
1655 from = curwin->w_cursor.lnum;
1656 to = VIsual.lnum;
1657 }
1658 else
1659 {
1660 from = VIsual.lnum;
1661 to = curwin->w_cursor.lnum;
1662 }
1663 /* redraw more when the cursor moved as well */
1664 if (wp->w_old_cursor_lnum < from)
1665 from = wp->w_old_cursor_lnum;
1666 if (wp->w_old_cursor_lnum > to)
1667 to = wp->w_old_cursor_lnum;
1668 if (wp->w_old_visual_lnum < from)
1669 from = wp->w_old_visual_lnum;
1670 if (wp->w_old_visual_lnum > to)
1671 to = wp->w_old_visual_lnum;
1672 }
1673 else
1674 {
1675 /*
1676 * Find the line numbers that need to be updated: The lines
1677 * between the old cursor position and the current cursor
1678 * position. Also check if the Visual position changed.
1679 */
1680 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1681 {
1682 from = curwin->w_cursor.lnum;
1683 to = wp->w_old_cursor_lnum;
1684 }
1685 else
1686 {
1687 from = wp->w_old_cursor_lnum;
1688 to = curwin->w_cursor.lnum;
1689 if (from == 0) /* Visual mode just started */
1690 from = to;
1691 }
1692
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001693 if (VIsual.lnum != wp->w_old_visual_lnum
1694 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 {
1696 if (wp->w_old_visual_lnum < from
1697 && wp->w_old_visual_lnum != 0)
1698 from = wp->w_old_visual_lnum;
1699 if (wp->w_old_visual_lnum > to)
1700 to = wp->w_old_visual_lnum;
1701 if (VIsual.lnum < from)
1702 from = VIsual.lnum;
1703 if (VIsual.lnum > to)
1704 to = VIsual.lnum;
1705 }
1706 }
1707
1708 /*
1709 * If in block mode and changed column or curwin->w_curswant:
1710 * update all lines.
1711 * First compute the actual start and end column.
1712 */
1713 if (VIsual_mode == Ctrl_V)
1714 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001715 colnr_T fromc, toc;
1716#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1717 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
Bram Moolenaar404406a2014-10-09 13:24:43 +02001719 if (curwin->w_p_lbr)
1720 ve_flags = VE_ALL;
1721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001723#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1724 ve_flags = save_ve_flags;
1725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 ++toc;
1727 if (curwin->w_curswant == MAXCOL)
1728 toc = MAXCOL;
1729
1730 if (fromc != wp->w_old_cursor_fcol
1731 || toc != wp->w_old_cursor_lcol)
1732 {
1733 if (from > VIsual.lnum)
1734 from = VIsual.lnum;
1735 if (to < VIsual.lnum)
1736 to = VIsual.lnum;
1737 }
1738 wp->w_old_cursor_fcol = fromc;
1739 wp->w_old_cursor_lcol = toc;
1740 }
1741 }
1742 else
1743 {
1744 /* Use the line numbers of the old Visual area. */
1745 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1746 {
1747 from = wp->w_old_cursor_lnum;
1748 to = wp->w_old_visual_lnum;
1749 }
1750 else
1751 {
1752 from = wp->w_old_visual_lnum;
1753 to = wp->w_old_cursor_lnum;
1754 }
1755 }
1756
1757 /*
1758 * There is no need to update lines above the top of the window.
1759 */
1760 if (from < wp->w_topline)
1761 from = wp->w_topline;
1762
1763 /*
1764 * If we know the value of w_botline, use it to restrict the update to
1765 * the lines that are visible in the window.
1766 */
1767 if (wp->w_valid & VALID_BOTLINE)
1768 {
1769 if (from >= wp->w_botline)
1770 from = wp->w_botline - 1;
1771 if (to >= wp->w_botline)
1772 to = wp->w_botline - 1;
1773 }
1774
1775 /*
1776 * Find the minimal part to be updated.
1777 * Watch out for scrolling that made entries in w_lines[] invalid.
1778 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1779 * top_end; need to redraw from top_end to the "to" line.
1780 * A middle mouse click with a Visual selection may change the text
1781 * above the Visual area and reset wl_valid, do count these for
1782 * mid_end (in srow).
1783 */
1784 if (mid_start > 0)
1785 {
1786 lnum = wp->w_topline;
1787 idx = 0;
1788 srow = 0;
1789 if (scrolled_down)
1790 mid_start = top_end;
1791 else
1792 mid_start = 0;
1793 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1794 {
1795 if (wp->w_lines[idx].wl_valid)
1796 mid_start += wp->w_lines[idx].wl_size;
1797 else if (!scrolled_down)
1798 srow += wp->w_lines[idx].wl_size;
1799 ++idx;
1800# ifdef FEAT_FOLDING
1801 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1802 lnum = wp->w_lines[idx].wl_lnum;
1803 else
1804# endif
1805 ++lnum;
1806 }
1807 srow += mid_start;
1808 mid_end = wp->w_height;
1809 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1810 {
1811 if (wp->w_lines[idx].wl_valid
1812 && wp->w_lines[idx].wl_lnum >= to + 1)
1813 {
1814 /* Only update until first row of this line */
1815 mid_end = srow;
1816 break;
1817 }
1818 srow += wp->w_lines[idx].wl_size;
1819 }
1820 }
1821 }
1822
1823 if (VIsual_active && buf == curwin->w_buffer)
1824 {
1825 wp->w_old_visual_mode = VIsual_mode;
1826 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1827 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001828 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 wp->w_old_curswant = curwin->w_curswant;
1830 }
1831 else
1832 {
1833 wp->w_old_visual_mode = 0;
1834 wp->w_old_cursor_lnum = 0;
1835 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001836 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1840 /* reset got_int, otherwise regexp won't work */
1841 save_got_int = got_int;
1842 got_int = 0;
1843#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001844#ifdef SYN_TIME_LIMIT
1845 /* Set the time limit to 'redrawtime'. */
1846 profile_setlimit(p_rdt, &syntax_tm);
1847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848#ifdef FEAT_FOLDING
1849 win_foldinfo.fi_level = 0;
1850#endif
1851
1852 /*
1853 * Update all the window rows.
1854 */
1855 idx = 0; /* first entry in w_lines[].wl_size */
1856 row = 0;
1857 srow = 0;
1858 lnum = wp->w_topline; /* first line shown in window */
1859 for (;;)
1860 {
1861 /* stop updating when reached the end of the window (check for _past_
1862 * the end of the window is at the end of the loop) */
1863 if (row == wp->w_height)
1864 {
1865 didline = TRUE;
1866 break;
1867 }
1868
1869 /* stop updating when hit the end of the file */
1870 if (lnum > buf->b_ml.ml_line_count)
1871 {
1872 eof = TRUE;
1873 break;
1874 }
1875
1876 /* Remember the starting row of the line that is going to be dealt
1877 * with. It is used further down when the line doesn't fit. */
1878 srow = row;
1879
1880 /*
1881 * Update a line when it is in an area that needs updating, when it
1882 * has changes or w_lines[idx] is invalid.
1883 * bot_start may be halfway a wrapped line after using
1884 * win_del_lines(), check if the current line includes it.
1885 * When syntax folding is being used, the saved syntax states will
1886 * already have been updated, we can't see where the syntax state is
1887 * the same again, just update until the end of the window.
1888 */
1889 if (row < top_end
1890 || (row >= mid_start && row < mid_end)
1891#ifdef FEAT_SEARCH_EXTRA
1892 || top_to_mod
1893#endif
1894 || idx >= wp->w_lines_valid
1895 || (row + wp->w_lines[idx].wl_size > bot_start)
1896 || (mod_top != 0
1897 && (lnum == mod_top
1898 || (lnum >= mod_top
1899 && (lnum < mod_bot
1900#ifdef FEAT_SYN_HL
1901 || did_update == DID_FOLD
1902 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001903 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 && (
1905# ifdef FEAT_FOLDING
1906 (foldmethodIsSyntax(wp)
1907 && hasAnyFolding(wp)) ||
1908# endif
1909 syntax_check_changed(lnum)))
1910#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001911#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001912 /* match in fixed position might need redraw
1913 * if lines were inserted or deleted */
1914 || (wp->w_match_head != NULL
1915 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 )))))
1918 {
1919#ifdef FEAT_SEARCH_EXTRA
1920 if (lnum == mod_top)
1921 top_to_mod = FALSE;
1922#endif
1923
1924 /*
1925 * When at start of changed lines: May scroll following lines
1926 * up or down to minimize redrawing.
1927 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001928 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 */
1930 if (lnum == mod_top
1931 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001932 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 {
1934 int old_rows = 0;
1935 int new_rows = 0;
1936 int xtra_rows;
1937 linenr_T l;
1938
1939 /* Count the old number of window rows, using w_lines[], which
1940 * should still contain the sizes for the lines as they are
1941 * currently displayed. */
1942 for (i = idx; i < wp->w_lines_valid; ++i)
1943 {
1944 /* Only valid lines have a meaningful wl_lnum. Invalid
1945 * lines are part of the changed area. */
1946 if (wp->w_lines[i].wl_valid
1947 && wp->w_lines[i].wl_lnum == mod_bot)
1948 break;
1949 old_rows += wp->w_lines[i].wl_size;
1950#ifdef FEAT_FOLDING
1951 if (wp->w_lines[i].wl_valid
1952 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1953 {
1954 /* Must have found the last valid entry above mod_bot.
1955 * Add following invalid entries. */
1956 ++i;
1957 while (i < wp->w_lines_valid
1958 && !wp->w_lines[i].wl_valid)
1959 old_rows += wp->w_lines[i++].wl_size;
1960 break;
1961 }
1962#endif
1963 }
1964
1965 if (i >= wp->w_lines_valid)
1966 {
1967 /* We can't find a valid line below the changed lines,
1968 * need to redraw until the end of the window.
1969 * Inserting/deleting lines has no use. */
1970 bot_start = 0;
1971 }
1972 else
1973 {
1974 /* Able to count old number of rows: Count new window
1975 * rows, and may insert/delete lines */
1976 j = idx;
1977 for (l = lnum; l < mod_bot; ++l)
1978 {
1979#ifdef FEAT_FOLDING
1980 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1981 ++new_rows;
1982 else
1983#endif
1984#ifdef FEAT_DIFF
1985 if (l == wp->w_topline)
1986 new_rows += plines_win_nofill(wp, l, TRUE)
1987 + wp->w_topfill;
1988 else
1989#endif
1990 new_rows += plines_win(wp, l, TRUE);
1991 ++j;
1992 if (new_rows > wp->w_height - row - 2)
1993 {
1994 /* it's getting too much, must redraw the rest */
1995 new_rows = 9999;
1996 break;
1997 }
1998 }
1999 xtra_rows = new_rows - old_rows;
2000 if (xtra_rows < 0)
2001 {
2002 /* May scroll text up. If there is not enough
2003 * remaining text or scrolling fails, must redraw the
2004 * rest. If scrolling works, must redraw the text
2005 * below the scrolled text. */
2006 if (row - xtra_rows >= wp->w_height - 2)
2007 mod_bot = MAXLNUM;
2008 else
2009 {
2010 check_for_delay(FALSE);
2011 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002012 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 mod_bot = MAXLNUM;
2014 else
2015 bot_start = wp->w_height + xtra_rows;
2016 }
2017 }
2018 else if (xtra_rows > 0)
2019 {
2020 /* May scroll text down. If there is not enough
2021 * remaining text of scrolling fails, must redraw the
2022 * rest. */
2023 if (row + xtra_rows >= wp->w_height - 2)
2024 mod_bot = MAXLNUM;
2025 else
2026 {
2027 check_for_delay(FALSE);
2028 if (win_ins_lines(wp, row + old_rows,
2029 xtra_rows, FALSE, FALSE) == FAIL)
2030 mod_bot = MAXLNUM;
2031 else if (top_end > row + old_rows)
2032 /* Scrolled the part at the top that requires
2033 * updating down. */
2034 top_end += xtra_rows;
2035 }
2036 }
2037
2038 /* When not updating the rest, may need to move w_lines[]
2039 * entries. */
2040 if (mod_bot != MAXLNUM && i != j)
2041 {
2042 if (j < i)
2043 {
2044 int x = row + new_rows;
2045
2046 /* move entries in w_lines[] upwards */
2047 for (;;)
2048 {
2049 /* stop at last valid entry in w_lines[] */
2050 if (i >= wp->w_lines_valid)
2051 {
2052 wp->w_lines_valid = j;
2053 break;
2054 }
2055 wp->w_lines[j] = wp->w_lines[i];
2056 /* stop at a line that won't fit */
2057 if (x + (int)wp->w_lines[j].wl_size
2058 > wp->w_height)
2059 {
2060 wp->w_lines_valid = j + 1;
2061 break;
2062 }
2063 x += wp->w_lines[j++].wl_size;
2064 ++i;
2065 }
2066 if (bot_start > x)
2067 bot_start = x;
2068 }
2069 else /* j > i */
2070 {
2071 /* move entries in w_lines[] downwards */
2072 j -= i;
2073 wp->w_lines_valid += j;
2074 if (wp->w_lines_valid > wp->w_height)
2075 wp->w_lines_valid = wp->w_height;
2076 for (i = wp->w_lines_valid; i - j >= idx; --i)
2077 wp->w_lines[i] = wp->w_lines[i - j];
2078
2079 /* The w_lines[] entries for inserted lines are
2080 * now invalid, but wl_size may be used above.
2081 * Reset to zero. */
2082 while (i >= idx)
2083 {
2084 wp->w_lines[i].wl_size = 0;
2085 wp->w_lines[i--].wl_valid = FALSE;
2086 }
2087 }
2088 }
2089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 }
2091
2092#ifdef FEAT_FOLDING
2093 /*
2094 * When lines are folded, display one line for all of them.
2095 * Otherwise, display normally (can be several display lines when
2096 * 'wrap' is on).
2097 */
2098 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2099 if (fold_count != 0)
2100 {
2101 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2102 ++row;
2103 --fold_count;
2104 wp->w_lines[idx].wl_folded = TRUE;
2105 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2106# ifdef FEAT_SYN_HL
2107 did_update = DID_FOLD;
2108# endif
2109 }
2110 else
2111#endif
2112 if (idx < wp->w_lines_valid
2113 && wp->w_lines[idx].wl_valid
2114 && wp->w_lines[idx].wl_lnum == lnum
2115 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002116 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 && srow + wp->w_lines[idx].wl_size > wp->w_height
2118#ifdef FEAT_DIFF
2119 && diff_check_fill(wp, lnum) == 0
2120#endif
2121 )
2122 {
2123 /* This line is not going to fit. Don't draw anything here,
2124 * will draw "@ " lines below. */
2125 row = wp->w_height + 1;
2126 }
2127 else
2128 {
2129#ifdef FEAT_SEARCH_EXTRA
2130 prepare_search_hl(wp, lnum);
2131#endif
2132#ifdef FEAT_SYN_HL
2133 /* Let the syntax stuff know we skipped a few lines. */
2134 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002135 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 syntax_end_parsing(syntax_last_parsed + 1);
2137#endif
2138
2139 /*
2140 * Display one line.
2141 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002142 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2143#ifdef SYN_TIME_LIMIT
2144 &syntax_tm
2145#else
2146 NULL
2147#endif
2148 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150#ifdef FEAT_FOLDING
2151 wp->w_lines[idx].wl_folded = FALSE;
2152 wp->w_lines[idx].wl_lastlnum = lnum;
2153#endif
2154#ifdef FEAT_SYN_HL
2155 did_update = DID_LINE;
2156 syntax_last_parsed = lnum;
2157#endif
2158 }
2159
2160 wp->w_lines[idx].wl_lnum = lnum;
2161 wp->w_lines[idx].wl_valid = TRUE;
2162 if (row > wp->w_height) /* past end of screen */
2163 {
2164 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002165 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2167 ++idx;
2168 break;
2169 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002170 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 wp->w_lines[idx].wl_size = row - srow;
2172 ++idx;
2173#ifdef FEAT_FOLDING
2174 lnum += fold_count + 1;
2175#else
2176 ++lnum;
2177#endif
2178 }
2179 else
2180 {
2181 /* This line does not need updating, advance to the next one */
2182 row += wp->w_lines[idx++].wl_size;
2183 if (row > wp->w_height) /* past end of screen */
2184 break;
2185#ifdef FEAT_FOLDING
2186 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2187#else
2188 ++lnum;
2189#endif
2190#ifdef FEAT_SYN_HL
2191 did_update = DID_NONE;
2192#endif
2193 }
2194
2195 if (lnum > buf->b_ml.ml_line_count)
2196 {
2197 eof = TRUE;
2198 break;
2199 }
2200 }
2201 /*
2202 * End of loop over all window lines.
2203 */
2204
2205
2206 if (idx > wp->w_lines_valid)
2207 wp->w_lines_valid = idx;
2208
2209#ifdef FEAT_SYN_HL
2210 /*
2211 * Let the syntax stuff know we stop parsing here.
2212 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002213 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 syntax_end_parsing(syntax_last_parsed + 1);
2215#endif
2216
2217 /*
2218 * If we didn't hit the end of the file, and we didn't finish the last
2219 * line we were working on, then the line didn't fit.
2220 */
2221 wp->w_empty_rows = 0;
2222#ifdef FEAT_DIFF
2223 wp->w_filler_rows = 0;
2224#endif
2225 if (!eof && !didline)
2226 {
2227 if (lnum == wp->w_topline)
2228 {
2229 /*
2230 * Single line that does not fit!
2231 * Don't overwrite it, it can be edited.
2232 */
2233 wp->w_botline = lnum + 1;
2234 }
2235#ifdef FEAT_DIFF
2236 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2237 {
2238 /* Window ends in filler lines. */
2239 wp->w_botline = lnum;
2240 wp->w_filler_rows = wp->w_height - srow;
2241 }
2242#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002243 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2244 {
2245 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2246
2247 /*
2248 * Last line isn't finished: Display "@@@" in the last screen line.
2249 */
2250 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002251 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002252 screen_fill(scr_row, scr_row + 1,
2253 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002254 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002255 set_empty_rows(wp, srow);
2256 wp->w_botline = lnum;
2257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2259 {
2260 /*
2261 * Last line isn't finished: Display "@@@" at the end.
2262 */
2263 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2264 W_WINROW(wp) + wp->w_height,
2265 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002266 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 set_empty_rows(wp, srow);
2268 wp->w_botline = lnum;
2269 }
2270 else
2271 {
2272 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2273 wp->w_botline = lnum;
2274 }
2275 }
2276 else
2277 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002278#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 draw_vsep_win(wp, row);
2280#endif
2281 if (eof) /* we hit the end of the file */
2282 {
2283 wp->w_botline = buf->b_ml.ml_line_count + 1;
2284#ifdef FEAT_DIFF
2285 j = diff_check_fill(wp, wp->w_botline);
2286 if (j > 0 && !wp->w_botfill)
2287 {
2288 /*
2289 * Display filler lines at the end of the file
2290 */
2291 if (char2cells(fill_diff) > 1)
2292 i = '-';
2293 else
2294 i = fill_diff;
2295 if (row + j > wp->w_height)
2296 j = wp->w_height - row;
2297 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2298 row += j;
2299 }
2300#endif
2301 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002302 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 wp->w_botline = lnum;
2304
2305 /* make sure the rest of the screen is blank */
2306 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002307 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
2309
2310 /* Reset the type of redrawing required, the window has been updated. */
2311 wp->w_redr_type = 0;
2312#ifdef FEAT_DIFF
2313 wp->w_old_topfill = wp->w_topfill;
2314 wp->w_old_botfill = wp->w_botfill;
2315#endif
2316
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002317 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
2319 /*
2320 * There is a trick with w_botline. If we invalidate it on each
2321 * change that might modify it, this will cause a lot of expensive
2322 * calls to plines() in update_topline() each time. Therefore the
2323 * value of w_botline is often approximated, and this value is used to
2324 * compute the value of w_topline. If the value of w_botline was
2325 * wrong, check that the value of w_topline is correct (cursor is on
2326 * the visible part of the text). If it's not, we need to redraw
2327 * again. Mostly this just means scrolling up a few lines, so it
2328 * doesn't look too bad. Only do this for the current window (where
2329 * changes are relevant).
2330 */
2331 wp->w_valid |= VALID_BOTLINE;
2332 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2333 {
2334 recursive = TRUE;
2335 curwin->w_valid &= ~VALID_TOPLINE;
2336 update_topline(); /* may invalidate w_botline again */
2337 if (must_redraw != 0)
2338 {
2339 /* Don't update for changes in buffer again. */
2340 i = curbuf->b_mod_set;
2341 curbuf->b_mod_set = FALSE;
2342 win_update(curwin);
2343 must_redraw = 0;
2344 curbuf->b_mod_set = i;
2345 }
2346 recursive = FALSE;
2347 }
2348 }
2349
2350#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2351 /* restore got_int, unless CTRL-C was hit while redrawing */
2352 if (!got_int)
2353 got_int = save_got_int;
2354#endif
2355}
2356
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357/*
2358 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2359 * as the filler character.
2360 */
2361 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002362win_draw_end(
2363 win_T *wp,
2364 int c1,
2365 int c2,
2366 int row,
2367 int endrow,
2368 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2371 int n = 0;
2372# define FDC_OFF n
2373#else
2374# define FDC_OFF 0
2375#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002376#ifdef FEAT_FOLDING
2377 int fdc = compute_foldcolumn(wp, 0);
2378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380#ifdef FEAT_RIGHTLEFT
2381 if (wp->w_p_rl)
2382 {
2383 /* No check for cmdline window: should never be right-left. */
2384# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002385 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387 if (n > 0)
2388 {
2389 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002390 if (n > W_WIDTH(wp))
2391 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2393 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002394 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396# endif
2397# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002398 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
2400 int nn = n + 2;
2401
2402 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002403 if (nn > W_WIDTH(wp))
2404 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2406 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002407 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 n = nn;
2409 }
2410# endif
2411 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2412 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002413 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2415 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002416 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418 else
2419#endif
2420 {
2421#ifdef FEAT_CMDWIN
2422 if (cmdwin_type != 0 && wp == curwin)
2423 {
2424 /* draw the cmdline character in the leftmost column */
2425 n = 1;
2426 if (n > wp->w_width)
2427 n = wp->w_width;
2428 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2429 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002430 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
2432#endif
2433#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002434 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002436 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 /* draw the fold column at the left */
2439 if (nn > W_WIDTH(wp))
2440 nn = W_WIDTH(wp);
2441 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2442 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002443 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 n = nn;
2445 }
2446#endif
2447#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002448 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
2450 int nn = n + 2;
2451
2452 /* draw the sign column after the fold column */
2453 if (nn > W_WIDTH(wp))
2454 nn = W_WIDTH(wp);
2455 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2456 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002457 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 n = nn;
2459 }
2460#endif
2461 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2462 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002463 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
2465 set_empty_rows(wp, row);
2466}
2467
Bram Moolenaar1a384422010-07-14 19:53:30 +02002468#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002469static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002470
2471/*
2472 * Advance **color_cols and return TRUE when there are columns to draw.
2473 */
2474 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002475advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002476{
2477 while (**color_cols >= 0 && vcol > **color_cols)
2478 ++*color_cols;
2479 return (**color_cols >= 0);
2480}
2481#endif
2482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483#ifdef FEAT_FOLDING
2484/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002485 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2486 * space is available for window "wp", minus "col".
2487 */
2488 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002489compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002490{
2491 int fdc = wp->w_p_fdc;
2492 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2493 int wwidth = W_WIDTH(wp);
2494
2495 if (fdc > wwidth - (col + wmw))
2496 fdc = wwidth - (col + wmw);
2497 return fdc;
2498}
2499
2500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 * Display one folded line.
2502 */
2503 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002504fold_line(
2505 win_T *wp,
2506 long fold_count,
2507 foldinfo_T *foldinfo,
2508 linenr_T lnum,
2509 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002511 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 pos_T *top, *bot;
2513 linenr_T lnume = lnum + fold_count - 1;
2514 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002515 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 int col;
2518 int txtcol;
2519 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002520 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
2522 /* Build the fold line:
2523 * 1. Add the cmdwin_type for the command-line window
2524 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002525 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 * 4. Compose the text
2527 * 5. Add the text
2528 * 6. set highlighting for the Visual area an other text
2529 */
2530 col = 0;
2531
2532 /*
2533 * 1. Add the cmdwin_type for the command-line window
2534 * Ignores 'rightleft', this window is never right-left.
2535 */
2536#ifdef FEAT_CMDWIN
2537 if (cmdwin_type != 0 && wp == curwin)
2538 {
2539 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002540 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541#ifdef FEAT_MBYTE
2542 if (enc_utf8)
2543 ScreenLinesUC[off] = 0;
2544#endif
2545 ++col;
2546 }
2547#endif
2548
2549 /*
2550 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002551 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002553 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (fdc > 0)
2555 {
2556 fill_foldcolumn(buf, wp, TRUE, lnum);
2557#ifdef FEAT_RIGHTLEFT
2558 if (wp->w_p_rl)
2559 {
2560 int i;
2561
2562 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002563 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 /* reverse the fold column */
2565 for (i = 0; i < fdc; ++i)
2566 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2567 }
2568 else
2569#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002570 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 col += fdc;
2572 }
2573
2574#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002575# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2576 for (ri = 0; ri < l; ++ri) \
2577 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2578 else \
2579 for (ri = 0; ri < l; ++ri) \
2580 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002582# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2583 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584#endif
2585
Bram Moolenaar64486672010-05-16 15:46:46 +02002586 /* Set all attributes of the 'number' or 'relativenumber' column and the
2587 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002588 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
2590#ifdef FEAT_SIGNS
2591 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002592 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
2594 len = W_WIDTH(wp) - col;
2595 if (len > 0)
2596 {
2597 if (len > 2)
2598 len = 2;
2599# ifdef FEAT_RIGHTLEFT
2600 if (wp->w_p_rl)
2601 /* the line number isn't reversed */
2602 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002603 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 else
2605# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002606 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 col += len;
2608 }
2609 }
2610#endif
2611
2612 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002613 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002615 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
2617 len = W_WIDTH(wp) - col;
2618 if (len > 0)
2619 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002620 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002621 long num;
2622 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002623
2624 if (len > w + 1)
2625 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002626
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002627 if (wp->w_p_nu && !wp->w_p_rnu)
2628 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002629 num = (long)lnum;
2630 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002631 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002632 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002633 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002634 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002635 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002636 /* 'number' + 'relativenumber': cursor line shows absolute
2637 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002638 num = lnum;
2639 fmt = "%-*ld ";
2640 }
2641 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002642
Bram Moolenaar700e7342013-01-30 12:31:36 +01002643 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644#ifdef FEAT_RIGHTLEFT
2645 if (wp->w_p_rl)
2646 /* the line number isn't reversed */
2647 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002648 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 else
2650#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002651 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 col += len;
2653 }
2654 }
2655
2656 /*
2657 * 4. Compose the folded-line string with 'foldtext', if set.
2658 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002659 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 txtcol = col; /* remember where text starts */
2662
2663 /*
2664 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2665 * Right-left text is put in columns 0 - number-col, normal text is put
2666 * in columns number-col - window-width.
2667 */
2668#ifdef FEAT_MBYTE
2669 if (has_mbyte)
2670 {
2671 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002672 int u8c, u8cc[MAX_MCO];
2673 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 int idx;
2675 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002676 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677# ifdef FEAT_ARABIC
2678 int prev_c = 0; /* previous Arabic character */
2679 int prev_c1 = 0; /* first composing char for prev_c */
2680# endif
2681
2682# ifdef FEAT_RIGHTLEFT
2683 if (wp->w_p_rl)
2684 idx = off;
2685 else
2686# endif
2687 idx = off + col;
2688
2689 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2690 for (p = text; *p != NUL; )
2691 {
2692 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002693 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (col + cells > W_WIDTH(wp)
2695# ifdef FEAT_RIGHTLEFT
2696 - (wp->w_p_rl ? col : 0)
2697# endif
2698 )
2699 break;
2700 ScreenLines[idx] = *p;
2701 if (enc_utf8)
2702 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002703 u8c = utfc_ptr2char(p, u8cc);
2704 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 {
2706 ScreenLinesUC[idx] = 0;
2707#ifdef FEAT_ARABIC
2708 prev_c = u8c;
2709#endif
2710 }
2711 else
2712 {
2713#ifdef FEAT_ARABIC
2714 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2715 {
2716 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002717 int pc, pc1, nc;
2718 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 int firstbyte = *p;
2720
2721 /* The idea of what is the previous and next
2722 * character depends on 'rightleft'. */
2723 if (wp->w_p_rl)
2724 {
2725 pc = prev_c;
2726 pc1 = prev_c1;
2727 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002728 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730 else
2731 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002732 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002734 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736 prev_c = u8c;
2737
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002738 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 pc, pc1, nc);
2740 ScreenLines[idx] = firstbyte;
2741 }
2742 else
2743 prev_c = u8c;
2744#endif
2745 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002746#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if (u8c >= 0x10000)
2748 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2749 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002752 for (i = 0; i < Screen_mco; ++i)
2753 {
2754 ScreenLinesC[i][idx] = u8cc[i];
2755 if (u8cc[i] == 0)
2756 break;
2757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
2759 if (cells > 1)
2760 ScreenLines[idx + 1] = 0;
2761 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002762 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2763 /* double-byte single width character */
2764 ScreenLines2[idx] = p[1];
2765 else if (cells > 1)
2766 /* double-width character */
2767 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 col += cells;
2769 idx += cells;
2770 p += c_len;
2771 }
2772 }
2773 else
2774#endif
2775 {
2776 len = (int)STRLEN(text);
2777 if (len > W_WIDTH(wp) - col)
2778 len = W_WIDTH(wp) - col;
2779 if (len > 0)
2780 {
2781#ifdef FEAT_RIGHTLEFT
2782 if (wp->w_p_rl)
2783 STRNCPY(current_ScreenLine, text, len);
2784 else
2785#endif
2786 STRNCPY(current_ScreenLine + col, text, len);
2787 col += len;
2788 }
2789 }
2790
2791 /* Fill the rest of the line with the fold filler */
2792#ifdef FEAT_RIGHTLEFT
2793 if (wp->w_p_rl)
2794 col -= txtcol;
2795#endif
2796 while (col < W_WIDTH(wp)
2797#ifdef FEAT_RIGHTLEFT
2798 - (wp->w_p_rl ? txtcol : 0)
2799#endif
2800 )
2801 {
2802#ifdef FEAT_MBYTE
2803 if (enc_utf8)
2804 {
2805 if (fill_fold >= 0x80)
2806 {
2807 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002808 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002809 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
2811 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002812 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002814 ScreenLines[off + col] = fill_fold;
2815 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002816 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002818 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002820 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
2822
2823 if (text != buf)
2824 vim_free(text);
2825
2826 /*
2827 * 6. set highlighting for the Visual area an other text.
2828 * If all folded lines are in the Visual area, highlight the line.
2829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2831 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002832 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
2834 /* Visual is after curwin->w_cursor */
2835 top = &curwin->w_cursor;
2836 bot = &VIsual;
2837 }
2838 else
2839 {
2840 /* Visual is before curwin->w_cursor */
2841 top = &VIsual;
2842 bot = &curwin->w_cursor;
2843 }
2844 if (lnum >= top->lnum
2845 && lnume <= bot->lnum
2846 && (VIsual_mode != 'v'
2847 || ((lnum > top->lnum
2848 || (lnum == top->lnum
2849 && top->col == 0))
2850 && (lnume < bot->lnum
2851 || (lnume == bot->lnum
2852 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002853 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
2855 if (VIsual_mode == Ctrl_V)
2856 {
2857 /* Visual block mode: highlight the chars part of the block */
2858 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2859 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002860 if (wp->w_old_cursor_lcol != MAXCOL
2861 && wp->w_old_cursor_lcol + txtcol
2862 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 len = wp->w_old_cursor_lcol;
2864 else
2865 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002866 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002867 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869 }
2870 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002873 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
2876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002878#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002879 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2880 if (wp->w_p_cc_cols)
2881 {
2882 int i = 0;
2883 int j = wp->w_p_cc_cols[i];
2884 int old_txtcol = txtcol;
2885
2886 while (j > -1)
2887 {
2888 txtcol += j;
2889 if (wp->w_p_wrap)
2890 txtcol -= wp->w_skipcol;
2891 else
2892 txtcol -= wp->w_leftcol;
2893 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2894 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002895 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002896 txtcol = old_txtcol;
2897 j = wp->w_p_cc_cols[++i];
2898 }
2899 }
2900
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002901 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002902 if (wp->w_p_cuc)
2903 {
2904 txtcol += wp->w_virtcol;
2905 if (wp->w_p_wrap)
2906 txtcol -= wp->w_skipcol;
2907 else
2908 txtcol -= wp->w_leftcol;
2909 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2910 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002911 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002912 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002915 screen_line(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 (int)W_WIDTH(wp), FALSE);
2917
2918 /*
2919 * Update w_cline_height and w_cline_folded if the cursor line was
2920 * updated (saves a call to plines() later).
2921 */
2922 if (wp == curwin
2923 && lnum <= curwin->w_cursor.lnum
2924 && lnume >= curwin->w_cursor.lnum)
2925 {
2926 curwin->w_cline_row = row;
2927 curwin->w_cline_height = 1;
2928 curwin->w_cline_folded = TRUE;
2929 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2930 }
2931}
2932
2933/*
2934 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2935 */
2936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002937copy_text_attr(
2938 int off,
2939 char_u *buf,
2940 int len,
2941 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002943 int i;
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 mch_memmove(ScreenLines + off, buf, (size_t)len);
2946# ifdef FEAT_MBYTE
2947 if (enc_utf8)
2948 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2949# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002950 for (i = 0; i < len; ++i)
2951 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952}
2953
2954/*
2955 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002956 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 */
2958 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002959fill_foldcolumn(
2960 char_u *p,
2961 win_T *wp,
2962 int closed, /* TRUE of FALSE */
2963 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
2965 int i = 0;
2966 int level;
2967 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002968 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002969 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
2971 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002972 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 level = win_foldinfo.fi_level;
2975 if (level > 0)
2976 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002977 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002978 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002979
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 /* If the column is too narrow, we start at the lowest level that
2981 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002982 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 if (first_level < 1)
2984 first_level = 1;
2985
Bram Moolenaar1c934292015-01-27 16:39:29 +01002986 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
2988 if (win_foldinfo.fi_lnum == lnum
2989 && first_level + i >= win_foldinfo.fi_low_level)
2990 p[i] = '-';
2991 else if (first_level == 1)
2992 p[i] = '|';
2993 else if (first_level + i <= 9)
2994 p[i] = '0' + first_level + i;
2995 else
2996 p[i] = '>';
2997 if (first_level + i == level)
2998 break;
2999 }
3000 }
3001 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003002 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003}
3004#endif /* FEAT_FOLDING */
3005
3006/*
3007 * Display line "lnum" of window 'wp' on the screen.
3008 * Start at row "startrow", stop when "endrow" is reached.
3009 * wp->w_virtcol needs to be valid.
3010 *
3011 * Return the number of last row the line occupies.
3012 */
3013 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003014win_line(
3015 win_T *wp,
3016 linenr_T lnum,
3017 int startrow,
3018 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003019 int nochange UNUSED, /* not updating for changed text */
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003020 proftime_T *syntax_tm UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003022 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3024 int c = 0; /* init for GCC */
3025 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003026#ifdef FEAT_LINEBREAK
3027 long vcol_sbr = -1; /* virtual column after showbreak */
3028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 long vcol_prev = -1; /* "vcol" of previous character */
3030 char_u *line; /* current line */
3031 char_u *ptr; /* current position in "line" */
3032 int row; /* row in the window, excl w_winrow */
3033 int screen_row; /* row on the screen, incl w_winrow */
3034
3035 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3036 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003037 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003038 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 int c_extra = NUL; /* extra chars, all the same */
3040 int extra_attr = 0; /* attributes when n_extra != 0 */
3041 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3042 displaying lcs_eol at end-of-line */
3043 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3044 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3045
3046 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3047 int saved_n_extra = 0;
3048 char_u *saved_p_extra = NULL;
3049 int saved_c_extra = 0;
3050 int saved_char_attr = 0;
3051
3052 int n_attr = 0; /* chars with special attr */
3053 int saved_attr2 = 0; /* char_attr saved for n_attr */
3054 int n_attr3 = 0; /* chars with overruling special attr */
3055 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3056
3057 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3058
3059 int fromcol, tocol; /* start/end of inverting */
3060 int fromcol_prev = -2; /* start of inverting after cursor */
3061 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003063 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 pos_T pos;
3065 long v;
3066
3067 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003068 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3070 in this line */
3071 int attr = 0; /* attributes for area highlighting */
3072 int area_attr = 0; /* attributes desired by highlighting */
3073 int search_attr = 0; /* attributes desired by 'hlsearch' */
3074#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003075 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 int syntax_attr = 0; /* attributes desired by syntax */
3077 int has_syntax = FALSE; /* this buffer has syntax highl. */
3078 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003079 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003080 int draw_color_col = FALSE; /* highlight colorcolumn */
3081 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003082#endif
3083#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003084 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003085# define SPWORDLEN 150
3086 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003087 int nextlinecol = 0; /* column where nextline[] starts */
3088 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003089 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003090 int spell_attr = 0; /* attributes desired by spelling */
3091 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003092 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3093 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003094 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003095 static int cap_col = -1; /* column to check for Cap word */
3096 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003097 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098#endif
3099 int extra_check; /* has syntax or linebreak */
3100#ifdef FEAT_MBYTE
3101 int multi_attr = 0; /* attributes desired by multibyte */
3102 int mb_l = 1; /* multi-byte byte length */
3103 int mb_c = 0; /* decoded multi-byte character */
3104 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003105 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106#endif
3107#ifdef FEAT_DIFF
3108 int filler_lines; /* nr of filler lines to be drawn */
3109 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003110 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 int change_start = MAXCOL; /* first col of changed area */
3112 int change_end = -1; /* last col of changed area */
3113#endif
3114 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3115#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003116 int need_showbreak = FALSE; /* overlong line, skipping first x
3117 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003119#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3120 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003122 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#endif
3124#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003125 matchitem_T *cur; /* points to the match list */
3126 match_T *shl; /* points to search_hl or a match */
3127 int shl_flag; /* flag to indicate whether search_hl
3128 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003129 int pos_inprogress; /* marks that position match search is
3130 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003131 int prevcol_hl_flag; /* flag to indicate whether prevcol
3132 equals startcol of search_hl or one
3133 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134#endif
3135#ifdef FEAT_ARABIC
3136 int prev_c = 0; /* previous Arabic character */
3137 int prev_c1 = 0; /* first composing char for prev_c */
3138#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003139#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003140 int did_line_attr = 0;
3141#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003142#ifdef FEAT_TERMINAL
3143 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003144 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147 /* draw_state: items that are drawn in sequence: */
3148#define WL_START 0 /* nothing done yet */
3149#ifdef FEAT_CMDWIN
3150# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3151#else
3152# define WL_CMDLINE WL_START
3153#endif
3154#ifdef FEAT_FOLDING
3155# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3156#else
3157# define WL_FOLD WL_CMDLINE
3158#endif
3159#ifdef FEAT_SIGNS
3160# define WL_SIGN WL_FOLD + 1 /* column for signs */
3161#else
3162# define WL_SIGN WL_FOLD /* column for signs */
3163#endif
3164#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003165#ifdef FEAT_LINEBREAK
3166# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003168# define WL_BRI WL_NR
3169#endif
3170#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3171# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3172#else
3173# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174#endif
3175#define WL_LINE WL_SBR + 1 /* text in the line */
3176 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003177#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 int feedback_col = 0;
3179 int feedback_old_attr = -1;
3180#endif
3181
Bram Moolenaar860cae12010-06-05 23:22:07 +02003182#ifdef FEAT_CONCEAL
3183 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003184 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003185 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003186 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003187 int is_concealing = FALSE;
3188 int boguscols = 0; /* nonexistent columns added to force
3189 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003190 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003191 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003192 int match_conc = 0; /* cchar for match functions */
3193 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003194 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003195# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003196# define FIX_FOR_BOGUSCOLS \
3197 { \
3198 n_extra += vcol_off; \
3199 vcol -= vcol_off; \
3200 vcol_off = 0; \
3201 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003202 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003203 boguscols = 0; \
3204 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003205#else
3206# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
3209 if (startrow > endrow) /* past the end already! */
3210 return startrow;
3211
3212 row = startrow;
3213 screen_row = row + W_WINROW(wp);
3214
3215 /*
3216 * To speed up the loop below, set extra_check when there is linebreak,
3217 * trailing white space and/or syntax processing to be done.
3218 */
3219#ifdef FEAT_LINEBREAK
3220 extra_check = wp->w_p_lbr;
3221#else
3222 extra_check = 0;
3223#endif
3224#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003225 if (syntax_present(wp) && !wp->w_s->b_syn_error
3226# ifdef SYN_TIME_LIMIT
3227 && !wp->w_s->b_syn_slow
3228# endif
3229 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 /* Prepare for syntax highlighting in this line. When there is an
3232 * error, stop syntax highlighting. */
3233 save_did_emsg = did_emsg;
3234 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003235 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003237 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 else
3239 {
3240 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003241#ifdef SYN_TIME_LIMIT
3242 if (!wp->w_s->b_syn_slow)
3243#endif
3244 {
3245 has_syntax = TRUE;
3246 extra_check = TRUE;
3247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
3249 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003250
3251 /* Check for columns to display for 'colorcolumn'. */
3252 color_cols = wp->w_p_cc_cols;
3253 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003254 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003255#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003256
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003257#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003258 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003259 {
3260 extra_check = TRUE;
3261 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003262 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003263 }
3264#endif
3265
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003266#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003267 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003268 && *wp->w_s->b_p_spl != NUL
3269 && wp->w_s->b_langp.ga_len > 0
3270 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003271 {
3272 /* Prepare for spell checking. */
3273 has_spell = TRUE;
3274 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003275
3276 /* Get the start of the next line, so that words that wrap to the next
3277 * line are found too: "et<line-break>al.".
3278 * Trick: skip a few chars for C/shell/Vim comments */
3279 nextline[SPWORDLEN] = NUL;
3280 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3281 {
3282 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3283 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3284 }
3285
3286 /* When a word wrapped from the previous line the start of the current
3287 * line is valid. */
3288 if (lnum == checked_lnum)
3289 cur_checked_col = checked_col;
3290 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003291
3292 /* When there was a sentence end in the previous line may require a
3293 * word starting with capital in this line. In line 1 always check
3294 * the first word. */
3295 if (lnum != capcol_lnum)
3296 cap_col = -1;
3297 if (lnum == 1)
3298 cap_col = 0;
3299 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301#endif
3302
3303 /*
3304 * handle visual active in this window
3305 */
3306 fromcol = -10;
3307 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3309 {
3310 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003311 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 top = &curwin->w_cursor;
3314 bot = &VIsual;
3315 }
3316 else /* Visual is before curwin->w_cursor */
3317 {
3318 top = &VIsual;
3319 bot = &curwin->w_cursor;
3320 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003321 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 if (VIsual_mode == Ctrl_V) /* block mode */
3323 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003324 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
3326 fromcol = wp->w_old_cursor_fcol;
3327 tocol = wp->w_old_cursor_lcol;
3328 }
3329 }
3330 else /* non-block mode */
3331 {
3332 if (lnum > top->lnum && lnum <= bot->lnum)
3333 fromcol = 0;
3334 else if (lnum == top->lnum)
3335 {
3336 if (VIsual_mode == 'V') /* linewise */
3337 fromcol = 0;
3338 else
3339 {
3340 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3341 if (gchar_pos(top) == NUL)
3342 tocol = fromcol + 1;
3343 }
3344 }
3345 if (VIsual_mode != 'V' && lnum == bot->lnum)
3346 {
3347 if (*p_sel == 'e' && bot->col == 0
3348#ifdef FEAT_VIRTUALEDIT
3349 && bot->coladd == 0
3350#endif
3351 )
3352 {
3353 fromcol = -10;
3354 tocol = MAXCOL;
3355 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003356 else if (bot->col == MAXCOL)
3357 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 else
3359 {
3360 pos = *bot;
3361 if (*p_sel == 'e')
3362 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3363 else
3364 {
3365 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3366 ++tocol;
3367 }
3368 }
3369 }
3370 }
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 /* Check if the character under the cursor should not be inverted */
3373 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003374#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 )
3378 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379
3380 /* if inverting in this line set area_highlighting */
3381 if (fromcol >= 0)
3382 {
3383 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003384 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003386 if ((clip_star.available && !clip_star.owned
3387 && clip_isautosel_star())
3388 || (clip_plus.available && !clip_plus.owned
3389 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003390 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391#endif
3392 }
3393 }
3394
3395 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003396 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003398 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 && wp == curwin
3400 && lnum >= curwin->w_cursor.lnum
3401 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3402 {
3403 if (lnum == curwin->w_cursor.lnum)
3404 getvcol(curwin, &(curwin->w_cursor),
3405 (colnr_T *)&fromcol, NULL, NULL);
3406 else
3407 fromcol = 0;
3408 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3409 {
3410 pos.lnum = lnum;
3411 pos.col = search_match_endcol;
3412 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3413 }
3414 else
3415 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003416 /* do at least one character; happens when past end of line */
3417 if (fromcol == tocol)
3418 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003420 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 }
3422
3423#ifdef FEAT_DIFF
3424 filler_lines = diff_check(wp, lnum);
3425 if (filler_lines < 0)
3426 {
3427 if (filler_lines == -1)
3428 {
3429 if (diff_find_change(wp, lnum, &change_start, &change_end))
3430 diff_hlf = HLF_ADD; /* added line */
3431 else if (change_start == 0)
3432 diff_hlf = HLF_TXD; /* changed text */
3433 else
3434 diff_hlf = HLF_CHD; /* changed line */
3435 }
3436 else
3437 diff_hlf = HLF_ADD; /* added line */
3438 filler_lines = 0;
3439 area_highlighting = TRUE;
3440 }
3441 if (lnum == wp->w_topline)
3442 filler_lines = wp->w_topfill;
3443 filler_todo = filler_lines;
3444#endif
3445
3446#ifdef LINE_ATTR
3447# ifdef FEAT_SIGNS
3448 /* If this line has a sign with line highlighting set line_attr. */
3449 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3450 if (v != 0)
3451 line_attr = sign_get_attr((int)v, TRUE);
3452# endif
3453# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3454 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003455 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003456 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457# endif
3458 if (line_attr != 0)
3459 area_highlighting = TRUE;
3460#endif
3461
3462 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3463 ptr = line;
3464
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003465#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003466 if (has_spell)
3467 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003468 /* For checking first word with a capital skip white space. */
3469 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003470 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003471
Bram Moolenaar30abd282005-06-22 22:35:10 +00003472 /* To be able to spell-check over line boundaries copy the end of the
3473 * current line into nextline[]. Above the start of the next line was
3474 * copied to nextline[SPWORDLEN]. */
3475 if (nextline[SPWORDLEN] == NUL)
3476 {
3477 /* No next line or it is empty. */
3478 nextlinecol = MAXCOL;
3479 nextline_idx = 0;
3480 }
3481 else
3482 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003483 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003484 if (v < SPWORDLEN)
3485 {
3486 /* Short line, use it completely and append the start of the
3487 * next line. */
3488 nextlinecol = 0;
3489 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003490 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003491 nextline_idx = v + 1;
3492 }
3493 else
3494 {
3495 /* Long line, use only the last SPWORDLEN bytes. */
3496 nextlinecol = v - SPWORDLEN;
3497 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3498 nextline_idx = SPWORDLEN + 1;
3499 }
3500 }
3501 }
3502#endif
3503
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003504 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003506 if (lcs_space || lcs_trail)
3507 extra_check = TRUE;
3508 /* find start of trailing whitespace */
3509 if (lcs_trail)
3510 {
3511 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003512 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003513 --trailcol;
3514 trailcol += (colnr_T) (ptr - line);
3515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517
3518 /*
3519 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3520 * first character to be displayed.
3521 */
3522 if (wp->w_p_wrap)
3523 v = wp->w_skipcol;
3524 else
3525 v = wp->w_leftcol;
3526 if (v > 0)
3527 {
3528#ifdef FEAT_MBYTE
3529 char_u *prev_ptr = ptr;
3530#endif
3531 while (vcol < v && *ptr != NUL)
3532 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003533 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 vcol += c;
3535#ifdef FEAT_MBYTE
3536 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003538 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003541 /* When:
3542 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003543 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003544 * - 'virtualedit' is set, or
3545 * - the visual mode is active,
3546 * the end of the line may be before the start of the displayed part.
3547 */
3548 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003549#ifdef FEAT_SYN_HL
3550 wp->w_p_cuc || draw_color_col ||
3551#endif
3552#ifdef FEAT_VIRTUALEDIT
3553 virtual_active() ||
3554#endif
3555 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003556 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
3560 /* Handle a character that's not completely on the screen: Put ptr at
3561 * that character but skip the first few screen characters. */
3562 if (vcol > v)
3563 {
3564 vcol -= c;
3565#ifdef FEAT_MBYTE
3566 ptr = prev_ptr;
3567#else
3568 --ptr;
3569#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003570 /* If the character fits on the screen, don't need to skip it.
3571 * Except for a TAB. */
3572 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003573#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003574 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003575#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003576 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003577 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579
3580 /*
3581 * Adjust for when the inverted text is before the screen,
3582 * and when the start of the inverted text is before the screen.
3583 */
3584 if (tocol <= vcol)
3585 fromcol = 0;
3586 else if (fromcol >= 0 && fromcol < vcol)
3587 fromcol = vcol;
3588
3589#ifdef FEAT_LINEBREAK
3590 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3591 if (wp->w_p_wrap)
3592 need_showbreak = TRUE;
3593#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003594#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003595 /* When spell checking a word we need to figure out the start of the
3596 * word and if it's badly spelled or not. */
3597 if (has_spell)
3598 {
3599 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003600 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003601 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003602
3603 pos = wp->w_cursor;
3604 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003605 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003606 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003607
3608 /* spell_move_to() may call ml_get() and make "line" invalid */
3609 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3610 ptr = line + linecol;
3611
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003612 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003613 {
3614 /* no bad word found at line start, don't check until end of a
3615 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003616 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003617 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003618 }
3619 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003620 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003621 /* bad word found, use attributes until end of word */
3622 word_end = wp->w_cursor.col + len + 1;
3623
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003624 /* Turn index into actual attributes. */
3625 if (spell_hlf != HLF_COUNT)
3626 spell_attr = highlight_attr[spell_hlf];
3627 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003628 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003629
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003630# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003631 /* Need to restart syntax highlighting for this line. */
3632 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003633 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003634# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003635 }
3636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
3638
3639 /*
3640 * Correct highlighting for cursor that can't be disabled.
3641 * Avoids having to check this for each character.
3642 */
3643 if (fromcol >= 0)
3644 {
3645 if (noinvcur)
3646 {
3647 if ((colnr_T)fromcol == wp->w_virtcol)
3648 {
3649 /* highlighting starts at cursor, let it start just after the
3650 * cursor */
3651 fromcol_prev = fromcol;
3652 fromcol = -1;
3653 }
3654 else if ((colnr_T)fromcol < wp->w_virtcol)
3655 /* restart highlighting after the cursor */
3656 fromcol_prev = wp->w_virtcol;
3657 }
3658 if (fromcol >= tocol)
3659 fromcol = -1;
3660 }
3661
3662#ifdef FEAT_SEARCH_EXTRA
3663 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003664 * Handle highlighting the last used search pattern and matches.
3665 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003667 cur = wp->w_match_head;
3668 shl_flag = FALSE;
3669 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003671 if (shl_flag == FALSE)
3672 {
3673 shl = &search_hl;
3674 shl_flag = TRUE;
3675 }
3676 else
3677 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003678 shl->startcol = MAXCOL;
3679 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003681 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003682 v = (long)(ptr - line);
3683 if (cur != NULL)
3684 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003685 next_search_hl(wp, shl, lnum, (colnr_T)v,
3686 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003687
3688 /* Need to get the line again, a multi-line regexp may have made it
3689 * invalid. */
3690 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3691 ptr = line + v;
3692
3693 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003695 if (shl->lnum == lnum)
3696 shl->startcol = shl->rm.startpos[0].col;
3697 else
3698 shl->startcol = 0;
3699 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3700 - shl->rm.startpos[0].lnum)
3701 shl->endcol = shl->rm.endpos[0].col;
3702 else
3703 shl->endcol = MAXCOL;
3704 /* Highlight one character for an empty match. */
3705 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003708 if (has_mbyte && line[shl->endcol] != NUL)
3709 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3710 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003712 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003714 if ((long)shl->startcol < v) /* match at leftcol */
3715 {
3716 shl->attr_cur = shl->attr;
3717 search_attr = shl->attr;
3718 }
3719 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003721 if (shl != &search_hl && cur != NULL)
3722 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
3724#endif
3725
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003726#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003727 /* Cursor line highlighting for 'cursorline' in the current window. Not
3728 * when Visual mode is active, because it's not clear what is selected
3729 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003730 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003731 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003732 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003733 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003734 area_highlighting = TRUE;
3735 }
3736#endif
3737
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003738 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 col = 0;
3740#ifdef FEAT_RIGHTLEFT
3741 if (wp->w_p_rl)
3742 {
3743 /* Rightleft window: process the text in the normal direction, but put
3744 * it in current_ScreenLine[] from right to left. Start at the
3745 * rightmost column of the window. */
3746 col = W_WIDTH(wp) - 1;
3747 off += col;
3748 }
3749#endif
3750
3751 /*
3752 * Repeat for the whole displayed line.
3753 */
3754 for (;;)
3755 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003756#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003757 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 /* Skip this quickly when working on the text. */
3760 if (draw_state != WL_LINE)
3761 {
3762#ifdef FEAT_CMDWIN
3763 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3764 {
3765 draw_state = WL_CMDLINE;
3766 if (cmdwin_type != 0 && wp == curwin)
3767 {
3768 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003770 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003771 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773 }
3774#endif
3775
3776#ifdef FEAT_FOLDING
3777 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3778 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003779 int fdc = compute_foldcolumn(wp, 0);
3780
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003782 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003784 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003785 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003786 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003787 p_extra_free = alloc(12 + 1);
3788
3789 if (p_extra_free != NULL)
3790 {
3791 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3792 n_extra = fdc;
3793 p_extra_free[n_extra] = NUL;
3794 p_extra = p_extra_free;
3795 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003796 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 }
3799 }
3800#endif
3801
3802#ifdef FEAT_SIGNS
3803 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3804 {
3805 draw_state = WL_SIGN;
3806 /* Show the sign column when there are any signs in this
3807 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003808 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003810 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003812 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813# endif
3814
3815 /* Draw two cells with the sign value or blank. */
3816 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003817 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 n_extra = 2;
3819
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003820 if (row == startrow
3821#ifdef FEAT_DIFF
3822 + filler_lines && filler_todo <= 0
3823#endif
3824 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 {
3826 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3827 SIGN_TEXT);
3828# ifdef FEAT_SIGN_ICONS
3829 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3830 SIGN_ICON);
3831 if (gui.in_use && icon_sign != 0)
3832 {
3833 /* Use the image in this position. */
3834 c_extra = SIGN_BYTE;
3835# ifdef FEAT_NETBEANS_INTG
3836 if (buf_signcount(wp->w_buffer, lnum) > 1)
3837 c_extra = MULTISIGN_BYTE;
3838# endif
3839 char_attr = icon_sign;
3840 }
3841 else
3842# endif
3843 if (text_sign != 0)
3844 {
3845 p_extra = sign_get_text(text_sign);
3846 if (p_extra != NULL)
3847 {
3848 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003849 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851 char_attr = sign_get_attr(text_sign, FALSE);
3852 }
3853 }
3854 }
3855 }
3856#endif
3857
3858 if (draw_state == WL_NR - 1 && n_extra == 0)
3859 {
3860 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003861 /* Display the absolute or relative line number. After the
3862 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3863 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 && (row == startrow
3865#ifdef FEAT_DIFF
3866 + filler_lines
3867#endif
3868 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3869 {
3870 /* Draw the line number (empty space after wrapping). */
3871 if (row == startrow
3872#ifdef FEAT_DIFF
3873 + filler_lines
3874#endif
3875 )
3876 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003877 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003878 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003879
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003880 if (wp->w_p_nu && !wp->w_p_rnu)
3881 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003882 num = (long)lnum;
3883 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003884 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003885 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003886 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003887 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003888 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003889 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003890 num = lnum;
3891 fmt = "%-*ld ";
3892 }
3893 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003894
Bram Moolenaar700e7342013-01-30 12:31:36 +01003895 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003896 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 if (wp->w_skipcol > 0)
3898 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3899 *p_extra = '-';
3900#ifdef FEAT_RIGHTLEFT
3901 if (wp->w_p_rl) /* reverse line numbers */
3902 rl_mirror(extra);
3903#endif
3904 p_extra = extra;
3905 c_extra = NUL;
3906 }
3907 else
3908 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003909 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003910 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003911#ifdef FEAT_SYN_HL
3912 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003913 * the current line differently.
3914 * TODO: Can we use CursorLine instead of CursorLineNr
3915 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003916 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003917 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003918 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921 }
3922
Bram Moolenaar597a4222014-06-25 14:39:50 +02003923#ifdef FEAT_LINEBREAK
3924 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3925 && n_extra == 0 && *p_sbr != NUL)
3926 /* draw indent after showbreak value */
3927 draw_state = WL_BRI;
3928 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3929 /* After the showbreak, draw the breakindent */
3930 draw_state = WL_BRI - 1;
3931
3932 /* draw 'breakindent': indent wrapped text accordingly */
3933 if (draw_state == WL_BRI - 1 && n_extra == 0)
3934 {
3935 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003936 /* if need_showbreak is set, breakindent also applies */
3937 if (wp->w_p_bri && n_extra == 0
3938 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003939# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003940 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003941# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003942 )
3943 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003944 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003945# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003946 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003947 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003948 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003949# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003950 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3951 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003952 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003953# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003954 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003955# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003956 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003957 c_extra = ' ';
3958 n_extra = get_breakindent_win(wp,
3959 ml_get_buf(wp->w_buffer, lnum, FALSE));
3960 /* Correct end of highlighted area for 'breakindent',
3961 * required when 'linebreak' is also set. */
3962 if (tocol == vcol)
3963 tocol += n_extra;
3964 }
3965 }
3966#endif
3967
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3969 if (draw_state == WL_SBR - 1 && n_extra == 0)
3970 {
3971 draw_state = WL_SBR;
3972# ifdef FEAT_DIFF
3973 if (filler_todo > 0)
3974 {
3975 /* Draw "deleted" diff line(s). */
3976 if (char2cells(fill_diff) > 1)
3977 c_extra = '-';
3978 else
3979 c_extra = fill_diff;
3980# ifdef FEAT_RIGHTLEFT
3981 if (wp->w_p_rl)
3982 n_extra = col + 1;
3983 else
3984# endif
3985 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003986 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 }
3988# endif
3989# ifdef FEAT_LINEBREAK
3990 if (*p_sbr != NUL && need_showbreak)
3991 {
3992 /* Draw 'showbreak' at the start of each broken line. */
3993 p_extra = p_sbr;
3994 c_extra = NUL;
3995 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003996 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003998 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 /* Correct end of highlighted area for 'showbreak',
4000 * required when 'linebreak' is also set. */
4001 if (tocol == vcol)
4002 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004003#ifdef FEAT_SYN_HL
4004 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004005 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004006 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004007 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
4010# endif
4011 }
4012#endif
4013
4014 if (draw_state == WL_LINE - 1 && n_extra == 0)
4015 {
4016 draw_state = WL_LINE;
4017 if (saved_n_extra)
4018 {
4019 /* Continue item from end of wrapped line. */
4020 n_extra = saved_n_extra;
4021 c_extra = saved_c_extra;
4022 p_extra = saved_p_extra;
4023 char_attr = saved_char_attr;
4024 }
4025 else
4026 char_attr = 0;
4027 }
4028 }
4029
4030 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004031 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004032 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033#ifdef FEAT_DIFF
4034 && filler_todo <= 0
4035#endif
4036 )
4037 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004038 screen_line(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004039 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004040 /* Pretend we have finished updating the window. Except when
4041 * 'cursorcolumn' is set. */
4042#ifdef FEAT_SYN_HL
4043 if (wp->w_p_cuc)
4044 row = wp->w_cline_row + wp->w_cline_height;
4045 else
4046#endif
4047 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 break;
4049 }
4050
4051 if (draw_state == WL_LINE && area_highlighting)
4052 {
4053 /* handle Visual or match highlighting in this line */
4054 if (vcol == fromcol
4055#ifdef FEAT_MBYTE
4056 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4057 && (*mb_ptr2cells)(ptr) > 1)
4058#endif
4059 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004060 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 && vcol < tocol))
4062 area_attr = attr; /* start highlighting */
4063 else if (area_attr != 0
4064 && (vcol == tocol
4065 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068#ifdef FEAT_SEARCH_EXTRA
4069 if (!n_extra)
4070 {
4071 /*
4072 * Check for start/end of search pattern match.
4073 * After end, check for start/end of next match.
4074 * When another match, have to check for start again.
4075 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004076 * Do this for 'search_hl' and the match list (ordered by
4077 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004079 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004080 cur = wp->w_match_head;
4081 shl_flag = FALSE;
4082 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004084 if (shl_flag == FALSE
4085 && ((cur != NULL
4086 && cur->priority > SEARCH_HL_PRIORITY)
4087 || cur == NULL))
4088 {
4089 shl = &search_hl;
4090 shl_flag = TRUE;
4091 }
4092 else
4093 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004094 if (cur != NULL)
4095 cur->pos.cur = 0;
4096 pos_inprogress = TRUE;
4097 while (shl->rm.regprog != NULL
4098 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004100 if (shl->startcol != MAXCOL
4101 && v >= (long)shl->startcol
4102 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004104#ifdef FEAT_MBYTE
4105 int tmp_col = v + MB_PTR2LEN(ptr);
4106
4107 if (shl->endcol < tmp_col)
4108 shl->endcol = tmp_col;
4109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004111#ifdef FEAT_CONCEAL
4112 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4113 == cur->hlg_id)
4114 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004115 has_match_conc =
4116 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004117 match_conc = cur->conceal_char;
4118 }
4119 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004120 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004123 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
4125 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004126 next_search_hl(wp, shl, lnum, (colnr_T)v,
4127 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004128 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004129 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
4131 /* Need to get the line again, a multi-line regexp
4132 * may have made it invalid. */
4133 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4134 ptr = line + v;
4135
4136 if (shl->lnum == lnum)
4137 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004138 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004140 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004142 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004144 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
4146 /* highlight empty match, try again after
4147 * it */
4148#ifdef FEAT_MBYTE
4149 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004150 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004151 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 else
4153#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004154 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156
4157 /* Loop to check if the match starts at the
4158 * current position */
4159 continue;
4160 }
4161 }
4162 break;
4163 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004164 if (shl != &search_hl && cur != NULL)
4165 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004167
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004168 /* Use attributes from match with highest priority among
4169 * 'search_hl' and the match list. */
4170 search_attr = search_hl.attr_cur;
4171 cur = wp->w_match_head;
4172 shl_flag = FALSE;
4173 while (cur != NULL || shl_flag == FALSE)
4174 {
4175 if (shl_flag == FALSE
4176 && ((cur != NULL
4177 && cur->priority > SEARCH_HL_PRIORITY)
4178 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004179 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004180 shl = &search_hl;
4181 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004182 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004183 else
4184 shl = &cur->hl;
4185 if (shl->attr_cur != 0)
4186 search_attr = shl->attr_cur;
4187 if (shl != &search_hl && cur != NULL)
4188 cur = cur->next;
4189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191#endif
4192
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004194 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004196 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4197 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004199 if (diff_hlf == HLF_TXD && ptr - line > change_end
4200 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004202 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004203 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004204 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004207
4208 /* Decide which of the highlight attributes to use. */
4209 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004210#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004211 if (area_attr != 0)
4212 char_attr = hl_combine_attr(line_attr, area_attr);
4213 else if (search_attr != 0)
4214 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004215 /* Use line_attr when not in the Visual or 'incsearch' area
4216 * (area_attr may be 0 when "noinvcur" is set). */
4217 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004218 || vcol < fromcol || vcol_prev < fromcol_prev
4219 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004220 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004221#else
4222 if (area_attr != 0)
4223 char_attr = area_attr;
4224 else if (search_attr != 0)
4225 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004226#endif
4227 else
4228 {
4229 attr_pri = FALSE;
4230#ifdef FEAT_SYN_HL
4231 if (has_syntax)
4232 char_attr = syntax_attr;
4233 else
4234#endif
4235 char_attr = 0;
4236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 }
4238
4239 /*
4240 * Get the next character to put on the screen.
4241 */
4242 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004243 * The "p_extra" points to the extra stuff that is inserted to
4244 * represent special characters (non-printable stuff) and other
4245 * things. When all characters are the same, c_extra is used.
4246 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4247 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4249 */
4250 if (n_extra > 0)
4251 {
4252 if (c_extra != NUL)
4253 {
4254 c = c_extra;
4255#ifdef FEAT_MBYTE
4256 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004257 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
4259 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004260 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004261 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263 else
4264 mb_utf8 = FALSE;
4265#endif
4266 }
4267 else
4268 {
4269 c = *p_extra;
4270#ifdef FEAT_MBYTE
4271 if (has_mbyte)
4272 {
4273 mb_c = c;
4274 if (enc_utf8)
4275 {
4276 /* If the UTF-8 character is more than one byte:
4277 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004278 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 mb_utf8 = FALSE;
4280 if (mb_l > n_extra)
4281 mb_l = 1;
4282 else if (mb_l > 1)
4283 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004284 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004286 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 }
4289 else
4290 {
4291 /* if this is a DBCS character, put it in "mb_c" */
4292 mb_l = MB_BYTE2LEN(c);
4293 if (mb_l >= n_extra)
4294 mb_l = 1;
4295 else if (mb_l > 1)
4296 mb_c = (c << 8) + p_extra[1];
4297 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004298 if (mb_l == 0) /* at the NUL at end-of-line */
4299 mb_l = 1;
4300
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 /* If a double-width char doesn't fit display a '>' in the
4302 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004303 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304# ifdef FEAT_RIGHTLEFT
4305 wp->w_p_rl ? (col <= 0) :
4306# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004307 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 && (*mb_char2cells)(mb_c) == 2)
4309 {
4310 c = '>';
4311 mb_c = c;
4312 mb_l = 1;
4313 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004314 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 /* put the pointer back to output the double-width
4316 * character at the start of the next line. */
4317 ++n_extra;
4318 --p_extra;
4319 }
4320 else
4321 {
4322 n_extra -= mb_l - 1;
4323 p_extra += mb_l - 1;
4324 }
4325 }
4326#endif
4327 ++p_extra;
4328 }
4329 --n_extra;
4330 }
4331 else
4332 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004333#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004334 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004335#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004336
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004337 if (p_extra_free != NULL)
4338 {
4339 vim_free(p_extra_free);
4340 p_extra_free = NULL;
4341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 /*
4343 * Get a character from the line itself.
4344 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004345 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004346#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004347 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349#ifdef FEAT_MBYTE
4350 if (has_mbyte)
4351 {
4352 mb_c = c;
4353 if (enc_utf8)
4354 {
4355 /* If the UTF-8 character is more than one byte: Decode it
4356 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004357 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 mb_utf8 = FALSE;
4359 if (mb_l > 1)
4360 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004361 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 /* Overlong encoded ASCII or ASCII with composing char
4363 * is displayed normally, except a NUL. */
4364 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004365 {
4366 c = mb_c;
4367# ifdef FEAT_LINEBREAK
4368 c0 = mb_c;
4369# endif
4370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004372
4373 /* At start of the line we can have a composing char.
4374 * Draw it as a space with a composing char. */
4375 if (utf_iscomposing(mb_c))
4376 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004377 int i;
4378
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004379 for (i = Screen_mco - 1; i > 0; --i)
4380 u8cc[i] = u8cc[i - 1];
4381 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004382 mb_c = ' ';
4383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 }
4385
4386 if ((mb_l == 1 && c >= 0x80)
4387 || (mb_l >= 1 && mb_c == 0)
4388 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004389# ifdef UNICODE16
4390 || mb_c >= 0x10000
4391# endif
4392 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
4394 /*
4395 * Illegal UTF-8 byte: display as <xx>.
4396 * Non-BMP character : display as ? or fullwidth ?.
4397 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004398# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
4402 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004403# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 if (wp->w_p_rl) /* reverse */
4405 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004408# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 else if (utf_char2cells(mb_c) != 2)
4410 STRCPY(extra, "?");
4411 else
4412 /* 0xff1f in UTF-8: full-width '?' */
4413 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004414# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
4416 p_extra = extra;
4417 c = *p_extra;
4418 mb_c = mb_ptr2char_adv(&p_extra);
4419 mb_utf8 = (c >= 0x80);
4420 n_extra = (int)STRLEN(p_extra);
4421 c_extra = NUL;
4422 if (area_attr == 0 && search_attr == 0)
4423 {
4424 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004425 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 saved_attr2 = char_attr; /* save current attr */
4427 }
4428 }
4429 else if (mb_l == 0) /* at the NUL at end-of-line */
4430 mb_l = 1;
4431#ifdef FEAT_ARABIC
4432 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4433 {
4434 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004435 int pc, pc1, nc;
4436 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 /* The idea of what is the previous and next
4439 * character depends on 'rightleft'. */
4440 if (wp->w_p_rl)
4441 {
4442 pc = prev_c;
4443 pc1 = prev_c1;
4444 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004445 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447 else
4448 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004449 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004451 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453 prev_c = mb_c;
4454
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004455 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
4457 else
4458 prev_c = mb_c;
4459#endif
4460 }
4461 else /* enc_dbcs */
4462 {
4463 mb_l = MB_BYTE2LEN(c);
4464 if (mb_l == 0) /* at the NUL at end-of-line */
4465 mb_l = 1;
4466 else if (mb_l > 1)
4467 {
4468 /* We assume a second byte below 32 is illegal.
4469 * Hopefully this is OK for all double-byte encodings!
4470 */
4471 if (ptr[1] >= 32)
4472 mb_c = (c << 8) + ptr[1];
4473 else
4474 {
4475 if (ptr[1] == NUL)
4476 {
4477 /* head byte at end of line */
4478 mb_l = 1;
4479 transchar_nonprint(extra, c);
4480 }
4481 else
4482 {
4483 /* illegal tail byte */
4484 mb_l = 2;
4485 STRCPY(extra, "XX");
4486 }
4487 p_extra = extra;
4488 n_extra = (int)STRLEN(extra) - 1;
4489 c_extra = NUL;
4490 c = *p_extra++;
4491 if (area_attr == 0 && search_attr == 0)
4492 {
4493 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004494 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 saved_attr2 = char_attr; /* save current attr */
4496 }
4497 mb_c = c;
4498 }
4499 }
4500 }
4501 /* If a double-width char doesn't fit display a '>' in the
4502 * last column; the character is displayed at the start of the
4503 * next line. */
4504 if ((
4505# ifdef FEAT_RIGHTLEFT
4506 wp->w_p_rl ? (col <= 0) :
4507# endif
4508 (col >= W_WIDTH(wp) - 1))
4509 && (*mb_char2cells)(mb_c) == 2)
4510 {
4511 c = '>';
4512 mb_c = c;
4513 mb_utf8 = FALSE;
4514 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004515 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 /* Put pointer back so that the character will be
4517 * displayed at the start of the next line. */
4518 --ptr;
4519 }
4520 else if (*ptr != NUL)
4521 ptr += mb_l - 1;
4522
4523 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004524 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004525 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004526 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004529 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 c = ' ';
4531 if (area_attr == 0 && search_attr == 0)
4532 {
4533 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004534 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 saved_attr2 = char_attr; /* save current attr */
4536 }
4537 mb_c = c;
4538 mb_utf8 = FALSE;
4539 mb_l = 1;
4540 }
4541
4542 }
4543#endif
4544 ++ptr;
4545
4546 if (extra_check)
4547 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004548#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004549 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004550#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004551
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004552#ifdef FEAT_TERMINAL
4553 if (get_term_attr)
4554 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004555 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004556
4557 if (!attr_pri)
4558 char_attr = syntax_attr;
4559 else
4560 char_attr = hl_combine_attr(syntax_attr, char_attr);
4561 }
4562#endif
4563
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004564#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 /* Get syntax attribute, unless still at the start of the line
4566 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004567 v = (long)(ptr - line);
4568 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
4570 /* Get the syntax attribute for the character. If there
4571 * is an error, disable syntax highlighting. */
4572 save_did_emsg = did_emsg;
4573 did_emsg = FALSE;
4574
Bram Moolenaar217ad922005-03-20 22:37:15 +00004575 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004576# ifdef FEAT_SPELL
4577 has_spell ? &can_spell :
4578# endif
4579 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
4581 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004582 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004583 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004584 has_syntax = FALSE;
4585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 else
4587 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004588#ifdef SYN_TIME_LIMIT
4589 if (wp->w_s->b_syn_slow)
4590 has_syntax = FALSE;
4591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 /* Need to get the line again, a multi-line regexp may
4594 * have made it invalid. */
4595 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4596 ptr = line + v;
4597
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004598 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004600 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004601 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004602# ifdef FEAT_CONCEAL
4603 /* no concealing past the end of the line, it interferes
4604 * with line highlighting */
4605 if (c == NUL)
4606 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004607 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004608 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004611#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004612
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004613#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004614 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004615 * Only do this when there is no syntax highlighting, the
4616 * @Spell cluster is not used or the current syntax item
4617 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004618 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004619 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004620 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004621# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004622 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004623 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004624# endif
4625 if (c != 0 && (
4626# ifdef FEAT_SYN_HL
4627 !has_syntax ||
4628# endif
4629 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004630 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004631 char_u *prev_ptr, *p;
4632 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004633 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004634# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004635 if (has_mbyte)
4636 {
4637 prev_ptr = ptr - mb_l;
4638 v -= mb_l - 1;
4639 }
4640 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004641# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004642 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004643
4644 /* Use nextline[] if possible, it has the start of the
4645 * next line concatenated. */
4646 if ((prev_ptr - line) - nextlinecol >= 0)
4647 p = nextline + (prev_ptr - line) - nextlinecol;
4648 else
4649 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004650 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004651 len = spell_check(wp, p, &spell_hlf, &cap_col,
4652 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004653 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004654
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004655 /* In Insert mode only highlight a word that
4656 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004657 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004658 && (State & INSERT) != 0
4659 && wp->w_cursor.lnum == lnum
4660 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004661 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004662 && wp->w_cursor.col < (colnr_T)word_end)
4663 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004664 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004665 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004666 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004667
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004668 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004669 && (p - nextline) + len > nextline_idx)
4670 {
4671 /* Remember that the good word continues at the
4672 * start of the next line. */
4673 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004674 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004675 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004676
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004677 /* Turn index into actual attributes. */
4678 if (spell_hlf != HLF_COUNT)
4679 spell_attr = highlight_attr[spell_hlf];
4680
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004681 if (cap_col > 0)
4682 {
4683 if (p != prev_ptr
4684 && (p - nextline) + cap_col >= nextline_idx)
4685 {
4686 /* Remember that the word in the next line
4687 * must start with a capital. */
4688 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004689 cap_col = (int)((p - nextline) + cap_col
4690 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004691 }
4692 else
4693 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004694 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004695 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004696 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004697 }
4698 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004699 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004700 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004701 char_attr = hl_combine_attr(char_attr, spell_attr);
4702 else
4703 char_attr = hl_combine_attr(spell_attr, char_attr);
4704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705#endif
4706#ifdef FEAT_LINEBREAK
4707 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004708 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004710 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004711 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004713# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004714 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004715# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004716 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004718 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004720 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004721
Bram Moolenaar597a4222014-06-25 14:39:50 +02004722 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004723 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004724 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004725 if (c == TAB && n_extra + col > W_WIDTH(wp))
4726 n_extra = (int)wp->w_buffer->b_p_ts
4727 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4728
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004729# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004730 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004731# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004733# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004734 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004735 {
4736#ifdef FEAT_CONCEAL
4737 if (c == TAB)
4738 /* See "Tab alignment" below. */
4739 FIX_FOR_BOGUSCOLS;
4740#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004741 if (!wp->w_p_list)
4742 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745#endif
4746
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004747 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4748 */
4749 if (wp->w_p_list
4750 && (((c == 160
4751#ifdef FEAT_MBYTE
4752 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4753#endif
4754 ) && lcs_nbsp)
4755 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4756 {
4757 c = (c == ' ') ? lcs_space : lcs_nbsp;
4758 if (area_attr == 0 && search_attr == 0)
4759 {
4760 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004761 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004762 saved_attr2 = char_attr; /* save current attr */
4763 }
4764#ifdef FEAT_MBYTE
4765 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004766 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004767 {
4768 mb_utf8 = TRUE;
4769 u8cc[0] = 0;
4770 c = 0xc0;
4771 }
4772 else
4773 mb_utf8 = FALSE;
4774#endif
4775 }
4776
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4778 {
4779 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004780 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 {
4782 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004783 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 saved_attr2 = char_attr; /* save current attr */
4785 }
4786#ifdef FEAT_MBYTE
4787 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004788 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
4790 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004791 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004792 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 }
4794 else
4795 mb_utf8 = FALSE;
4796#endif
4797 }
4798 }
4799
4800 /*
4801 * Handling of non-printable characters.
4802 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004803 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
4805 /*
4806 * when getting a character from the file, we may have to
4807 * turn it into something else on the way to putting it
4808 * into "ScreenLines".
4809 */
4810 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4811 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004812 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004813 long vcol_adjusted = vcol; /* removed showbreak length */
4814#ifdef FEAT_LINEBREAK
4815 /* only adjust the tab_len, when at the first column
4816 * after the showbreak value was drawn */
4817 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4818 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004821 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004822 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4823
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004824#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004825 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004826#endif
4827 /* tab amount depends on current column */
4828 n_extra = tab_len;
4829#ifdef FEAT_LINEBREAK
4830 else
4831 {
4832 char_u *p;
4833 int len = n_extra;
4834 int i;
4835 int saved_nextra = n_extra;
4836
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004837#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004838 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004839 /* there are characters to conceal */
4840 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004841 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4842 */
4843 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4844 && n_extra > tab_len)
4845 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004846#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004847
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004848 /* if n_extra > 0, it gives the number of chars, to
4849 * use for a tab, else we need to calculate the width
4850 * for a tab */
4851#ifdef FEAT_MBYTE
4852 len = (tab_len * mb_char2len(lcs_tab2));
4853 if (n_extra > 0)
4854 len += n_extra - tab_len;
4855#endif
4856 c = lcs_tab1;
4857 p = alloc((unsigned)(len + 1));
4858 vim_memset(p, ' ', len);
4859 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004860 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004861 p_extra_free = p;
4862 for (i = 0; i < tab_len; i++)
4863 {
4864#ifdef FEAT_MBYTE
4865 mb_char2bytes(lcs_tab2, p);
4866 p += mb_char2len(lcs_tab2);
4867 n_extra += mb_char2len(lcs_tab2)
4868 - (saved_nextra > 0 ? 1 : 0);
4869#else
4870 p[i] = lcs_tab2;
4871#endif
4872 }
4873 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004874#ifdef FEAT_CONCEAL
4875 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4876 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004877 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004878 n_extra -= vcol_off;
4879#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004880 }
4881#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004882#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004883 {
4884 int vc_saved = vcol_off;
4885
4886 /* Tab alignment should be identical regardless of
4887 * 'conceallevel' value. So tab compensates of all
4888 * previous concealed characters, and thus resets
4889 * vcol_off and boguscols accumulated so far in the
4890 * line. Note that the tab can be longer than
4891 * 'tabstop' when there are concealed characters. */
4892 FIX_FOR_BOGUSCOLS;
4893
4894 /* Make sure, the highlighting for the tab char will be
4895 * correctly set further below (effectively reverts the
4896 * FIX_FOR_BOGSUCOLS macro */
4897 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004898 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004899 tab_len += vc_saved;
4900 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902#ifdef FEAT_MBYTE
4903 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4904#endif
4905 if (wp->w_p_list)
4906 {
4907 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004908#ifdef FEAT_LINEBREAK
4909 if (wp->w_p_lbr)
4910 c_extra = NUL; /* using p_extra from above */
4911 else
4912#endif
4913 c_extra = lcs_tab2;
4914 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004915 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 saved_attr2 = char_attr; /* save current attr */
4917#ifdef FEAT_MBYTE
4918 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004919 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
4921 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004922 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004923 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
4925#endif
4926 }
4927 else
4928 {
4929 c_extra = ' ';
4930 c = ' ';
4931 }
4932 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004933 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004934 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004935 || ((fromcol >= 0 || fromcol_prev >= 0)
4936 && tocol > vcol
4937 && VIsual_mode != Ctrl_V
4938 && (
4939# ifdef FEAT_RIGHTLEFT
4940 wp->w_p_rl ? (col >= 0) :
4941# endif
4942 (col < W_WIDTH(wp)))
4943 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004944 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004945 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004946 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004948 /* Display a '$' after the line or highlight an extra
4949 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4951 /* For a diff line the highlighting continues after the
4952 * "$". */
4953 if (
4954# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004955 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956# ifdef LINE_ATTR
4957 &&
4958# endif
4959# endif
4960# ifdef LINE_ATTR
4961 line_attr == 0
4962# endif
4963 )
4964#endif
4965 {
4966#ifdef FEAT_VIRTUALEDIT
4967 /* In virtualedit, visual selections may extend
4968 * beyond end of line. */
4969 if (area_highlighting && virtual_active()
4970 && tocol != MAXCOL && vcol < tocol)
4971 n_extra = 0;
4972 else
4973#endif
4974 {
4975 p_extra = at_end_str;
4976 n_extra = 1;
4977 c_extra = NUL;
4978 }
4979 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004980 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004981 c = lcs_eol;
4982 else
4983 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 lcs_eol_one = -1;
4985 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004986 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004988 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 n_attr = 1;
4990 }
4991#ifdef FEAT_MBYTE
4992 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004993 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
4995 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004996 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004997 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
4999 else
5000 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5001#endif
5002 }
5003 else if (c != NUL)
5004 {
5005 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005006 if (n_extra == 0)
5007 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008#ifdef FEAT_RIGHTLEFT
5009 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5010 rl_mirror(p_extra); /* reverse "<12>" */
5011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005013#ifdef FEAT_LINEBREAK
5014 if (wp->w_p_lbr)
5015 {
5016 char_u *p;
5017
5018 c = *p_extra;
5019 p = alloc((unsigned)n_extra + 1);
5020 vim_memset(p, ' ', n_extra);
5021 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5022 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005023 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005024 p_extra_free = p_extra = p;
5025 }
5026 else
5027#endif
5028 {
5029 n_extra = byte2cells(c) - 1;
5030 c = *p_extra++;
5031 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005032 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
5034 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005035 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 saved_attr2 = char_attr; /* save current attr */
5037 }
5038#ifdef FEAT_MBYTE
5039 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5040#endif
5041 }
5042#ifdef FEAT_VIRTUALEDIT
5043 else if (VIsual_active
5044 && (VIsual_mode == Ctrl_V
5045 || VIsual_mode == 'v')
5046 && virtual_active()
5047 && tocol != MAXCOL
5048 && vcol < tocol
5049 && (
5050# ifdef FEAT_RIGHTLEFT
5051 wp->w_p_rl ? (col >= 0) :
5052# endif
5053 (col < W_WIDTH(wp))))
5054 {
5055 c = ' ';
5056 --ptr; /* put it back at the NUL */
5057 }
5058#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005059#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 else if ((
5061# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005062 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005064# ifdef FEAT_TERMINAL
5065 term_attr != 0 ||
5066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 ) && (
5069# ifdef FEAT_RIGHTLEFT
5070 wp->w_p_rl ? (col >= 0) :
5071# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005072 (col
5073# ifdef FEAT_CONCEAL
5074 - boguscols
5075# endif
5076 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
5078 /* Highlight until the right side of the window */
5079 c = ' ';
5080 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005081
5082 /* Remember we do the char for line highlighting. */
5083 ++did_line_attr;
5084
5085 /* don't do search HL for the rest of the line */
5086 if (line_attr != 0 && char_attr == search_attr && col > 0)
5087 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088# ifdef FEAT_DIFF
5089 if (diff_hlf == HLF_TXD)
5090 {
5091 diff_hlf = HLF_CHD;
5092 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005093 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005094 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005095 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5096 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005097 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
5100# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005101# ifdef FEAT_TERMINAL
5102 if (term_attr != 0)
5103 {
5104 char_attr = term_attr;
5105 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5106 char_attr = hl_combine_attr(char_attr,
5107 HL_ATTR(HLF_CUL));
5108 }
5109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
5111#endif
5112 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005113
5114#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005115 if ( wp->w_p_cole > 0
5116 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005117 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005118 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005119 && !(lnum_in_visual_area
5120 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005121 {
5122 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005123 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005124 && (syn_get_sub_char() != NUL || match_conc
5125 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005126 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005127 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005128 /* First time at this concealed item: display one
5129 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005130 if (match_conc)
5131 c = match_conc;
5132 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005133 c = syn_get_sub_char();
5134 else if (lcs_conceal != NUL)
5135 c = lcs_conceal;
5136 else
5137 c = ' ';
5138
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005139 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005140
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005141 if (n_extra > 0)
5142 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005143 vcol += n_extra;
5144 if (wp->w_p_wrap && n_extra > 0)
5145 {
5146# ifdef FEAT_RIGHTLEFT
5147 if (wp->w_p_rl)
5148 {
5149 col -= n_extra;
5150 boguscols -= n_extra;
5151 }
5152 else
5153# endif
5154 {
5155 boguscols += n_extra;
5156 col += n_extra;
5157 }
5158 }
5159 n_extra = 0;
5160 n_attr = 0;
5161 }
5162 else if (n_skip == 0)
5163 {
5164 is_concealing = TRUE;
5165 n_skip = 1;
5166 }
5167# ifdef FEAT_MBYTE
5168 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005169 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005170 {
5171 mb_utf8 = TRUE;
5172 u8cc[0] = 0;
5173 c = 0xc0;
5174 }
5175 else
5176 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5177# endif
5178 }
5179 else
5180 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005181 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005182 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005183 }
5184#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005187#ifdef FEAT_CONCEAL
5188 /* In the cursor line and we may be concealing characters: correct
5189 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005190 if (!did_wcol && draw_state == WL_LINE
5191 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005192 && conceal_cursor_line(wp)
5193 && (int)wp->w_virtcol <= vcol + n_skip)
5194 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005195# ifdef FEAT_RIGHTLEFT
5196 if (wp->w_p_rl)
5197 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5198 else
5199# endif
5200 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005201 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005202 did_wcol = TRUE;
5203 }
5204#endif
5205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 /* Don't override visual selection highlighting. */
5207 if (n_attr > 0
5208 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005209 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 char_attr = extra_attr;
5211
Bram Moolenaar81695252004-12-29 20:58:21 +00005212#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 /* XIM don't send preedit_start and preedit_end, but they send
5214 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5215 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005216 if (p_imst == IM_ON_THE_SPOT
5217 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005218 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 && (State & INSERT)
5220 && !p_imdisable
5221 && im_is_preediting()
5222 && draw_state == WL_LINE)
5223 {
5224 colnr_T tcol;
5225
5226 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005227 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 else
5229 tcol = preedit_end_col;
5230 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5231 {
5232 if (feedback_old_attr < 0)
5233 {
5234 feedback_col = 0;
5235 feedback_old_attr = char_attr;
5236 }
5237 char_attr = im_get_feedback_attr(feedback_col);
5238 if (char_attr < 0)
5239 char_attr = feedback_old_attr;
5240 feedback_col++;
5241 }
5242 else if (feedback_old_attr >= 0)
5243 {
5244 char_attr = feedback_old_attr;
5245 feedback_old_attr = -1;
5246 feedback_col = 0;
5247 }
5248 }
5249#endif
5250 /*
5251 * Handle the case where we are in column 0 but not on the first
5252 * character of the line and the user wants us to show us a
5253 * special character (via 'listchars' option "precedes:<char>".
5254 */
5255 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005256 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5258#ifdef FEAT_DIFF
5259 && filler_todo <= 0
5260#endif
5261 && draw_state > WL_NR
5262 && c != NUL)
5263 {
5264 c = lcs_prec;
5265 lcs_prec_todo = NUL;
5266#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005267 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5268 {
5269 /* Double-width character being overwritten by the "precedes"
5270 * character, need to fill up half the character. */
5271 c_extra = MB_FILLER_CHAR;
5272 n_extra = 1;
5273 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005274 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005277 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
5279 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005280 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005281 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
5283 else
5284 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5285#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005286 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 {
5288 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005289 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 n_attr3 = 1;
5291 }
5292 }
5293
5294 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005295 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005297 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005298#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005299 || did_line_attr == 1
5300#endif
5301 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005303#ifdef FEAT_SEARCH_EXTRA
5304 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005305
5306 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005307 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005308 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005309#endif
5310
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005311 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 * highlight match at end of line. If it's beyond the last
5313 * char on the screen, just overwrite that one (tricky!) Not
5314 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005315#ifdef FEAT_SEARCH_EXTRA
5316 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005317 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005318 prevcol_hl_flag = TRUE;
5319 else
5320 {
5321 cur = wp->w_match_head;
5322 while (cur != NULL)
5323 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005324 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005325 {
5326 prevcol_hl_flag = TRUE;
5327 break;
5328 }
5329 cur = cur->next;
5330 }
5331 }
5332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005334 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005335 && (VIsual_mode != Ctrl_V
5336 || lnum == VIsual.lnum
5337 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005338 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#ifdef FEAT_SEARCH_EXTRA
5340 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005341 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005342# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005343 && did_line_attr <= 1
5344# endif
5345 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346#endif
5347 ))
5348 {
5349 int n = 0;
5350
5351#ifdef FEAT_RIGHTLEFT
5352 if (wp->w_p_rl)
5353 {
5354 if (col < 0)
5355 n = 1;
5356 }
5357 else
5358#endif
5359 {
5360 if (col >= W_WIDTH(wp))
5361 n = -1;
5362 }
5363 if (n != 0)
5364 {
5365 /* At the window boundary, highlight the last character
5366 * instead (better than nothing). */
5367 off += n;
5368 col += n;
5369 }
5370 else
5371 {
5372 /* Add a blank character to highlight. */
5373 ScreenLines[off] = ' ';
5374#ifdef FEAT_MBYTE
5375 if (enc_utf8)
5376 ScreenLinesUC[off] = 0;
5377#endif
5378 }
5379#ifdef FEAT_SEARCH_EXTRA
5380 if (area_attr == 0)
5381 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005382 /* Use attributes from match with highest priority among
5383 * 'search_hl' and the match list. */
5384 char_attr = search_hl.attr;
5385 cur = wp->w_match_head;
5386 shl_flag = FALSE;
5387 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005388 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005389 if (shl_flag == FALSE
5390 && ((cur != NULL
5391 && cur->priority > SEARCH_HL_PRIORITY)
5392 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005393 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005394 shl = &search_hl;
5395 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005396 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005397 else
5398 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005399 if ((ptr - line) - 1 == (long)shl->startcol
5400 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005401 char_attr = shl->attr;
5402 if (shl != &search_hl && cur != NULL)
5403 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 }
5406#endif
5407 ScreenAttrs[off] = char_attr;
5408#ifdef FEAT_RIGHTLEFT
5409 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005412 --off;
5413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 else
5415#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005418 ++off;
5419 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005420 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005421#ifdef FEAT_SYN_HL
5422 eol_hl_off = 1;
5423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
Bram Moolenaar91170f82006-05-05 21:15:17 +00005427 /*
5428 * At end of the text line.
5429 */
5430 if (c == NUL)
5431 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005432#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005433 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5434 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005435 {
5436 /* highlight last char after line */
5437 --col;
5438 --off;
5439 --vcol;
5440 }
5441
Bram Moolenaar1a384422010-07-14 19:53:30 +02005442 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005443 if (wp->w_p_wrap)
5444 v = wp->w_skipcol;
5445 else
5446 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005447
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005448 /* check if line ends before left margin */
5449 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005450 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005451#ifdef FEAT_CONCEAL
5452 /* Get rid of the boguscols now, we want to draw until the right
5453 * edge for 'cursorcolumn'. */
5454 col -= boguscols;
5455 boguscols = 0;
5456#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005457
5458 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005459 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005460
5461 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005462 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5463 && (int)wp->w_virtcol <
5464 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005465 && lnum != wp->w_cursor.lnum)
5466 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005467# ifdef FEAT_RIGHTLEFT
5468 && !wp->w_p_rl
5469# endif
5470 )
5471 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005472 int rightmost_vcol = 0;
5473 int i;
5474
5475 if (wp->w_p_cuc)
5476 rightmost_vcol = wp->w_virtcol;
5477 if (draw_color_col)
5478 /* determine rightmost colorcolumn to possibly draw */
5479 for (i = 0; color_cols[i] >= 0; ++i)
5480 if (rightmost_vcol < color_cols[i])
5481 rightmost_vcol = color_cols[i];
5482
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005483 while (col < W_WIDTH(wp))
5484 {
5485 ScreenLines[off] = ' ';
5486#ifdef FEAT_MBYTE
5487 if (enc_utf8)
5488 ScreenLinesUC[off] = 0;
5489#endif
5490 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005491 if (draw_color_col)
5492 draw_color_col = advance_color_col(VCOL_HLC,
5493 &color_cols);
5494
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005495 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005496 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005497 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005498 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005499 else
5500 ScreenAttrs[off++] = 0;
5501
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005502 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005503 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005504
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005505 ++vcol;
5506 }
5507 }
5508#endif
5509
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005510 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005511 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 row++;
5513
5514 /*
5515 * Update w_cline_height and w_cline_folded if the cursor line was
5516 * updated (saves a call to plines() later).
5517 */
5518 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5519 {
5520 curwin->w_cline_row = startrow;
5521 curwin->w_cline_height = row - startrow;
5522#ifdef FEAT_FOLDING
5523 curwin->w_cline_folded = FALSE;
5524#endif
5525 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5526 }
5527
5528 break;
5529 }
5530
5531 /* line continues beyond line end */
5532 if (lcs_ext
5533 && !wp->w_p_wrap
5534#ifdef FEAT_DIFF
5535 && filler_todo <= 0
5536#endif
5537 && (
5538#ifdef FEAT_RIGHTLEFT
5539 wp->w_p_rl ? col == 0 :
5540#endif
5541 col == W_WIDTH(wp) - 1)
5542 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005543 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5545 {
5546 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005547 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548#ifdef FEAT_MBYTE
5549 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005550 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 {
5552 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005553 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005554 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556 else
5557 mb_utf8 = FALSE;
5558#endif
5559 }
5560
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005561#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005562 /* advance to the next 'colorcolumn' */
5563 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005564 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005565
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005566 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005567 * highlight the cursor position itself.
5568 * Also highlight the 'colorcolumn' if it is different than
5569 * 'cursorcolumn' */
5570 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005571 if (draw_state == WL_LINE && !lnum_in_visual_area
5572 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005573 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005574 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005575 && lnum != wp->w_cursor.lnum)
5576 {
5577 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005578 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005579 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005580 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005581 {
5582 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005583 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005584 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005585 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005586#endif
5587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 /*
5589 * Store character to be displayed.
5590 * Skip characters that are left of the screen for 'nowrap'.
5591 */
5592 vcol_prev = vcol;
5593 if (draw_state < WL_LINE || n_skip <= 0)
5594 {
5595 /*
5596 * Store the character.
5597 */
5598#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5599 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5600 {
5601 /* A double-wide character is: put first halve in left cell. */
5602 --off;
5603 --col;
5604 }
5605#endif
5606 ScreenLines[off] = c;
5607#ifdef FEAT_MBYTE
5608 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005609 {
5610 if ((mb_c & 0xff00) == 0x8e00)
5611 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 else if (enc_utf8)
5615 {
5616 if (mb_utf8)
5617 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005618 int i;
5619
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005621 if ((c & 0xff) == 0)
5622 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005623 for (i = 0; i < Screen_mco; ++i)
5624 {
5625 ScreenLinesC[i][off] = u8cc[i];
5626 if (u8cc[i] == 0)
5627 break;
5628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630 else
5631 ScreenLinesUC[off] = 0;
5632 }
5633 if (multi_attr)
5634 {
5635 ScreenAttrs[off] = multi_attr;
5636 multi_attr = 0;
5637 }
5638 else
5639#endif
5640 ScreenAttrs[off] = char_attr;
5641
5642#ifdef FEAT_MBYTE
5643 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5644 {
5645 /* Need to fill two screen columns. */
5646 ++off;
5647 ++col;
5648 if (enc_utf8)
5649 /* UTF-8: Put a 0 in the second screen char. */
5650 ScreenLines[off] = 0;
5651 else
5652 /* DBCS: Put second byte in the second screen char. */
5653 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005654 if (draw_state > WL_NR
5655#ifdef FEAT_DIFF
5656 && filler_todo <= 0
5657#endif
5658 )
5659 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 /* When "tocol" is halfway a character, set it to the end of
5661 * the character, otherwise highlighting won't stop. */
5662 if (tocol == vcol)
5663 ++tocol;
5664#ifdef FEAT_RIGHTLEFT
5665 if (wp->w_p_rl)
5666 {
5667 /* now it's time to backup one cell */
5668 --off;
5669 --col;
5670 }
5671#endif
5672 }
5673#endif
5674#ifdef FEAT_RIGHTLEFT
5675 if (wp->w_p_rl)
5676 {
5677 --off;
5678 --col;
5679 }
5680 else
5681#endif
5682 {
5683 ++off;
5684 ++col;
5685 }
5686 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005687#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005688 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005689 {
5690 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005691 ++vcol_off;
5692 if (n_extra > 0)
5693 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005694 if (wp->w_p_wrap)
5695 {
5696 /*
5697 * Special voodoo required if 'wrap' is on.
5698 *
5699 * Advance the column indicator to force the line
5700 * drawing to wrap early. This will make the line
5701 * take up the same screen space when parts are concealed,
5702 * so that cursor line computations aren't messed up.
5703 *
5704 * To avoid the fictitious advance of 'col' causing
5705 * trailing junk to be written out of the screen line
5706 * we are building, 'boguscols' keeps track of the number
5707 * of bad columns we have advanced.
5708 */
5709 if (n_extra > 0)
5710 {
5711 vcol += n_extra;
5712# ifdef FEAT_RIGHTLEFT
5713 if (wp->w_p_rl)
5714 {
5715 col -= n_extra;
5716 boguscols -= n_extra;
5717 }
5718 else
5719# endif
5720 {
5721 col += n_extra;
5722 boguscols += n_extra;
5723 }
5724 n_extra = 0;
5725 n_attr = 0;
5726 }
5727
5728
5729# ifdef FEAT_MBYTE
5730 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5731 {
5732 /* Need to fill two screen columns. */
5733# ifdef FEAT_RIGHTLEFT
5734 if (wp->w_p_rl)
5735 {
5736 --boguscols;
5737 --col;
5738 }
5739 else
5740# endif
5741 {
5742 ++boguscols;
5743 ++col;
5744 }
5745 }
5746# endif
5747
5748# ifdef FEAT_RIGHTLEFT
5749 if (wp->w_p_rl)
5750 {
5751 --boguscols;
5752 --col;
5753 }
5754 else
5755# endif
5756 {
5757 ++boguscols;
5758 ++col;
5759 }
5760 }
5761 else
5762 {
5763 if (n_extra > 0)
5764 {
5765 vcol += n_extra;
5766 n_extra = 0;
5767 n_attr = 0;
5768 }
5769 }
5770
5771 }
5772#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 else
5774 --n_skip;
5775
Bram Moolenaar64486672010-05-16 15:46:46 +02005776 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5777 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005778 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779#ifdef FEAT_DIFF
5780 && filler_todo <= 0
5781#endif
5782 )
5783 ++vcol;
5784
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005785#ifdef FEAT_SYN_HL
5786 if (vcol_save_attr >= 0)
5787 char_attr = vcol_save_attr;
5788#endif
5789
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 /* restore attributes after "predeces" in 'listchars' */
5791 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5792 char_attr = saved_attr3;
5793
5794 /* restore attributes after last 'listchars' or 'number' char */
5795 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5796 char_attr = saved_attr2;
5797
5798 /*
5799 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005800 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 */
5802 if ((
5803#ifdef FEAT_RIGHTLEFT
5804 wp->w_p_rl ? (col < 0) :
5805#endif
5806 (col >= W_WIDTH(wp)))
5807 && (*ptr != NUL
5808#ifdef FEAT_DIFF
5809 || filler_todo > 0
5810#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005811 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5813 )
5814 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005815#ifdef FEAT_CONCEAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005816 screen_line(screen_row, W_WINCOL(wp), col - boguscols,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005817 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005818 boguscols = 0;
5819#else
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005820 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005821 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 ++row;
5824 ++screen_row;
5825
5826 /* When not wrapping and finished diff lines, or when displayed
5827 * '$' and highlighting until last column, break here. */
5828 if ((!wp->w_p_wrap
5829#ifdef FEAT_DIFF
5830 && filler_todo <= 0
5831#endif
5832 ) || lcs_eol_one == -1)
5833 break;
5834
5835 /* When the window is too narrow draw all "@" lines. */
5836 if (draw_state != WL_LINE
5837#ifdef FEAT_DIFF
5838 && filler_todo <= 0
5839#endif
5840 )
5841 {
5842 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005843#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 draw_vsep_win(wp, row);
5845#endif
5846 row = endrow;
5847 }
5848
5849 /* When line got too long for screen break here. */
5850 if (row == endrow)
5851 {
5852 ++row;
5853 break;
5854 }
5855
5856 if (screen_cur_row == screen_row - 1
5857#ifdef FEAT_DIFF
5858 && filler_todo <= 0
5859#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005860#ifdef FEAT_WINDOWS
5861 && W_WIDTH(wp) == Columns
5862#endif
5863 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 {
5865 /* Remember that the line wraps, used for modeless copy. */
5866 LineWraps[screen_row - 1] = TRUE;
5867
5868 /*
5869 * Special trick to make copy/paste of wrapped lines work with
5870 * xterm/screen: write an extra character beyond the end of
5871 * the line. This will work with all terminal types
5872 * (regardless of the xn,am settings).
5873 * Only do this on a fast tty.
5874 * Only do this if the cursor is on the current line
5875 * (something has been written in it).
5876 * Don't do this for the GUI.
5877 * Don't do this for double-width characters.
5878 * Don't do this for a window not at the right screen border.
5879 */
5880 if (p_tf
5881#ifdef FEAT_GUI
5882 && !gui.in_use
5883#endif
5884#ifdef FEAT_MBYTE
5885 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005886 && ((*mb_off2cells)(LineOffset[screen_row],
5887 LineOffset[screen_row] + screen_Columns)
5888 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005890 + (int)Columns - 2,
5891 LineOffset[screen_row] + screen_Columns)
5892 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#endif
5894 )
5895 {
5896 /* First make sure we are at the end of the screen line,
5897 * then output the same character again to let the
5898 * terminal know about the wrap. If the terminal doesn't
5899 * auto-wrap, we overwrite the character. */
5900 if (screen_cur_col != W_WIDTH(wp))
5901 screen_char(LineOffset[screen_row - 1]
5902 + (unsigned)Columns - 1,
5903 screen_row - 1, (int)(Columns - 1));
5904
5905#ifdef FEAT_MBYTE
5906 /* When there is a multi-byte character, just output a
5907 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005908 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5909 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 out_char(' ');
5911 else
5912#endif
5913 out_char(ScreenLines[LineOffset[screen_row - 1]
5914 + (Columns - 1)]);
5915 /* force a redraw of the first char on the next line */
5916 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5917 screen_start(); /* don't know where cursor is now */
5918 }
5919 }
5920
5921 col = 0;
5922 off = (unsigned)(current_ScreenLine - ScreenLines);
5923#ifdef FEAT_RIGHTLEFT
5924 if (wp->w_p_rl)
5925 {
5926 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5927 off += col;
5928 }
5929#endif
5930
5931 /* reset the drawing state for the start of a wrapped line */
5932 draw_state = WL_START;
5933 saved_n_extra = n_extra;
5934 saved_p_extra = p_extra;
5935 saved_c_extra = c_extra;
5936 saved_char_attr = char_attr;
5937 n_extra = 0;
5938 lcs_prec_todo = lcs_prec;
5939#ifdef FEAT_LINEBREAK
5940# ifdef FEAT_DIFF
5941 if (filler_todo <= 0)
5942# endif
5943 need_showbreak = TRUE;
5944#endif
5945#ifdef FEAT_DIFF
5946 --filler_todo;
5947 /* When the filler lines are actually below the last line of the
5948 * file, don't draw the line itself, break here. */
5949 if (filler_todo == 0 && wp->w_botfill)
5950 break;
5951#endif
5952 }
5953
5954 } /* for every character in the line */
5955
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005956#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005957 /* After an empty line check first word for capital. */
5958 if (*skipwhite(line) == NUL)
5959 {
5960 capcol_lnum = lnum + 1;
5961 cap_col = 0;
5962 }
5963#endif
5964
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005965 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 return row;
5967}
5968
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005969#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005970static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005971
5972/*
5973 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005974 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005975 */
5976 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005977comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005978{
5979 int i;
5980
5981 for (i = 0; i < Screen_mco; ++i)
5982 {
5983 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5984 return TRUE;
5985 if (ScreenLinesC[i][off_from] == 0)
5986 break;
5987 }
5988 return FALSE;
5989}
5990#endif
5991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992/*
5993 * Check whether the given character needs redrawing:
5994 * - the (first byte of the) character is different
5995 * - the attributes are different
5996 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005997 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 */
5999 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006000char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001{
6002 if (cols > 0
6003 && ((ScreenLines[off_from] != ScreenLines[off_to]
6004 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6005
6006#ifdef FEAT_MBYTE
6007 || (enc_dbcs != 0
6008 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6009 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6010 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6011 : (cols > 1 && ScreenLines[off_from + 1]
6012 != ScreenLines[off_to + 1])))
6013 || (enc_utf8
6014 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6015 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006016 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006017 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6018 && ScreenLines[off_from + 1]
6019 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020#endif
6021 ))
6022 return TRUE;
6023 return FALSE;
6024}
6025
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006026#if defined(FEAT_TERMINAL) || defined(PROTO)
6027/*
6028 * Return the index in ScreenLines[] for the current screen line.
6029 */
6030 int
6031screen_get_current_line_off()
6032{
6033 return (int)(current_ScreenLine - ScreenLines);
6034}
6035#endif
6036
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037/*
6038 * Move one "cooked" screen line to the screen, but only the characters that
6039 * have actually changed. Handle insert/delete character.
6040 * "coloff" gives the first column on the screen for this line.
6041 * "endcol" gives the columns where valid characters are.
6042 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6043 * needs to be cleared, negative otherwise.
6044 * "rlflag" is TRUE in a rightleft window:
6045 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6046 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6047 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006049screen_line(
6050 int row,
6051 int coloff,
6052 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006053 int clear_width,
6054 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055{
6056 unsigned off_from;
6057 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006058#ifdef FEAT_MBYTE
6059 unsigned max_off_from;
6060 unsigned max_off_to;
6061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006063#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 int hl;
6065#endif
6066 int force = FALSE; /* force update rest of the line */
6067 int redraw_this /* bool: does character need redraw? */
6068#ifdef FEAT_GUI
6069 = TRUE /* For GUI when while-loop empty */
6070#endif
6071 ;
6072 int redraw_next; /* redraw_this for next character */
6073#ifdef FEAT_MBYTE
6074 int clear_next = FALSE;
6075 int char_cells; /* 1: normal char */
6076 /* 2: occupies two display cells */
6077# define CHAR_CELLS char_cells
6078#else
6079# define CHAR_CELLS 1
6080#endif
6081
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006082 /* Check for illegal row and col, just in case. */
6083 if (row >= Rows)
6084 row = Rows - 1;
6085 if (endcol > Columns)
6086 endcol = Columns;
6087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088# ifdef FEAT_CLIPBOARD
6089 clip_may_clear_selection(row, row);
6090# endif
6091
6092 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6093 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006094#ifdef FEAT_MBYTE
6095 max_off_from = off_from + screen_Columns;
6096 max_off_to = LineOffset[row] + screen_Columns;
6097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098
6099#ifdef FEAT_RIGHTLEFT
6100 if (rlflag)
6101 {
6102 /* Clear rest first, because it's left of the text. */
6103 if (clear_width > 0)
6104 {
6105 while (col <= endcol && ScreenLines[off_to] == ' '
6106 && ScreenAttrs[off_to] == 0
6107# ifdef FEAT_MBYTE
6108 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6109# endif
6110 )
6111 {
6112 ++off_to;
6113 ++col;
6114 }
6115 if (col <= endcol)
6116 screen_fill(row, row + 1, col + coloff,
6117 endcol + coloff + 1, ' ', ' ', 0);
6118 }
6119 col = endcol + 1;
6120 off_to = LineOffset[row] + col + coloff;
6121 off_from += col;
6122 endcol = (clear_width > 0 ? clear_width : -clear_width);
6123 }
6124#endif /* FEAT_RIGHTLEFT */
6125
6126 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6127
6128 while (col < endcol)
6129 {
6130#ifdef FEAT_MBYTE
6131 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006132 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 else
6134 char_cells = 1;
6135#endif
6136
6137 redraw_this = redraw_next;
6138 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6139 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6140
6141#ifdef FEAT_GUI
6142 /* If the next character was bold, then redraw the current character to
6143 * remove any pixels that might have spilt over into us. This only
6144 * happens in the GUI.
6145 */
6146 if (redraw_next && gui.in_use)
6147 {
6148 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006149 if (hl > HL_ALL)
6150 hl = syn_attr2attr(hl);
6151 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 redraw_this = TRUE;
6153 }
6154#endif
6155
6156 if (redraw_this)
6157 {
6158 /*
6159 * Special handling when 'xs' termcap flag set (hpterm):
6160 * Attributes for characters are stored at the position where the
6161 * cursor is when writing the highlighting code. The
6162 * start-highlighting code must be written with the cursor on the
6163 * first highlighted character. The stop-highlighting code must
6164 * be written with the cursor just after the last highlighted
6165 * character.
6166 * Overwriting a character doesn't remove it's highlighting. Need
6167 * to clear the rest of the line, and force redrawing it
6168 * completely.
6169 */
6170 if ( p_wiv
6171 && !force
6172#ifdef FEAT_GUI
6173 && !gui.in_use
6174#endif
6175 && ScreenAttrs[off_to] != 0
6176 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6177 {
6178 /*
6179 * Need to remove highlighting attributes here.
6180 */
6181 windgoto(row, col + coloff);
6182 out_str(T_CE); /* clear rest of this screen line */
6183 screen_start(); /* don't know where cursor is now */
6184 force = TRUE; /* force redraw of rest of the line */
6185 redraw_next = TRUE; /* or else next char would miss out */
6186
6187 /*
6188 * If the previous character was highlighted, need to stop
6189 * highlighting at this character.
6190 */
6191 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6192 {
6193 screen_attr = ScreenAttrs[off_to - 1];
6194 term_windgoto(row, col + coloff);
6195 screen_stop_highlight();
6196 }
6197 else
6198 screen_attr = 0; /* highlighting has stopped */
6199 }
6200#ifdef FEAT_MBYTE
6201 if (enc_dbcs != 0)
6202 {
6203 /* Check if overwriting a double-byte with a single-byte or
6204 * the other way around requires another character to be
6205 * redrawn. For UTF-8 this isn't needed, because comparing
6206 * ScreenLinesUC[] is sufficient. */
6207 if (char_cells == 1
6208 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006209 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 {
6211 /* Writing a single-cell character over a double-cell
6212 * character: need to redraw the next cell. */
6213 ScreenLines[off_to + 1] = 0;
6214 redraw_next = TRUE;
6215 }
6216 else if (char_cells == 2
6217 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006218 && (*mb_off2cells)(off_to, max_off_to) == 1
6219 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 {
6221 /* Writing the second half of a double-cell character over
6222 * a double-cell character: need to redraw the second
6223 * cell. */
6224 ScreenLines[off_to + 2] = 0;
6225 redraw_next = TRUE;
6226 }
6227
6228 if (enc_dbcs == DBCS_JPNU)
6229 ScreenLines2[off_to] = ScreenLines2[off_from];
6230 }
6231 /* When writing a single-width character over a double-width
6232 * character and at the end of the redrawn text, need to clear out
6233 * the right halve of the old character.
6234 * Also required when writing the right halve of a double-width
6235 * char over the left halve of an existing one. */
6236 if (has_mbyte && col + char_cells == endcol
6237 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006238 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006240 && (*mb_off2cells)(off_to, max_off_to) == 1
6241 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 clear_next = TRUE;
6243#endif
6244
6245 ScreenLines[off_to] = ScreenLines[off_from];
6246#ifdef FEAT_MBYTE
6247 if (enc_utf8)
6248 {
6249 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6250 if (ScreenLinesUC[off_from] != 0)
6251 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006252 int i;
6253
6254 for (i = 0; i < Screen_mco; ++i)
6255 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 }
6257 }
6258 if (char_cells == 2)
6259 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6260#endif
6261
6262#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006263 /* The bold trick makes a single column of pixels appear in the
6264 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 * character should be redrawn too. This happens for our own GUI
6266 * and for some xterms. */
6267 if (
6268# ifdef FEAT_GUI
6269 gui.in_use
6270# endif
6271# if defined(FEAT_GUI) && defined(UNIX)
6272 ||
6273# endif
6274# ifdef UNIX
6275 term_is_xterm
6276# endif
6277 )
6278 {
6279 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006280 if (hl > HL_ALL)
6281 hl = syn_attr2attr(hl);
6282 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 redraw_next = TRUE;
6284 }
6285#endif
6286 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6287#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006288 /* For simplicity set the attributes of second half of a
6289 * double-wide character equal to the first half. */
6290 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006292
6293 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 else
6296#endif
6297 screen_char(off_to, row, col + coloff);
6298 }
6299 else if ( p_wiv
6300#ifdef FEAT_GUI
6301 && !gui.in_use
6302#endif
6303 && col + coloff > 0)
6304 {
6305 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6306 {
6307 /*
6308 * Don't output stop-highlight when moving the cursor, it will
6309 * stop the highlighting when it should continue.
6310 */
6311 screen_attr = 0;
6312 }
6313 else if (screen_attr != 0)
6314 screen_stop_highlight();
6315 }
6316
6317 off_to += CHAR_CELLS;
6318 off_from += CHAR_CELLS;
6319 col += CHAR_CELLS;
6320 }
6321
6322#ifdef FEAT_MBYTE
6323 if (clear_next)
6324 {
6325 /* Clear the second half of a double-wide character of which the left
6326 * half was overwritten with a single-wide character. */
6327 ScreenLines[off_to] = ' ';
6328 if (enc_utf8)
6329 ScreenLinesUC[off_to] = 0;
6330 screen_char(off_to, row, col + coloff);
6331 }
6332#endif
6333
6334 if (clear_width > 0
6335#ifdef FEAT_RIGHTLEFT
6336 && !rlflag
6337#endif
6338 )
6339 {
6340#ifdef FEAT_GUI
6341 int startCol = col;
6342#endif
6343
6344 /* blank out the rest of the line */
6345 while (col < clear_width && ScreenLines[off_to] == ' '
6346 && ScreenAttrs[off_to] == 0
6347#ifdef FEAT_MBYTE
6348 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6349#endif
6350 )
6351 {
6352 ++off_to;
6353 ++col;
6354 }
6355 if (col < clear_width)
6356 {
6357#ifdef FEAT_GUI
6358 /*
6359 * In the GUI, clearing the rest of the line may leave pixels
6360 * behind if the first character cleared was bold. Some bold
6361 * fonts spill over the left. In this case we redraw the previous
6362 * character too. If we didn't skip any blanks above, then we
6363 * only redraw if the character wasn't already redrawn anyway.
6364 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006365 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 {
6367 hl = ScreenAttrs[off_to];
6368 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006369 {
6370 int prev_cells = 1;
6371# ifdef FEAT_MBYTE
6372 if (enc_utf8)
6373 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6374 * that its width is 2. */
6375 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6376 else if (enc_dbcs != 0)
6377 {
6378 /* find previous character by counting from first
6379 * column and get its width. */
6380 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006381 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006382
6383 while (off < off_to)
6384 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006385 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006386 off += prev_cells;
6387 }
6388 }
6389
6390 if (enc_dbcs != 0 && prev_cells > 1)
6391 screen_char_2(off_to - prev_cells, row,
6392 col + coloff - prev_cells);
6393 else
6394# endif
6395 screen_char(off_to - prev_cells, row,
6396 col + coloff - prev_cells);
6397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399#endif
6400 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6401 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006402#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 off_to += clear_width - col;
6404 col = clear_width;
6405#endif
6406 }
6407 }
6408
6409 if (clear_width > 0)
6410 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006411#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 /* For a window that's left of another, draw the separator char. */
6413 if (col + coloff < Columns)
6414 {
6415 int c;
6416
6417 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006418 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006420 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6421 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422# endif
6423 || ScreenAttrs[off_to] != hl)
6424 {
6425 ScreenLines[off_to] = c;
6426 ScreenAttrs[off_to] = hl;
6427# ifdef FEAT_MBYTE
6428 if (enc_utf8)
6429 {
6430 if (c >= 0x80)
6431 {
6432 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006433 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 }
6435 else
6436 ScreenLinesUC[off_to] = 0;
6437 }
6438# endif
6439 screen_char(off_to, row, col + coloff);
6440 }
6441 }
6442 else
6443#endif
6444 LineWraps[row] = FALSE;
6445 }
6446}
6447
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006448#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006450 * Mirror text "str" for right-left displaying.
6451 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006453 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006454rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
6456 char_u *p1, *p2;
6457 int t;
6458
6459 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6460 {
6461 t = *p1;
6462 *p1 = *p2;
6463 *p2 = t;
6464 }
6465}
6466#endif
6467
6468#if defined(FEAT_WINDOWS) || defined(PROTO)
6469/*
6470 * mark all status lines for redraw; used after first :cd
6471 */
6472 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006473status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
6475 win_T *wp;
6476
Bram Moolenaar29323592016-07-24 22:04:11 +02006477 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 if (wp->w_status_height)
6479 {
6480 wp->w_redr_status = TRUE;
6481 redraw_later(VALID);
6482 }
6483}
6484
6485/*
6486 * mark all status lines of the current buffer for redraw
6487 */
6488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006489status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
6491 win_T *wp;
6492
Bram Moolenaar29323592016-07-24 22:04:11 +02006493 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6495 {
6496 wp->w_redr_status = TRUE;
6497 redraw_later(VALID);
6498 }
6499}
6500
6501/*
6502 * Redraw all status lines that need to be redrawn.
6503 */
6504 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006505redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
6507 win_T *wp;
6508
Bram Moolenaar29323592016-07-24 22:04:11 +02006509 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 if (wp->w_redr_status)
6511 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006512 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006513 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514}
6515#endif
6516
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006517#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518/*
6519 * Redraw all status lines at the bottom of frame "frp".
6520 */
6521 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006522win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
6524 if (frp->fr_layout == FR_LEAF)
6525 frp->fr_win->w_redr_status = TRUE;
6526 else if (frp->fr_layout == FR_ROW)
6527 {
6528 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6529 win_redraw_last_status(frp);
6530 }
6531 else /* frp->fr_layout == FR_COL */
6532 {
6533 frp = frp->fr_child;
6534 while (frp->fr_next != NULL)
6535 frp = frp->fr_next;
6536 win_redraw_last_status(frp);
6537 }
6538}
6539#endif
6540
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006541#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542/*
6543 * Draw the verticap separator right of window "wp" starting with line "row".
6544 */
6545 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006546draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
6548 int hl;
6549 int c;
6550
6551 if (wp->w_vsep_width)
6552 {
6553 /* draw the vertical separator right of this window */
6554 c = fillchar_vsep(&hl);
6555 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6556 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6557 c, ' ', hl);
6558 }
6559}
6560#endif
6561
6562#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006563static int status_match_len(expand_T *xp, char_u *s);
6564static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565
6566/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006567 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 */
6569 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006570status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
6572 int len = 0;
6573
6574#ifdef FEAT_MENU
6575 int emenu = (xp->xp_context == EXPAND_MENUS
6576 || xp->xp_context == EXPAND_MENUNAMES);
6577
6578 /* Check for menu separators - replace with '|'. */
6579 if (emenu && menu_is_separator(s))
6580 return 1;
6581#endif
6582
6583 while (*s != NUL)
6584 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006585 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006586 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006587 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 }
6589
6590 return len;
6591}
6592
6593/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006594 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006595 * These are backslashes used for escaping. Do show backslashes in help tags.
6596 */
6597 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006598skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006599{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006600 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006601#ifdef FEAT_MENU
6602 || ((xp->xp_context == EXPAND_MENUS
6603 || xp->xp_context == EXPAND_MENUNAMES)
6604 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6605#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006606 )
6607 {
6608#ifndef BACKSLASH_IN_FILENAME
6609 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6610 return 2;
6611#endif
6612 return 1;
6613 }
6614 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006615}
6616
6617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 * Show wildchar matches in the status line.
6619 * Show at least the "match" item.
6620 * We start at item 'first_match' in the list and show all matches that fit.
6621 *
6622 * If inversion is possible we use it. Else '=' characters are used.
6623 */
6624 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006625win_redr_status_matches(
6626 expand_T *xp,
6627 int num_matches,
6628 char_u **matches, /* list of matches */
6629 int match,
6630 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
6632#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6633 int row;
6634 char_u *buf;
6635 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006636 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 int fillchar;
6638 int attr;
6639 int i;
6640 int highlight = TRUE;
6641 char_u *selstart = NULL;
6642 int selstart_col = 0;
6643 char_u *selend = NULL;
6644 static int first_match = 0;
6645 int add_left = FALSE;
6646 char_u *s;
6647#ifdef FEAT_MENU
6648 int emenu;
6649#endif
6650#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6651 int l;
6652#endif
6653
6654 if (matches == NULL) /* interrupted completion? */
6655 return;
6656
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006657#ifdef FEAT_MBYTE
6658 if (has_mbyte)
6659 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6660 else
6661#endif
6662 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 if (buf == NULL)
6664 return;
6665
6666 if (match == -1) /* don't show match but original text */
6667 {
6668 match = 0;
6669 highlight = FALSE;
6670 }
6671 /* count 1 for the ending ">" */
6672 clen = status_match_len(xp, L_MATCH(match)) + 3;
6673 if (match == 0)
6674 first_match = 0;
6675 else if (match < first_match)
6676 {
6677 /* jumping left, as far as we can go */
6678 first_match = match;
6679 add_left = TRUE;
6680 }
6681 else
6682 {
6683 /* check if match fits on the screen */
6684 for (i = first_match; i < match; ++i)
6685 clen += status_match_len(xp, L_MATCH(i)) + 2;
6686 if (first_match > 0)
6687 clen += 2;
6688 /* jumping right, put match at the left */
6689 if ((long)clen > Columns)
6690 {
6691 first_match = match;
6692 /* if showing the last match, we can add some on the left */
6693 clen = 2;
6694 for (i = match; i < num_matches; ++i)
6695 {
6696 clen += status_match_len(xp, L_MATCH(i)) + 2;
6697 if ((long)clen >= Columns)
6698 break;
6699 }
6700 if (i == num_matches)
6701 add_left = TRUE;
6702 }
6703 }
6704 if (add_left)
6705 while (first_match > 0)
6706 {
6707 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6708 if ((long)clen >= Columns)
6709 break;
6710 --first_match;
6711 }
6712
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006713 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
6715 if (first_match == 0)
6716 {
6717 *buf = NUL;
6718 len = 0;
6719 }
6720 else
6721 {
6722 STRCPY(buf, "< ");
6723 len = 2;
6724 }
6725 clen = len;
6726
6727 i = first_match;
6728 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6729 {
6730 if (i == match)
6731 {
6732 selstart = buf + len;
6733 selstart_col = clen;
6734 }
6735
6736 s = L_MATCH(i);
6737 /* Check for menu separators - replace with '|' */
6738#ifdef FEAT_MENU
6739 emenu = (xp->xp_context == EXPAND_MENUS
6740 || xp->xp_context == EXPAND_MENUNAMES);
6741 if (emenu && menu_is_separator(s))
6742 {
6743 STRCPY(buf + len, transchar('|'));
6744 l = (int)STRLEN(buf + len);
6745 len += l;
6746 clen += l;
6747 }
6748 else
6749#endif
6750 for ( ; *s != NUL; ++s)
6751 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006752 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 clen += ptr2cells(s);
6754#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006755 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {
6757 STRNCPY(buf + len, s, l);
6758 s += l - 1;
6759 len += l;
6760 }
6761 else
6762#endif
6763 {
6764 STRCPY(buf + len, transchar_byte(*s));
6765 len += (int)STRLEN(buf + len);
6766 }
6767 }
6768 if (i == match)
6769 selend = buf + len;
6770
6771 *(buf + len++) = ' ';
6772 *(buf + len++) = ' ';
6773 clen += 2;
6774 if (++i == num_matches)
6775 break;
6776 }
6777
6778 if (i != num_matches)
6779 {
6780 *(buf + len++) = '>';
6781 ++clen;
6782 }
6783
6784 buf[len] = NUL;
6785
6786 row = cmdline_row - 1;
6787 if (row >= 0)
6788 {
6789 if (wild_menu_showing == 0)
6790 {
6791 if (msg_scrolled > 0)
6792 {
6793 /* Put the wildmenu just above the command line. If there is
6794 * no room, scroll the screen one line up. */
6795 if (cmdline_row == Rows - 1)
6796 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006797 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 ++msg_scrolled;
6799 }
6800 else
6801 {
6802 ++cmdline_row;
6803 ++row;
6804 }
6805 wild_menu_showing = WM_SCROLLED;
6806 }
6807 else
6808 {
6809 /* Create status line if needed by setting 'laststatus' to 2.
6810 * Set 'winminheight' to zero to avoid that the window is
6811 * resized. */
6812 if (lastwin->w_status_height == 0)
6813 {
6814 save_p_ls = p_ls;
6815 save_p_wmh = p_wmh;
6816 p_ls = 2;
6817 p_wmh = 0;
6818 last_status(FALSE);
6819 }
6820 wild_menu_showing = WM_SHOWN;
6821 }
6822 }
6823
6824 screen_puts(buf, row, 0, attr);
6825 if (selstart != NULL && highlight)
6826 {
6827 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006828 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 }
6830
6831 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6832 }
6833
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006834#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 win_redraw_last_status(topframe);
6836#else
6837 lastwin->w_redr_status = TRUE;
6838#endif
6839 vim_free(buf);
6840}
6841#endif
6842
6843#if defined(FEAT_WINDOWS) || defined(PROTO)
6844/*
6845 * Redraw the status line of window wp.
6846 *
6847 * If inversion is possible we use it. Else '=' characters are used.
6848 */
6849 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006850win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851{
6852 int row;
6853 char_u *p;
6854 int len;
6855 int fillchar;
6856 int attr;
6857 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006858 static int busy = FALSE;
6859
6860 /* It's possible to get here recursively when 'statusline' (indirectly)
6861 * invokes ":redrawstatus". Simply ignore the call then. */
6862 if (busy)
6863 return;
6864 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865
6866 wp->w_redr_status = FALSE;
6867 if (wp->w_status_height == 0)
6868 {
6869 /* no status line, can only be last window */
6870 redraw_cmdline = TRUE;
6871 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006872 else if (!redrawing()
6873#ifdef FEAT_INS_EXPAND
6874 /* don't update status line when popup menu is visible and may be
6875 * drawn over it */
6876 || pum_visible()
6877#endif
6878 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {
6880 /* Don't redraw right now, do it later. */
6881 wp->w_redr_status = TRUE;
6882 }
6883#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006884 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
6886 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006887 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 }
6889#endif
6890 else
6891 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006892 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006894 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 p = NameBuff;
6896 len = (int)STRLEN(p);
6897
Bram Moolenaard85f2712017-07-28 21:51:57 +02006898 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899#ifdef FEAT_QUICKFIX
6900 || wp->w_p_pvw
6901#endif
6902 || bufIsChanged(wp->w_buffer)
6903 || wp->w_buffer->b_p_ro)
6904 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006905 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006907 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 len += (int)STRLEN(p + len);
6909 }
6910#ifdef FEAT_QUICKFIX
6911 if (wp->w_p_pvw)
6912 {
6913 STRCPY(p + len, _("[Preview]"));
6914 len += (int)STRLEN(p + len);
6915 }
6916#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006917 if (bufIsChanged(wp->w_buffer)
6918#ifdef FEAT_TERMINAL
6919 && !bt_terminal(wp->w_buffer)
6920#endif
6921 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 {
6923 STRCPY(p + len, "[+]");
6924 len += 3;
6925 }
6926 if (wp->w_buffer->b_p_ro)
6927 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006928 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006929 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 }
6931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6933 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6934 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6935 if (this_ru_col <= 1)
6936 {
6937 p = (char_u *)"<"; /* No room for file name! */
6938 len = 1;
6939 }
6940 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941#ifdef FEAT_MBYTE
6942 if (has_mbyte)
6943 {
6944 int clen = 0, i;
6945
6946 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006947 clen = mb_string2cells(p, -1);
6948
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 /* Find first character that will fit.
6950 * Going from start to end is much faster for DBCS. */
6951 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006952 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 clen -= (*mb_ptr2cells)(p + i);
6954 len = clen;
6955 if (i > 0)
6956 {
6957 p = p + i - 1;
6958 *p = '<';
6959 ++len;
6960 }
6961
6962 }
6963 else
6964#endif
6965 if (len > this_ru_col - 1)
6966 {
6967 p += len - (this_ru_col - 1);
6968 *p = '<';
6969 len = this_ru_col - 1;
6970 }
6971
6972 row = W_WINROW(wp) + wp->w_height;
6973 screen_puts(p, row, W_WINCOL(wp), attr);
6974 screen_fill(row, row + 1, len + W_WINCOL(wp),
6975 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6976
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006977 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6979 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6980 - 1 + W_WINCOL(wp)), attr);
6981
6982#ifdef FEAT_CMDL_INFO
6983 win_redr_ruler(wp, TRUE);
6984#endif
6985 }
6986
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 /*
6988 * May need to draw the character below the vertical separator.
6989 */
6990 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6991 {
6992 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006993 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 else
6995 fillchar = fillchar_vsep(&attr);
6996 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6997 attr);
6998 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006999 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000}
7001
Bram Moolenaar238a5642006-02-21 22:12:05 +00007002#ifdef FEAT_STL_OPT
7003/*
7004 * Redraw the status line according to 'statusline' and take care of any
7005 * errors encountered.
7006 */
7007 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007008redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007009{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007010 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007011 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007012
7013 /* When called recursively return. This can happen when the statusline
7014 * contains an expression that triggers a redraw. */
7015 if (entered)
7016 return;
7017 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007018
Bram Moolenaara742e082016-04-05 21:10:38 +02007019 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007020 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007021 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007022 {
7023 /* When there is an error disable the statusline, otherwise the
7024 * display is messed up with errors and a redraw triggers the problem
7025 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007026 set_string_option_direct((char_u *)"statusline", -1,
7027 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007028 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007029 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007030 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007031 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007032}
7033#endif
7034
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035/*
7036 * Return TRUE if the status line of window "wp" is connected to the status
7037 * line of the window right of it. If not, then it's a vertical separator.
7038 * Only call if (wp->w_vsep_width != 0).
7039 */
7040 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007041stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 frame_T *fr;
7044
7045 fr = wp->w_frame;
7046 while (fr->fr_parent != NULL)
7047 {
7048 if (fr->fr_parent->fr_layout == FR_COL)
7049 {
7050 if (fr->fr_next != NULL)
7051 break;
7052 }
7053 else
7054 {
7055 if (fr->fr_next != NULL)
7056 return TRUE;
7057 }
7058 fr = fr->fr_parent;
7059 }
7060 return FALSE;
7061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062
7063#endif /* FEAT_WINDOWS */
7064
7065#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
7066/*
7067 * Get the value to show for the language mappings, active 'keymap'.
7068 */
7069 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007070get_keymap_str(
7071 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007072 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007073 char_u *buf, /* buffer for the result */
7074 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 char_u *p;
7077
7078 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7079 return FALSE;
7080
7081 {
7082#ifdef FEAT_EVAL
7083 buf_T *old_curbuf = curbuf;
7084 win_T *old_curwin = curwin;
7085 char_u *s;
7086
7087 curbuf = wp->w_buffer;
7088 curwin = wp;
7089 STRCPY(buf, "b:keymap_name"); /* must be writable */
7090 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007091 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 --emsg_skip;
7093 curbuf = old_curbuf;
7094 curwin = old_curwin;
7095 if (p == NULL || *p == NUL)
7096#endif
7097 {
7098#ifdef FEAT_KEYMAP
7099 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7100 p = wp->w_buffer->b_p_keymap;
7101 else
7102#endif
7103 p = (char_u *)"lang";
7104 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007105 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 buf[0] = NUL;
7107#ifdef FEAT_EVAL
7108 vim_free(s);
7109#endif
7110 }
7111 return buf[0] != NUL;
7112}
7113#endif
7114
7115#if defined(FEAT_STL_OPT) || defined(PROTO)
7116/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007117 * Redraw the status line or ruler of window "wp".
7118 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 */
7120 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007121win_redr_custom(
7122 win_T *wp,
7123 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007125 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 int attr;
7127 int curattr;
7128 int row;
7129 int col = 0;
7130 int maxwidth;
7131 int width;
7132 int n;
7133 int len;
7134 int fillchar;
7135 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007136 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007138 struct stl_hlrec hltab[STL_MAX_ITEM];
7139 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007140 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007141 win_T *ewp;
7142 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143
Bram Moolenaar1d633412013-12-11 15:52:01 +01007144 /* There is a tiny chance that this gets called recursively: When
7145 * redrawing a status line triggers redrawing the ruler or tabline.
7146 * Avoid trouble by not allowing recursion. */
7147 if (entered)
7148 return;
7149 entered = TRUE;
7150
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007152 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007154 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007155 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007156 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007157 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007158 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007159 maxwidth = Columns;
7160# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007161 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007164 else
7165 {
7166 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007167 fillchar = fillchar_status(&attr, wp);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007168 maxwidth = W_WIDTH(wp);
7169
7170 if (draw_ruler)
7171 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007172 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007173 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007174 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007175 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007176 if (*++stl == '-')
7177 stl++;
7178 if (atoi((char *)stl))
7179 while (VIM_ISDIGIT(*stl))
7180 stl++;
7181 if (*stl++ != '(')
7182 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007183 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007184#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007185 col = ru_col - (Columns - W_WIDTH(wp));
7186 if (col < (W_WIDTH(wp) + 1) / 2)
7187 col = (W_WIDTH(wp) + 1) / 2;
7188#else
7189 col = ru_col;
7190 if (col > (Columns + 1) / 2)
7191 col = (Columns + 1) / 2;
7192#endif
7193 maxwidth = W_WIDTH(wp) - col;
7194#ifdef FEAT_WINDOWS
7195 if (!wp->w_status_height)
7196#endif
7197 {
7198 row = Rows - 1;
7199 --maxwidth; /* writing in last column may cause scrolling */
7200 fillchar = ' ';
7201 attr = 0;
7202 }
7203
7204# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007205 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007206# endif
7207 }
7208 else
7209 {
7210 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007211 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007212 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007213 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007214# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007215 use_sandbox = was_set_insecurely((char_u *)"statusline",
7216 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007217# endif
7218 }
7219
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007220#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007221 col += W_WINCOL(wp);
7222#endif
7223 }
7224
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007226 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227
Bram Moolenaar61452852011-02-01 18:01:11 +01007228 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7229 * the cursor away and back. */
7230 ewp = wp == NULL ? curwin : wp;
7231 p_crb_save = ewp->w_p_crb;
7232 ewp->w_p_crb = FALSE;
7233
Bram Moolenaar362f3562009-11-03 16:20:34 +00007234 /* Make a copy, because the statusline may include a function call that
7235 * might change the option value and free the memory. */
7236 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007237 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007238 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007239 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007240 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007241 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007243 /* Make all characters printable. */
7244 p = transstr(buf);
7245 if (p != NULL)
7246 {
7247 vim_strncpy(buf, p, sizeof(buf) - 1);
7248 vim_free(p);
7249 }
7250
7251 /* fill up with "fillchar" */
7252 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007253 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 {
7255#ifdef FEAT_MBYTE
7256 len += (*mb_char2bytes)(fillchar, buf + len);
7257#else
7258 buf[len++] = fillchar;
7259#endif
7260 ++width;
7261 }
7262 buf[len] = NUL;
7263
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007264 /*
7265 * Draw each snippet with the specified highlighting.
7266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 curattr = attr;
7268 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007269 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007271 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 screen_puts_len(p, len, row, col, curattr);
7273 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007274 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007276 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007278 else if (hltab[n].userhl < 0)
7279 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280#ifdef FEAT_WINDOWS
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007281# ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007282 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7283 && wp->w_status_height != 0)
7284 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007285 else if (wp != NULL && bt_terminal(wp->w_buffer)
7286 && wp->w_status_height != 0)
7287 curattr = highlight_stlterm[hltab[n].userhl - 1];
7288# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007289 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007290 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291#endif
7292 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007293 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 }
7295 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007296
7297 if (wp == NULL)
7298 {
7299 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7300 col = 0;
7301 len = 0;
7302 p = buf;
7303 fillchar = 0;
7304 for (n = 0; tabtab[n].start != NULL; n++)
7305 {
7306 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7307 while (col < len)
7308 TabPageIdxs[col++] = fillchar;
7309 p = tabtab[n].start;
7310 fillchar = tabtab[n].userhl;
7311 }
7312 while (col < Columns)
7313 TabPageIdxs[col++] = fillchar;
7314 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007315
7316theend:
7317 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318}
7319
7320#endif /* FEAT_STL_OPT */
7321
7322/*
7323 * Output a single character directly to the screen and update ScreenLines.
7324 */
7325 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007326screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 char_u buf[MB_MAXBYTES + 1];
7329
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007330#ifdef FEAT_MBYTE
7331 if (has_mbyte)
7332 buf[(*mb_char2bytes)(c, buf)] = NUL;
7333 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007335 {
7336 buf[0] = c;
7337 buf[1] = NUL;
7338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 screen_puts(buf, row, col, attr);
7340}
7341
7342/*
7343 * Get a single character directly from ScreenLines into "bytes[]".
7344 * Also return its attribute in *attrp;
7345 */
7346 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007347screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348{
7349 unsigned off;
7350
7351 /* safety check */
7352 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7353 {
7354 off = LineOffset[row] + col;
7355 *attrp = ScreenAttrs[off];
7356 bytes[0] = ScreenLines[off];
7357 bytes[1] = NUL;
7358
7359#ifdef FEAT_MBYTE
7360 if (enc_utf8 && ScreenLinesUC[off] != 0)
7361 bytes[utfc_char2bytes(off, bytes)] = NUL;
7362 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7363 {
7364 bytes[0] = ScreenLines[off];
7365 bytes[1] = ScreenLines2[off];
7366 bytes[2] = NUL;
7367 }
7368 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7369 {
7370 bytes[1] = ScreenLines[off + 1];
7371 bytes[2] = NUL;
7372 }
7373#endif
7374 }
7375}
7376
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007377#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007378static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007379
7380/*
7381 * Return TRUE if composing characters for screen posn "off" differs from
7382 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007383 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007384 */
7385 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007386screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007387{
7388 int i;
7389
7390 for (i = 0; i < Screen_mco; ++i)
7391 {
7392 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7393 return TRUE;
7394 if (u8cc[i] == 0)
7395 break;
7396 }
7397 return FALSE;
7398}
7399#endif
7400
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401/*
7402 * Put string '*text' on the screen at position 'row' and 'col', with
7403 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7404 * Note: only outputs within one row, message is truncated at screen boundary!
7405 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7406 */
7407 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007408screen_puts(
7409 char_u *text,
7410 int row,
7411 int col,
7412 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413{
7414 screen_puts_len(text, -1, row, col, attr);
7415}
7416
7417/*
7418 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7419 * a NUL.
7420 */
7421 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007422screen_puts_len(
7423 char_u *text,
7424 int textlen,
7425 int row,
7426 int col,
7427 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429 unsigned off;
7430 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007431 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 int c;
7433#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007434 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 int mbyte_blen = 1;
7436 int mbyte_cells = 1;
7437 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007438 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 int clear_next_cell = FALSE;
7440# ifdef FEAT_ARABIC
7441 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007442 int pc, nc, nc1;
7443 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444# endif
7445#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007446#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7447 int force_redraw_this;
7448 int force_redraw_next = FALSE;
7449#endif
7450 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451
7452 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7453 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007454 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455
Bram Moolenaarc236c162008-07-13 17:41:49 +00007456#ifdef FEAT_MBYTE
7457 /* When drawing over the right halve of a double-wide char clear out the
7458 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007459 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007460# ifdef FEAT_GUI
7461 && !gui.in_use
7462# endif
7463 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007464 {
7465 ScreenLines[off - 1] = ' ';
7466 ScreenAttrs[off - 1] = 0;
7467 if (enc_utf8)
7468 {
7469 ScreenLinesUC[off - 1] = 0;
7470 ScreenLinesC[0][off - 1] = 0;
7471 }
7472 /* redraw the previous cell, make it empty */
7473 screen_char(off - 1, row, col - 1);
7474 /* force the cell at "col" to be redrawn */
7475 force_redraw_next = TRUE;
7476 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007477#endif
7478
Bram Moolenaar367329b2007-08-30 11:53:22 +00007479#ifdef FEAT_MBYTE
7480 max_off = LineOffset[row] + screen_Columns;
7481#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007482 while (col < screen_Columns
7483 && (len < 0 || (int)(ptr - text) < len)
7484 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 {
7486 c = *ptr;
7487#ifdef FEAT_MBYTE
7488 /* check if this is the first byte of a multibyte */
7489 if (has_mbyte)
7490 {
7491 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007492 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007494 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7496 mbyte_cells = 1;
7497 else if (enc_dbcs != 0)
7498 mbyte_cells = mbyte_blen;
7499 else /* enc_utf8 */
7500 {
7501 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007502 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 (int)((text + len) - ptr));
7504 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007505 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007507# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 /* Non-BMP character: display as ? or fullwidth ?. */
7509 if (u8c >= 0x10000)
7510 {
7511 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7512 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007513 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516# ifdef FEAT_ARABIC
7517 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7518 {
7519 /* Do Arabic shaping. */
7520 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7521 {
7522 /* Past end of string to be displayed. */
7523 nc = NUL;
7524 nc1 = NUL;
7525 }
7526 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007527 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007528 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7529 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007530 nc1 = pcc[0];
7531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 pc = prev_c;
7533 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007534 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 }
7536 else
7537 prev_c = u8c;
7538# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007539 if (col + mbyte_cells > screen_Columns)
7540 {
7541 /* Only 1 cell left, but character requires 2 cells:
7542 * display a '>' in the last column to avoid wrapping. */
7543 c = '>';
7544 mbyte_cells = 1;
7545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 }
7547 }
7548#endif
7549
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007550#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7551 force_redraw_this = force_redraw_next;
7552 force_redraw_next = FALSE;
7553#endif
7554
7555 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556#ifdef FEAT_MBYTE
7557 || (mbyte_cells == 2
7558 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7559 || (enc_dbcs == DBCS_JPNU
7560 && c == 0x8e
7561 && ScreenLines2[off] != ptr[1])
7562 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007563 && (ScreenLinesUC[off] !=
7564 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7565 || (ScreenLinesUC[off] != 0
7566 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567#endif
7568 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007569 || exmode_active;
7570
7571 if (need_redraw
7572#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7573 || force_redraw_this
7574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 )
7576 {
7577#if defined(FEAT_GUI) || defined(UNIX)
7578 /* The bold trick makes a single row of pixels appear in the next
7579 * character. When a bold character is removed, the next
7580 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007581 * and for some xterms. */
7582 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583# ifdef FEAT_GUI
7584 gui.in_use
7585# endif
7586# if defined(FEAT_GUI) && defined(UNIX)
7587 ||
7588# endif
7589# ifdef UNIX
7590 term_is_xterm
7591# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007592 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007594 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007596 if (n > HL_ALL)
7597 n = syn_attr2attr(n);
7598 if (n & HL_BOLD)
7599 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 }
7601#endif
7602#ifdef FEAT_MBYTE
7603 /* When at the end of the text and overwriting a two-cell
7604 * character with a one-cell character, need to clear the next
7605 * cell. Also when overwriting the left halve of a two-cell char
7606 * with the right halve of a two-cell char. Do this only once
7607 * (mb_off2cells() may return 2 on the right halve). */
7608 if (clear_next_cell)
7609 clear_next_cell = FALSE;
7610 else if (has_mbyte
7611 && (len < 0 ? ptr[mbyte_blen] == NUL
7612 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007613 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007615 && (*mb_off2cells)(off, max_off) == 1
7616 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 clear_next_cell = TRUE;
7618
7619 /* Make sure we never leave a second byte of a double-byte behind,
7620 * it confuses mb_off2cells(). */
7621 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007622 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007624 && (*mb_off2cells)(off, max_off) == 1
7625 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 ScreenLines[off + mbyte_blen] = 0;
7627#endif
7628 ScreenLines[off] = c;
7629 ScreenAttrs[off] = attr;
7630#ifdef FEAT_MBYTE
7631 if (enc_utf8)
7632 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007633 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 ScreenLinesUC[off] = 0;
7635 else
7636 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007637 int i;
7638
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007640 for (i = 0; i < Screen_mco; ++i)
7641 {
7642 ScreenLinesC[i][off] = u8cc[i];
7643 if (u8cc[i] == 0)
7644 break;
7645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 }
7647 if (mbyte_cells == 2)
7648 {
7649 ScreenLines[off + 1] = 0;
7650 ScreenAttrs[off + 1] = attr;
7651 }
7652 screen_char(off, row, col);
7653 }
7654 else if (mbyte_cells == 2)
7655 {
7656 ScreenLines[off + 1] = ptr[1];
7657 ScreenAttrs[off + 1] = attr;
7658 screen_char_2(off, row, col);
7659 }
7660 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7661 {
7662 ScreenLines2[off] = ptr[1];
7663 screen_char(off, row, col);
7664 }
7665 else
7666#endif
7667 screen_char(off, row, col);
7668 }
7669#ifdef FEAT_MBYTE
7670 if (has_mbyte)
7671 {
7672 off += mbyte_cells;
7673 col += mbyte_cells;
7674 ptr += mbyte_blen;
7675 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007676 {
7677 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007679 len = -1;
7680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 }
7682 else
7683#endif
7684 {
7685 ++off;
7686 ++col;
7687 ++ptr;
7688 }
7689 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007690
7691#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7692 /* If we detected the next character needs to be redrawn, but the text
7693 * doesn't extend up to there, update the character here. */
7694 if (force_redraw_next && col < screen_Columns)
7695 {
7696# ifdef FEAT_MBYTE
7697 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7698 screen_char_2(off, row, col);
7699 else
7700# endif
7701 screen_char(off, row, col);
7702 }
7703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704}
7705
7706#ifdef FEAT_SEARCH_EXTRA
7707/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007708 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 */
7710 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007711start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 if (p_hls && !no_hlsearch)
7714 {
7715 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007716 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007717# ifdef FEAT_RELTIME
7718 /* Set the time limit to 'redrawtime'. */
7719 profile_setlimit(p_rdt, &search_hl.tm);
7720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 }
7722}
7723
7724/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007725 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 */
7727 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007728end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729{
7730 if (search_hl.rm.regprog != NULL)
7731 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007732 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 search_hl.rm.regprog = NULL;
7734 }
7735}
7736
7737/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007738 * Init for calling prepare_search_hl().
7739 */
7740 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007741init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007742{
7743 matchitem_T *cur;
7744
7745 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7746 * match */
7747 cur = wp->w_match_head;
7748 while (cur != NULL)
7749 {
7750 cur->hl.rm = cur->match;
7751 if (cur->hlg_id == 0)
7752 cur->hl.attr = 0;
7753 else
7754 cur->hl.attr = syn_id2attr(cur->hlg_id);
7755 cur->hl.buf = wp->w_buffer;
7756 cur->hl.lnum = 0;
7757 cur->hl.first_lnum = 0;
7758# ifdef FEAT_RELTIME
7759 /* Set the time limit to 'redrawtime'. */
7760 profile_setlimit(p_rdt, &(cur->hl.tm));
7761# endif
7762 cur = cur->next;
7763 }
7764 search_hl.buf = wp->w_buffer;
7765 search_hl.lnum = 0;
7766 search_hl.first_lnum = 0;
7767 /* time limit is set at the toplevel, for all windows */
7768}
7769
7770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 * Advance to the match in window "wp" line "lnum" or past it.
7772 */
7773 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007774prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007776 matchitem_T *cur; /* points to the match list */
7777 match_T *shl; /* points to search_hl or a match */
7778 int shl_flag; /* flag to indicate whether search_hl
7779 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007780 int pos_inprogress; /* marks that position match search is
7781 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 int n;
7783
7784 /*
7785 * When using a multi-line pattern, start searching at the top
7786 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007787 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007789 cur = wp->w_match_head;
7790 shl_flag = FALSE;
7791 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007793 if (shl_flag == FALSE)
7794 {
7795 shl = &search_hl;
7796 shl_flag = TRUE;
7797 }
7798 else
7799 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 if (shl->rm.regprog != NULL
7801 && shl->lnum == 0
7802 && re_multiline(shl->rm.regprog))
7803 {
7804 if (shl->first_lnum == 0)
7805 {
7806# ifdef FEAT_FOLDING
7807 for (shl->first_lnum = lnum;
7808 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7809 if (hasFoldingWin(wp, shl->first_lnum - 1,
7810 NULL, NULL, TRUE, NULL))
7811 break;
7812# else
7813 shl->first_lnum = wp->w_topline;
7814# endif
7815 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007816 if (cur != NULL)
7817 cur->pos.cur = 0;
7818 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007820 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7821 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007823 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7824 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007825 pos_inprogress = cur == NULL || cur->pos.cur == 0
7826 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 if (shl->lnum != 0)
7828 {
7829 shl->first_lnum = shl->lnum
7830 + shl->rm.endpos[0].lnum
7831 - shl->rm.startpos[0].lnum;
7832 n = shl->rm.endpos[0].col;
7833 }
7834 else
7835 {
7836 ++shl->first_lnum;
7837 n = 0;
7838 }
7839 }
7840 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007841 if (shl != &search_hl && cur != NULL)
7842 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 }
7844}
7845
7846/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007847 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 * Uses shl->buf.
7849 * Sets shl->lnum and shl->rm contents.
7850 * Note: Assumes a previous match is always before "lnum", unless
7851 * shl->lnum is zero.
7852 * Careful: Any pointers for buffer lines will become invalid.
7853 */
7854 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007855next_search_hl(
7856 win_T *win,
7857 match_T *shl, /* points to search_hl or a match */
7858 linenr_T lnum,
7859 colnr_T mincol, /* minimal column for a match */
7860 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
7862 linenr_T l;
7863 colnr_T matchcol;
7864 long nmatched;
7865
7866 if (shl->lnum != 0)
7867 {
7868 /* Check for three situations:
7869 * 1. If the "lnum" is below a previous match, start a new search.
7870 * 2. If the previous match includes "mincol", use it.
7871 * 3. Continue after the previous match.
7872 */
7873 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7874 if (lnum > l)
7875 shl->lnum = 0;
7876 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7877 return;
7878 }
7879
7880 /*
7881 * Repeat searching for a match until one is found that includes "mincol"
7882 * or none is found in this line.
7883 */
7884 called_emsg = FALSE;
7885 for (;;)
7886 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007887#ifdef FEAT_RELTIME
7888 /* Stop searching after passing the time limit. */
7889 if (profile_passed_limit(&(shl->tm)))
7890 {
7891 shl->lnum = 0; /* no match found in time */
7892 break;
7893 }
7894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 /* Three situations:
7896 * 1. No useful previous match: search from start of line.
7897 * 2. Not Vi compatible or empty match: continue at next character.
7898 * Break the loop if this is beyond the end of the line.
7899 * 3. Vi compatible searching: continue at end of previous match.
7900 */
7901 if (shl->lnum == 0)
7902 matchcol = 0;
7903 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7904 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007905 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007907 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007908
7909 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007910 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007911 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007913 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 shl->lnum = 0;
7915 break;
7916 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007917#ifdef FEAT_MBYTE
7918 if (has_mbyte)
7919 matchcol += mb_ptr2len(ml);
7920 else
7921#endif
7922 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 }
7924 else
7925 matchcol = shl->rm.endpos[0].col;
7926
7927 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007928 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007930 /* Remember whether shl->rm is using a copy of the regprog in
7931 * cur->match. */
7932 int regprog_is_copy = (shl != &search_hl && cur != NULL
7933 && shl == &cur->hl
7934 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007935 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007936
Bram Moolenaarb3414592014-06-17 17:48:32 +02007937 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7938 matchcol,
7939#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007940 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007941#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007942 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007943#endif
7944 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007945 /* Copy the regprog, in case it got freed and recompiled. */
7946 if (regprog_is_copy)
7947 cur->match.regprog = cur->hl.rm.regprog;
7948
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007949 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007950 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007951 /* Error while handling regexp: stop using this regexp. */
7952 if (shl == &search_hl)
7953 {
7954 /* don't free regprog in the match list, it's a copy */
7955 vim_regfree(shl->rm.regprog);
7956 SET_NO_HLSEARCH(TRUE);
7957 }
7958 shl->rm.regprog = NULL;
7959 shl->lnum = 0;
7960 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7961 message */
7962 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007963 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007964 }
7965 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007966 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007967 else
7968 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 if (nmatched == 0)
7970 {
7971 shl->lnum = 0; /* no match found */
7972 break;
7973 }
7974 if (shl->rm.startpos[0].lnum > 0
7975 || shl->rm.startpos[0].col >= mincol
7976 || nmatched > 1
7977 || shl->rm.endpos[0].col > mincol)
7978 {
7979 shl->lnum += shl->rm.startpos[0].lnum;
7980 break; /* useful match found */
7981 }
7982 }
7983}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984
Bram Moolenaar85077472016-10-16 14:35:48 +02007985/*
7986 * If there is a match fill "shl" and return one.
7987 * Return zero otherwise.
7988 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007989 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007990next_search_hl_pos(
7991 match_T *shl, /* points to a match */
7992 linenr_T lnum,
7993 posmatch_T *posmatch, /* match positions */
7994 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007995{
7996 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007997 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007998
Bram Moolenaarb3414592014-06-17 17:48:32 +02007999 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8000 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008001 llpos_T *pos = &posmatch->pos[i];
8002
8003 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008004 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008005 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008006 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008007 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008008 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008009 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008010 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008011 /* if this match comes before the one at "found" then swap
8012 * them */
8013 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008014 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008015 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008016
Bram Moolenaar85077472016-10-16 14:35:48 +02008017 *pos = posmatch->pos[found];
8018 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008019 }
8020 }
8021 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008022 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008023 }
8024 }
8025 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008026 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008027 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008028 colnr_T start = posmatch->pos[found].col == 0
8029 ? 0 : posmatch->pos[found].col - 1;
8030 colnr_T end = posmatch->pos[found].col == 0
8031 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008032
Bram Moolenaar85077472016-10-16 14:35:48 +02008033 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008034 shl->rm.startpos[0].lnum = 0;
8035 shl->rm.startpos[0].col = start;
8036 shl->rm.endpos[0].lnum = 0;
8037 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008038 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008039 posmatch->cur = found + 1;
8040 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008041 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008042 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008043}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008044#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008045
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008047screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048{
8049 attrentry_T *aep = NULL;
8050
8051 screen_attr = attr;
8052 if (full_screen
8053#ifdef WIN3264
8054 && termcap_active
8055#endif
8056 )
8057 {
8058#ifdef FEAT_GUI
8059 if (gui.in_use)
8060 {
8061 char buf[20];
8062
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008063 /* The GUI handles this internally. */
8064 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 OUT_STR(buf);
8066 }
8067 else
8068#endif
8069 {
8070 if (attr > HL_ALL) /* special HL attr. */
8071 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008072 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 aep = syn_cterm_attr2entry(attr);
8074 else
8075 aep = syn_term_attr2entry(attr);
8076 if (aep == NULL) /* did ":syntax clear" */
8077 attr = 0;
8078 else
8079 attr = aep->ae_attr;
8080 }
8081 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8082 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008083 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008084#ifdef FEAT_TERMGUICOLORS
8085 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008086 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008087#endif
8088 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008089#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008090 )
8091#endif
8092 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008093 /* If the Normal FG color has BOLD attribute and the new HL
8094 * has a FG color defined, clear BOLD. */
8095 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8097 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008098 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8099 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 out_str(T_US);
8101 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8102 out_str(T_CZH);
8103 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8104 out_str(T_MR);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008105 if ((attr & HL_STRIKETHROUGH) && T_STS != NULL) /* strike */
8106 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107
8108 /*
8109 * Output the color or start string after bold etc., in case the
8110 * bold etc. override the color setting.
8111 */
8112 if (aep != NULL)
8113 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008114#ifdef FEAT_TERMGUICOLORS
8115 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008117 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008118 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008119 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008120 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 }
8122 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008125 if (t_colors > 1)
8126 {
8127 if (aep->ae_u.cterm.fg_color)
8128 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8129 if (aep->ae_u.cterm.bg_color)
8130 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8131 }
8132 else
8133 {
8134 if (aep->ae_u.term.start != NULL)
8135 out_str(aep->ae_u.term.start);
8136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 }
8138 }
8139 }
8140 }
8141}
8142
8143 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008144screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145{
8146 int do_ME = FALSE; /* output T_ME code */
8147
8148 if (screen_attr != 0
8149#ifdef WIN3264
8150 && termcap_active
8151#endif
8152 )
8153 {
8154#ifdef FEAT_GUI
8155 if (gui.in_use)
8156 {
8157 char buf[20];
8158
8159 /* use internal GUI code */
8160 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8161 OUT_STR(buf);
8162 }
8163 else
8164#endif
8165 {
8166 if (screen_attr > HL_ALL) /* special HL attr. */
8167 {
8168 attrentry_T *aep;
8169
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008170 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {
8172 /*
8173 * Assume that t_me restores the original colors!
8174 */
8175 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008176 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008177#ifdef FEAT_TERMGUICOLORS
8178 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008179 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8180 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008181#endif
8182 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008183#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008184 )
8185#endif
8186 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 do_ME = TRUE;
8188 }
8189 else
8190 {
8191 aep = syn_term_attr2entry(screen_attr);
8192 if (aep != NULL && aep->ae_u.term.stop != NULL)
8193 {
8194 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8195 do_ME = TRUE;
8196 else
8197 out_str(aep->ae_u.term.stop);
8198 }
8199 }
8200 if (aep == NULL) /* did ":syntax clear" */
8201 screen_attr = 0;
8202 else
8203 screen_attr = aep->ae_attr;
8204 }
8205
8206 /*
8207 * Often all ending-codes are equal to T_ME. Avoid outputting the
8208 * same sequence several times.
8209 */
8210 if (screen_attr & HL_STANDOUT)
8211 {
8212 if (STRCMP(T_SE, T_ME) == 0)
8213 do_ME = TRUE;
8214 else
8215 out_str(T_SE);
8216 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008217 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
8219 if (STRCMP(T_UE, T_ME) == 0)
8220 do_ME = TRUE;
8221 else
8222 out_str(T_UE);
8223 }
8224 if (screen_attr & HL_ITALIC)
8225 {
8226 if (STRCMP(T_CZR, T_ME) == 0)
8227 do_ME = TRUE;
8228 else
8229 out_str(T_CZR);
8230 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008231 if (screen_attr & HL_STRIKETHROUGH)
8232 {
8233 if (STRCMP(T_STE, T_ME) == 0)
8234 do_ME = TRUE;
8235 else
8236 out_str(T_STE);
8237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8239 out_str(T_ME);
8240
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008241#ifdef FEAT_TERMGUICOLORS
8242 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008244 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008245 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008246 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008247 term_bg_rgb_color(cterm_normal_bg_gui_color);
8248 }
8249 else
8250#endif
8251 {
8252 if (t_colors > 1)
8253 {
8254 /* set Normal cterm colors */
8255 if (cterm_normal_fg_color != 0)
8256 term_fg_color(cterm_normal_fg_color - 1);
8257 if (cterm_normal_bg_color != 0)
8258 term_bg_color(cterm_normal_bg_color - 1);
8259 if (cterm_normal_fg_bold)
8260 out_str(T_MD);
8261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 }
8263 }
8264 }
8265 screen_attr = 0;
8266}
8267
8268/*
8269 * Reset the colors for a cterm. Used when leaving Vim.
8270 * The machine specific code may override this again.
8271 */
8272 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008273reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008275 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {
8277 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008278#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008279 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8280 || cterm_normal_bg_gui_color != INVALCOLOR)
8281 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008282#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {
8286 out_str(T_OP);
8287 screen_attr = -1;
8288 }
8289 if (cterm_normal_fg_bold)
8290 {
8291 out_str(T_ME);
8292 screen_attr = -1;
8293 }
8294 }
8295}
8296
8297/*
8298 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8299 * using the attributes from ScreenAttrs["off"].
8300 */
8301 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008302screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
8304 int attr;
8305
8306 /* Check for illegal values, just in case (could happen just after
8307 * resizing). */
8308 if (row >= screen_Rows || col >= screen_Columns)
8309 return;
8310
Bram Moolenaar494838a2015-02-10 19:20:37 +01008311 /* Outputting a character in the last cell on the screen may scroll the
8312 * screen up. Only do it when the "xn" termcap property is set, otherwise
8313 * mark the character invalid (update it when scrolled up). */
8314 if (*T_XN == NUL
8315 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316#ifdef FEAT_RIGHTLEFT
8317 /* account for first command-line character in rightleft mode */
8318 && !cmdmsg_rl
8319#endif
8320 )
8321 {
8322 ScreenAttrs[off] = (sattr_T)-1;
8323 return;
8324 }
8325
8326 /*
8327 * Stop highlighting first, so it's easier to move the cursor.
8328 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008329#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 if (screen_char_attr != 0)
8331 attr = screen_char_attr;
8332 else
8333#endif
8334 attr = ScreenAttrs[off];
8335 if (screen_attr != attr)
8336 screen_stop_highlight();
8337
8338 windgoto(row, col);
8339
8340 if (screen_attr != attr)
8341 screen_start_highlight(attr);
8342
8343#ifdef FEAT_MBYTE
8344 if (enc_utf8 && ScreenLinesUC[off] != 0)
8345 {
8346 char_u buf[MB_MAXBYTES + 1];
8347
8348 /* Convert UTF-8 character to bytes and write it. */
8349
8350 buf[utfc_char2bytes(off, buf)] = NUL;
8351
8352 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008353 if (utf_ambiguous_width(ScreenLinesUC[off]))
8354 screen_cur_col = 9999;
8355 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 ++screen_cur_col;
8357 }
8358 else
8359#endif
8360 {
8361#ifdef FEAT_MBYTE
8362 out_flush_check();
8363#endif
8364 out_char(ScreenLines[off]);
8365#ifdef FEAT_MBYTE
8366 /* double-byte character in single-width cell */
8367 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8368 out_char(ScreenLines2[off]);
8369#endif
8370 }
8371
8372 screen_cur_col++;
8373}
8374
8375#ifdef FEAT_MBYTE
8376
8377/*
8378 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8379 * on the screen at position 'row' and 'col'.
8380 * The attributes of the first byte is used for all. This is required to
8381 * output the two bytes of a double-byte character with nothing in between.
8382 */
8383 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008384screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385{
8386 /* Check for illegal values (could be wrong when screen was resized). */
8387 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8388 return;
8389
8390 /* Outputting the last character on the screen may scrollup the screen.
8391 * Don't to it! Mark the character invalid (update it when scrolled up) */
8392 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8393 {
8394 ScreenAttrs[off] = (sattr_T)-1;
8395 return;
8396 }
8397
8398 /* Output the first byte normally (positions the cursor), then write the
8399 * second byte directly. */
8400 screen_char(off, row, col);
8401 out_char(ScreenLines[off + 1]);
8402 ++screen_cur_col;
8403}
8404#endif
8405
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008406#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407/*
8408 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8409 * This uses the contents of ScreenLines[] and doesn't change it.
8410 */
8411 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008412screen_draw_rectangle(
8413 int row,
8414 int col,
8415 int height,
8416 int width,
8417 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418{
8419 int r, c;
8420 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008421#ifdef FEAT_MBYTE
8422 int max_off;
8423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008425 /* Can't use ScreenLines unless initialized */
8426 if (ScreenLines == NULL)
8427 return;
8428
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 if (invert)
8430 screen_char_attr = HL_INVERSE;
8431 for (r = row; r < row + height; ++r)
8432 {
8433 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008434#ifdef FEAT_MBYTE
8435 max_off = off + screen_Columns;
8436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 for (c = col; c < col + width; ++c)
8438 {
8439#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008440 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 {
8442 screen_char_2(off + c, r, c);
8443 ++c;
8444 }
8445 else
8446#endif
8447 {
8448 screen_char(off + c, r, c);
8449#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008450 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 ++c;
8452#endif
8453 }
8454 }
8455 }
8456 screen_char_attr = 0;
8457}
8458#endif
8459
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008460#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461/*
8462 * Redraw the characters for a vertically split window.
8463 */
8464 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008465redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466{
8467 int col;
8468 int width;
8469
8470# ifdef FEAT_CLIPBOARD
8471 clip_may_clear_selection(row, end - 1);
8472# endif
8473
8474 if (wp == NULL)
8475 {
8476 col = 0;
8477 width = Columns;
8478 }
8479 else
8480 {
8481 col = wp->w_wincol;
8482 width = wp->w_width;
8483 }
8484 screen_draw_rectangle(row, col, end - row, width, FALSE);
8485}
8486#endif
8487
8488/*
8489 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8490 * with character 'c1' in first column followed by 'c2' in the other columns.
8491 * Use attributes 'attr'.
8492 */
8493 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008494screen_fill(
8495 int start_row,
8496 int end_row,
8497 int start_col,
8498 int end_col,
8499 int c1,
8500 int c2,
8501 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
8503 int row;
8504 int col;
8505 int off;
8506 int end_off;
8507 int did_delete;
8508 int c;
8509 int norm_term;
8510#if defined(FEAT_GUI) || defined(UNIX)
8511 int force_next = FALSE;
8512#endif
8513
8514 if (end_row > screen_Rows) /* safety check */
8515 end_row = screen_Rows;
8516 if (end_col > screen_Columns) /* safety check */
8517 end_col = screen_Columns;
8518 if (ScreenLines == NULL
8519 || start_row >= end_row
8520 || start_col >= end_col) /* nothing to do */
8521 return;
8522
8523 /* it's a "normal" terminal when not in a GUI or cterm */
8524 norm_term = (
8525#ifdef FEAT_GUI
8526 !gui.in_use &&
8527#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008528 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 for (row = start_row; row < end_row; ++row)
8530 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008531#ifdef FEAT_MBYTE
8532 if (has_mbyte
8533# ifdef FEAT_GUI
8534 && !gui.in_use
8535# endif
8536 )
8537 {
8538 /* When drawing over the right halve of a double-wide char clear
8539 * out the left halve. When drawing over the left halve of a
8540 * double wide-char clear out the right halve. Only needed in a
8541 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008542 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008543 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008544 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008545 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008546 }
8547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 /*
8549 * Try to use delete-line termcap code, when no attributes or in a
8550 * "normal" terminal, where a bold/italic space is just a
8551 * space.
8552 */
8553 did_delete = FALSE;
8554 if (c2 == ' '
8555 && end_col == Columns
8556 && can_clear(T_CE)
8557 && (attr == 0
8558 || (norm_term
8559 && attr <= HL_ALL
8560 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8561 {
8562 /*
8563 * check if we really need to clear something
8564 */
8565 col = start_col;
8566 if (c1 != ' ') /* don't clear first char */
8567 ++col;
8568
8569 off = LineOffset[row] + col;
8570 end_off = LineOffset[row] + end_col;
8571
8572 /* skip blanks (used often, keep it fast!) */
8573#ifdef FEAT_MBYTE
8574 if (enc_utf8)
8575 while (off < end_off && ScreenLines[off] == ' '
8576 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8577 ++off;
8578 else
8579#endif
8580 while (off < end_off && ScreenLines[off] == ' '
8581 && ScreenAttrs[off] == 0)
8582 ++off;
8583 if (off < end_off) /* something to be cleared */
8584 {
8585 col = off - LineOffset[row];
8586 screen_stop_highlight();
8587 term_windgoto(row, col);/* clear rest of this screen line */
8588 out_str(T_CE);
8589 screen_start(); /* don't know where cursor is now */
8590 col = end_col - col;
8591 while (col--) /* clear chars in ScreenLines */
8592 {
8593 ScreenLines[off] = ' ';
8594#ifdef FEAT_MBYTE
8595 if (enc_utf8)
8596 ScreenLinesUC[off] = 0;
8597#endif
8598 ScreenAttrs[off] = 0;
8599 ++off;
8600 }
8601 }
8602 did_delete = TRUE; /* the chars are cleared now */
8603 }
8604
8605 off = LineOffset[row] + start_col;
8606 c = c1;
8607 for (col = start_col; col < end_col; ++col)
8608 {
8609 if (ScreenLines[off] != c
8610#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008611 || (enc_utf8 && (int)ScreenLinesUC[off]
8612 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613#endif
8614 || ScreenAttrs[off] != attr
8615#if defined(FEAT_GUI) || defined(UNIX)
8616 || force_next
8617#endif
8618 )
8619 {
8620#if defined(FEAT_GUI) || defined(UNIX)
8621 /* The bold trick may make a single row of pixels appear in
8622 * the next character. When a bold character is removed, the
8623 * next character should be redrawn too. This happens for our
8624 * own GUI and for some xterms. */
8625 if (
8626# ifdef FEAT_GUI
8627 gui.in_use
8628# endif
8629# if defined(FEAT_GUI) && defined(UNIX)
8630 ||
8631# endif
8632# ifdef UNIX
8633 term_is_xterm
8634# endif
8635 )
8636 {
8637 if (ScreenLines[off] != ' '
8638 && (ScreenAttrs[off] > HL_ALL
8639 || ScreenAttrs[off] & HL_BOLD))
8640 force_next = TRUE;
8641 else
8642 force_next = FALSE;
8643 }
8644#endif
8645 ScreenLines[off] = c;
8646#ifdef FEAT_MBYTE
8647 if (enc_utf8)
8648 {
8649 if (c >= 0x80)
8650 {
8651 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008652 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 }
8654 else
8655 ScreenLinesUC[off] = 0;
8656 }
8657#endif
8658 ScreenAttrs[off] = attr;
8659 if (!did_delete || c != ' ')
8660 screen_char(off, row, col);
8661 }
8662 ++off;
8663 if (col == start_col)
8664 {
8665 if (did_delete)
8666 break;
8667 c = c2;
8668 }
8669 }
8670 if (end_col == Columns)
8671 LineWraps[row] = FALSE;
8672 if (row == Rows - 1) /* overwritten the command line */
8673 {
8674 redraw_cmdline = TRUE;
8675 if (c1 == ' ' && c2 == ' ')
8676 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008677 if (start_col == 0)
8678 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 }
8680 }
8681}
8682
8683/*
8684 * Check if there should be a delay. Used before clearing or redrawing the
8685 * screen or the command line.
8686 */
8687 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008688check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689{
8690 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8691 && !did_wait_return
8692 && emsg_silent == 0)
8693 {
8694 out_flush();
8695 ui_delay(1000L, TRUE);
8696 emsg_on_display = FALSE;
8697 if (check_msg_scroll)
8698 msg_scroll = FALSE;
8699 }
8700}
8701
8702/*
8703 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008704 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 * Returns TRUE if there is a valid screen to write to.
8706 * Returns FALSE when starting up and screen not initialized yet.
8707 */
8708 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008709screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008711 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 return (ScreenLines != NULL);
8713}
8714
8715/*
8716 * Resize the shell to Rows and Columns.
8717 * Allocate ScreenLines[] and associated items.
8718 *
8719 * There may be some time between setting Rows and Columns and (re)allocating
8720 * ScreenLines[]. This happens when starting up and when (manually) changing
8721 * the shell size. Always use screen_Rows and screen_Columns to access items
8722 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8723 * final size of the shell is needed.
8724 */
8725 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008726screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727{
8728 int new_row, old_row;
8729#ifdef FEAT_GUI
8730 int old_Rows;
8731#endif
8732 win_T *wp;
8733 int outofmem = FALSE;
8734 int len;
8735 schar_T *new_ScreenLines;
8736#ifdef FEAT_MBYTE
8737 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008738 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008740 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741#endif
8742 sattr_T *new_ScreenAttrs;
8743 unsigned *new_LineOffset;
8744 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008745#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008746 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008747 tabpage_T *tp;
8748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008750 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008751#ifdef FEAT_AUTOCMD
8752 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008754retry:
8755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 /*
8757 * Allocation of the screen buffers is done only when the size changes and
8758 * when Rows and Columns have been set and we have started doing full
8759 * screen stuff.
8760 */
8761 if ((ScreenLines != NULL
8762 && Rows == screen_Rows
8763 && Columns == screen_Columns
8764#ifdef FEAT_MBYTE
8765 && enc_utf8 == (ScreenLinesUC != NULL)
8766 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008767 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768#endif
8769 )
8770 || Rows == 0
8771 || Columns == 0
8772 || (!full_screen && ScreenLines == NULL))
8773 return;
8774
8775 /*
8776 * It's possible that we produce an out-of-memory message below, which
8777 * will cause this function to be called again. To break the loop, just
8778 * return here.
8779 */
8780 if (entered)
8781 return;
8782 entered = TRUE;
8783
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008784 /*
8785 * Note that the window sizes are updated before reallocating the arrays,
8786 * thus we must not redraw here!
8787 */
8788 ++RedrawingDisabled;
8789
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 win_new_shellsize(); /* fit the windows in the new sized shell */
8791
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 comp_col(); /* recompute columns for shown command and ruler */
8793
8794 /*
8795 * We're changing the size of the screen.
8796 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8797 * - Move lines from the old arrays into the new arrays, clear extra
8798 * lines (unless the screen is going to be cleared).
8799 * - Free the old arrays.
8800 *
8801 * If anything fails, make ScreenLines NULL, so we don't do anything!
8802 * Continuing with the old ScreenLines may result in a crash, because the
8803 * size is wrong.
8804 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008805 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008807#ifdef FEAT_AUTOCMD
8808 if (aucmd_win != NULL)
8809 win_free_lsize(aucmd_win);
8810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811
8812 new_ScreenLines = (schar_T *)lalloc((long_u)(
8813 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8814#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008815 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 if (enc_utf8)
8817 {
8818 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8819 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008820 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008821 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8823 }
8824 if (enc_dbcs == DBCS_JPNU)
8825 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8826 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8827#endif
8828 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8829 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8830 new_LineOffset = (unsigned *)lalloc((long_u)(
8831 Rows * sizeof(unsigned)), FALSE);
8832 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008833#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008834 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008837 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {
8839 if (win_alloc_lines(wp) == FAIL)
8840 {
8841 outofmem = TRUE;
8842#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008843 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844#endif
8845 }
8846 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008847#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008848 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8849 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008850 outofmem = TRUE;
8851#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008852#ifdef FEAT_WINDOWS
8853give_up:
8854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008856#ifdef FEAT_MBYTE
8857 for (i = 0; i < p_mco; ++i)
8858 if (new_ScreenLinesC[i] == NULL)
8859 break;
8860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (new_ScreenLines == NULL
8862#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008863 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8865#endif
8866 || new_ScreenAttrs == NULL
8867 || new_LineOffset == NULL
8868 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008869#ifdef FEAT_WINDOWS
8870 || new_TabPageIdxs == NULL
8871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 || outofmem)
8873 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008874 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008875 {
8876 /* guess the size */
8877 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8878
8879 /* Remember we did this to avoid getting outofmem messages over
8880 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008881 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 vim_free(new_ScreenLines);
8884 new_ScreenLines = NULL;
8885#ifdef FEAT_MBYTE
8886 vim_free(new_ScreenLinesUC);
8887 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008888 for (i = 0; i < p_mco; ++i)
8889 {
8890 vim_free(new_ScreenLinesC[i]);
8891 new_ScreenLinesC[i] = NULL;
8892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 vim_free(new_ScreenLines2);
8894 new_ScreenLines2 = NULL;
8895#endif
8896 vim_free(new_ScreenAttrs);
8897 new_ScreenAttrs = NULL;
8898 vim_free(new_LineOffset);
8899 new_LineOffset = NULL;
8900 vim_free(new_LineWraps);
8901 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008902#ifdef FEAT_WINDOWS
8903 vim_free(new_TabPageIdxs);
8904 new_TabPageIdxs = NULL;
8905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 }
8907 else
8908 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008909 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008910
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 for (new_row = 0; new_row < Rows; ++new_row)
8912 {
8913 new_LineOffset[new_row] = new_row * Columns;
8914 new_LineWraps[new_row] = FALSE;
8915
8916 /*
8917 * If the screen is not going to be cleared, copy as much as
8918 * possible from the old screen to the new one and clear the rest
8919 * (used when resizing the window at the "--more--" prompt or when
8920 * executing an external command, for the GUI).
8921 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008922 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 {
8924 (void)vim_memset(new_ScreenLines + new_row * Columns,
8925 ' ', (size_t)Columns * sizeof(schar_T));
8926#ifdef FEAT_MBYTE
8927 if (enc_utf8)
8928 {
8929 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8930 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008931 for (i = 0; i < p_mco; ++i)
8932 (void)vim_memset(new_ScreenLinesC[i]
8933 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 0, (size_t)Columns * sizeof(u8char_T));
8935 }
8936 if (enc_dbcs == DBCS_JPNU)
8937 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8938 0, (size_t)Columns * sizeof(schar_T));
8939#endif
8940 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8941 0, (size_t)Columns * sizeof(sattr_T));
8942 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008943 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 {
8945 if (screen_Columns < Columns)
8946 len = screen_Columns;
8947 else
8948 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008949#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008950 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008951 * may be invalid now. Also when p_mco changes. */
8952 if (!(enc_utf8 && ScreenLinesUC == NULL)
8953 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008954#endif
8955 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8956 ScreenLines + LineOffset[old_row],
8957 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008959 if (enc_utf8 && ScreenLinesUC != NULL
8960 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 {
8962 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8963 ScreenLinesUC + LineOffset[old_row],
8964 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008965 for (i = 0; i < p_mco; ++i)
8966 mch_memmove(new_ScreenLinesC[i]
8967 + new_LineOffset[new_row],
8968 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 (size_t)len * sizeof(u8char_T));
8970 }
8971 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8972 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8973 ScreenLines2 + LineOffset[old_row],
8974 (size_t)len * sizeof(schar_T));
8975#endif
8976 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8977 ScreenAttrs + LineOffset[old_row],
8978 (size_t)len * sizeof(sattr_T));
8979 }
8980 }
8981 }
8982 /* Use the last line of the screen for the current line. */
8983 current_ScreenLine = new_ScreenLines + Rows * Columns;
8984 }
8985
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008986 free_screenlines();
8987
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 ScreenLines = new_ScreenLines;
8989#ifdef FEAT_MBYTE
8990 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008991 for (i = 0; i < p_mco; ++i)
8992 ScreenLinesC[i] = new_ScreenLinesC[i];
8993 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 ScreenLines2 = new_ScreenLines2;
8995#endif
8996 ScreenAttrs = new_ScreenAttrs;
8997 LineOffset = new_LineOffset;
8998 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008999#ifdef FEAT_WINDOWS
9000 TabPageIdxs = new_TabPageIdxs;
9001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002
9003 /* It's important that screen_Rows and screen_Columns reflect the actual
9004 * size of ScreenLines[]. Set them before calling anything. */
9005#ifdef FEAT_GUI
9006 old_Rows = screen_Rows;
9007#endif
9008 screen_Rows = Rows;
9009 screen_Columns = Columns;
9010
9011 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009012 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 screenclear2();
9014
9015#ifdef FEAT_GUI
9016 else if (gui.in_use
9017 && !gui.starting
9018 && ScreenLines != NULL
9019 && old_Rows != Rows)
9020 {
9021 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9022 /*
9023 * Adjust the position of the cursor, for when executing an external
9024 * command.
9025 */
9026 if (msg_row >= Rows) /* Rows got smaller */
9027 msg_row = Rows - 1; /* put cursor at last row */
9028 else if (Rows > old_Rows) /* Rows got bigger */
9029 msg_row += Rows - old_Rows; /* put cursor in same place */
9030 if (msg_col >= Columns) /* Columns got smaller */
9031 msg_col = Columns - 1; /* put cursor at last column */
9032 }
9033#endif
9034
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009036 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009037
9038#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009039 /*
9040 * Do not apply autocommands more than 3 times to avoid an endless loop
9041 * in case applying autocommands always changes Rows or Columns.
9042 */
9043 if (starting == 0 && ++retry_count <= 3)
9044 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009045 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009046 /* In rare cases, autocommands may have altered Rows or Columns,
9047 * jump back to check if we need to allocate the screen again. */
9048 goto retry;
9049 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051}
9052
9053 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009054free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009055{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009056#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009057 int i;
9058
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009059 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009060 for (i = 0; i < Screen_mco; ++i)
9061 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009062 vim_free(ScreenLines2);
9063#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009064 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009065 vim_free(ScreenAttrs);
9066 vim_free(LineOffset);
9067 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009068#ifdef FEAT_WINDOWS
9069 vim_free(TabPageIdxs);
9070#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009071}
9072
9073 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009074screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075{
9076 check_for_delay(FALSE);
9077 screenalloc(FALSE); /* allocate screen buffers if size changed */
9078 screenclear2(); /* clear the screen */
9079}
9080
9081 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009082screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
9084 int i;
9085
9086 if (starting == NO_SCREEN || ScreenLines == NULL
9087#ifdef FEAT_GUI
9088 || (gui.in_use && gui.starting)
9089#endif
9090 )
9091 return;
9092
9093#ifdef FEAT_GUI
9094 if (!gui.in_use)
9095#endif
9096 screen_attr = -1; /* force setting the Normal colors */
9097 screen_stop_highlight(); /* don't want highlighting here */
9098
9099#ifdef FEAT_CLIPBOARD
9100 /* disable selection without redrawing it */
9101 clip_scroll_selection(9999);
9102#endif
9103
9104 /* blank out ScreenLines */
9105 for (i = 0; i < Rows; ++i)
9106 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009107 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 LineWraps[i] = FALSE;
9109 }
9110
9111 if (can_clear(T_CL))
9112 {
9113 out_str(T_CL); /* clear the display */
9114 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009115 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 }
9117 else
9118 {
9119 /* can't clear the screen, mark all chars with invalid attributes */
9120 for (i = 0; i < Rows; ++i)
9121 lineinvalid(LineOffset[i], (int)Columns);
9122 clear_cmdline = TRUE;
9123 }
9124
9125 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9126
9127 win_rest_invalid(firstwin);
9128 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009129#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009130 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 if (must_redraw == CLEAR) /* no need to clear again */
9133 must_redraw = NOT_VALID;
9134 compute_cmdrow();
9135 msg_row = cmdline_row; /* put cursor on last line for messages */
9136 msg_col = 0;
9137 screen_start(); /* don't know where cursor is now */
9138 msg_scrolled = 0; /* can't scroll back */
9139 msg_didany = FALSE;
9140 msg_didout = FALSE;
9141}
9142
9143/*
9144 * Clear one line in ScreenLines.
9145 */
9146 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009147lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148{
9149 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9150#ifdef FEAT_MBYTE
9151 if (enc_utf8)
9152 (void)vim_memset(ScreenLinesUC + off, 0,
9153 (size_t)width * sizeof(u8char_T));
9154#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009155 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156}
9157
9158/*
9159 * Mark one line in ScreenLines invalid by setting the attributes to an
9160 * invalid value.
9161 */
9162 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009163lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
9165 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9166}
9167
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009168#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169/*
9170 * Copy part of a Screenline for vertically split window "wp".
9171 */
9172 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009173linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174{
9175 unsigned off_to = LineOffset[to] + wp->w_wincol;
9176 unsigned off_from = LineOffset[from] + wp->w_wincol;
9177
9178 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9179 wp->w_width * sizeof(schar_T));
9180# ifdef FEAT_MBYTE
9181 if (enc_utf8)
9182 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009183 int i;
9184
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9186 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009187 for (i = 0; i < p_mco; ++i)
9188 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9189 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 }
9191 if (enc_dbcs == DBCS_JPNU)
9192 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9193 wp->w_width * sizeof(schar_T));
9194# endif
9195 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9196 wp->w_width * sizeof(sattr_T));
9197}
9198#endif
9199
9200/*
9201 * Return TRUE if clearing with term string "p" would work.
9202 * It can't work when the string is empty or it won't set the right background.
9203 */
9204 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009205can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206{
9207 return (*p != NUL && (t_colors <= 1
9208#ifdef FEAT_GUI
9209 || gui.in_use
9210#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009211#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009212 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009213 || (!p_tgc && cterm_normal_bg_color == 0)
9214#else
9215 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009216#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009217 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218}
9219
9220/*
9221 * Reset cursor position. Use whenever cursor was moved because of outputting
9222 * something directly to the screen (shell commands) or a terminal control
9223 * code.
9224 */
9225 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009226screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227{
9228 screen_cur_row = screen_cur_col = 9999;
9229}
9230
9231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 * Move the cursor to position "row","col" in the screen.
9233 * This tries to find the most efficient way to move, minimizing the number of
9234 * characters sent to the terminal.
9235 */
9236 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009237windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009239 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 int i;
9241 int plan;
9242 int cost;
9243 int wouldbe_col;
9244 int noinvcurs;
9245 char_u *bs;
9246 int goto_cost;
9247 int attr;
9248
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009249#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9251
9252#define PLAN_LE 1
9253#define PLAN_CR 2
9254#define PLAN_NL 3
9255#define PLAN_WRITE 4
9256 /* Can't use ScreenLines unless initialized */
9257 if (ScreenLines == NULL)
9258 return;
9259
9260 if (col != screen_cur_col || row != screen_cur_row)
9261 {
9262 /* Check for valid position. */
9263 if (row < 0) /* window without text lines? */
9264 row = 0;
9265 if (row >= screen_Rows)
9266 row = screen_Rows - 1;
9267 if (col >= screen_Columns)
9268 col = screen_Columns - 1;
9269
9270 /* check if no cursor movement is allowed in highlight mode */
9271 if (screen_attr && *T_MS == NUL)
9272 noinvcurs = HIGHL_COST;
9273 else
9274 noinvcurs = 0;
9275 goto_cost = GOTO_COST + noinvcurs;
9276
9277 /*
9278 * Plan how to do the positioning:
9279 * 1. Use CR to move it to column 0, same row.
9280 * 2. Use T_LE to move it a few columns to the left.
9281 * 3. Use NL to move a few lines down, column 0.
9282 * 4. Move a few columns to the right with T_ND or by writing chars.
9283 *
9284 * Don't do this if the cursor went beyond the last column, the cursor
9285 * position is unknown then (some terminals wrap, some don't )
9286 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009287 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 * characters to move the cursor to the right.
9289 */
9290 if (row >= screen_cur_row && screen_cur_col < Columns)
9291 {
9292 /*
9293 * If the cursor is in the same row, bigger col, we can use CR
9294 * or T_LE.
9295 */
9296 bs = NULL; /* init for GCC */
9297 attr = screen_attr;
9298 if (row == screen_cur_row && col < screen_cur_col)
9299 {
9300 /* "le" is preferred over "bc", because "bc" is obsolete */
9301 if (*T_LE)
9302 bs = T_LE; /* "cursor left" */
9303 else
9304 bs = T_BC; /* "backspace character (old) */
9305 if (*bs)
9306 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9307 else
9308 cost = 999;
9309 if (col + 1 < cost) /* using CR is less characters */
9310 {
9311 plan = PLAN_CR;
9312 wouldbe_col = 0;
9313 cost = 1; /* CR is just one character */
9314 }
9315 else
9316 {
9317 plan = PLAN_LE;
9318 wouldbe_col = col;
9319 }
9320 if (noinvcurs) /* will stop highlighting */
9321 {
9322 cost += noinvcurs;
9323 attr = 0;
9324 }
9325 }
9326
9327 /*
9328 * If the cursor is above where we want to be, we can use CR LF.
9329 */
9330 else if (row > screen_cur_row)
9331 {
9332 plan = PLAN_NL;
9333 wouldbe_col = 0;
9334 cost = (row - screen_cur_row) * 2; /* CR LF */
9335 if (noinvcurs) /* will stop highlighting */
9336 {
9337 cost += noinvcurs;
9338 attr = 0;
9339 }
9340 }
9341
9342 /*
9343 * If the cursor is in the same row, smaller col, just use write.
9344 */
9345 else
9346 {
9347 plan = PLAN_WRITE;
9348 wouldbe_col = screen_cur_col;
9349 cost = 0;
9350 }
9351
9352 /*
9353 * Check if any characters that need to be written have the
9354 * correct attributes. Also avoid UTF-8 characters.
9355 */
9356 i = col - wouldbe_col;
9357 if (i > 0)
9358 cost += i;
9359 if (cost < goto_cost && i > 0)
9360 {
9361 /*
9362 * Check if the attributes are correct without additionally
9363 * stopping highlighting.
9364 */
9365 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9366 while (i && *p++ == attr)
9367 --i;
9368 if (i != 0)
9369 {
9370 /*
9371 * Try if it works when highlighting is stopped here.
9372 */
9373 if (*--p == 0)
9374 {
9375 cost += noinvcurs;
9376 while (i && *p++ == 0)
9377 --i;
9378 }
9379 if (i != 0)
9380 cost = 999; /* different attributes, don't do it */
9381 }
9382#ifdef FEAT_MBYTE
9383 if (enc_utf8)
9384 {
9385 /* Don't use an UTF-8 char for positioning, it's slow. */
9386 for (i = wouldbe_col; i < col; ++i)
9387 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9388 {
9389 cost = 999;
9390 break;
9391 }
9392 }
9393#endif
9394 }
9395
9396 /*
9397 * We can do it without term_windgoto()!
9398 */
9399 if (cost < goto_cost)
9400 {
9401 if (plan == PLAN_LE)
9402 {
9403 if (noinvcurs)
9404 screen_stop_highlight();
9405 while (screen_cur_col > col)
9406 {
9407 out_str(bs);
9408 --screen_cur_col;
9409 }
9410 }
9411 else if (plan == PLAN_CR)
9412 {
9413 if (noinvcurs)
9414 screen_stop_highlight();
9415 out_char('\r');
9416 screen_cur_col = 0;
9417 }
9418 else if (plan == PLAN_NL)
9419 {
9420 if (noinvcurs)
9421 screen_stop_highlight();
9422 while (screen_cur_row < row)
9423 {
9424 out_char('\n');
9425 ++screen_cur_row;
9426 }
9427 screen_cur_col = 0;
9428 }
9429
9430 i = col - screen_cur_col;
9431 if (i > 0)
9432 {
9433 /*
9434 * Use cursor-right if it's one character only. Avoids
9435 * removing a line of pixels from the last bold char, when
9436 * using the bold trick in the GUI.
9437 */
9438 if (T_ND[0] != NUL && T_ND[1] == NUL)
9439 {
9440 while (i-- > 0)
9441 out_char(*T_ND);
9442 }
9443 else
9444 {
9445 int off;
9446
9447 off = LineOffset[row] + screen_cur_col;
9448 while (i-- > 0)
9449 {
9450 if (ScreenAttrs[off] != screen_attr)
9451 screen_stop_highlight();
9452#ifdef FEAT_MBYTE
9453 out_flush_check();
9454#endif
9455 out_char(ScreenLines[off]);
9456#ifdef FEAT_MBYTE
9457 if (enc_dbcs == DBCS_JPNU
9458 && ScreenLines[off] == 0x8e)
9459 out_char(ScreenLines2[off]);
9460#endif
9461 ++off;
9462 }
9463 }
9464 }
9465 }
9466 }
9467 else
9468 cost = 999;
9469
9470 if (cost >= goto_cost)
9471 {
9472 if (noinvcurs)
9473 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009474 if (row == screen_cur_row && (col > screen_cur_col)
9475 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 term_cursor_right(col - screen_cur_col);
9477 else
9478 term_windgoto(row, col);
9479 }
9480 screen_cur_row = row;
9481 screen_cur_col = col;
9482 }
9483}
9484
9485/*
9486 * Set cursor to its position in the current window.
9487 */
9488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009489setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490{
9491 if (redrawing())
9492 {
9493 validate_cursor();
9494 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9495 W_WINCOL(curwin) + (
9496#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009497 /* With 'rightleft' set and the cursor on a double-wide
9498 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9500# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009501 (has_mbyte
9502 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9503 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504# endif
9505 1)) :
9506#endif
9507 curwin->w_wcol));
9508 }
9509}
9510
9511
9512/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009513 * Insert 'line_count' lines at 'row' in window 'wp'.
9514 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9515 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 * scrolling.
9517 * Returns FAIL if the lines are not inserted, OK for success.
9518 */
9519 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009520win_ins_lines(
9521 win_T *wp,
9522 int row,
9523 int line_count,
9524 int invalid,
9525 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526{
9527 int did_delete;
9528 int nextrow;
9529 int lastrow;
9530 int retval;
9531
9532 if (invalid)
9533 wp->w_lines_valid = 0;
9534
9535 if (wp->w_height < 5)
9536 return FAIL;
9537
9538 if (line_count > wp->w_height - row)
9539 line_count = wp->w_height - row;
9540
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009541 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 if (retval != MAYBE)
9543 return retval;
9544
9545 /*
9546 * If there is a next window or a status line, we first try to delete the
9547 * lines at the bottom to avoid messing what is after the window.
9548 * If this fails and there are following windows, don't do anything to avoid
9549 * messing up those windows, better just redraw.
9550 */
9551 did_delete = FALSE;
9552#ifdef FEAT_WINDOWS
9553 if (wp->w_next != NULL || wp->w_status_height)
9554 {
9555 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009556 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 did_delete = TRUE;
9558 else if (wp->w_next)
9559 return FAIL;
9560 }
9561#endif
9562 /*
9563 * if no lines deleted, blank the lines that will end up below the window
9564 */
9565 if (!did_delete)
9566 {
9567#ifdef FEAT_WINDOWS
9568 wp->w_redr_status = TRUE;
9569#endif
9570 redraw_cmdline = TRUE;
9571 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9572 lastrow = nextrow + line_count;
9573 if (lastrow > Rows)
9574 lastrow = Rows;
9575 screen_fill(nextrow - line_count, lastrow - line_count,
9576 W_WINCOL(wp), (int)W_ENDCOL(wp),
9577 ' ', ' ', 0);
9578 }
9579
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009580 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 == FAIL)
9582 {
9583 /* deletion will have messed up other windows */
9584 if (did_delete)
9585 {
9586#ifdef FEAT_WINDOWS
9587 wp->w_redr_status = TRUE;
9588#endif
9589 win_rest_invalid(W_NEXT(wp));
9590 }
9591 return FAIL;
9592 }
9593
9594 return OK;
9595}
9596
9597/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009598 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9600 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9601 * scrolling
9602 * Return OK for success, FAIL if the lines are not deleted.
9603 */
9604 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009605win_del_lines(
9606 win_T *wp,
9607 int row,
9608 int line_count,
9609 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009610 int mayclear,
9611 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612{
9613 int retval;
9614
9615 if (invalid)
9616 wp->w_lines_valid = 0;
9617
9618 if (line_count > wp->w_height - row)
9619 line_count = wp->w_height - row;
9620
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009621 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 if (retval != MAYBE)
9623 return retval;
9624
9625 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009626 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 return FAIL;
9628
9629#ifdef FEAT_WINDOWS
9630 /*
9631 * If there are windows or status lines below, try to put them at the
9632 * correct place. If we can't do that, they have to be redrawn.
9633 */
9634 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9635 {
9636 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009637 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 {
9639 wp->w_redr_status = TRUE;
9640 win_rest_invalid(wp->w_next);
9641 }
9642 }
9643 /*
9644 * If this is the last window and there is no status line, redraw the
9645 * command line later.
9646 */
9647 else
9648#endif
9649 redraw_cmdline = TRUE;
9650 return OK;
9651}
9652
9653/*
9654 * Common code for win_ins_lines() and win_del_lines().
9655 * Returns OK or FAIL when the work has been done.
9656 * Returns MAYBE when not finished yet.
9657 */
9658 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009659win_do_lines(
9660 win_T *wp,
9661 int row,
9662 int line_count,
9663 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009664 int del,
9665 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666{
9667 int retval;
9668
9669 if (!redrawing() || line_count <= 0)
9670 return FAIL;
9671
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009672 /* When inserting lines would result in loss of command output, just redraw
9673 * the lines. */
9674 if (no_win_do_lines_ins && !del)
9675 return FAIL;
9676
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 /* only a few lines left: redraw is faster */
9678 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009679#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 && wp->w_width == Columns
9681#endif
9682 )
9683 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009684 if (!no_win_do_lines_ins)
9685 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 return FAIL;
9687 }
9688
9689 /*
9690 * Delete all remaining lines
9691 */
9692 if (row + line_count >= wp->w_height)
9693 {
9694 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9695 W_WINCOL(wp), (int)W_ENDCOL(wp),
9696 ' ', ' ', 0);
9697 return OK;
9698 }
9699
9700 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009701 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009703 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009705 if (!no_win_do_lines_ins)
9706 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707
9708 /*
9709 * If the terminal can set a scroll region, use that.
9710 * Always do this in a vertically split window. This will redraw from
9711 * ScreenLines[] when t_CV isn't defined. That's faster than using
9712 * win_line().
9713 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009714 * a character in the lower right corner of the scroll region may cause a
9715 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 */
9717 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009718#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 || W_WIDTH(wp) != Columns
9720#endif
9721 )
9722 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009723#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9725#endif
9726 scroll_region_set(wp, row);
9727 if (del)
9728 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009729 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 else
9731 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009732 wp->w_height - row, clear_attr, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009733#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9735#endif
9736 scroll_region_reset();
9737 return retval;
9738 }
9739
9740#ifdef FEAT_WINDOWS
9741 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9742 return FAIL;
9743#endif
9744
9745 return MAYBE;
9746}
9747
9748/*
9749 * window 'wp' and everything after it is messed up, mark it for redraw
9750 */
9751 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009752win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753{
9754#ifdef FEAT_WINDOWS
9755 while (wp != NULL)
9756#else
9757 if (wp != NULL)
9758#endif
9759 {
9760 redraw_win_later(wp, NOT_VALID);
9761#ifdef FEAT_WINDOWS
9762 wp->w_redr_status = TRUE;
9763 wp = wp->w_next;
9764#endif
9765 }
9766 redraw_cmdline = TRUE;
9767}
9768
9769/*
9770 * The rest of the routines in this file perform screen manipulations. The
9771 * given operation is performed physically on the screen. The corresponding
9772 * change is also made to the internal screen image. In this way, the editor
9773 * anticipates the effect of editing changes on the appearance of the screen.
9774 * That way, when we call screenupdate a complete redraw isn't usually
9775 * necessary. Another advantage is that we can keep adding code to anticipate
9776 * screen changes, and in the meantime, everything still works.
9777 */
9778
9779/*
9780 * types for inserting or deleting lines
9781 */
9782#define USE_T_CAL 1
9783#define USE_T_CDL 2
9784#define USE_T_AL 3
9785#define USE_T_CE 4
9786#define USE_T_DL 5
9787#define USE_T_SR 6
9788#define USE_NL 7
9789#define USE_T_CD 8
9790#define USE_REDRAW 9
9791
9792/*
9793 * insert lines on the screen and update ScreenLines[]
9794 * 'end' is the line after the scrolled part. Normally it is Rows.
9795 * When scrolling region used 'off' is the offset from the top for the region.
9796 * 'row' and 'end' are relative to the start of the region.
9797 *
9798 * return FAIL for failure, OK for success.
9799 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009800 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009801screen_ins_lines(
9802 int off,
9803 int row,
9804 int line_count,
9805 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009806 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009807 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808{
9809 int i;
9810 int j;
9811 unsigned temp;
9812 int cursor_row;
9813 int type;
9814 int result_empty;
9815 int can_ce = can_clear(T_CE);
9816
9817 /*
9818 * FAIL if
9819 * - there is no valid screen
9820 * - the screen has to be redrawn completely
9821 * - the line count is less than one
9822 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009823 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009825 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9826#ifdef FEAT_CLIPBOARD
9827 || (clip_star.state != SELECT_CLEARED
9828 && redrawing_for_callback > 0)
9829#endif
9830 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 return FAIL;
9832
9833 /*
9834 * There are seven ways to insert lines:
9835 * 0. When in a vertically split window and t_CV isn't set, redraw the
9836 * characters from ScreenLines[].
9837 * 1. Use T_CD (clear to end of display) if it exists and the result of
9838 * the insert is just empty lines
9839 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9840 * present or line_count > 1. It looks better if we do all the inserts
9841 * at once.
9842 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9843 * insert is just empty lines and T_CE is not present or line_count >
9844 * 1.
9845 * 4. Use T_AL (insert line) if it exists.
9846 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9847 * just empty lines.
9848 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9849 * just empty lines.
9850 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9851 * the 'da' flag is not set or we have clear line capability.
9852 * 8. redraw the characters from ScreenLines[].
9853 *
9854 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9855 * the scrollbar for the window. It does have insert line, use that if it
9856 * exists.
9857 */
9858 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009859#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9861 type = USE_REDRAW;
9862 else
9863#endif
9864 if (can_clear(T_CD) && result_empty)
9865 type = USE_T_CD;
9866 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9867 type = USE_T_CAL;
9868 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9869 type = USE_T_CDL;
9870 else if (*T_AL != NUL)
9871 type = USE_T_AL;
9872 else if (can_ce && result_empty)
9873 type = USE_T_CE;
9874 else if (*T_DL != NUL && result_empty)
9875 type = USE_T_DL;
9876 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9877 type = USE_T_SR;
9878 else
9879 return FAIL;
9880
9881 /*
9882 * For clearing the lines screen_del_lines() is used. This will also take
9883 * care of t_db if necessary.
9884 */
9885 if (type == USE_T_CD || type == USE_T_CDL ||
9886 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009887 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888
9889 /*
9890 * If text is retained below the screen, first clear or delete as many
9891 * lines at the bottom of the window as are about to be inserted so that
9892 * the deleted lines won't later surface during a screen_del_lines.
9893 */
9894 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009895 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896
9897#ifdef FEAT_CLIPBOARD
9898 /* Remove a modeless selection when inserting lines halfway the screen
9899 * or not the full width of the screen. */
9900 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009901# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 || (wp != NULL && wp->w_width != Columns)
9903# endif
9904 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009905 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 else
9907 clip_scroll_selection(-line_count);
9908#endif
9909
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910#ifdef FEAT_GUI
9911 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9912 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009913 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914#endif
9915
9916 if (*T_CCS != NUL) /* cursor relative to region */
9917 cursor_row = row;
9918 else
9919 cursor_row = row + off;
9920
9921 /*
9922 * Shift LineOffset[] line_count down to reflect the inserted lines.
9923 * Clear the inserted lines in ScreenLines[].
9924 */
9925 row += off;
9926 end += off;
9927 for (i = 0; i < line_count; ++i)
9928 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009929#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 if (wp != NULL && wp->w_width != Columns)
9931 {
9932 /* need to copy part of a line */
9933 j = end - 1 - i;
9934 while ((j -= line_count) >= row)
9935 linecopy(j + line_count, j, wp);
9936 j += line_count;
9937 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009938 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9939 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 else
9941 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9942 LineWraps[j] = FALSE;
9943 }
9944 else
9945#endif
9946 {
9947 j = end - 1 - i;
9948 temp = LineOffset[j];
9949 while ((j -= line_count) >= row)
9950 {
9951 LineOffset[j + line_count] = LineOffset[j];
9952 LineWraps[j + line_count] = LineWraps[j];
9953 }
9954 LineOffset[j + line_count] = temp;
9955 LineWraps[j + line_count] = FALSE;
9956 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009957 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 else
9959 lineinvalid(temp, (int)Columns);
9960 }
9961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962
9963 screen_stop_highlight();
9964 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009965 if (clear_attr != 0)
9966 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009968#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 /* redraw the characters */
9970 if (type == USE_REDRAW)
9971 redraw_block(row, end, wp);
9972 else
9973#endif
9974 if (type == USE_T_CAL)
9975 {
9976 term_append_lines(line_count);
9977 screen_start(); /* don't know where cursor is now */
9978 }
9979 else
9980 {
9981 for (i = 0; i < line_count; i++)
9982 {
9983 if (type == USE_T_AL)
9984 {
9985 if (i && cursor_row != 0)
9986 windgoto(cursor_row, 0);
9987 out_str(T_AL);
9988 }
9989 else /* type == USE_T_SR */
9990 out_str(T_SR);
9991 screen_start(); /* don't know where cursor is now */
9992 }
9993 }
9994
9995 /*
9996 * With scroll-reverse and 'da' flag set we need to clear the lines that
9997 * have been scrolled down into the region.
9998 */
9999 if (type == USE_T_SR && *T_DA)
10000 {
10001 for (i = 0; i < line_count; ++i)
10002 {
10003 windgoto(off + i, 0);
10004 out_str(T_CE);
10005 screen_start(); /* don't know where cursor is now */
10006 }
10007 }
10008
10009#ifdef FEAT_GUI
10010 gui_can_update_cursor();
10011 if (gui.in_use)
10012 out_flush(); /* always flush after a scroll */
10013#endif
10014 return OK;
10015}
10016
10017/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010018 * Delete lines on the screen and update ScreenLines[].
10019 * "end" is the line after the scrolled part. Normally it is Rows.
10020 * When scrolling region used "off" is the offset from the top for the region.
10021 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 *
10023 * Return OK for success, FAIL if the lines are not deleted.
10024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010026screen_del_lines(
10027 int off,
10028 int row,
10029 int line_count,
10030 int end,
10031 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010032 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010033 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034{
10035 int j;
10036 int i;
10037 unsigned temp;
10038 int cursor_row;
10039 int cursor_end;
10040 int result_empty; /* result is empty until end of region */
10041 int can_delete; /* deleting line codes can be used */
10042 int type;
10043
10044 /*
10045 * FAIL if
10046 * - there is no valid screen
10047 * - the screen has to be redrawn completely
10048 * - the line count is less than one
10049 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010050 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010052 if (!screen_valid(TRUE) || line_count <= 0
10053 || (!force && line_count > p_ttyscroll)
10054#ifdef FEAT_CLIPBOARD
10055 || (clip_star.state != SELECT_CLEARED
10056 && redrawing_for_callback > 0)
10057#endif
10058 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 return FAIL;
10060
10061 /*
10062 * Check if the rest of the current region will become empty.
10063 */
10064 result_empty = row + line_count >= end;
10065
10066 /*
10067 * We can delete lines only when 'db' flag not set or when 'ce' option
10068 * available.
10069 */
10070 can_delete = (*T_DB == NUL || can_clear(T_CE));
10071
10072 /*
10073 * There are six ways to delete lines:
10074 * 0. When in a vertically split window and t_CV isn't set, redraw the
10075 * characters from ScreenLines[].
10076 * 1. Use T_CD if it exists and the result is empty.
10077 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10078 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10079 * none of the other ways work.
10080 * 4. Use T_CE (erase line) if the result is empty.
10081 * 5. Use T_DL (delete line) if it exists.
10082 * 6. redraw the characters from ScreenLines[].
10083 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010084#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10086 type = USE_REDRAW;
10087 else
10088#endif
10089 if (can_clear(T_CD) && result_empty)
10090 type = USE_T_CD;
10091#if defined(__BEOS__) && defined(BEOS_DR8)
10092 /*
10093 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10094 * its internal termcap... this works okay for tests which test *T_DB !=
10095 * NUL. It has the disadvantage that the user cannot use any :set t_*
10096 * command to get T_DB (back) to empty_option, only :set term=... will do
10097 * the trick...
10098 * Anyway, this hack will hopefully go away with the next OS release.
10099 * (Olaf Seibert)
10100 */
10101 else if (row == 0 && T_DB == empty_option
10102 && (line_count == 1 || *T_CDL == NUL))
10103#else
10104 else if (row == 0 && (
10105#ifndef AMIGA
10106 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10107 * up, so use delete-line command */
10108 line_count == 1 ||
10109#endif
10110 *T_CDL == NUL))
10111#endif
10112 type = USE_NL;
10113 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10114 type = USE_T_CDL;
10115 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010116#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 && (wp == NULL || wp->w_width == Columns)
10118#endif
10119 )
10120 type = USE_T_CE;
10121 else if (*T_DL != NUL && can_delete)
10122 type = USE_T_DL;
10123 else if (*T_CDL != NUL && can_delete)
10124 type = USE_T_CDL;
10125 else
10126 return FAIL;
10127
10128#ifdef FEAT_CLIPBOARD
10129 /* Remove a modeless selection when deleting lines halfway the screen or
10130 * not the full width of the screen. */
10131 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010132# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 || (wp != NULL && wp->w_width != Columns)
10134# endif
10135 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010136 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 else
10138 clip_scroll_selection(line_count);
10139#endif
10140
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141#ifdef FEAT_GUI
10142 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10143 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010144 gui_dont_update_cursor(gui.cursor_row >= row + off
10145 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146#endif
10147
10148 if (*T_CCS != NUL) /* cursor relative to region */
10149 {
10150 cursor_row = row;
10151 cursor_end = end;
10152 }
10153 else
10154 {
10155 cursor_row = row + off;
10156 cursor_end = end + off;
10157 }
10158
10159 /*
10160 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10161 * Clear the inserted lines in ScreenLines[].
10162 */
10163 row += off;
10164 end += off;
10165 for (i = 0; i < line_count; ++i)
10166 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010167#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 if (wp != NULL && wp->w_width != Columns)
10169 {
10170 /* need to copy part of a line */
10171 j = row + i;
10172 while ((j += line_count) <= end - 1)
10173 linecopy(j - line_count, j, wp);
10174 j -= line_count;
10175 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010176 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10177 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 else
10179 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10180 LineWraps[j] = FALSE;
10181 }
10182 else
10183#endif
10184 {
10185 /* whole width, moving the line pointers is faster */
10186 j = row + i;
10187 temp = LineOffset[j];
10188 while ((j += line_count) <= end - 1)
10189 {
10190 LineOffset[j - line_count] = LineOffset[j];
10191 LineWraps[j - line_count] = LineWraps[j];
10192 }
10193 LineOffset[j - line_count] = temp;
10194 LineWraps[j - line_count] = FALSE;
10195 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010196 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 else
10198 lineinvalid(temp, (int)Columns);
10199 }
10200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010202 if (screen_attr != clear_attr)
10203 screen_stop_highlight();
10204 if (clear_attr != 0)
10205 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010207#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 /* redraw the characters */
10209 if (type == USE_REDRAW)
10210 redraw_block(row, end, wp);
10211 else
10212#endif
10213 if (type == USE_T_CD) /* delete the lines */
10214 {
10215 windgoto(cursor_row, 0);
10216 out_str(T_CD);
10217 screen_start(); /* don't know where cursor is now */
10218 }
10219 else if (type == USE_T_CDL)
10220 {
10221 windgoto(cursor_row, 0);
10222 term_delete_lines(line_count);
10223 screen_start(); /* don't know where cursor is now */
10224 }
10225 /*
10226 * Deleting lines at top of the screen or scroll region: Just scroll
10227 * the whole screen (scroll region) up by outputting newlines on the
10228 * last line.
10229 */
10230 else if (type == USE_NL)
10231 {
10232 windgoto(cursor_end - 1, 0);
10233 for (i = line_count; --i >= 0; )
10234 out_char('\n'); /* cursor will remain on same line */
10235 }
10236 else
10237 {
10238 for (i = line_count; --i >= 0; )
10239 {
10240 if (type == USE_T_DL)
10241 {
10242 windgoto(cursor_row, 0);
10243 out_str(T_DL); /* delete a line */
10244 }
10245 else /* type == USE_T_CE */
10246 {
10247 windgoto(cursor_row + i, 0);
10248 out_str(T_CE); /* erase a line */
10249 }
10250 screen_start(); /* don't know where cursor is now */
10251 }
10252 }
10253
10254 /*
10255 * If the 'db' flag is set, we need to clear the lines that have been
10256 * scrolled up at the bottom of the region.
10257 */
10258 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10259 {
10260 for (i = line_count; i > 0; --i)
10261 {
10262 windgoto(cursor_end - i, 0);
10263 out_str(T_CE); /* erase a line */
10264 screen_start(); /* don't know where cursor is now */
10265 }
10266 }
10267
10268#ifdef FEAT_GUI
10269 gui_can_update_cursor();
10270 if (gui.in_use)
10271 out_flush(); /* always flush after a scroll */
10272#endif
10273
10274 return OK;
10275}
10276
10277/*
10278 * show the current mode and ruler
10279 *
10280 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10281 * If clear_cmdline is FALSE there may be a message there that needs to be
10282 * cleared only if a mode is shown.
10283 * Return the length of the message (0 if no message).
10284 */
10285 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010286showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287{
10288 int need_clear;
10289 int length = 0;
10290 int do_mode;
10291 int attr;
10292 int nwr_save;
10293#ifdef FEAT_INS_EXPAND
10294 int sub_attr;
10295#endif
10296
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010297 do_mode = ((p_smd && msg_silent == 0)
10298 && ((State & INSERT)
10299 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010300 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 if (do_mode || Recording)
10302 {
10303 /*
10304 * Don't show mode right now, when not redrawing or inside a mapping.
10305 * Call char_avail() only when we are going to show something, because
10306 * it takes a bit of time.
10307 */
10308 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10309 {
10310 redraw_cmdline = TRUE; /* show mode later */
10311 return 0;
10312 }
10313
10314 nwr_save = need_wait_return;
10315
10316 /* wait a bit before overwriting an important message */
10317 check_for_delay(FALSE);
10318
10319 /* if the cmdline is more than one line high, erase top lines */
10320 need_clear = clear_cmdline;
10321 if (clear_cmdline && cmdline_row < Rows - 1)
10322 msg_clr_cmdline(); /* will reset clear_cmdline */
10323
10324 /* Position on the last line in the window, column 0 */
10325 msg_pos_mode();
10326 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010327 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 if (do_mode)
10329 {
10330 MSG_PUTS_ATTR("--", attr);
10331#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010332 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010333# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010334 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010335# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010336 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010337# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010338 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010339# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 MSG_PUTS_ATTR(" IM", attr);
10341# else
10342 MSG_PUTS_ATTR(" XIM", attr);
10343# endif
10344#endif
10345#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10346 if (gui.in_use)
10347 {
10348 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010349 {
10350 /* HANGUL */
10351 if (enc_utf8)
10352 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10353 else
10354 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 }
10357#endif
10358#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010359 /* CTRL-X in Insert mode */
10360 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 {
10362 /* These messages can get long, avoid a wrap in a narrow
10363 * window. Prefer showing edit_submode_extra. */
10364 length = (Rows - msg_row) * Columns - 3;
10365 if (edit_submode_extra != NULL)
10366 length -= vim_strsize(edit_submode_extra);
10367 if (length > 0)
10368 {
10369 if (edit_submode_pre != NULL)
10370 length -= vim_strsize(edit_submode_pre);
10371 if (length - vim_strsize(edit_submode) > 0)
10372 {
10373 if (edit_submode_pre != NULL)
10374 msg_puts_attr(edit_submode_pre, attr);
10375 msg_puts_attr(edit_submode, attr);
10376 }
10377 if (edit_submode_extra != NULL)
10378 {
10379 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10380 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010381 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 else
10383 sub_attr = attr;
10384 msg_puts_attr(edit_submode_extra, sub_attr);
10385 }
10386 }
10387 length = 0;
10388 }
10389 else
10390#endif
10391 {
10392#ifdef FEAT_VREPLACE
10393 if (State & VREPLACE_FLAG)
10394 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10395 else
10396#endif
10397 if (State & REPLACE_FLAG)
10398 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10399 else if (State & INSERT)
10400 {
10401#ifdef FEAT_RIGHTLEFT
10402 if (p_ri)
10403 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10404#endif
10405 MSG_PUTS_ATTR(_(" INSERT"), attr);
10406 }
10407 else if (restart_edit == 'I')
10408 MSG_PUTS_ATTR(_(" (insert)"), attr);
10409 else if (restart_edit == 'R')
10410 MSG_PUTS_ATTR(_(" (replace)"), attr);
10411 else if (restart_edit == 'V')
10412 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10413#ifdef FEAT_RIGHTLEFT
10414 if (p_hkmap)
10415 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10416# ifdef FEAT_FKMAP
10417 if (p_fkmap)
10418 MSG_PUTS_ATTR(farsi_text_5, attr);
10419# endif
10420#endif
10421#ifdef FEAT_KEYMAP
10422 if (State & LANGMAP)
10423 {
10424# ifdef FEAT_ARABIC
10425 if (curwin->w_p_arab)
10426 MSG_PUTS_ATTR(_(" Arabic"), attr);
10427 else
10428# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010429 if (get_keymap_str(curwin, (char_u *)" (%s)",
10430 NameBuff, MAXPATHL))
10431 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 }
10433#endif
10434 if ((State & INSERT) && p_paste)
10435 MSG_PUTS_ATTR(_(" (paste)"), attr);
10436
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 if (VIsual_active)
10438 {
10439 char *p;
10440
10441 /* Don't concatenate separate words to avoid translation
10442 * problems. */
10443 switch ((VIsual_select ? 4 : 0)
10444 + (VIsual_mode == Ctrl_V) * 2
10445 + (VIsual_mode == 'V'))
10446 {
10447 case 0: p = N_(" VISUAL"); break;
10448 case 1: p = N_(" VISUAL LINE"); break;
10449 case 2: p = N_(" VISUAL BLOCK"); break;
10450 case 4: p = N_(" SELECT"); break;
10451 case 5: p = N_(" SELECT LINE"); break;
10452 default: p = N_(" SELECT BLOCK"); break;
10453 }
10454 MSG_PUTS_ATTR(_(p), attr);
10455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 MSG_PUTS_ATTR(" --", attr);
10457 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010458
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 need_clear = TRUE;
10460 }
10461 if (Recording
10462#ifdef FEAT_INS_EXPAND
10463 && edit_submode == NULL /* otherwise it gets too long */
10464#endif
10465 )
10466 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010467 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 need_clear = TRUE;
10469 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010470
10471 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 if (need_clear || clear_cmdline)
10473 msg_clr_eos();
10474 msg_didout = FALSE; /* overwrite this message */
10475 length = msg_col;
10476 msg_col = 0;
10477 need_wait_return = nwr_save; /* never ask for hit-return for this */
10478 }
10479 else if (clear_cmdline && msg_silent == 0)
10480 /* Clear the whole command line. Will reset "clear_cmdline". */
10481 msg_clr_cmdline();
10482
10483#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 /* In Visual mode the size of the selected area must be redrawn. */
10485 if (VIsual_active)
10486 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487
10488 /* If the last window has no status line, the ruler is after the mode
10489 * message and must be redrawn */
10490 if (redrawing()
10491# ifdef FEAT_WINDOWS
10492 && lastwin->w_status_height == 0
10493# endif
10494 )
10495 win_redr_ruler(lastwin, TRUE);
10496#endif
10497 redraw_cmdline = FALSE;
10498 clear_cmdline = FALSE;
10499
10500 return length;
10501}
10502
10503/*
10504 * Position for a mode message.
10505 */
10506 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010507msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 msg_col = 0;
10510 msg_row = Rows - 1;
10511}
10512
10513/*
10514 * Delete mode message. Used when ESC is typed which is expected to end
10515 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010516 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 */
10518 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010519unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520{
10521 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010522 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 */
10524 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10525 redraw_cmdline = TRUE; /* delete mode later */
10526 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010527 clearmode();
10528}
10529
10530/*
10531 * Clear the mode message.
10532 */
10533 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010534clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010535{
10536 msg_pos_mode();
10537 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010538 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010539 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540}
10541
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010542 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010543recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010544{
10545 MSG_PUTS_ATTR(_("recording"), attr);
10546 if (!shortmess(SHM_RECORDING))
10547 {
10548 char_u s[4];
10549 sprintf((char *)s, " @%c", Recording);
10550 MSG_PUTS_ATTR(s, attr);
10551 }
10552}
10553
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010554#if defined(FEAT_WINDOWS)
10555/*
10556 * Draw the tab pages line at the top of the Vim window.
10557 */
10558 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010559draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010560{
10561 int tabcount = 0;
10562 tabpage_T *tp;
10563 int tabwidth;
10564 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010565 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010566 int attr;
10567 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010568 win_T *cwp;
10569 int wincount;
10570 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010571 int c;
10572 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010573 int attr_sel = HL_ATTR(HLF_TPS);
10574 int attr_nosel = HL_ATTR(HLF_TP);
10575 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010576 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010577 int room;
10578 int use_sep_chars = (t_colors < 8
10579#ifdef FEAT_GUI
10580 && !gui.in_use
10581#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010582#ifdef FEAT_TERMGUICOLORS
10583 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010584#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010585 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010586
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010587 if (ScreenLines == NULL)
10588 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010589 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010590
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010591#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010592 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010593 if (gui_use_tabline())
10594 {
10595 gui_update_tabline();
10596 return;
10597 }
10598#endif
10599
10600 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010601 return;
10602
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010603#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010604
10605 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10606 for (scol = 0; scol < Columns; ++scol)
10607 TabPageIdxs[scol] = 0;
10608
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010609 /* Use the 'tabline' option if it's set. */
10610 if (*p_tal != NUL)
10611 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010612 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613
10614 /* Check for an error. If there is one we would loop in redrawing the
10615 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010616 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010617 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010618 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010619 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010620 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010621 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010622 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010623 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010624#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010625 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010626 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010628
Bram Moolenaar238a5642006-02-21 22:12:05 +000010629 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10630 if (tabwidth < 6)
10631 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010632
Bram Moolenaar238a5642006-02-21 22:12:05 +000010633 attr = attr_nosel;
10634 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010635 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010636 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10637 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010638 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010639 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010640
Bram Moolenaar238a5642006-02-21 22:12:05 +000010641 if (tp->tp_topframe == topframe)
10642 attr = attr_sel;
10643 if (use_sep_chars && col > 0)
10644 screen_putchar('|', 0, col++, attr);
10645
10646 if (tp->tp_topframe != topframe)
10647 attr = attr_nosel;
10648
10649 screen_putchar(' ', 0, col++, attr);
10650
10651 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010652 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010653 cwp = curwin;
10654 wp = firstwin;
10655 }
10656 else
10657 {
10658 cwp = tp->tp_curwin;
10659 wp = tp->tp_firstwin;
10660 }
10661
10662 modified = FALSE;
10663 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10664 if (bufIsChanged(wp->w_buffer))
10665 modified = TRUE;
10666 if (modified || wincount > 1)
10667 {
10668 if (wincount > 1)
10669 {
10670 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010671 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010672 if (col + len >= Columns - 3)
10673 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010674 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010675#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010676 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010677#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010678 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010679#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010680 );
10681 col += len;
10682 }
10683 if (modified)
10684 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10685 screen_putchar(' ', 0, col++, attr);
10686 }
10687
10688 room = scol - col + tabwidth - 1;
10689 if (room > 0)
10690 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010691 /* Get buffer name in NameBuff[] */
10692 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010693 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010694 len = vim_strsize(NameBuff);
10695 p = NameBuff;
10696#ifdef FEAT_MBYTE
10697 if (has_mbyte)
10698 while (len > room)
10699 {
10700 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010701 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010702 }
10703 else
10704#endif
10705 if (len > room)
10706 {
10707 p += len - room;
10708 len = room;
10709 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010710 if (len > Columns - col - 1)
10711 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010712
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010713 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010714 col += len;
10715 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010716 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010717
10718 /* Store the tab page number in TabPageIdxs[], so that
10719 * jump_to_mouse() knows where each one is. */
10720 ++tabcount;
10721 while (scol < col)
10722 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010723 }
10724
Bram Moolenaar238a5642006-02-21 22:12:05 +000010725 if (use_sep_chars)
10726 c = '_';
10727 else
10728 c = ' ';
10729 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010730
10731 /* Put an "X" for closing the current tab if there are several. */
10732 if (first_tabpage->tp_next != NULL)
10733 {
10734 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10735 TabPageIdxs[Columns - 1] = -999;
10736 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010737 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010738
10739 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10740 * set. */
10741 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010742}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010743
10744/*
10745 * Get buffer name for "buf" into NameBuff[].
10746 * Takes care of special buffer names and translates special characters.
10747 */
10748 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010749get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010750{
10751 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010752 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010753 else
10754 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10755 trans_characters(NameBuff, MAXPATHL);
10756}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010757#endif
10758
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10760/*
10761 * Get the character to use in a status line. Get its attributes in "*attr".
10762 */
10763 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010764fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765{
10766 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010767
10768#ifdef FEAT_TERMINAL
10769 if (bt_terminal(wp->w_buffer))
10770 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010771 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010772 {
10773 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010774 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010775 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010776 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010777 {
10778 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010779 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010780 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010781 }
10782 else
10783#endif
10784 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010786 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 fill = fill_stl;
10788 }
10789 else
10790 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010791 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 fill = fill_stlnc;
10793 }
10794 /* Use fill when there is highlighting, and highlighting of current
10795 * window differs, or the fillchars differ, or this is not the
10796 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010797 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010798 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 || (fill_stl != fill_stlnc)))
10800 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010801 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 return '^';
10803 return '=';
10804}
10805#endif
10806
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010807#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808/*
10809 * Get the character to use in a separator between vertically split windows.
10810 * Get its attributes in "*attr".
10811 */
10812 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010813fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010815 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 if (*attr == 0 && fill_vert == ' ')
10817 return '|';
10818 else
10819 return fill_vert;
10820}
10821#endif
10822
10823/*
10824 * Return TRUE if redrawing should currently be done.
10825 */
10826 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010827redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010829#ifdef FEAT_EVAL
10830 if (disable_redraw_for_testing)
10831 return 0;
10832 else
10833#endif
10834 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10836}
10837
10838/*
10839 * Return TRUE if printing messages should currently be done.
10840 */
10841 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010842messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843{
10844 return (!(p_lz && char_avail() && !KeyTyped));
10845}
10846
10847/*
10848 * Show current status info in ruler and various other places
10849 * If always is FALSE, only show ruler if position has changed.
10850 */
10851 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010852showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853{
10854 if (!always && !redrawing())
10855 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010856#ifdef FEAT_INS_EXPAND
10857 if (pum_visible())
10858 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010859# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010860 /* Don't redraw right now, do it later. */
10861 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010862# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010863 return;
10864 }
10865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010867 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010868 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010869 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 else
10872#endif
10873#ifdef FEAT_CMDL_INFO
10874 win_redr_ruler(curwin, always);
10875#endif
10876
10877#ifdef FEAT_TITLE
10878 if (need_maketitle
10879# ifdef FEAT_STL_OPT
10880 || (p_icon && (stl_syntax & STL_IN_ICON))
10881 || (p_title && (stl_syntax & STL_IN_TITLE))
10882# endif
10883 )
10884 maketitle();
10885#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010886#ifdef FEAT_WINDOWS
10887 /* Redraw the tab pages line if needed. */
10888 if (redraw_tabline)
10889 draw_tabline();
10890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891}
10892
10893#ifdef FEAT_CMDL_INFO
10894 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010895win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010897#define RULER_BUF_LEN 70
10898 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 int row;
10900 int fillchar;
10901 int attr;
10902 int empty_line = FALSE;
10903 colnr_T virtcol;
10904 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010905 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010907#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 int this_ru_col;
10909 int off = 0;
10910 int width = Columns;
10911# define WITH_OFF(x) x
10912# define WITH_WIDTH(x) x
10913#else
10914# define WITH_OFF(x) 0
10915# define WITH_WIDTH(x) Columns
10916# define this_ru_col ru_col
10917#endif
10918
10919 /* If 'ruler' off or redrawing disabled, don't do anything */
10920 if (!p_ru)
10921 return;
10922
10923 /*
10924 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10925 * after deleting lines, before cursor.lnum is corrected.
10926 */
10927 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10928 return;
10929
10930#ifdef FEAT_INS_EXPAND
10931 /* Don't draw the ruler while doing insert-completion, it might overwrite
10932 * the (long) mode message. */
10933# ifdef FEAT_WINDOWS
10934 if (wp == lastwin && lastwin->w_status_height == 0)
10935# endif
10936 if (edit_submode != NULL)
10937 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010938 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10939 if (pum_visible())
10940 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941#endif
10942
10943#ifdef FEAT_STL_OPT
10944 if (*p_ruf)
10945 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010946 int save_called_emsg = called_emsg;
10947
10948 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010950 if (called_emsg)
10951 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010952 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010953 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 return;
10955 }
10956#endif
10957
10958 /*
10959 * Check if not in Insert mode and the line is empty (will show "0-1").
10960 */
10961 if (!(State & INSERT)
10962 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10963 empty_line = TRUE;
10964
10965 /*
10966 * Only draw the ruler when something changed.
10967 */
10968 validate_virtcol_win(wp);
10969 if ( redraw_cmdline
10970 || always
10971 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10972 || wp->w_cursor.col != wp->w_ru_cursor.col
10973 || wp->w_virtcol != wp->w_ru_virtcol
10974#ifdef FEAT_VIRTUALEDIT
10975 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10976#endif
10977 || wp->w_topline != wp->w_ru_topline
10978 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10979#ifdef FEAT_DIFF
10980 || wp->w_topfill != wp->w_ru_topfill
10981#endif
10982 || empty_line != wp->w_ru_empty)
10983 {
10984 cursor_off();
10985#ifdef FEAT_WINDOWS
10986 if (wp->w_status_height)
10987 {
10988 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010989 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 off = W_WINCOL(wp);
10991 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 }
10993 else
10994#endif
10995 {
10996 row = Rows - 1;
10997 fillchar = ' ';
10998 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010999#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 width = Columns;
11001 off = 0;
11002#endif
11003 }
11004
11005 /* In list mode virtcol needs to be recomputed */
11006 virtcol = wp->w_virtcol;
11007 if (wp->w_p_list && lcs_tab1 == NUL)
11008 {
11009 wp->w_p_list = FALSE;
11010 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11011 wp->w_p_list = TRUE;
11012 }
11013
11014 /*
11015 * Some sprintfs return the length, some return a pointer.
11016 * To avoid portability problems we use strlen() here.
11017 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011018 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11020 ? 0L
11021 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011022 len = STRLEN(buffer);
11023 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11025 (int)virtcol + 1);
11026
11027 /*
11028 * Add a "50%" if there is room for it.
11029 * On the last line, don't print in the last column (scrolls the
11030 * screen up on some terminals).
11031 */
11032 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011033 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 o = i + vim_strsize(buffer + i + 1);
11035#ifdef FEAT_WINDOWS
11036 if (wp->w_status_height == 0) /* can't use last char of screen */
11037#endif
11038 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010011039#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 this_ru_col = ru_col - (Columns - width);
11041 if (this_ru_col < 0)
11042 this_ru_col = 0;
11043#endif
11044 /* Never use more than half the window/screen width, leave the other
11045 * half for the filename. */
11046 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
11047 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
11048 if (this_ru_col + o < WITH_WIDTH(width))
11049 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011050 /* need at least 3 chars left for get_rel_pos() + NUL */
11051 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 {
11053#ifdef FEAT_MBYTE
11054 if (has_mbyte)
11055 i += (*mb_char2bytes)(fillchar, buffer + i);
11056 else
11057#endif
11058 buffer[i++] = fillchar;
11059 ++o;
11060 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011061 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 }
11063 /* Truncate at window boundary. */
11064#ifdef FEAT_MBYTE
11065 if (has_mbyte)
11066 {
11067 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011068 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 {
11070 o += (*mb_ptr2cells)(buffer + i);
11071 if (this_ru_col + o > WITH_WIDTH(width))
11072 {
11073 buffer[i] = NUL;
11074 break;
11075 }
11076 }
11077 }
11078 else
11079#endif
11080 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
11081 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
11082
11083 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
11084 i = redraw_cmdline;
11085 screen_fill(row, row + 1,
11086 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
11087 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
11088 fillchar, fillchar, attr);
11089 /* don't redraw the cmdline because of showing the ruler */
11090 redraw_cmdline = i;
11091 wp->w_ru_cursor = wp->w_cursor;
11092 wp->w_ru_virtcol = wp->w_virtcol;
11093 wp->w_ru_empty = empty_line;
11094 wp->w_ru_topline = wp->w_topline;
11095 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11096#ifdef FEAT_DIFF
11097 wp->w_ru_topfill = wp->w_topfill;
11098#endif
11099 }
11100}
11101#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011102
11103#if defined(FEAT_LINEBREAK) || defined(PROTO)
11104/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011105 * Return the width of the 'number' and 'relativenumber' column.
11106 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011107 * Otherwise it depends on 'numberwidth' and the line count.
11108 */
11109 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011110number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011111{
11112 int n;
11113 linenr_T lnum;
11114
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011115 if (wp->w_p_rnu && !wp->w_p_nu)
11116 /* cursor line shows "0" */
11117 lnum = wp->w_height;
11118 else
11119 /* cursor line shows absolute line number */
11120 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011121
Bram Moolenaar6b314672015-03-20 15:42:10 +010011122 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011123 return wp->w_nrwidth_width;
11124 wp->w_nrwidth_line_count = lnum;
11125
11126 n = 0;
11127 do
11128 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011129 lnum /= 10;
11130 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011131 } while (lnum > 0);
11132
11133 /* 'numberwidth' gives the minimal width plus one */
11134 if (n < wp->w_p_nuw - 1)
11135 n = wp->w_p_nuw - 1;
11136
11137 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011138 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011139 return n;
11140}
11141#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011142
11143/*
11144 * Return the current cursor column. This is the actual position on the
11145 * screen. First column is 0.
11146 */
11147 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011148screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011149{
11150 return screen_cur_col;
11151}
11152
11153/*
11154 * Return the current cursor row. This is the actual position on the screen.
11155 * First row is 0.
11156 */
11157 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011158screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011159{
11160 return screen_cur_row;
11161}