blob: 42995c727b7950e2a242d177f1c0819a32922ba2 [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.
541 */
542 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200543update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200545 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 win_T *wp;
547 static int did_intro = FALSE;
548#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
549 int did_one;
550#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200551#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200552 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200553 int gui_cursor_col;
554 int gui_cursor_row;
555#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200556 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000558 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 if (!screen_valid(TRUE))
560 return;
561
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200562 if (type == VALID_NO_UPDATE)
563 {
564 no_update = TRUE;
565 type = 0;
566 }
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (must_redraw)
569 {
570 if (type < must_redraw) /* use maximal type */
571 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000572
573 /* must_redraw is reset here, so that when we run into some weird
574 * reason to redraw while busy redrawing (e.g., asynchronous
575 * scrolling), or update_topline() in win_update() will cause a
576 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 must_redraw = 0;
578 }
579
580 /* Need to update w_lines[]. */
581 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
582 type = NOT_VALID;
583
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000584 /* Postpone the redrawing when it's not needed and when being called
585 * recursively. */
586 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {
588 redraw_later(type); /* remember type for next time */
589 must_redraw = type;
590 if (type > INVERTED_ALL)
591 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
592 return;
593 }
594
595 updating_screen = TRUE;
596#ifdef FEAT_SYN_HL
597 ++display_tick; /* let syntax code know we're in a next round of
598 * display updating */
599#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200600 if (no_update)
601 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602
603 /*
604 * if the screen was scrolled up when displaying a message, scroll it down
605 */
606 if (msg_scrolled)
607 {
608 clear_cmdline = TRUE;
609 if (msg_scrolled > Rows - 5) /* clearing is faster */
610 type = CLEAR;
611 else if (type != CLEAR)
612 {
613 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200614 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
615 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 type = CLEAR;
617 FOR_ALL_WINDOWS(wp)
618 {
619 if (W_WINROW(wp) < msg_scrolled)
620 {
621 if (W_WINROW(wp) + wp->w_height > msg_scrolled
622 && wp->w_redr_type < REDRAW_TOP
623 && wp->w_lines_valid > 0
624 && wp->w_topline == wp->w_lines[0].wl_lnum)
625 {
626 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
627 wp->w_redr_type = REDRAW_TOP;
628 }
629 else
630 {
631 wp->w_redr_type = NOT_VALID;
632#ifdef FEAT_WINDOWS
633 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
634 <= msg_scrolled)
635 wp->w_redr_status = TRUE;
636#endif
637 }
638 }
639 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200640 if (!no_update)
641 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000642#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000643 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
646 msg_scrolled = 0;
647 need_wait_return = FALSE;
648 }
649
650 /* reset cmdline_row now (may have been changed temporarily) */
651 compute_cmdrow();
652
653 /* Check for changed highlighting */
654 if (need_highlight_changed)
655 highlight_changed();
656
657 if (type == CLEAR) /* first clear screen */
658 {
659 screenclear(); /* will reset clear_cmdline */
660 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200661 /* must_redraw may be set indirectly, avoid another redraw later */
662 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664
665 if (clear_cmdline) /* going to clear cmdline (done below) */
666 check_for_delay(FALSE);
667
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000668#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200669 /* Force redraw when width of 'number' or 'relativenumber' column
670 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000671 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200672 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
673 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000674 curwin->w_redr_type = NOT_VALID;
675#endif
676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 /*
678 * Only start redrawing if there is really something to do.
679 */
680 if (type == INVERTED)
681 update_curswant();
682 if (curwin->w_redr_type < type
683 && !((type == VALID
684 && curwin->w_lines[0].wl_valid
685#ifdef FEAT_DIFF
686 && curwin->w_topfill == curwin->w_old_topfill
687 && curwin->w_botfill == curwin->w_old_botfill
688#endif
689 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000691 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
693 && curwin->w_old_visual_mode == VIsual_mode
694 && (curwin->w_valid & VALID_VIRTCOL)
695 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 ))
697 curwin->w_redr_type = type;
698
Bram Moolenaar5a305422006-04-28 22:38:25 +0000699#ifdef FEAT_WINDOWS
700 /* Redraw the tab pages line if needed. */
701 if (redraw_tabline || type >= NOT_VALID)
702 draw_tabline();
703#endif
704
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705#ifdef FEAT_SYN_HL
706 /*
707 * Correct stored syntax highlighting info for changes in each displayed
708 * buffer. Each buffer must only be done once.
709 */
710 FOR_ALL_WINDOWS(wp)
711 {
712 if (wp->w_buffer->b_mod_set)
713 {
714# ifdef FEAT_WINDOWS
715 win_T *wwp;
716
717 /* Check if we already did this buffer. */
718 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
719 if (wwp->w_buffer == wp->w_buffer)
720 break;
721# endif
722 if (
723# ifdef FEAT_WINDOWS
724 wwp == wp &&
725# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200726 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 syn_stack_apply_changes(wp->w_buffer);
728 }
729 }
730#endif
731
732 /*
733 * Go from top to bottom through the windows, redrawing the ones that need
734 * it.
735 */
736#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
737 did_one = FALSE;
738#endif
739#ifdef FEAT_SEARCH_EXTRA
740 search_hl.rm.regprog = NULL;
741#endif
742 FOR_ALL_WINDOWS(wp)
743 {
744 if (wp->w_redr_type != 0)
745 {
746 cursor_off();
747#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
748 if (!did_one)
749 {
750 did_one = TRUE;
751# ifdef FEAT_SEARCH_EXTRA
752 start_search_hl();
753# endif
754# ifdef FEAT_CLIPBOARD
755 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200756 if (clip_star.available && clip_isautosel_star())
757 clip_update_selection(&clip_star);
758 if (clip_plus.available && clip_isautosel_plus())
759 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760# endif
761#ifdef FEAT_GUI
762 /* Remove the cursor before starting to do anything, because
763 * scrolling may make it difficult to redraw the text under
764 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200765 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200766 {
767 gui_cursor_col = gui.cursor_col;
768 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200770 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
773 }
774#endif
775 win_update(wp);
776 }
777
778#ifdef FEAT_WINDOWS
779 /* redraw status line after the window to minimize cursor movement */
780 if (wp->w_redr_status)
781 {
782 cursor_off();
783 win_redr_status(wp);
784 }
785#endif
786 }
787#if defined(FEAT_SEARCH_EXTRA)
788 end_search_hl();
789#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100790#ifdef FEAT_INS_EXPAND
791 /* May need to redraw the popup menu. */
792 if (pum_visible())
793 pum_redraw();
794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796#ifdef FEAT_WINDOWS
797 /* Reset b_mod_set flags. Going through all windows is probably faster
798 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200799 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 wp->w_buffer->b_mod_set = FALSE;
801#else
802 curbuf->b_mod_set = FALSE;
803#endif
804
805 updating_screen = FALSE;
806#ifdef FEAT_GUI
807 gui_may_resize_shell();
808#endif
809
810 /* Clear or redraw the command line. Done last, because scrolling may
811 * mess up the command line. */
812 if (clear_cmdline || redraw_cmdline)
813 showmode();
814
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200815 if (no_update)
816 --no_win_do_lines_ins;
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200819 if (!did_intro)
820 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 did_intro = TRUE;
822
823#ifdef FEAT_GUI
824 /* Redraw the cursor and update the scrollbars when all screen updating is
825 * done. */
826 if (gui.in_use)
827 {
828 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200829 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200830 {
831 /* Put the GUI position where the cursor was, gui_update_cursor()
832 * uses that. */
833 gui.col = gui_cursor_col;
834 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200835# ifdef FEAT_MBYTE
836 gui.col = mb_fix_col(gui.col, gui.row);
837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200839 screen_cur_col = gui.col;
840 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 gui_update_scrollbars(FALSE);
843 }
844#endif
845}
846
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100847#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
848/*
849 * Prepare for updating one or more windows.
850 * Caller must check for "updating_screen" already set to avoid recursiveness.
851 */
852 static void
853update_prepare(void)
854{
855 cursor_off();
856 updating_screen = TRUE;
857#ifdef FEAT_GUI
858 /* Remove the cursor before starting to do anything, because scrolling may
859 * make it difficult to redraw the text under it. */
860 if (gui.in_use)
861 gui_undraw_cursor();
862#endif
863#ifdef FEAT_SEARCH_EXTRA
864 start_search_hl();
865#endif
866}
867
868/*
869 * Finish updating one or more windows.
870 */
871 static void
872update_finish(void)
873{
874 if (redraw_cmdline)
875 showmode();
876
877# ifdef FEAT_SEARCH_EXTRA
878 end_search_hl();
879# endif
880
881 updating_screen = FALSE;
882
883# ifdef FEAT_GUI
884 gui_may_resize_shell();
885
886 /* Redraw the cursor and update the scrollbars when all screen updating is
887 * done. */
888 if (gui.in_use)
889 {
890 out_flush(); /* required before updating the cursor */
891 gui_update_cursor(FALSE, FALSE);
892 gui_update_scrollbars(FALSE);
893 }
894# endif
895}
896#endif
897
Bram Moolenaar860cae12010-06-05 23:22:07 +0200898#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200899/*
900 * Return TRUE if the cursor line in window "wp" may be concealed, according
901 * to the 'concealcursor' option.
902 */
903 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100904conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200905{
906 int c;
907
908 if (*wp->w_p_cocu == NUL)
909 return FALSE;
910 if (get_real_state() & VISUAL)
911 c = 'v';
912 else if (State & INSERT)
913 c = 'i';
914 else if (State & NORMAL)
915 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200916 else if (State & CMDLINE)
917 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200918 else
919 return FALSE;
920 return vim_strchr(wp->w_p_cocu, c) != NULL;
921}
922
923/*
924 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
925 */
926 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100927conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200928{
929 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
930 {
931 need_cursor_line_redraw = TRUE;
932 /* Need to recompute cursor column, e.g., when starting Visual mode
933 * without concealing. */
934 curs_columns(TRUE);
935 }
936}
937
Bram Moolenaar860cae12010-06-05 23:22:07 +0200938 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100939update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200940{
941 int row;
942 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200943#ifdef SYN_TIME_LIMIT
944 proftime_T syntax_tm;
945#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200946
Bram Moolenaar908be432016-05-24 10:51:30 +0200947 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100948 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200949 return;
950
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200952 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200953 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200954#ifdef SYN_TIME_LIMIT
955 /* Set the time limit to 'redrawtime'. */
956 profile_setlimit(p_rdt, &syntax_tm);
957#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100958 update_prepare();
959
Bram Moolenaar860cae12010-06-05 23:22:07 +0200960 row = 0;
961 for (j = 0; j < wp->w_lines_valid; ++j)
962 {
963 if (lnum == wp->w_lines[j].wl_lnum)
964 {
965 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200966# ifdef FEAT_SEARCH_EXTRA
967 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200968 start_search_hl();
969 prepare_search_hl(wp, lnum);
970# endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200971 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE,
972#ifdef SYN_TIME_LIMIT
973 &syntax_tm
974#else
975 NULL
976#endif
977 );
Bram Moolenaar860cae12010-06-05 23:22:07 +0200978# if defined(FEAT_SEARCH_EXTRA)
979 end_search_hl();
980# endif
981 break;
982 }
983 row += wp->w_lines[j].wl_size;
984 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100985
986 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200987 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200988 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990#endif
991
992#if defined(FEAT_SIGNS) || defined(PROTO)
993 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100994update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
996 win_T *wp;
997 int doit = FALSE;
998
999# ifdef FEAT_FOLDING
1000 win_foldinfo.fi_level = 0;
1001# endif
1002
1003 /* update/delete a specific mark */
1004 FOR_ALL_WINDOWS(wp)
1005 {
1006 if (buf != NULL && lnum > 0)
1007 {
1008 if (wp->w_buffer == buf && lnum >= wp->w_topline
1009 && lnum < wp->w_botline)
1010 {
1011 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1012 wp->w_redraw_top = lnum;
1013 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1014 wp->w_redraw_bot = lnum;
1015 redraw_win_later(wp, VALID);
1016 }
1017 }
1018 else
1019 redraw_win_later(wp, VALID);
1020 if (wp->w_redr_type != 0)
1021 doit = TRUE;
1022 }
1023
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001024 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001025 * happening (recursive call), messages on the screen or still starting up.
1026 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001027 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001028 || State == ASKMORE || State == HITRETURN
1029 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001030#ifdef FEAT_GUI
1031 || gui.starting
1032#endif
1033 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 return;
1035
1036 /* update all windows that need updating */
1037 update_prepare();
1038
1039# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001040 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
1042 if (wp->w_redr_type != 0)
1043 win_update(wp);
1044 if (wp->w_redr_status)
1045 win_redr_status(wp);
1046 }
1047# else
1048 if (curwin->w_redr_type != 0)
1049 win_update(curwin);
1050# endif
1051
1052 update_finish();
1053}
1054#endif
1055
1056
1057#if defined(FEAT_GUI) || defined(PROTO)
1058/*
1059 * Update a single window, its status line and maybe the command line msg.
1060 * Used for the GUI scrollbar.
1061 */
1062 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001063updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001065 /* return if already busy updating */
1066 if (updating_screen)
1067 return;
1068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 update_prepare();
1070
1071#ifdef FEAT_CLIPBOARD
1072 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001073 if (clip_star.available && clip_isautosel_star())
1074 clip_update_selection(&clip_star);
1075 if (clip_plus.available && clip_isautosel_plus())
1076 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001082 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001083 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001084 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001085
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (wp->w_redr_status
1087# ifdef FEAT_CMDL_INFO
1088 || p_ru
1089# endif
1090# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001091 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092# endif
1093 )
1094 win_redr_status(wp);
1095#endif
1096
1097 update_finish();
1098}
1099#endif
1100
1101/*
1102 * Update a single window.
1103 *
1104 * This may cause the windows below it also to be redrawn (when clearing the
1105 * screen or scrolling lines).
1106 *
1107 * How the window is redrawn depends on wp->w_redr_type. Each type also
1108 * implies the one below it.
1109 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001110 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1112 * INVERTED redraw the changed part of the Visual area
1113 * INVERTED_ALL redraw the whole Visual area
1114 * VALID 1. scroll up/down to adjust for a changed w_topline
1115 * 2. update lines at the top when scrolled down
1116 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001117 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 * b_mod_top and b_mod_bot.
1119 * - if wp->w_redraw_top non-zero, redraw lines between
1120 * wp->w_redraw_top and wp->w_redr_bot.
1121 * - continue redrawing when syntax status is invalid.
1122 * 4. if scrolled up, update lines at the bottom.
1123 * This results in three areas that may need updating:
1124 * top: from first row to top_end (when scrolled down)
1125 * mid: from mid_start to mid_end (update inversion or changed text)
1126 * bot: from bot_start to last row (when scrolled up)
1127 */
1128 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001129win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130{
1131 buf_T *buf = wp->w_buffer;
1132 int type;
1133 int top_end = 0; /* Below last row of the top area that needs
1134 updating. 0 when no top area updating. */
1135 int mid_start = 999;/* first row of the mid area that needs
1136 updating. 999 when no mid area updating. */
1137 int mid_end = 0; /* Below last row of the mid area that needs
1138 updating. 0 when no mid area updating. */
1139 int bot_start = 999;/* first row of the bot area that needs
1140 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 int scrolled_down = FALSE; /* TRUE when scrolled down when
1142 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001144 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 int top_to_mod = FALSE; /* redraw above mod_top */
1146#endif
1147
1148 int row; /* current window row to display */
1149 linenr_T lnum; /* current buffer lnum to display */
1150 int idx; /* current index in w_lines[] */
1151 int srow; /* starting row of the current line */
1152
1153 int eof = FALSE; /* if TRUE, we hit the end of the file */
1154 int didline = FALSE; /* if TRUE, we finished the last line */
1155 int i;
1156 long j;
1157 static int recursive = FALSE; /* being called recursively */
1158 int old_botline = wp->w_botline;
1159#ifdef FEAT_FOLDING
1160 long fold_count;
1161#endif
1162#ifdef FEAT_SYN_HL
1163 /* remember what happened to the previous line, to know if
1164 * check_visual_highlight() can be used */
1165#define DID_NONE 1 /* didn't update a line */
1166#define DID_LINE 2 /* updated a normal line */
1167#define DID_FOLD 3 /* updated a folded line */
1168 int did_update = DID_NONE;
1169 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1170#endif
1171 linenr_T mod_top = 0;
1172 linenr_T mod_bot = 0;
1173#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1174 int save_got_int;
1175#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001176#ifdef SYN_TIME_LIMIT
1177 proftime_T syntax_tm;
1178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179
1180 type = wp->w_redr_type;
1181
1182 if (type == NOT_VALID)
1183 {
1184#ifdef FEAT_WINDOWS
1185 wp->w_redr_status = TRUE;
1186#endif
1187 wp->w_lines_valid = 0;
1188 }
1189
1190 /* Window is zero-height: nothing to draw. */
1191 if (wp->w_height == 0)
1192 {
1193 wp->w_redr_type = 0;
1194 return;
1195 }
1196
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001197#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 /* Window is zero-width: Only need to draw the separator. */
1199 if (wp->w_width == 0)
1200 {
1201 /* draw the vertical separator right of this window */
1202 draw_vsep_win(wp, 0);
1203 wp->w_redr_type = 0;
1204 return;
1205 }
1206#endif
1207
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001208#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001209 /* If this window contains a terminal, redraw works completely differently.
1210 */
1211 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001212 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001213 wp->w_redr_type = 0;
1214 return;
1215 }
1216#endif
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001219 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#endif
1221
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001222#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001223 /* Force redraw when width of 'number' or 'relativenumber' column
1224 * changes. */
1225 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001226 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001227 {
1228 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001229 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001230 }
1231 else
1232#endif
1233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1235 {
1236 /*
1237 * When there are both inserted/deleted lines and specific lines to be
1238 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1239 * everything (only happens when redrawing is off for while).
1240 */
1241 type = NOT_VALID;
1242 }
1243 else
1244 {
1245 /*
1246 * Set mod_top to the first line that needs displaying because of
1247 * changes. Set mod_bot to the first line after the changes.
1248 */
1249 mod_top = wp->w_redraw_top;
1250 if (wp->w_redraw_bot != 0)
1251 mod_bot = wp->w_redraw_bot + 1;
1252 else
1253 mod_bot = 0;
1254 wp->w_redraw_top = 0; /* reset for next time */
1255 wp->w_redraw_bot = 0;
1256 if (buf->b_mod_set)
1257 {
1258 if (mod_top == 0 || mod_top > buf->b_mod_top)
1259 {
1260 mod_top = buf->b_mod_top;
1261#ifdef FEAT_SYN_HL
1262 /* Need to redraw lines above the change that may be included
1263 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001264 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001266 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 if (mod_top < 1)
1268 mod_top = 1;
1269 }
1270#endif
1271 }
1272 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1273 mod_bot = buf->b_mod_bot;
1274
1275#ifdef FEAT_SEARCH_EXTRA
1276 /* When 'hlsearch' is on and using a multi-line search pattern, a
1277 * change in one line may make the Search highlighting in a
1278 * previous line invalid. Simple solution: redraw all visible
1279 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001280 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001282 if (search_hl.rm.regprog != NULL
1283 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001285 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001286 {
1287 cur = wp->w_match_head;
1288 while (cur != NULL)
1289 {
1290 if (cur->match.regprog != NULL
1291 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001292 {
1293 top_to_mod = TRUE;
1294 break;
1295 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001296 cur = cur->next;
1297 }
1298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299#endif
1300 }
1301#ifdef FEAT_FOLDING
1302 if (mod_top != 0 && hasAnyFolding(wp))
1303 {
1304 linenr_T lnumt, lnumb;
1305
1306 /*
1307 * A change in a line can cause lines above it to become folded or
1308 * unfolded. Find the top most buffer line that may be affected.
1309 * If the line was previously folded and displayed, get the first
1310 * line of that fold. If the line is folded now, get the first
1311 * folded line. Use the minimum of these two.
1312 */
1313
1314 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1315 * the line below it. If there is no valid entry, use w_topline.
1316 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1317 * to this line. If there is no valid entry, use MAXLNUM. */
1318 lnumt = wp->w_topline;
1319 lnumb = MAXLNUM;
1320 for (i = 0; i < wp->w_lines_valid; ++i)
1321 if (wp->w_lines[i].wl_valid)
1322 {
1323 if (wp->w_lines[i].wl_lastlnum < mod_top)
1324 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1325 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1326 {
1327 lnumb = wp->w_lines[i].wl_lnum;
1328 /* When there is a fold column it might need updating
1329 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001330 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 ++lnumb;
1332 }
1333 }
1334
1335 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1336 if (mod_top > lnumt)
1337 mod_top = lnumt;
1338
1339 /* Now do the same for the bottom line (one above mod_bot). */
1340 --mod_bot;
1341 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1342 ++mod_bot;
1343 if (mod_bot < lnumb)
1344 mod_bot = lnumb;
1345 }
1346#endif
1347
1348 /* When a change starts above w_topline and the end is below
1349 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001350 * If the end of the change is above w_topline: do like no change was
1351 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (mod_top != 0 && mod_top < wp->w_topline)
1353 {
1354 if (mod_bot > wp->w_topline)
1355 mod_top = wp->w_topline;
1356#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001357 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 top_end = 1;
1359#endif
1360 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001361
1362 /* When line numbers are displayed need to redraw all lines below
1363 * inserted/deleted lines. */
1364 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1365 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
1367
1368 /*
1369 * When only displaying the lines at the top, set top_end. Used when
1370 * window has scrolled down for msg_scrolled.
1371 */
1372 if (type == REDRAW_TOP)
1373 {
1374 j = 0;
1375 for (i = 0; i < wp->w_lines_valid; ++i)
1376 {
1377 j += wp->w_lines[i].wl_size;
1378 if (j >= wp->w_upd_rows)
1379 {
1380 top_end = j;
1381 break;
1382 }
1383 }
1384 if (top_end == 0)
1385 /* not found (cannot happen?): redraw everything */
1386 type = NOT_VALID;
1387 else
1388 /* top area defined, the rest is VALID */
1389 type = VALID;
1390 }
1391
Bram Moolenaar367329b2007-08-30 11:53:22 +00001392 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001393 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1394 * non-zero and thus not FALSE) will indicate that screenclear() was not
1395 * called. */
1396 if (screen_cleared)
1397 screen_cleared = MAYBE;
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 /*
1400 * If there are no changes on the screen that require a complete redraw,
1401 * handle three cases:
1402 * 1: we are off the top of the screen by a few lines: scroll down
1403 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1404 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1405 * w_lines[] that needs updating.
1406 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001407 if ((type == VALID || type == SOME_VALID
1408 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409#ifdef FEAT_DIFF
1410 && !wp->w_botfill && !wp->w_old_botfill
1411#endif
1412 )
1413 {
1414 if (mod_top != 0 && wp->w_topline == mod_top)
1415 {
1416 /*
1417 * w_topline is the first changed line, the scrolling will be done
1418 * further down.
1419 */
1420 }
1421 else if (wp->w_lines[0].wl_valid
1422 && (wp->w_topline < wp->w_lines[0].wl_lnum
1423#ifdef FEAT_DIFF
1424 || (wp->w_topline == wp->w_lines[0].wl_lnum
1425 && wp->w_topfill > wp->w_old_topfill)
1426#endif
1427 ))
1428 {
1429 /*
1430 * New topline is above old topline: May scroll down.
1431 */
1432#ifdef FEAT_FOLDING
1433 if (hasAnyFolding(wp))
1434 {
1435 linenr_T ln;
1436
1437 /* count the number of lines we are off, counting a sequence
1438 * of folded lines as one */
1439 j = 0;
1440 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1441 {
1442 ++j;
1443 if (j >= wp->w_height - 2)
1444 break;
1445 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1446 }
1447 }
1448 else
1449#endif
1450 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1451 if (j < wp->w_height - 2) /* not too far off */
1452 {
1453 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1454#ifdef FEAT_DIFF
1455 /* insert extra lines for previously invisible filler lines */
1456 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1457 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1458 - wp->w_old_topfill;
1459#endif
1460 if (i < wp->w_height - 2) /* less than a screen off */
1461 {
1462 /*
1463 * Try to insert the correct number of lines.
1464 * If not the last window, delete the lines at the bottom.
1465 * win_ins_lines may fail when the terminal can't do it.
1466 */
1467 if (i > 0)
1468 check_for_delay(FALSE);
1469 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1470 {
1471 if (wp->w_lines_valid != 0)
1472 {
1473 /* Need to update rows that are new, stop at the
1474 * first one that scrolled down. */
1475 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
1478 /* Move the entries that were scrolled, disable
1479 * the entries for the lines to be redrawn. */
1480 if ((wp->w_lines_valid += j) > wp->w_height)
1481 wp->w_lines_valid = wp->w_height;
1482 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1483 wp->w_lines[idx] = wp->w_lines[idx - j];
1484 while (idx >= 0)
1485 wp->w_lines[idx--].wl_valid = FALSE;
1486 }
1487 }
1488 else
1489 mid_start = 0; /* redraw all lines */
1490 }
1491 else
1492 mid_start = 0; /* redraw all lines */
1493 }
1494 else
1495 mid_start = 0; /* redraw all lines */
1496 }
1497 else
1498 {
1499 /*
1500 * New topline is at or below old topline: May scroll up.
1501 * When topline didn't change, find first entry in w_lines[] that
1502 * needs updating.
1503 */
1504
1505 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1506 j = -1;
1507 row = 0;
1508 for (i = 0; i < wp->w_lines_valid; i++)
1509 {
1510 if (wp->w_lines[i].wl_valid
1511 && wp->w_lines[i].wl_lnum == wp->w_topline)
1512 {
1513 j = i;
1514 break;
1515 }
1516 row += wp->w_lines[i].wl_size;
1517 }
1518 if (j == -1)
1519 {
1520 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1521 * lines */
1522 mid_start = 0;
1523 }
1524 else
1525 {
1526 /*
1527 * Try to delete the correct number of lines.
1528 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1529 */
1530#ifdef FEAT_DIFF
1531 /* If the topline didn't change, delete old filler lines,
1532 * otherwise delete filler lines of the new topline... */
1533 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1534 row += wp->w_old_topfill;
1535 else
1536 row += diff_check_fill(wp, wp->w_topline);
1537 /* ... but don't delete new filler lines. */
1538 row -= wp->w_topfill;
1539#endif
1540 if (row > 0)
1541 {
1542 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001543 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1544 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 bot_start = wp->w_height - row;
1546 else
1547 mid_start = 0; /* redraw all lines */
1548 }
1549 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1550 {
1551 /*
1552 * Skip the lines (below the deleted lines) that are still
1553 * valid and don't need redrawing. Copy their info
1554 * upwards, to compensate for the deleted lines. Set
1555 * bot_start to the first row that needs redrawing.
1556 */
1557 bot_start = 0;
1558 idx = 0;
1559 for (;;)
1560 {
1561 wp->w_lines[idx] = wp->w_lines[j];
1562 /* stop at line that didn't fit, unless it is still
1563 * valid (no lines deleted) */
1564 if (row > 0 && bot_start + row
1565 + (int)wp->w_lines[j].wl_size > wp->w_height)
1566 {
1567 wp->w_lines_valid = idx + 1;
1568 break;
1569 }
1570 bot_start += wp->w_lines[idx++].wl_size;
1571
1572 /* stop at the last valid entry in w_lines[].wl_size */
1573 if (++j >= wp->w_lines_valid)
1574 {
1575 wp->w_lines_valid = idx;
1576 break;
1577 }
1578 }
1579#ifdef FEAT_DIFF
1580 /* Correct the first entry for filler lines at the top
1581 * when it won't get updated below. */
1582 if (wp->w_p_diff && bot_start > 0)
1583 wp->w_lines[0].wl_size =
1584 plines_win_nofill(wp, wp->w_topline, TRUE)
1585 + wp->w_topfill;
1586#endif
1587 }
1588 }
1589 }
1590
1591 /* When starting redraw in the first line, redraw all lines. When
1592 * there is only one window it's probably faster to clear the screen
1593 * first. */
1594 if (mid_start == 0)
1595 {
1596 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001597 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001598 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001599 /* Clear the screen when it was not done by win_del_lines() or
1600 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1601 * then. */
1602 if (screen_cleared != TRUE)
1603 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001604#ifdef FEAT_WINDOWS
1605 /* The screen was cleared, redraw the tab pages line. */
1606 if (redraw_tabline)
1607 draw_tabline();
1608#endif
1609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001611
1612 /* When win_del_lines() or win_ins_lines() caused the screen to be
1613 * cleared (only happens for the first window) or when screenclear()
1614 * was called directly above, "must_redraw" will have been set to
1615 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1616 if (screen_cleared == TRUE)
1617 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 else
1620 {
1621 /* Not VALID or INVERTED: redraw all lines. */
1622 mid_start = 0;
1623 mid_end = wp->w_height;
1624 }
1625
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001626 if (type == SOME_VALID)
1627 {
1628 /* SOME_VALID: redraw all lines. */
1629 mid_start = 0;
1630 mid_end = wp->w_height;
1631 type = NOT_VALID;
1632 }
1633
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 /* check if we are updating or removing the inverted part */
1635 if ((VIsual_active && buf == curwin->w_buffer)
1636 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1637 {
1638 linenr_T from, to;
1639
1640 if (VIsual_active)
1641 {
1642 if (VIsual_active
1643 && (VIsual_mode != wp->w_old_visual_mode
1644 || type == INVERTED_ALL))
1645 {
1646 /*
1647 * If the type of Visual selection changed, redraw the whole
1648 * selection. Also when the ownership of the X selection is
1649 * gained or lost.
1650 */
1651 if (curwin->w_cursor.lnum < VIsual.lnum)
1652 {
1653 from = curwin->w_cursor.lnum;
1654 to = VIsual.lnum;
1655 }
1656 else
1657 {
1658 from = VIsual.lnum;
1659 to = curwin->w_cursor.lnum;
1660 }
1661 /* redraw more when the cursor moved as well */
1662 if (wp->w_old_cursor_lnum < from)
1663 from = wp->w_old_cursor_lnum;
1664 if (wp->w_old_cursor_lnum > to)
1665 to = wp->w_old_cursor_lnum;
1666 if (wp->w_old_visual_lnum < from)
1667 from = wp->w_old_visual_lnum;
1668 if (wp->w_old_visual_lnum > to)
1669 to = wp->w_old_visual_lnum;
1670 }
1671 else
1672 {
1673 /*
1674 * Find the line numbers that need to be updated: The lines
1675 * between the old cursor position and the current cursor
1676 * position. Also check if the Visual position changed.
1677 */
1678 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1679 {
1680 from = curwin->w_cursor.lnum;
1681 to = wp->w_old_cursor_lnum;
1682 }
1683 else
1684 {
1685 from = wp->w_old_cursor_lnum;
1686 to = curwin->w_cursor.lnum;
1687 if (from == 0) /* Visual mode just started */
1688 from = to;
1689 }
1690
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001691 if (VIsual.lnum != wp->w_old_visual_lnum
1692 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
1694 if (wp->w_old_visual_lnum < from
1695 && wp->w_old_visual_lnum != 0)
1696 from = wp->w_old_visual_lnum;
1697 if (wp->w_old_visual_lnum > to)
1698 to = wp->w_old_visual_lnum;
1699 if (VIsual.lnum < from)
1700 from = VIsual.lnum;
1701 if (VIsual.lnum > to)
1702 to = VIsual.lnum;
1703 }
1704 }
1705
1706 /*
1707 * If in block mode and changed column or curwin->w_curswant:
1708 * update all lines.
1709 * First compute the actual start and end column.
1710 */
1711 if (VIsual_mode == Ctrl_V)
1712 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001713 colnr_T fromc, toc;
1714#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1715 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
Bram Moolenaar404406a2014-10-09 13:24:43 +02001717 if (curwin->w_p_lbr)
1718 ve_flags = VE_ALL;
1719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001721#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1722 ve_flags = save_ve_flags;
1723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 ++toc;
1725 if (curwin->w_curswant == MAXCOL)
1726 toc = MAXCOL;
1727
1728 if (fromc != wp->w_old_cursor_fcol
1729 || toc != wp->w_old_cursor_lcol)
1730 {
1731 if (from > VIsual.lnum)
1732 from = VIsual.lnum;
1733 if (to < VIsual.lnum)
1734 to = VIsual.lnum;
1735 }
1736 wp->w_old_cursor_fcol = fromc;
1737 wp->w_old_cursor_lcol = toc;
1738 }
1739 }
1740 else
1741 {
1742 /* Use the line numbers of the old Visual area. */
1743 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1744 {
1745 from = wp->w_old_cursor_lnum;
1746 to = wp->w_old_visual_lnum;
1747 }
1748 else
1749 {
1750 from = wp->w_old_visual_lnum;
1751 to = wp->w_old_cursor_lnum;
1752 }
1753 }
1754
1755 /*
1756 * There is no need to update lines above the top of the window.
1757 */
1758 if (from < wp->w_topline)
1759 from = wp->w_topline;
1760
1761 /*
1762 * If we know the value of w_botline, use it to restrict the update to
1763 * the lines that are visible in the window.
1764 */
1765 if (wp->w_valid & VALID_BOTLINE)
1766 {
1767 if (from >= wp->w_botline)
1768 from = wp->w_botline - 1;
1769 if (to >= wp->w_botline)
1770 to = wp->w_botline - 1;
1771 }
1772
1773 /*
1774 * Find the minimal part to be updated.
1775 * Watch out for scrolling that made entries in w_lines[] invalid.
1776 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1777 * top_end; need to redraw from top_end to the "to" line.
1778 * A middle mouse click with a Visual selection may change the text
1779 * above the Visual area and reset wl_valid, do count these for
1780 * mid_end (in srow).
1781 */
1782 if (mid_start > 0)
1783 {
1784 lnum = wp->w_topline;
1785 idx = 0;
1786 srow = 0;
1787 if (scrolled_down)
1788 mid_start = top_end;
1789 else
1790 mid_start = 0;
1791 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1792 {
1793 if (wp->w_lines[idx].wl_valid)
1794 mid_start += wp->w_lines[idx].wl_size;
1795 else if (!scrolled_down)
1796 srow += wp->w_lines[idx].wl_size;
1797 ++idx;
1798# ifdef FEAT_FOLDING
1799 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1800 lnum = wp->w_lines[idx].wl_lnum;
1801 else
1802# endif
1803 ++lnum;
1804 }
1805 srow += mid_start;
1806 mid_end = wp->w_height;
1807 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1808 {
1809 if (wp->w_lines[idx].wl_valid
1810 && wp->w_lines[idx].wl_lnum >= to + 1)
1811 {
1812 /* Only update until first row of this line */
1813 mid_end = srow;
1814 break;
1815 }
1816 srow += wp->w_lines[idx].wl_size;
1817 }
1818 }
1819 }
1820
1821 if (VIsual_active && buf == curwin->w_buffer)
1822 {
1823 wp->w_old_visual_mode = VIsual_mode;
1824 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1825 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001826 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 wp->w_old_curswant = curwin->w_curswant;
1828 }
1829 else
1830 {
1831 wp->w_old_visual_mode = 0;
1832 wp->w_old_cursor_lnum = 0;
1833 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001834 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
1837#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1838 /* reset got_int, otherwise regexp won't work */
1839 save_got_int = got_int;
1840 got_int = 0;
1841#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001842#ifdef SYN_TIME_LIMIT
1843 /* Set the time limit to 'redrawtime'. */
1844 profile_setlimit(p_rdt, &syntax_tm);
1845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846#ifdef FEAT_FOLDING
1847 win_foldinfo.fi_level = 0;
1848#endif
1849
1850 /*
1851 * Update all the window rows.
1852 */
1853 idx = 0; /* first entry in w_lines[].wl_size */
1854 row = 0;
1855 srow = 0;
1856 lnum = wp->w_topline; /* first line shown in window */
1857 for (;;)
1858 {
1859 /* stop updating when reached the end of the window (check for _past_
1860 * the end of the window is at the end of the loop) */
1861 if (row == wp->w_height)
1862 {
1863 didline = TRUE;
1864 break;
1865 }
1866
1867 /* stop updating when hit the end of the file */
1868 if (lnum > buf->b_ml.ml_line_count)
1869 {
1870 eof = TRUE;
1871 break;
1872 }
1873
1874 /* Remember the starting row of the line that is going to be dealt
1875 * with. It is used further down when the line doesn't fit. */
1876 srow = row;
1877
1878 /*
1879 * Update a line when it is in an area that needs updating, when it
1880 * has changes or w_lines[idx] is invalid.
1881 * bot_start may be halfway a wrapped line after using
1882 * win_del_lines(), check if the current line includes it.
1883 * When syntax folding is being used, the saved syntax states will
1884 * already have been updated, we can't see where the syntax state is
1885 * the same again, just update until the end of the window.
1886 */
1887 if (row < top_end
1888 || (row >= mid_start && row < mid_end)
1889#ifdef FEAT_SEARCH_EXTRA
1890 || top_to_mod
1891#endif
1892 || idx >= wp->w_lines_valid
1893 || (row + wp->w_lines[idx].wl_size > bot_start)
1894 || (mod_top != 0
1895 && (lnum == mod_top
1896 || (lnum >= mod_top
1897 && (lnum < mod_bot
1898#ifdef FEAT_SYN_HL
1899 || did_update == DID_FOLD
1900 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001901 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 && (
1903# ifdef FEAT_FOLDING
1904 (foldmethodIsSyntax(wp)
1905 && hasAnyFolding(wp)) ||
1906# endif
1907 syntax_check_changed(lnum)))
1908#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001909#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001910 /* match in fixed position might need redraw
1911 * if lines were inserted or deleted */
1912 || (wp->w_match_head != NULL
1913 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 )))))
1916 {
1917#ifdef FEAT_SEARCH_EXTRA
1918 if (lnum == mod_top)
1919 top_to_mod = FALSE;
1920#endif
1921
1922 /*
1923 * When at start of changed lines: May scroll following lines
1924 * up or down to minimize redrawing.
1925 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001926 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 */
1928 if (lnum == mod_top
1929 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001930 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
1932 int old_rows = 0;
1933 int new_rows = 0;
1934 int xtra_rows;
1935 linenr_T l;
1936
1937 /* Count the old number of window rows, using w_lines[], which
1938 * should still contain the sizes for the lines as they are
1939 * currently displayed. */
1940 for (i = idx; i < wp->w_lines_valid; ++i)
1941 {
1942 /* Only valid lines have a meaningful wl_lnum. Invalid
1943 * lines are part of the changed area. */
1944 if (wp->w_lines[i].wl_valid
1945 && wp->w_lines[i].wl_lnum == mod_bot)
1946 break;
1947 old_rows += wp->w_lines[i].wl_size;
1948#ifdef FEAT_FOLDING
1949 if (wp->w_lines[i].wl_valid
1950 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1951 {
1952 /* Must have found the last valid entry above mod_bot.
1953 * Add following invalid entries. */
1954 ++i;
1955 while (i < wp->w_lines_valid
1956 && !wp->w_lines[i].wl_valid)
1957 old_rows += wp->w_lines[i++].wl_size;
1958 break;
1959 }
1960#endif
1961 }
1962
1963 if (i >= wp->w_lines_valid)
1964 {
1965 /* We can't find a valid line below the changed lines,
1966 * need to redraw until the end of the window.
1967 * Inserting/deleting lines has no use. */
1968 bot_start = 0;
1969 }
1970 else
1971 {
1972 /* Able to count old number of rows: Count new window
1973 * rows, and may insert/delete lines */
1974 j = idx;
1975 for (l = lnum; l < mod_bot; ++l)
1976 {
1977#ifdef FEAT_FOLDING
1978 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1979 ++new_rows;
1980 else
1981#endif
1982#ifdef FEAT_DIFF
1983 if (l == wp->w_topline)
1984 new_rows += plines_win_nofill(wp, l, TRUE)
1985 + wp->w_topfill;
1986 else
1987#endif
1988 new_rows += plines_win(wp, l, TRUE);
1989 ++j;
1990 if (new_rows > wp->w_height - row - 2)
1991 {
1992 /* it's getting too much, must redraw the rest */
1993 new_rows = 9999;
1994 break;
1995 }
1996 }
1997 xtra_rows = new_rows - old_rows;
1998 if (xtra_rows < 0)
1999 {
2000 /* May scroll text up. If there is not enough
2001 * remaining text or scrolling fails, must redraw the
2002 * rest. If scrolling works, must redraw the text
2003 * below the scrolled text. */
2004 if (row - xtra_rows >= wp->w_height - 2)
2005 mod_bot = MAXLNUM;
2006 else
2007 {
2008 check_for_delay(FALSE);
2009 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002010 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 mod_bot = MAXLNUM;
2012 else
2013 bot_start = wp->w_height + xtra_rows;
2014 }
2015 }
2016 else if (xtra_rows > 0)
2017 {
2018 /* May scroll text down. If there is not enough
2019 * remaining text of scrolling fails, must redraw the
2020 * rest. */
2021 if (row + xtra_rows >= wp->w_height - 2)
2022 mod_bot = MAXLNUM;
2023 else
2024 {
2025 check_for_delay(FALSE);
2026 if (win_ins_lines(wp, row + old_rows,
2027 xtra_rows, FALSE, FALSE) == FAIL)
2028 mod_bot = MAXLNUM;
2029 else if (top_end > row + old_rows)
2030 /* Scrolled the part at the top that requires
2031 * updating down. */
2032 top_end += xtra_rows;
2033 }
2034 }
2035
2036 /* When not updating the rest, may need to move w_lines[]
2037 * entries. */
2038 if (mod_bot != MAXLNUM && i != j)
2039 {
2040 if (j < i)
2041 {
2042 int x = row + new_rows;
2043
2044 /* move entries in w_lines[] upwards */
2045 for (;;)
2046 {
2047 /* stop at last valid entry in w_lines[] */
2048 if (i >= wp->w_lines_valid)
2049 {
2050 wp->w_lines_valid = j;
2051 break;
2052 }
2053 wp->w_lines[j] = wp->w_lines[i];
2054 /* stop at a line that won't fit */
2055 if (x + (int)wp->w_lines[j].wl_size
2056 > wp->w_height)
2057 {
2058 wp->w_lines_valid = j + 1;
2059 break;
2060 }
2061 x += wp->w_lines[j++].wl_size;
2062 ++i;
2063 }
2064 if (bot_start > x)
2065 bot_start = x;
2066 }
2067 else /* j > i */
2068 {
2069 /* move entries in w_lines[] downwards */
2070 j -= i;
2071 wp->w_lines_valid += j;
2072 if (wp->w_lines_valid > wp->w_height)
2073 wp->w_lines_valid = wp->w_height;
2074 for (i = wp->w_lines_valid; i - j >= idx; --i)
2075 wp->w_lines[i] = wp->w_lines[i - j];
2076
2077 /* The w_lines[] entries for inserted lines are
2078 * now invalid, but wl_size may be used above.
2079 * Reset to zero. */
2080 while (i >= idx)
2081 {
2082 wp->w_lines[i].wl_size = 0;
2083 wp->w_lines[i--].wl_valid = FALSE;
2084 }
2085 }
2086 }
2087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089
2090#ifdef FEAT_FOLDING
2091 /*
2092 * When lines are folded, display one line for all of them.
2093 * Otherwise, display normally (can be several display lines when
2094 * 'wrap' is on).
2095 */
2096 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2097 if (fold_count != 0)
2098 {
2099 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2100 ++row;
2101 --fold_count;
2102 wp->w_lines[idx].wl_folded = TRUE;
2103 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2104# ifdef FEAT_SYN_HL
2105 did_update = DID_FOLD;
2106# endif
2107 }
2108 else
2109#endif
2110 if (idx < wp->w_lines_valid
2111 && wp->w_lines[idx].wl_valid
2112 && wp->w_lines[idx].wl_lnum == lnum
2113 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002114 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 && srow + wp->w_lines[idx].wl_size > wp->w_height
2116#ifdef FEAT_DIFF
2117 && diff_check_fill(wp, lnum) == 0
2118#endif
2119 )
2120 {
2121 /* This line is not going to fit. Don't draw anything here,
2122 * will draw "@ " lines below. */
2123 row = wp->w_height + 1;
2124 }
2125 else
2126 {
2127#ifdef FEAT_SEARCH_EXTRA
2128 prepare_search_hl(wp, lnum);
2129#endif
2130#ifdef FEAT_SYN_HL
2131 /* Let the syntax stuff know we skipped a few lines. */
2132 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002133 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 syntax_end_parsing(syntax_last_parsed + 1);
2135#endif
2136
2137 /*
2138 * Display one line.
2139 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002140 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2141#ifdef SYN_TIME_LIMIT
2142 &syntax_tm
2143#else
2144 NULL
2145#endif
2146 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
2148#ifdef FEAT_FOLDING
2149 wp->w_lines[idx].wl_folded = FALSE;
2150 wp->w_lines[idx].wl_lastlnum = lnum;
2151#endif
2152#ifdef FEAT_SYN_HL
2153 did_update = DID_LINE;
2154 syntax_last_parsed = lnum;
2155#endif
2156 }
2157
2158 wp->w_lines[idx].wl_lnum = lnum;
2159 wp->w_lines[idx].wl_valid = TRUE;
2160 if (row > wp->w_height) /* past end of screen */
2161 {
2162 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002163 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2165 ++idx;
2166 break;
2167 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002168 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 wp->w_lines[idx].wl_size = row - srow;
2170 ++idx;
2171#ifdef FEAT_FOLDING
2172 lnum += fold_count + 1;
2173#else
2174 ++lnum;
2175#endif
2176 }
2177 else
2178 {
2179 /* This line does not need updating, advance to the next one */
2180 row += wp->w_lines[idx++].wl_size;
2181 if (row > wp->w_height) /* past end of screen */
2182 break;
2183#ifdef FEAT_FOLDING
2184 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2185#else
2186 ++lnum;
2187#endif
2188#ifdef FEAT_SYN_HL
2189 did_update = DID_NONE;
2190#endif
2191 }
2192
2193 if (lnum > buf->b_ml.ml_line_count)
2194 {
2195 eof = TRUE;
2196 break;
2197 }
2198 }
2199 /*
2200 * End of loop over all window lines.
2201 */
2202
2203
2204 if (idx > wp->w_lines_valid)
2205 wp->w_lines_valid = idx;
2206
2207#ifdef FEAT_SYN_HL
2208 /*
2209 * Let the syntax stuff know we stop parsing here.
2210 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002211 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 syntax_end_parsing(syntax_last_parsed + 1);
2213#endif
2214
2215 /*
2216 * If we didn't hit the end of the file, and we didn't finish the last
2217 * line we were working on, then the line didn't fit.
2218 */
2219 wp->w_empty_rows = 0;
2220#ifdef FEAT_DIFF
2221 wp->w_filler_rows = 0;
2222#endif
2223 if (!eof && !didline)
2224 {
2225 if (lnum == wp->w_topline)
2226 {
2227 /*
2228 * Single line that does not fit!
2229 * Don't overwrite it, it can be edited.
2230 */
2231 wp->w_botline = lnum + 1;
2232 }
2233#ifdef FEAT_DIFF
2234 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2235 {
2236 /* Window ends in filler lines. */
2237 wp->w_botline = lnum;
2238 wp->w_filler_rows = wp->w_height - srow;
2239 }
2240#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002241 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2242 {
2243 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2244
2245 /*
2246 * Last line isn't finished: Display "@@@" in the last screen line.
2247 */
2248 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002249 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002250 screen_fill(scr_row, scr_row + 1,
2251 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002252 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002253 set_empty_rows(wp, srow);
2254 wp->w_botline = lnum;
2255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2257 {
2258 /*
2259 * Last line isn't finished: Display "@@@" at the end.
2260 */
2261 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2262 W_WINROW(wp) + wp->w_height,
2263 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002264 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 set_empty_rows(wp, srow);
2266 wp->w_botline = lnum;
2267 }
2268 else
2269 {
2270 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2271 wp->w_botline = lnum;
2272 }
2273 }
2274 else
2275 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002276#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 draw_vsep_win(wp, row);
2278#endif
2279 if (eof) /* we hit the end of the file */
2280 {
2281 wp->w_botline = buf->b_ml.ml_line_count + 1;
2282#ifdef FEAT_DIFF
2283 j = diff_check_fill(wp, wp->w_botline);
2284 if (j > 0 && !wp->w_botfill)
2285 {
2286 /*
2287 * Display filler lines at the end of the file
2288 */
2289 if (char2cells(fill_diff) > 1)
2290 i = '-';
2291 else
2292 i = fill_diff;
2293 if (row + j > wp->w_height)
2294 j = wp->w_height - row;
2295 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2296 row += j;
2297 }
2298#endif
2299 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002300 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 wp->w_botline = lnum;
2302
2303 /* make sure the rest of the screen is blank */
2304 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002305 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 }
2307
2308 /* Reset the type of redrawing required, the window has been updated. */
2309 wp->w_redr_type = 0;
2310#ifdef FEAT_DIFF
2311 wp->w_old_topfill = wp->w_topfill;
2312 wp->w_old_botfill = wp->w_botfill;
2313#endif
2314
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002315 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 /*
2318 * There is a trick with w_botline. If we invalidate it on each
2319 * change that might modify it, this will cause a lot of expensive
2320 * calls to plines() in update_topline() each time. Therefore the
2321 * value of w_botline is often approximated, and this value is used to
2322 * compute the value of w_topline. If the value of w_botline was
2323 * wrong, check that the value of w_topline is correct (cursor is on
2324 * the visible part of the text). If it's not, we need to redraw
2325 * again. Mostly this just means scrolling up a few lines, so it
2326 * doesn't look too bad. Only do this for the current window (where
2327 * changes are relevant).
2328 */
2329 wp->w_valid |= VALID_BOTLINE;
2330 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2331 {
2332 recursive = TRUE;
2333 curwin->w_valid &= ~VALID_TOPLINE;
2334 update_topline(); /* may invalidate w_botline again */
2335 if (must_redraw != 0)
2336 {
2337 /* Don't update for changes in buffer again. */
2338 i = curbuf->b_mod_set;
2339 curbuf->b_mod_set = FALSE;
2340 win_update(curwin);
2341 must_redraw = 0;
2342 curbuf->b_mod_set = i;
2343 }
2344 recursive = FALSE;
2345 }
2346 }
2347
2348#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2349 /* restore got_int, unless CTRL-C was hit while redrawing */
2350 if (!got_int)
2351 got_int = save_got_int;
2352#endif
2353}
2354
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355/*
2356 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2357 * as the filler character.
2358 */
2359 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002360win_draw_end(
2361 win_T *wp,
2362 int c1,
2363 int c2,
2364 int row,
2365 int endrow,
2366 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
2368#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2369 int n = 0;
2370# define FDC_OFF n
2371#else
2372# define FDC_OFF 0
2373#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002374#ifdef FEAT_FOLDING
2375 int fdc = compute_foldcolumn(wp, 0);
2376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378#ifdef FEAT_RIGHTLEFT
2379 if (wp->w_p_rl)
2380 {
2381 /* No check for cmdline window: should never be right-left. */
2382# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002383 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385 if (n > 0)
2386 {
2387 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002388 if (n > W_WIDTH(wp))
2389 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2391 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002392 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
2394# endif
2395# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002396 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
2398 int nn = n + 2;
2399
2400 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002401 if (nn > W_WIDTH(wp))
2402 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2404 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002405 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 n = nn;
2407 }
2408# endif
2409 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2410 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002411 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2413 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002414 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 else
2417#endif
2418 {
2419#ifdef FEAT_CMDWIN
2420 if (cmdwin_type != 0 && wp == curwin)
2421 {
2422 /* draw the cmdline character in the leftmost column */
2423 n = 1;
2424 if (n > wp->w_width)
2425 n = wp->w_width;
2426 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2427 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002428 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430#endif
2431#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002432 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002434 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
2436 /* draw the fold column at the left */
2437 if (nn > W_WIDTH(wp))
2438 nn = W_WIDTH(wp);
2439 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2440 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002441 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 n = nn;
2443 }
2444#endif
2445#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002446 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
2448 int nn = n + 2;
2449
2450 /* draw the sign column after the fold column */
2451 if (nn > W_WIDTH(wp))
2452 nn = W_WIDTH(wp);
2453 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2454 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002455 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 n = nn;
2457 }
2458#endif
2459 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2460 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002461 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
2463 set_empty_rows(wp, row);
2464}
2465
Bram Moolenaar1a384422010-07-14 19:53:30 +02002466#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002467static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002468
2469/*
2470 * Advance **color_cols and return TRUE when there are columns to draw.
2471 */
2472 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002473advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002474{
2475 while (**color_cols >= 0 && vcol > **color_cols)
2476 ++*color_cols;
2477 return (**color_cols >= 0);
2478}
2479#endif
2480
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481#ifdef FEAT_FOLDING
2482/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002483 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2484 * space is available for window "wp", minus "col".
2485 */
2486 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002487compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002488{
2489 int fdc = wp->w_p_fdc;
2490 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2491 int wwidth = W_WIDTH(wp);
2492
2493 if (fdc > wwidth - (col + wmw))
2494 fdc = wwidth - (col + wmw);
2495 return fdc;
2496}
2497
2498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 * Display one folded line.
2500 */
2501 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002502fold_line(
2503 win_T *wp,
2504 long fold_count,
2505 foldinfo_T *foldinfo,
2506 linenr_T lnum,
2507 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002509 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 pos_T *top, *bot;
2511 linenr_T lnume = lnum + fold_count - 1;
2512 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002513 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 int col;
2516 int txtcol;
2517 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002518 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519
2520 /* Build the fold line:
2521 * 1. Add the cmdwin_type for the command-line window
2522 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002523 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * 4. Compose the text
2525 * 5. Add the text
2526 * 6. set highlighting for the Visual area an other text
2527 */
2528 col = 0;
2529
2530 /*
2531 * 1. Add the cmdwin_type for the command-line window
2532 * Ignores 'rightleft', this window is never right-left.
2533 */
2534#ifdef FEAT_CMDWIN
2535 if (cmdwin_type != 0 && wp == curwin)
2536 {
2537 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002538 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539#ifdef FEAT_MBYTE
2540 if (enc_utf8)
2541 ScreenLinesUC[off] = 0;
2542#endif
2543 ++col;
2544 }
2545#endif
2546
2547 /*
2548 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002549 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002551 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (fdc > 0)
2553 {
2554 fill_foldcolumn(buf, wp, TRUE, lnum);
2555#ifdef FEAT_RIGHTLEFT
2556 if (wp->w_p_rl)
2557 {
2558 int i;
2559
2560 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002561 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 /* reverse the fold column */
2563 for (i = 0; i < fdc; ++i)
2564 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2565 }
2566 else
2567#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002568 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 col += fdc;
2570 }
2571
2572#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002573# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2574 for (ri = 0; ri < l; ++ri) \
2575 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2576 else \
2577 for (ri = 0; ri < l; ++ri) \
2578 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002580# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2581 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582#endif
2583
Bram Moolenaar64486672010-05-16 15:46:46 +02002584 /* Set all attributes of the 'number' or 'relativenumber' column and the
2585 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002586 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
2588#ifdef FEAT_SIGNS
2589 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002590 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 {
2592 len = W_WIDTH(wp) - col;
2593 if (len > 0)
2594 {
2595 if (len > 2)
2596 len = 2;
2597# ifdef FEAT_RIGHTLEFT
2598 if (wp->w_p_rl)
2599 /* the line number isn't reversed */
2600 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002601 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 else
2603# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002604 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 col += len;
2606 }
2607 }
2608#endif
2609
2610 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002611 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002613 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 len = W_WIDTH(wp) - col;
2616 if (len > 0)
2617 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002618 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002619 long num;
2620 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002621
2622 if (len > w + 1)
2623 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002624
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002625 if (wp->w_p_nu && !wp->w_p_rnu)
2626 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002627 num = (long)lnum;
2628 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002629 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002630 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002631 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002632 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002633 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002634 /* 'number' + 'relativenumber': cursor line shows absolute
2635 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002636 num = lnum;
2637 fmt = "%-*ld ";
2638 }
2639 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002640
Bram Moolenaar700e7342013-01-30 12:31:36 +01002641 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642#ifdef FEAT_RIGHTLEFT
2643 if (wp->w_p_rl)
2644 /* the line number isn't reversed */
2645 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002646 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 else
2648#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002649 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 col += len;
2651 }
2652 }
2653
2654 /*
2655 * 4. Compose the folded-line string with 'foldtext', if set.
2656 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002657 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
2659 txtcol = col; /* remember where text starts */
2660
2661 /*
2662 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2663 * Right-left text is put in columns 0 - number-col, normal text is put
2664 * in columns number-col - window-width.
2665 */
2666#ifdef FEAT_MBYTE
2667 if (has_mbyte)
2668 {
2669 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002670 int u8c, u8cc[MAX_MCO];
2671 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 int idx;
2673 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002674 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675# ifdef FEAT_ARABIC
2676 int prev_c = 0; /* previous Arabic character */
2677 int prev_c1 = 0; /* first composing char for prev_c */
2678# endif
2679
2680# ifdef FEAT_RIGHTLEFT
2681 if (wp->w_p_rl)
2682 idx = off;
2683 else
2684# endif
2685 idx = off + col;
2686
2687 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2688 for (p = text; *p != NUL; )
2689 {
2690 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002691 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 if (col + cells > W_WIDTH(wp)
2693# ifdef FEAT_RIGHTLEFT
2694 - (wp->w_p_rl ? col : 0)
2695# endif
2696 )
2697 break;
2698 ScreenLines[idx] = *p;
2699 if (enc_utf8)
2700 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002701 u8c = utfc_ptr2char(p, u8cc);
2702 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
2704 ScreenLinesUC[idx] = 0;
2705#ifdef FEAT_ARABIC
2706 prev_c = u8c;
2707#endif
2708 }
2709 else
2710 {
2711#ifdef FEAT_ARABIC
2712 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2713 {
2714 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002715 int pc, pc1, nc;
2716 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 int firstbyte = *p;
2718
2719 /* The idea of what is the previous and next
2720 * character depends on 'rightleft'. */
2721 if (wp->w_p_rl)
2722 {
2723 pc = prev_c;
2724 pc1 = prev_c1;
2725 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002726 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728 else
2729 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002730 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002732 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
2734 prev_c = u8c;
2735
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002736 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 pc, pc1, nc);
2738 ScreenLines[idx] = firstbyte;
2739 }
2740 else
2741 prev_c = u8c;
2742#endif
2743 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002744#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (u8c >= 0x10000)
2746 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2747 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002750 for (i = 0; i < Screen_mco; ++i)
2751 {
2752 ScreenLinesC[i][idx] = u8cc[i];
2753 if (u8cc[i] == 0)
2754 break;
2755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757 if (cells > 1)
2758 ScreenLines[idx + 1] = 0;
2759 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002760 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2761 /* double-byte single width character */
2762 ScreenLines2[idx] = p[1];
2763 else if (cells > 1)
2764 /* double-width character */
2765 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 col += cells;
2767 idx += cells;
2768 p += c_len;
2769 }
2770 }
2771 else
2772#endif
2773 {
2774 len = (int)STRLEN(text);
2775 if (len > W_WIDTH(wp) - col)
2776 len = W_WIDTH(wp) - col;
2777 if (len > 0)
2778 {
2779#ifdef FEAT_RIGHTLEFT
2780 if (wp->w_p_rl)
2781 STRNCPY(current_ScreenLine, text, len);
2782 else
2783#endif
2784 STRNCPY(current_ScreenLine + col, text, len);
2785 col += len;
2786 }
2787 }
2788
2789 /* Fill the rest of the line with the fold filler */
2790#ifdef FEAT_RIGHTLEFT
2791 if (wp->w_p_rl)
2792 col -= txtcol;
2793#endif
2794 while (col < W_WIDTH(wp)
2795#ifdef FEAT_RIGHTLEFT
2796 - (wp->w_p_rl ? txtcol : 0)
2797#endif
2798 )
2799 {
2800#ifdef FEAT_MBYTE
2801 if (enc_utf8)
2802 {
2803 if (fill_fold >= 0x80)
2804 {
2805 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002806 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002807 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
2809 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002812 ScreenLines[off + col] = fill_fold;
2813 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002814 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002816 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002818 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
2820
2821 if (text != buf)
2822 vim_free(text);
2823
2824 /*
2825 * 6. set highlighting for the Visual area an other text.
2826 * If all folded lines are in the Visual area, highlight the line.
2827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2829 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002830 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
2832 /* Visual is after curwin->w_cursor */
2833 top = &curwin->w_cursor;
2834 bot = &VIsual;
2835 }
2836 else
2837 {
2838 /* Visual is before curwin->w_cursor */
2839 top = &VIsual;
2840 bot = &curwin->w_cursor;
2841 }
2842 if (lnum >= top->lnum
2843 && lnume <= bot->lnum
2844 && (VIsual_mode != 'v'
2845 || ((lnum > top->lnum
2846 || (lnum == top->lnum
2847 && top->col == 0))
2848 && (lnume < bot->lnum
2849 || (lnume == bot->lnum
2850 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002851 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
2853 if (VIsual_mode == Ctrl_V)
2854 {
2855 /* Visual block mode: highlight the chars part of the block */
2856 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2857 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002858 if (wp->w_old_cursor_lcol != MAXCOL
2859 && wp->w_old_cursor_lcol + txtcol
2860 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 len = wp->w_old_cursor_lcol;
2862 else
2863 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002864 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002865 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
2867 }
2868 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002871 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 }
2874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002876#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002877 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2878 if (wp->w_p_cc_cols)
2879 {
2880 int i = 0;
2881 int j = wp->w_p_cc_cols[i];
2882 int old_txtcol = txtcol;
2883
2884 while (j > -1)
2885 {
2886 txtcol += j;
2887 if (wp->w_p_wrap)
2888 txtcol -= wp->w_skipcol;
2889 else
2890 txtcol -= wp->w_leftcol;
2891 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2892 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002893 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002894 txtcol = old_txtcol;
2895 j = wp->w_p_cc_cols[++i];
2896 }
2897 }
2898
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002899 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002900 if (wp->w_p_cuc)
2901 {
2902 txtcol += wp->w_virtcol;
2903 if (wp->w_p_wrap)
2904 txtcol -= wp->w_skipcol;
2905 else
2906 txtcol -= wp->w_leftcol;
2907 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2908 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002909 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002910 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002913 screen_line(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 (int)W_WIDTH(wp), FALSE);
2915
2916 /*
2917 * Update w_cline_height and w_cline_folded if the cursor line was
2918 * updated (saves a call to plines() later).
2919 */
2920 if (wp == curwin
2921 && lnum <= curwin->w_cursor.lnum
2922 && lnume >= curwin->w_cursor.lnum)
2923 {
2924 curwin->w_cline_row = row;
2925 curwin->w_cline_height = 1;
2926 curwin->w_cline_folded = TRUE;
2927 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2928 }
2929}
2930
2931/*
2932 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2933 */
2934 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002935copy_text_attr(
2936 int off,
2937 char_u *buf,
2938 int len,
2939 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002941 int i;
2942
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 mch_memmove(ScreenLines + off, buf, (size_t)len);
2944# ifdef FEAT_MBYTE
2945 if (enc_utf8)
2946 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2947# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002948 for (i = 0; i < len; ++i)
2949 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951
2952/*
2953 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002954 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 */
2956 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002957fill_foldcolumn(
2958 char_u *p,
2959 win_T *wp,
2960 int closed, /* TRUE of FALSE */
2961 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
2963 int i = 0;
2964 int level;
2965 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002966 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002967 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
2969 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002970 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 level = win_foldinfo.fi_level;
2973 if (level > 0)
2974 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002975 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002976 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 /* If the column is too narrow, we start at the lowest level that
2979 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002980 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 if (first_level < 1)
2982 first_level = 1;
2983
Bram Moolenaar1c934292015-01-27 16:39:29 +01002984 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
2986 if (win_foldinfo.fi_lnum == lnum
2987 && first_level + i >= win_foldinfo.fi_low_level)
2988 p[i] = '-';
2989 else if (first_level == 1)
2990 p[i] = '|';
2991 else if (first_level + i <= 9)
2992 p[i] = '0' + first_level + i;
2993 else
2994 p[i] = '>';
2995 if (first_level + i == level)
2996 break;
2997 }
2998 }
2999 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003000 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001}
3002#endif /* FEAT_FOLDING */
3003
3004/*
3005 * Display line "lnum" of window 'wp' on the screen.
3006 * Start at row "startrow", stop when "endrow" is reached.
3007 * wp->w_virtcol needs to be valid.
3008 *
3009 * Return the number of last row the line occupies.
3010 */
3011 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003012win_line(
3013 win_T *wp,
3014 linenr_T lnum,
3015 int startrow,
3016 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003017 int nochange UNUSED, /* not updating for changed text */
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003018 proftime_T *syntax_tm UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003020 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3022 int c = 0; /* init for GCC */
3023 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003024#ifdef FEAT_LINEBREAK
3025 long vcol_sbr = -1; /* virtual column after showbreak */
3026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 long vcol_prev = -1; /* "vcol" of previous character */
3028 char_u *line; /* current line */
3029 char_u *ptr; /* current position in "line" */
3030 int row; /* row in the window, excl w_winrow */
3031 int screen_row; /* row on the screen, incl w_winrow */
3032
3033 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3034 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003035 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003036 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 int c_extra = NUL; /* extra chars, all the same */
3038 int extra_attr = 0; /* attributes when n_extra != 0 */
3039 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3040 displaying lcs_eol at end-of-line */
3041 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3042 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3043
3044 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3045 int saved_n_extra = 0;
3046 char_u *saved_p_extra = NULL;
3047 int saved_c_extra = 0;
3048 int saved_char_attr = 0;
3049
3050 int n_attr = 0; /* chars with special attr */
3051 int saved_attr2 = 0; /* char_attr saved for n_attr */
3052 int n_attr3 = 0; /* chars with overruling special attr */
3053 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3054
3055 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3056
3057 int fromcol, tocol; /* start/end of inverting */
3058 int fromcol_prev = -2; /* start of inverting after cursor */
3059 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003061 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 pos_T pos;
3063 long v;
3064
3065 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003066 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3068 in this line */
3069 int attr = 0; /* attributes for area highlighting */
3070 int area_attr = 0; /* attributes desired by highlighting */
3071 int search_attr = 0; /* attributes desired by 'hlsearch' */
3072#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003073 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 int syntax_attr = 0; /* attributes desired by syntax */
3075 int has_syntax = FALSE; /* this buffer has syntax highl. */
3076 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003077 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003078 int draw_color_col = FALSE; /* highlight colorcolumn */
3079 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003080#endif
3081#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003082 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003083# define SPWORDLEN 150
3084 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003085 int nextlinecol = 0; /* column where nextline[] starts */
3086 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003087 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003088 int spell_attr = 0; /* attributes desired by spelling */
3089 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003090 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3091 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003092 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003093 static int cap_col = -1; /* column to check for Cap word */
3094 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003095 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096#endif
3097 int extra_check; /* has syntax or linebreak */
3098#ifdef FEAT_MBYTE
3099 int multi_attr = 0; /* attributes desired by multibyte */
3100 int mb_l = 1; /* multi-byte byte length */
3101 int mb_c = 0; /* decoded multi-byte character */
3102 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003103 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104#endif
3105#ifdef FEAT_DIFF
3106 int filler_lines; /* nr of filler lines to be drawn */
3107 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003108 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 int change_start = MAXCOL; /* first col of changed area */
3110 int change_end = -1; /* last col of changed area */
3111#endif
3112 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3113#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003114 int need_showbreak = FALSE; /* overlong line, skipping first x
3115 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003117#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3118 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003120 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#endif
3122#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003123 matchitem_T *cur; /* points to the match list */
3124 match_T *shl; /* points to search_hl or a match */
3125 int shl_flag; /* flag to indicate whether search_hl
3126 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003127 int pos_inprogress; /* marks that position match search is
3128 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003129 int prevcol_hl_flag; /* flag to indicate whether prevcol
3130 equals startcol of search_hl or one
3131 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132#endif
3133#ifdef FEAT_ARABIC
3134 int prev_c = 0; /* previous Arabic character */
3135 int prev_c1 = 0; /* first composing char for prev_c */
3136#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003137#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003138 int did_line_attr = 0;
3139#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003140#ifdef FEAT_TERMINAL
3141 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003142 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144
3145 /* draw_state: items that are drawn in sequence: */
3146#define WL_START 0 /* nothing done yet */
3147#ifdef FEAT_CMDWIN
3148# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3149#else
3150# define WL_CMDLINE WL_START
3151#endif
3152#ifdef FEAT_FOLDING
3153# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3154#else
3155# define WL_FOLD WL_CMDLINE
3156#endif
3157#ifdef FEAT_SIGNS
3158# define WL_SIGN WL_FOLD + 1 /* column for signs */
3159#else
3160# define WL_SIGN WL_FOLD /* column for signs */
3161#endif
3162#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003163#ifdef FEAT_LINEBREAK
3164# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003166# define WL_BRI WL_NR
3167#endif
3168#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3169# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3170#else
3171# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172#endif
3173#define WL_LINE WL_SBR + 1 /* text in the line */
3174 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003175#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 int feedback_col = 0;
3177 int feedback_old_attr = -1;
3178#endif
3179
Bram Moolenaar860cae12010-06-05 23:22:07 +02003180#ifdef FEAT_CONCEAL
3181 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003182 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003183 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003184 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003185 int is_concealing = FALSE;
3186 int boguscols = 0; /* nonexistent columns added to force
3187 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003188 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003189 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003190 int match_conc = 0; /* cchar for match functions */
3191 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003192 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003193# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003194# define FIX_FOR_BOGUSCOLS \
3195 { \
3196 n_extra += vcol_off; \
3197 vcol -= vcol_off; \
3198 vcol_off = 0; \
3199 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003200 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003201 boguscols = 0; \
3202 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003203#else
3204# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
3207 if (startrow > endrow) /* past the end already! */
3208 return startrow;
3209
3210 row = startrow;
3211 screen_row = row + W_WINROW(wp);
3212
3213 /*
3214 * To speed up the loop below, set extra_check when there is linebreak,
3215 * trailing white space and/or syntax processing to be done.
3216 */
3217#ifdef FEAT_LINEBREAK
3218 extra_check = wp->w_p_lbr;
3219#else
3220 extra_check = 0;
3221#endif
3222#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003223 if (syntax_present(wp) && !wp->w_s->b_syn_error
3224# ifdef SYN_TIME_LIMIT
3225 && !wp->w_s->b_syn_slow
3226# endif
3227 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 {
3229 /* Prepare for syntax highlighting in this line. When there is an
3230 * error, stop syntax highlighting. */
3231 save_did_emsg = did_emsg;
3232 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003233 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003235 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 else
3237 {
3238 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003239#ifdef SYN_TIME_LIMIT
3240 if (!wp->w_s->b_syn_slow)
3241#endif
3242 {
3243 has_syntax = TRUE;
3244 extra_check = TRUE;
3245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003248
3249 /* Check for columns to display for 'colorcolumn'. */
3250 color_cols = wp->w_p_cc_cols;
3251 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003252 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003253#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003254
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003255#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003256 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003257 {
3258 extra_check = TRUE;
3259 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003260 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003261 }
3262#endif
3263
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003264#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003265 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003266 && *wp->w_s->b_p_spl != NUL
3267 && wp->w_s->b_langp.ga_len > 0
3268 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003269 {
3270 /* Prepare for spell checking. */
3271 has_spell = TRUE;
3272 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003273
3274 /* Get the start of the next line, so that words that wrap to the next
3275 * line are found too: "et<line-break>al.".
3276 * Trick: skip a few chars for C/shell/Vim comments */
3277 nextline[SPWORDLEN] = NUL;
3278 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3279 {
3280 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3281 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3282 }
3283
3284 /* When a word wrapped from the previous line the start of the current
3285 * line is valid. */
3286 if (lnum == checked_lnum)
3287 cur_checked_col = checked_col;
3288 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003289
3290 /* When there was a sentence end in the previous line may require a
3291 * word starting with capital in this line. In line 1 always check
3292 * the first word. */
3293 if (lnum != capcol_lnum)
3294 cap_col = -1;
3295 if (lnum == 1)
3296 cap_col = 0;
3297 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299#endif
3300
3301 /*
3302 * handle visual active in this window
3303 */
3304 fromcol = -10;
3305 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3307 {
3308 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003309 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
3311 top = &curwin->w_cursor;
3312 bot = &VIsual;
3313 }
3314 else /* Visual is before curwin->w_cursor */
3315 {
3316 top = &VIsual;
3317 bot = &curwin->w_cursor;
3318 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003319 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 if (VIsual_mode == Ctrl_V) /* block mode */
3321 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003322 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
3324 fromcol = wp->w_old_cursor_fcol;
3325 tocol = wp->w_old_cursor_lcol;
3326 }
3327 }
3328 else /* non-block mode */
3329 {
3330 if (lnum > top->lnum && lnum <= bot->lnum)
3331 fromcol = 0;
3332 else if (lnum == top->lnum)
3333 {
3334 if (VIsual_mode == 'V') /* linewise */
3335 fromcol = 0;
3336 else
3337 {
3338 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3339 if (gchar_pos(top) == NUL)
3340 tocol = fromcol + 1;
3341 }
3342 }
3343 if (VIsual_mode != 'V' && lnum == bot->lnum)
3344 {
3345 if (*p_sel == 'e' && bot->col == 0
3346#ifdef FEAT_VIRTUALEDIT
3347 && bot->coladd == 0
3348#endif
3349 )
3350 {
3351 fromcol = -10;
3352 tocol = MAXCOL;
3353 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003354 else if (bot->col == MAXCOL)
3355 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 else
3357 {
3358 pos = *bot;
3359 if (*p_sel == 'e')
3360 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3361 else
3362 {
3363 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3364 ++tocol;
3365 }
3366 }
3367 }
3368 }
3369
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 /* Check if the character under the cursor should not be inverted */
3371 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003372#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 )
3376 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
3378 /* if inverting in this line set area_highlighting */
3379 if (fromcol >= 0)
3380 {
3381 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003382 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003384 if ((clip_star.available && !clip_star.owned
3385 && clip_isautosel_star())
3386 || (clip_plus.available && !clip_plus.owned
3387 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003388 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389#endif
3390 }
3391 }
3392
3393 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003394 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003396 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 && wp == curwin
3398 && lnum >= curwin->w_cursor.lnum
3399 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3400 {
3401 if (lnum == curwin->w_cursor.lnum)
3402 getvcol(curwin, &(curwin->w_cursor),
3403 (colnr_T *)&fromcol, NULL, NULL);
3404 else
3405 fromcol = 0;
3406 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3407 {
3408 pos.lnum = lnum;
3409 pos.col = search_match_endcol;
3410 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3411 }
3412 else
3413 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003414 /* do at least one character; happens when past end of line */
3415 if (fromcol == tocol)
3416 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003418 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 }
3420
3421#ifdef FEAT_DIFF
3422 filler_lines = diff_check(wp, lnum);
3423 if (filler_lines < 0)
3424 {
3425 if (filler_lines == -1)
3426 {
3427 if (diff_find_change(wp, lnum, &change_start, &change_end))
3428 diff_hlf = HLF_ADD; /* added line */
3429 else if (change_start == 0)
3430 diff_hlf = HLF_TXD; /* changed text */
3431 else
3432 diff_hlf = HLF_CHD; /* changed line */
3433 }
3434 else
3435 diff_hlf = HLF_ADD; /* added line */
3436 filler_lines = 0;
3437 area_highlighting = TRUE;
3438 }
3439 if (lnum == wp->w_topline)
3440 filler_lines = wp->w_topfill;
3441 filler_todo = filler_lines;
3442#endif
3443
3444#ifdef LINE_ATTR
3445# ifdef FEAT_SIGNS
3446 /* If this line has a sign with line highlighting set line_attr. */
3447 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3448 if (v != 0)
3449 line_attr = sign_get_attr((int)v, TRUE);
3450# endif
3451# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3452 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003453 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003454 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455# endif
3456 if (line_attr != 0)
3457 area_highlighting = TRUE;
3458#endif
3459
3460 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3461 ptr = line;
3462
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003463#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003464 if (has_spell)
3465 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003466 /* For checking first word with a capital skip white space. */
3467 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003468 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003469
Bram Moolenaar30abd282005-06-22 22:35:10 +00003470 /* To be able to spell-check over line boundaries copy the end of the
3471 * current line into nextline[]. Above the start of the next line was
3472 * copied to nextline[SPWORDLEN]. */
3473 if (nextline[SPWORDLEN] == NUL)
3474 {
3475 /* No next line or it is empty. */
3476 nextlinecol = MAXCOL;
3477 nextline_idx = 0;
3478 }
3479 else
3480 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003481 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003482 if (v < SPWORDLEN)
3483 {
3484 /* Short line, use it completely and append the start of the
3485 * next line. */
3486 nextlinecol = 0;
3487 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003488 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003489 nextline_idx = v + 1;
3490 }
3491 else
3492 {
3493 /* Long line, use only the last SPWORDLEN bytes. */
3494 nextlinecol = v - SPWORDLEN;
3495 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3496 nextline_idx = SPWORDLEN + 1;
3497 }
3498 }
3499 }
3500#endif
3501
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003502 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003504 if (lcs_space || lcs_trail)
3505 extra_check = TRUE;
3506 /* find start of trailing whitespace */
3507 if (lcs_trail)
3508 {
3509 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003510 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003511 --trailcol;
3512 trailcol += (colnr_T) (ptr - line);
3513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515
3516 /*
3517 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3518 * first character to be displayed.
3519 */
3520 if (wp->w_p_wrap)
3521 v = wp->w_skipcol;
3522 else
3523 v = wp->w_leftcol;
3524 if (v > 0)
3525 {
3526#ifdef FEAT_MBYTE
3527 char_u *prev_ptr = ptr;
3528#endif
3529 while (vcol < v && *ptr != NUL)
3530 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003531 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 vcol += c;
3533#ifdef FEAT_MBYTE
3534 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003536 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003539 /* When:
3540 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003541 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003542 * - 'virtualedit' is set, or
3543 * - the visual mode is active,
3544 * the end of the line may be before the start of the displayed part.
3545 */
3546 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003547#ifdef FEAT_SYN_HL
3548 wp->w_p_cuc || draw_color_col ||
3549#endif
3550#ifdef FEAT_VIRTUALEDIT
3551 virtual_active() ||
3552#endif
3553 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
3558 /* Handle a character that's not completely on the screen: Put ptr at
3559 * that character but skip the first few screen characters. */
3560 if (vcol > v)
3561 {
3562 vcol -= c;
3563#ifdef FEAT_MBYTE
3564 ptr = prev_ptr;
3565#else
3566 --ptr;
3567#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003568 /* If the character fits on the screen, don't need to skip it.
3569 * Except for a TAB. */
3570 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003571#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003572 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003573#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003574 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003575 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577
3578 /*
3579 * Adjust for when the inverted text is before the screen,
3580 * and when the start of the inverted text is before the screen.
3581 */
3582 if (tocol <= vcol)
3583 fromcol = 0;
3584 else if (fromcol >= 0 && fromcol < vcol)
3585 fromcol = vcol;
3586
3587#ifdef FEAT_LINEBREAK
3588 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3589 if (wp->w_p_wrap)
3590 need_showbreak = TRUE;
3591#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003592#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003593 /* When spell checking a word we need to figure out the start of the
3594 * word and if it's badly spelled or not. */
3595 if (has_spell)
3596 {
3597 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003598 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003599 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003600
3601 pos = wp->w_cursor;
3602 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003603 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003604 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003605
3606 /* spell_move_to() may call ml_get() and make "line" invalid */
3607 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3608 ptr = line + linecol;
3609
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003610 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003611 {
3612 /* no bad word found at line start, don't check until end of a
3613 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003614 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003615 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003616 }
3617 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003618 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003619 /* bad word found, use attributes until end of word */
3620 word_end = wp->w_cursor.col + len + 1;
3621
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003622 /* Turn index into actual attributes. */
3623 if (spell_hlf != HLF_COUNT)
3624 spell_attr = highlight_attr[spell_hlf];
3625 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003626 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003627
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003628# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003629 /* Need to restart syntax highlighting for this line. */
3630 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003631 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003632# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003633 }
3634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
3636
3637 /*
3638 * Correct highlighting for cursor that can't be disabled.
3639 * Avoids having to check this for each character.
3640 */
3641 if (fromcol >= 0)
3642 {
3643 if (noinvcur)
3644 {
3645 if ((colnr_T)fromcol == wp->w_virtcol)
3646 {
3647 /* highlighting starts at cursor, let it start just after the
3648 * cursor */
3649 fromcol_prev = fromcol;
3650 fromcol = -1;
3651 }
3652 else if ((colnr_T)fromcol < wp->w_virtcol)
3653 /* restart highlighting after the cursor */
3654 fromcol_prev = wp->w_virtcol;
3655 }
3656 if (fromcol >= tocol)
3657 fromcol = -1;
3658 }
3659
3660#ifdef FEAT_SEARCH_EXTRA
3661 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003662 * Handle highlighting the last used search pattern and matches.
3663 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003665 cur = wp->w_match_head;
3666 shl_flag = FALSE;
3667 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003669 if (shl_flag == FALSE)
3670 {
3671 shl = &search_hl;
3672 shl_flag = TRUE;
3673 }
3674 else
3675 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003676 shl->startcol = MAXCOL;
3677 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003679 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003680 v = (long)(ptr - line);
3681 if (cur != NULL)
3682 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003683 next_search_hl(wp, shl, lnum, (colnr_T)v,
3684 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003685
3686 /* Need to get the line again, a multi-line regexp may have made it
3687 * invalid. */
3688 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3689 ptr = line + v;
3690
3691 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003693 if (shl->lnum == lnum)
3694 shl->startcol = shl->rm.startpos[0].col;
3695 else
3696 shl->startcol = 0;
3697 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3698 - shl->rm.startpos[0].lnum)
3699 shl->endcol = shl->rm.endpos[0].col;
3700 else
3701 shl->endcol = MAXCOL;
3702 /* Highlight one character for an empty match. */
3703 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003706 if (has_mbyte && line[shl->endcol] != NUL)
3707 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3708 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003710 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003712 if ((long)shl->startcol < v) /* match at leftcol */
3713 {
3714 shl->attr_cur = shl->attr;
3715 search_attr = shl->attr;
3716 }
3717 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003719 if (shl != &search_hl && cur != NULL)
3720 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722#endif
3723
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003724#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003725 /* Cursor line highlighting for 'cursorline' in the current window. Not
3726 * when Visual mode is active, because it's not clear what is selected
3727 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003728 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003729 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003730 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003731 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003732 area_highlighting = TRUE;
3733 }
3734#endif
3735
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003736 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 col = 0;
3738#ifdef FEAT_RIGHTLEFT
3739 if (wp->w_p_rl)
3740 {
3741 /* Rightleft window: process the text in the normal direction, but put
3742 * it in current_ScreenLine[] from right to left. Start at the
3743 * rightmost column of the window. */
3744 col = W_WIDTH(wp) - 1;
3745 off += col;
3746 }
3747#endif
3748
3749 /*
3750 * Repeat for the whole displayed line.
3751 */
3752 for (;;)
3753 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003754#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003755 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 /* Skip this quickly when working on the text. */
3758 if (draw_state != WL_LINE)
3759 {
3760#ifdef FEAT_CMDWIN
3761 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3762 {
3763 draw_state = WL_CMDLINE;
3764 if (cmdwin_type != 0 && wp == curwin)
3765 {
3766 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003768 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003769 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
3771 }
3772#endif
3773
3774#ifdef FEAT_FOLDING
3775 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3776 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003777 int fdc = compute_foldcolumn(wp, 0);
3778
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003780 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003782 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003783 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003784 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003785 p_extra_free = alloc(12 + 1);
3786
3787 if (p_extra_free != NULL)
3788 {
3789 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3790 n_extra = fdc;
3791 p_extra_free[n_extra] = NUL;
3792 p_extra = p_extra_free;
3793 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003794 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797 }
3798#endif
3799
3800#ifdef FEAT_SIGNS
3801 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3802 {
3803 draw_state = WL_SIGN;
3804 /* Show the sign column when there are any signs in this
3805 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003806 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003808 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003810 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811# endif
3812
3813 /* Draw two cells with the sign value or blank. */
3814 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003815 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 n_extra = 2;
3817
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003818 if (row == startrow
3819#ifdef FEAT_DIFF
3820 + filler_lines && filler_todo <= 0
3821#endif
3822 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
3824 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3825 SIGN_TEXT);
3826# ifdef FEAT_SIGN_ICONS
3827 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3828 SIGN_ICON);
3829 if (gui.in_use && icon_sign != 0)
3830 {
3831 /* Use the image in this position. */
3832 c_extra = SIGN_BYTE;
3833# ifdef FEAT_NETBEANS_INTG
3834 if (buf_signcount(wp->w_buffer, lnum) > 1)
3835 c_extra = MULTISIGN_BYTE;
3836# endif
3837 char_attr = icon_sign;
3838 }
3839 else
3840# endif
3841 if (text_sign != 0)
3842 {
3843 p_extra = sign_get_text(text_sign);
3844 if (p_extra != NULL)
3845 {
3846 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003847 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849 char_attr = sign_get_attr(text_sign, FALSE);
3850 }
3851 }
3852 }
3853 }
3854#endif
3855
3856 if (draw_state == WL_NR - 1 && n_extra == 0)
3857 {
3858 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003859 /* Display the absolute or relative line number. After the
3860 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3861 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 && (row == startrow
3863#ifdef FEAT_DIFF
3864 + filler_lines
3865#endif
3866 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3867 {
3868 /* Draw the line number (empty space after wrapping). */
3869 if (row == startrow
3870#ifdef FEAT_DIFF
3871 + filler_lines
3872#endif
3873 )
3874 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003875 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003876 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003877
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003878 if (wp->w_p_nu && !wp->w_p_rnu)
3879 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003880 num = (long)lnum;
3881 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003882 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003883 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003884 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003885 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003886 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003887 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003888 num = lnum;
3889 fmt = "%-*ld ";
3890 }
3891 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003892
Bram Moolenaar700e7342013-01-30 12:31:36 +01003893 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003894 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 if (wp->w_skipcol > 0)
3896 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3897 *p_extra = '-';
3898#ifdef FEAT_RIGHTLEFT
3899 if (wp->w_p_rl) /* reverse line numbers */
3900 rl_mirror(extra);
3901#endif
3902 p_extra = extra;
3903 c_extra = NUL;
3904 }
3905 else
3906 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003907 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003908 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003909#ifdef FEAT_SYN_HL
3910 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003911 * the current line differently.
3912 * TODO: Can we use CursorLine instead of CursorLineNr
3913 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003914 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003915 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003916 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
3919 }
3920
Bram Moolenaar597a4222014-06-25 14:39:50 +02003921#ifdef FEAT_LINEBREAK
3922 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3923 && n_extra == 0 && *p_sbr != NUL)
3924 /* draw indent after showbreak value */
3925 draw_state = WL_BRI;
3926 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3927 /* After the showbreak, draw the breakindent */
3928 draw_state = WL_BRI - 1;
3929
3930 /* draw 'breakindent': indent wrapped text accordingly */
3931 if (draw_state == WL_BRI - 1 && n_extra == 0)
3932 {
3933 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003934 /* if need_showbreak is set, breakindent also applies */
3935 if (wp->w_p_bri && n_extra == 0
3936 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003937# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003938 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003939# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003940 )
3941 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003942 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003943# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003944 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003945 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003946 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003947# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003948 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3949 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003950 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003951# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003952 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003953# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003954 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003955 c_extra = ' ';
3956 n_extra = get_breakindent_win(wp,
3957 ml_get_buf(wp->w_buffer, lnum, FALSE));
3958 /* Correct end of highlighted area for 'breakindent',
3959 * required when 'linebreak' is also set. */
3960 if (tocol == vcol)
3961 tocol += n_extra;
3962 }
3963 }
3964#endif
3965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3967 if (draw_state == WL_SBR - 1 && n_extra == 0)
3968 {
3969 draw_state = WL_SBR;
3970# ifdef FEAT_DIFF
3971 if (filler_todo > 0)
3972 {
3973 /* Draw "deleted" diff line(s). */
3974 if (char2cells(fill_diff) > 1)
3975 c_extra = '-';
3976 else
3977 c_extra = fill_diff;
3978# ifdef FEAT_RIGHTLEFT
3979 if (wp->w_p_rl)
3980 n_extra = col + 1;
3981 else
3982# endif
3983 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003984 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
3986# endif
3987# ifdef FEAT_LINEBREAK
3988 if (*p_sbr != NUL && need_showbreak)
3989 {
3990 /* Draw 'showbreak' at the start of each broken line. */
3991 p_extra = p_sbr;
3992 c_extra = NUL;
3993 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003994 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003996 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 /* Correct end of highlighted area for 'showbreak',
3998 * required when 'linebreak' is also set. */
3999 if (tocol == vcol)
4000 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004001#ifdef FEAT_SYN_HL
4002 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004003 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004004 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004005 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 }
4008# endif
4009 }
4010#endif
4011
4012 if (draw_state == WL_LINE - 1 && n_extra == 0)
4013 {
4014 draw_state = WL_LINE;
4015 if (saved_n_extra)
4016 {
4017 /* Continue item from end of wrapped line. */
4018 n_extra = saved_n_extra;
4019 c_extra = saved_c_extra;
4020 p_extra = saved_p_extra;
4021 char_attr = saved_char_attr;
4022 }
4023 else
4024 char_attr = 0;
4025 }
4026 }
4027
4028 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004029 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004030 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031#ifdef FEAT_DIFF
4032 && filler_todo <= 0
4033#endif
4034 )
4035 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004036 screen_line(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004037 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004038 /* Pretend we have finished updating the window. Except when
4039 * 'cursorcolumn' is set. */
4040#ifdef FEAT_SYN_HL
4041 if (wp->w_p_cuc)
4042 row = wp->w_cline_row + wp->w_cline_height;
4043 else
4044#endif
4045 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 break;
4047 }
4048
4049 if (draw_state == WL_LINE && area_highlighting)
4050 {
4051 /* handle Visual or match highlighting in this line */
4052 if (vcol == fromcol
4053#ifdef FEAT_MBYTE
4054 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4055 && (*mb_ptr2cells)(ptr) > 1)
4056#endif
4057 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004058 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 && vcol < tocol))
4060 area_attr = attr; /* start highlighting */
4061 else if (area_attr != 0
4062 && (vcol == tocol
4063 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066#ifdef FEAT_SEARCH_EXTRA
4067 if (!n_extra)
4068 {
4069 /*
4070 * Check for start/end of search pattern match.
4071 * After end, check for start/end of next match.
4072 * When another match, have to check for start again.
4073 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004074 * Do this for 'search_hl' and the match list (ordered by
4075 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004077 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004078 cur = wp->w_match_head;
4079 shl_flag = FALSE;
4080 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004082 if (shl_flag == FALSE
4083 && ((cur != NULL
4084 && cur->priority > SEARCH_HL_PRIORITY)
4085 || cur == NULL))
4086 {
4087 shl = &search_hl;
4088 shl_flag = TRUE;
4089 }
4090 else
4091 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004092 if (cur != NULL)
4093 cur->pos.cur = 0;
4094 pos_inprogress = TRUE;
4095 while (shl->rm.regprog != NULL
4096 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004098 if (shl->startcol != MAXCOL
4099 && v >= (long)shl->startcol
4100 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004102#ifdef FEAT_MBYTE
4103 int tmp_col = v + MB_PTR2LEN(ptr);
4104
4105 if (shl->endcol < tmp_col)
4106 shl->endcol = tmp_col;
4107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004109#ifdef FEAT_CONCEAL
4110 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4111 == cur->hlg_id)
4112 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004113 has_match_conc =
4114 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004115 match_conc = cur->conceal_char;
4116 }
4117 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004118 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004121 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 {
4123 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004124 next_search_hl(wp, shl, lnum, (colnr_T)v,
4125 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004126 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004127 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
4129 /* Need to get the line again, a multi-line regexp
4130 * may have made it invalid. */
4131 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4132 ptr = line + v;
4133
4134 if (shl->lnum == lnum)
4135 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004136 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004138 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004140 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004142 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 {
4144 /* highlight empty match, try again after
4145 * it */
4146#ifdef FEAT_MBYTE
4147 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004148 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004149 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 else
4151#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004152 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
4154
4155 /* Loop to check if the match starts at the
4156 * current position */
4157 continue;
4158 }
4159 }
4160 break;
4161 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004162 if (shl != &search_hl && cur != NULL)
4163 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004165
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004166 /* Use attributes from match with highest priority among
4167 * 'search_hl' and the match list. */
4168 search_attr = search_hl.attr_cur;
4169 cur = wp->w_match_head;
4170 shl_flag = FALSE;
4171 while (cur != NULL || shl_flag == FALSE)
4172 {
4173 if (shl_flag == FALSE
4174 && ((cur != NULL
4175 && cur->priority > SEARCH_HL_PRIORITY)
4176 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004177 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004178 shl = &search_hl;
4179 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004180 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004181 else
4182 shl = &cur->hl;
4183 if (shl->attr_cur != 0)
4184 search_attr = shl->attr_cur;
4185 if (shl != &search_hl && cur != NULL)
4186 cur = cur->next;
4187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189#endif
4190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004192 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004194 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4195 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004197 if (diff_hlf == HLF_TXD && ptr - line > change_end
4198 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004200 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004201 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004202 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004205
4206 /* Decide which of the highlight attributes to use. */
4207 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004208#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004209 if (area_attr != 0)
4210 char_attr = hl_combine_attr(line_attr, area_attr);
4211 else if (search_attr != 0)
4212 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004213 /* Use line_attr when not in the Visual or 'incsearch' area
4214 * (area_attr may be 0 when "noinvcur" is set). */
4215 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004216 || vcol < fromcol || vcol_prev < fromcol_prev
4217 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004218 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004219#else
4220 if (area_attr != 0)
4221 char_attr = area_attr;
4222 else if (search_attr != 0)
4223 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004224#endif
4225 else
4226 {
4227 attr_pri = FALSE;
4228#ifdef FEAT_SYN_HL
4229 if (has_syntax)
4230 char_attr = syntax_attr;
4231 else
4232#endif
4233 char_attr = 0;
4234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236
4237 /*
4238 * Get the next character to put on the screen.
4239 */
4240 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004241 * The "p_extra" points to the extra stuff that is inserted to
4242 * represent special characters (non-printable stuff) and other
4243 * things. When all characters are the same, c_extra is used.
4244 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4245 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4247 */
4248 if (n_extra > 0)
4249 {
4250 if (c_extra != NUL)
4251 {
4252 c = c_extra;
4253#ifdef FEAT_MBYTE
4254 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004255 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
4257 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004258 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004259 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261 else
4262 mb_utf8 = FALSE;
4263#endif
4264 }
4265 else
4266 {
4267 c = *p_extra;
4268#ifdef FEAT_MBYTE
4269 if (has_mbyte)
4270 {
4271 mb_c = c;
4272 if (enc_utf8)
4273 {
4274 /* If the UTF-8 character is more than one byte:
4275 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004276 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 mb_utf8 = FALSE;
4278 if (mb_l > n_extra)
4279 mb_l = 1;
4280 else if (mb_l > 1)
4281 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004282 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004284 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 }
4287 else
4288 {
4289 /* if this is a DBCS character, put it in "mb_c" */
4290 mb_l = MB_BYTE2LEN(c);
4291 if (mb_l >= n_extra)
4292 mb_l = 1;
4293 else if (mb_l > 1)
4294 mb_c = (c << 8) + p_extra[1];
4295 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004296 if (mb_l == 0) /* at the NUL at end-of-line */
4297 mb_l = 1;
4298
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 /* If a double-width char doesn't fit display a '>' in the
4300 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004301 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302# ifdef FEAT_RIGHTLEFT
4303 wp->w_p_rl ? (col <= 0) :
4304# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004305 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 && (*mb_char2cells)(mb_c) == 2)
4307 {
4308 c = '>';
4309 mb_c = c;
4310 mb_l = 1;
4311 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004312 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 /* put the pointer back to output the double-width
4314 * character at the start of the next line. */
4315 ++n_extra;
4316 --p_extra;
4317 }
4318 else
4319 {
4320 n_extra -= mb_l - 1;
4321 p_extra += mb_l - 1;
4322 }
4323 }
4324#endif
4325 ++p_extra;
4326 }
4327 --n_extra;
4328 }
4329 else
4330 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004331#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004332 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004333#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004334
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004335 if (p_extra_free != NULL)
4336 {
4337 vim_free(p_extra_free);
4338 p_extra_free = NULL;
4339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 /*
4341 * Get a character from the line itself.
4342 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004343 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004344#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004345 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347#ifdef FEAT_MBYTE
4348 if (has_mbyte)
4349 {
4350 mb_c = c;
4351 if (enc_utf8)
4352 {
4353 /* If the UTF-8 character is more than one byte: Decode it
4354 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004355 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 mb_utf8 = FALSE;
4357 if (mb_l > 1)
4358 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004359 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 /* Overlong encoded ASCII or ASCII with composing char
4361 * is displayed normally, except a NUL. */
4362 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004363 {
4364 c = mb_c;
4365# ifdef FEAT_LINEBREAK
4366 c0 = mb_c;
4367# endif
4368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004370
4371 /* At start of the line we can have a composing char.
4372 * Draw it as a space with a composing char. */
4373 if (utf_iscomposing(mb_c))
4374 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004375 int i;
4376
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004377 for (i = Screen_mco - 1; i > 0; --i)
4378 u8cc[i] = u8cc[i - 1];
4379 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004380 mb_c = ' ';
4381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383
4384 if ((mb_l == 1 && c >= 0x80)
4385 || (mb_l >= 1 && mb_c == 0)
4386 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004387# ifdef UNICODE16
4388 || mb_c >= 0x10000
4389# endif
4390 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 {
4392 /*
4393 * Illegal UTF-8 byte: display as <xx>.
4394 * Non-BMP character : display as ? or fullwidth ?.
4395 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004396# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
4400 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004401# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 if (wp->w_p_rl) /* reverse */
4403 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004406# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 else if (utf_char2cells(mb_c) != 2)
4408 STRCPY(extra, "?");
4409 else
4410 /* 0xff1f in UTF-8: full-width '?' */
4411 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414 p_extra = extra;
4415 c = *p_extra;
4416 mb_c = mb_ptr2char_adv(&p_extra);
4417 mb_utf8 = (c >= 0x80);
4418 n_extra = (int)STRLEN(p_extra);
4419 c_extra = NUL;
4420 if (area_attr == 0 && search_attr == 0)
4421 {
4422 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004423 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 saved_attr2 = char_attr; /* save current attr */
4425 }
4426 }
4427 else if (mb_l == 0) /* at the NUL at end-of-line */
4428 mb_l = 1;
4429#ifdef FEAT_ARABIC
4430 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4431 {
4432 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004433 int pc, pc1, nc;
4434 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436 /* The idea of what is the previous and next
4437 * character depends on 'rightleft'. */
4438 if (wp->w_p_rl)
4439 {
4440 pc = prev_c;
4441 pc1 = prev_c1;
4442 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004443 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445 else
4446 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004447 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004449 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 }
4451 prev_c = mb_c;
4452
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004453 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
4455 else
4456 prev_c = mb_c;
4457#endif
4458 }
4459 else /* enc_dbcs */
4460 {
4461 mb_l = MB_BYTE2LEN(c);
4462 if (mb_l == 0) /* at the NUL at end-of-line */
4463 mb_l = 1;
4464 else if (mb_l > 1)
4465 {
4466 /* We assume a second byte below 32 is illegal.
4467 * Hopefully this is OK for all double-byte encodings!
4468 */
4469 if (ptr[1] >= 32)
4470 mb_c = (c << 8) + ptr[1];
4471 else
4472 {
4473 if (ptr[1] == NUL)
4474 {
4475 /* head byte at end of line */
4476 mb_l = 1;
4477 transchar_nonprint(extra, c);
4478 }
4479 else
4480 {
4481 /* illegal tail byte */
4482 mb_l = 2;
4483 STRCPY(extra, "XX");
4484 }
4485 p_extra = extra;
4486 n_extra = (int)STRLEN(extra) - 1;
4487 c_extra = NUL;
4488 c = *p_extra++;
4489 if (area_attr == 0 && search_attr == 0)
4490 {
4491 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004492 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 saved_attr2 = char_attr; /* save current attr */
4494 }
4495 mb_c = c;
4496 }
4497 }
4498 }
4499 /* If a double-width char doesn't fit display a '>' in the
4500 * last column; the character is displayed at the start of the
4501 * next line. */
4502 if ((
4503# ifdef FEAT_RIGHTLEFT
4504 wp->w_p_rl ? (col <= 0) :
4505# endif
4506 (col >= W_WIDTH(wp) - 1))
4507 && (*mb_char2cells)(mb_c) == 2)
4508 {
4509 c = '>';
4510 mb_c = c;
4511 mb_utf8 = FALSE;
4512 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004513 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 /* Put pointer back so that the character will be
4515 * displayed at the start of the next line. */
4516 --ptr;
4517 }
4518 else if (*ptr != NUL)
4519 ptr += mb_l - 1;
4520
4521 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004522 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004523 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004524 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004527 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 c = ' ';
4529 if (area_attr == 0 && search_attr == 0)
4530 {
4531 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004532 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 saved_attr2 = char_attr; /* save current attr */
4534 }
4535 mb_c = c;
4536 mb_utf8 = FALSE;
4537 mb_l = 1;
4538 }
4539
4540 }
4541#endif
4542 ++ptr;
4543
4544 if (extra_check)
4545 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004546#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004547 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004548#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004549
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004550#ifdef FEAT_TERMINAL
4551 if (get_term_attr)
4552 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004553 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004554
4555 if (!attr_pri)
4556 char_attr = syntax_attr;
4557 else
4558 char_attr = hl_combine_attr(syntax_attr, char_attr);
4559 }
4560#endif
4561
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004562#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 /* Get syntax attribute, unless still at the start of the line
4564 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004565 v = (long)(ptr - line);
4566 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 {
4568 /* Get the syntax attribute for the character. If there
4569 * is an error, disable syntax highlighting. */
4570 save_did_emsg = did_emsg;
4571 did_emsg = FALSE;
4572
Bram Moolenaar217ad922005-03-20 22:37:15 +00004573 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004574# ifdef FEAT_SPELL
4575 has_spell ? &can_spell :
4576# endif
4577 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
4579 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004580 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004581 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004582 has_syntax = FALSE;
4583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 else
4585 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004586#ifdef SYN_TIME_LIMIT
4587 if (wp->w_s->b_syn_slow)
4588 has_syntax = FALSE;
4589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
4591 /* Need to get the line again, a multi-line regexp may
4592 * have made it invalid. */
4593 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4594 ptr = line + v;
4595
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004596 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004598 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004599 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004600# ifdef FEAT_CONCEAL
4601 /* no concealing past the end of the line, it interferes
4602 * with line highlighting */
4603 if (c == NUL)
4604 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004605 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004606 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004609#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004610
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004611#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004612 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004613 * Only do this when there is no syntax highlighting, the
4614 * @Spell cluster is not used or the current syntax item
4615 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004616 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004617 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004618 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004619# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004620 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004621 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004622# endif
4623 if (c != 0 && (
4624# ifdef FEAT_SYN_HL
4625 !has_syntax ||
4626# endif
4627 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004628 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004629 char_u *prev_ptr, *p;
4630 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004631 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004632# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004633 if (has_mbyte)
4634 {
4635 prev_ptr = ptr - mb_l;
4636 v -= mb_l - 1;
4637 }
4638 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004639# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004640 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004641
4642 /* Use nextline[] if possible, it has the start of the
4643 * next line concatenated. */
4644 if ((prev_ptr - line) - nextlinecol >= 0)
4645 p = nextline + (prev_ptr - line) - nextlinecol;
4646 else
4647 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004648 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004649 len = spell_check(wp, p, &spell_hlf, &cap_col,
4650 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004651 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004652
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004653 /* In Insert mode only highlight a word that
4654 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004655 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004656 && (State & INSERT) != 0
4657 && wp->w_cursor.lnum == lnum
4658 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004659 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004660 && wp->w_cursor.col < (colnr_T)word_end)
4661 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004662 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004663 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004664 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004665
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004666 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004667 && (p - nextline) + len > nextline_idx)
4668 {
4669 /* Remember that the good word continues at the
4670 * start of the next line. */
4671 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004672 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004673 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004674
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004675 /* Turn index into actual attributes. */
4676 if (spell_hlf != HLF_COUNT)
4677 spell_attr = highlight_attr[spell_hlf];
4678
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004679 if (cap_col > 0)
4680 {
4681 if (p != prev_ptr
4682 && (p - nextline) + cap_col >= nextline_idx)
4683 {
4684 /* Remember that the word in the next line
4685 * must start with a capital. */
4686 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004687 cap_col = (int)((p - nextline) + cap_col
4688 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004689 }
4690 else
4691 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004692 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004693 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004694 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004695 }
4696 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004697 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004698 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004699 char_attr = hl_combine_attr(char_attr, spell_attr);
4700 else
4701 char_attr = hl_combine_attr(spell_attr, char_attr);
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703#endif
4704#ifdef FEAT_LINEBREAK
4705 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004706 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004708 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004709 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004711# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004712 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004713# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004714 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004716 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004718 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004719
Bram Moolenaar597a4222014-06-25 14:39:50 +02004720 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004721 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004722 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004723 if (c == TAB && n_extra + col > W_WIDTH(wp))
4724 n_extra = (int)wp->w_buffer->b_p_ts
4725 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4726
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004727# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004728 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004729# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004731# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004732 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004733 {
4734#ifdef FEAT_CONCEAL
4735 if (c == TAB)
4736 /* See "Tab alignment" below. */
4737 FIX_FOR_BOGUSCOLS;
4738#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004739 if (!wp->w_p_list)
4740 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
4743#endif
4744
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004745 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4746 */
4747 if (wp->w_p_list
4748 && (((c == 160
4749#ifdef FEAT_MBYTE
4750 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4751#endif
4752 ) && lcs_nbsp)
4753 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4754 {
4755 c = (c == ' ') ? lcs_space : lcs_nbsp;
4756 if (area_attr == 0 && search_attr == 0)
4757 {
4758 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004759 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004760 saved_attr2 = char_attr; /* save current attr */
4761 }
4762#ifdef FEAT_MBYTE
4763 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004764 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004765 {
4766 mb_utf8 = TRUE;
4767 u8cc[0] = 0;
4768 c = 0xc0;
4769 }
4770 else
4771 mb_utf8 = FALSE;
4772#endif
4773 }
4774
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4776 {
4777 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004778 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 {
4780 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004781 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 saved_attr2 = char_attr; /* save current attr */
4783 }
4784#ifdef FEAT_MBYTE
4785 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004786 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
4788 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004789 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004790 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 else
4793 mb_utf8 = FALSE;
4794#endif
4795 }
4796 }
4797
4798 /*
4799 * Handling of non-printable characters.
4800 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004801 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 {
4803 /*
4804 * when getting a character from the file, we may have to
4805 * turn it into something else on the way to putting it
4806 * into "ScreenLines".
4807 */
4808 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4809 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004810 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004811 long vcol_adjusted = vcol; /* removed showbreak length */
4812#ifdef FEAT_LINEBREAK
4813 /* only adjust the tab_len, when at the first column
4814 * after the showbreak value was drawn */
4815 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4816 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004819 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004820 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4821
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004822#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004823 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004824#endif
4825 /* tab amount depends on current column */
4826 n_extra = tab_len;
4827#ifdef FEAT_LINEBREAK
4828 else
4829 {
4830 char_u *p;
4831 int len = n_extra;
4832 int i;
4833 int saved_nextra = n_extra;
4834
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004835#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004836 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004837 /* there are characters to conceal */
4838 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004839 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4840 */
4841 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4842 && n_extra > tab_len)
4843 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004844#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004845
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004846 /* if n_extra > 0, it gives the number of chars, to
4847 * use for a tab, else we need to calculate the width
4848 * for a tab */
4849#ifdef FEAT_MBYTE
4850 len = (tab_len * mb_char2len(lcs_tab2));
4851 if (n_extra > 0)
4852 len += n_extra - tab_len;
4853#endif
4854 c = lcs_tab1;
4855 p = alloc((unsigned)(len + 1));
4856 vim_memset(p, ' ', len);
4857 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004858 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004859 p_extra_free = p;
4860 for (i = 0; i < tab_len; i++)
4861 {
4862#ifdef FEAT_MBYTE
4863 mb_char2bytes(lcs_tab2, p);
4864 p += mb_char2len(lcs_tab2);
4865 n_extra += mb_char2len(lcs_tab2)
4866 - (saved_nextra > 0 ? 1 : 0);
4867#else
4868 p[i] = lcs_tab2;
4869#endif
4870 }
4871 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004872#ifdef FEAT_CONCEAL
4873 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4874 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004875 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004876 n_extra -= vcol_off;
4877#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004878 }
4879#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004880#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004881 {
4882 int vc_saved = vcol_off;
4883
4884 /* Tab alignment should be identical regardless of
4885 * 'conceallevel' value. So tab compensates of all
4886 * previous concealed characters, and thus resets
4887 * vcol_off and boguscols accumulated so far in the
4888 * line. Note that the tab can be longer than
4889 * 'tabstop' when there are concealed characters. */
4890 FIX_FOR_BOGUSCOLS;
4891
4892 /* Make sure, the highlighting for the tab char will be
4893 * correctly set further below (effectively reverts the
4894 * FIX_FOR_BOGSUCOLS macro */
4895 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004896 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004897 tab_len += vc_saved;
4898 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900#ifdef FEAT_MBYTE
4901 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4902#endif
4903 if (wp->w_p_list)
4904 {
4905 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004906#ifdef FEAT_LINEBREAK
4907 if (wp->w_p_lbr)
4908 c_extra = NUL; /* using p_extra from above */
4909 else
4910#endif
4911 c_extra = lcs_tab2;
4912 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004913 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 saved_attr2 = char_attr; /* save current attr */
4915#ifdef FEAT_MBYTE
4916 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004917 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
4919 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004920 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004921 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923#endif
4924 }
4925 else
4926 {
4927 c_extra = ' ';
4928 c = ' ';
4929 }
4930 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004931 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004932 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004933 || ((fromcol >= 0 || fromcol_prev >= 0)
4934 && tocol > vcol
4935 && VIsual_mode != Ctrl_V
4936 && (
4937# ifdef FEAT_RIGHTLEFT
4938 wp->w_p_rl ? (col >= 0) :
4939# endif
4940 (col < W_WIDTH(wp)))
4941 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004942 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004943 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004944 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004946 /* Display a '$' after the line or highlight an extra
4947 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4949 /* For a diff line the highlighting continues after the
4950 * "$". */
4951 if (
4952# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004953 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954# ifdef LINE_ATTR
4955 &&
4956# endif
4957# endif
4958# ifdef LINE_ATTR
4959 line_attr == 0
4960# endif
4961 )
4962#endif
4963 {
4964#ifdef FEAT_VIRTUALEDIT
4965 /* In virtualedit, visual selections may extend
4966 * beyond end of line. */
4967 if (area_highlighting && virtual_active()
4968 && tocol != MAXCOL && vcol < tocol)
4969 n_extra = 0;
4970 else
4971#endif
4972 {
4973 p_extra = at_end_str;
4974 n_extra = 1;
4975 c_extra = NUL;
4976 }
4977 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004978 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004979 c = lcs_eol;
4980 else
4981 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 lcs_eol_one = -1;
4983 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004984 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004986 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 n_attr = 1;
4988 }
4989#ifdef FEAT_MBYTE
4990 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004991 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
4993 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004994 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004995 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997 else
4998 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4999#endif
5000 }
5001 else if (c != NUL)
5002 {
5003 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005004 if (n_extra == 0)
5005 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006#ifdef FEAT_RIGHTLEFT
5007 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5008 rl_mirror(p_extra); /* reverse "<12>" */
5009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005011#ifdef FEAT_LINEBREAK
5012 if (wp->w_p_lbr)
5013 {
5014 char_u *p;
5015
5016 c = *p_extra;
5017 p = alloc((unsigned)n_extra + 1);
5018 vim_memset(p, ' ', n_extra);
5019 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5020 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005021 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005022 p_extra_free = p_extra = p;
5023 }
5024 else
5025#endif
5026 {
5027 n_extra = byte2cells(c) - 1;
5028 c = *p_extra++;
5029 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005030 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
5032 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005033 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 saved_attr2 = char_attr; /* save current attr */
5035 }
5036#ifdef FEAT_MBYTE
5037 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5038#endif
5039 }
5040#ifdef FEAT_VIRTUALEDIT
5041 else if (VIsual_active
5042 && (VIsual_mode == Ctrl_V
5043 || VIsual_mode == 'v')
5044 && virtual_active()
5045 && tocol != MAXCOL
5046 && vcol < tocol
5047 && (
5048# ifdef FEAT_RIGHTLEFT
5049 wp->w_p_rl ? (col >= 0) :
5050# endif
5051 (col < W_WIDTH(wp))))
5052 {
5053 c = ' ';
5054 --ptr; /* put it back at the NUL */
5055 }
5056#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005057#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 else if ((
5059# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005060 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005062# ifdef FEAT_TERMINAL
5063 term_attr != 0 ||
5064# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 ) && (
5067# ifdef FEAT_RIGHTLEFT
5068 wp->w_p_rl ? (col >= 0) :
5069# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005070 (col
5071# ifdef FEAT_CONCEAL
5072 - boguscols
5073# endif
5074 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
5076 /* Highlight until the right side of the window */
5077 c = ' ';
5078 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005079
5080 /* Remember we do the char for line highlighting. */
5081 ++did_line_attr;
5082
5083 /* don't do search HL for the rest of the line */
5084 if (line_attr != 0 && char_attr == search_attr && col > 0)
5085 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086# ifdef FEAT_DIFF
5087 if (diff_hlf == HLF_TXD)
5088 {
5089 diff_hlf = HLF_CHD;
5090 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005091 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005092 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005093 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5094 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005095 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005099# ifdef FEAT_TERMINAL
5100 if (term_attr != 0)
5101 {
5102 char_attr = term_attr;
5103 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5104 char_attr = hl_combine_attr(char_attr,
5105 HL_ATTR(HLF_CUL));
5106 }
5107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109#endif
5110 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005111
5112#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005113 if ( wp->w_p_cole > 0
5114 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005115 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005116 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005117 && !(lnum_in_visual_area
5118 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005119 {
5120 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005121 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005122 && (syn_get_sub_char() != NUL || match_conc
5123 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005124 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005125 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005126 /* First time at this concealed item: display one
5127 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005128 if (match_conc)
5129 c = match_conc;
5130 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005131 c = syn_get_sub_char();
5132 else if (lcs_conceal != NUL)
5133 c = lcs_conceal;
5134 else
5135 c = ' ';
5136
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005137 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005138
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005139 if (n_extra > 0)
5140 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005141 vcol += n_extra;
5142 if (wp->w_p_wrap && n_extra > 0)
5143 {
5144# ifdef FEAT_RIGHTLEFT
5145 if (wp->w_p_rl)
5146 {
5147 col -= n_extra;
5148 boguscols -= n_extra;
5149 }
5150 else
5151# endif
5152 {
5153 boguscols += n_extra;
5154 col += n_extra;
5155 }
5156 }
5157 n_extra = 0;
5158 n_attr = 0;
5159 }
5160 else if (n_skip == 0)
5161 {
5162 is_concealing = TRUE;
5163 n_skip = 1;
5164 }
5165# ifdef FEAT_MBYTE
5166 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005167 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005168 {
5169 mb_utf8 = TRUE;
5170 u8cc[0] = 0;
5171 c = 0xc0;
5172 }
5173 else
5174 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5175# endif
5176 }
5177 else
5178 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005179 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005180 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005181 }
5182#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
5184
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005185#ifdef FEAT_CONCEAL
5186 /* In the cursor line and we may be concealing characters: correct
5187 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005188 if (!did_wcol && draw_state == WL_LINE
5189 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005190 && conceal_cursor_line(wp)
5191 && (int)wp->w_virtcol <= vcol + n_skip)
5192 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005193# ifdef FEAT_RIGHTLEFT
5194 if (wp->w_p_rl)
5195 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5196 else
5197# endif
5198 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005199 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005200 did_wcol = TRUE;
5201 }
5202#endif
5203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 /* Don't override visual selection highlighting. */
5205 if (n_attr > 0
5206 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005207 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 char_attr = extra_attr;
5209
Bram Moolenaar81695252004-12-29 20:58:21 +00005210#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 /* XIM don't send preedit_start and preedit_end, but they send
5212 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5213 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005214 if (p_imst == IM_ON_THE_SPOT
5215 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005216 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 && (State & INSERT)
5218 && !p_imdisable
5219 && im_is_preediting()
5220 && draw_state == WL_LINE)
5221 {
5222 colnr_T tcol;
5223
5224 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005225 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 else
5227 tcol = preedit_end_col;
5228 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5229 {
5230 if (feedback_old_attr < 0)
5231 {
5232 feedback_col = 0;
5233 feedback_old_attr = char_attr;
5234 }
5235 char_attr = im_get_feedback_attr(feedback_col);
5236 if (char_attr < 0)
5237 char_attr = feedback_old_attr;
5238 feedback_col++;
5239 }
5240 else if (feedback_old_attr >= 0)
5241 {
5242 char_attr = feedback_old_attr;
5243 feedback_old_attr = -1;
5244 feedback_col = 0;
5245 }
5246 }
5247#endif
5248 /*
5249 * Handle the case where we are in column 0 but not on the first
5250 * character of the line and the user wants us to show us a
5251 * special character (via 'listchars' option "precedes:<char>".
5252 */
5253 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005254 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5256#ifdef FEAT_DIFF
5257 && filler_todo <= 0
5258#endif
5259 && draw_state > WL_NR
5260 && c != NUL)
5261 {
5262 c = lcs_prec;
5263 lcs_prec_todo = NUL;
5264#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005265 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5266 {
5267 /* Double-width character being overwritten by the "precedes"
5268 * character, need to fill up half the character. */
5269 c_extra = MB_FILLER_CHAR;
5270 n_extra = 1;
5271 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005272 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005275 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
5277 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005278 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005279 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
5281 else
5282 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5283#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005284 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 {
5286 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005287 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 n_attr3 = 1;
5289 }
5290 }
5291
5292 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005293 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005295 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005296#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005297 || did_line_attr == 1
5298#endif
5299 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005301#ifdef FEAT_SEARCH_EXTRA
5302 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005303
5304 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005305 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005306 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005307#endif
5308
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005309 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 * highlight match at end of line. If it's beyond the last
5311 * char on the screen, just overwrite that one (tricky!) Not
5312 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005313#ifdef FEAT_SEARCH_EXTRA
5314 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005315 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005316 prevcol_hl_flag = TRUE;
5317 else
5318 {
5319 cur = wp->w_match_head;
5320 while (cur != NULL)
5321 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005322 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005323 {
5324 prevcol_hl_flag = TRUE;
5325 break;
5326 }
5327 cur = cur->next;
5328 }
5329 }
5330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005332 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005333 && (VIsual_mode != Ctrl_V
5334 || lnum == VIsual.lnum
5335 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005336 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337#ifdef FEAT_SEARCH_EXTRA
5338 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005339 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005340# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005341 && did_line_attr <= 1
5342# endif
5343 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344#endif
5345 ))
5346 {
5347 int n = 0;
5348
5349#ifdef FEAT_RIGHTLEFT
5350 if (wp->w_p_rl)
5351 {
5352 if (col < 0)
5353 n = 1;
5354 }
5355 else
5356#endif
5357 {
5358 if (col >= W_WIDTH(wp))
5359 n = -1;
5360 }
5361 if (n != 0)
5362 {
5363 /* At the window boundary, highlight the last character
5364 * instead (better than nothing). */
5365 off += n;
5366 col += n;
5367 }
5368 else
5369 {
5370 /* Add a blank character to highlight. */
5371 ScreenLines[off] = ' ';
5372#ifdef FEAT_MBYTE
5373 if (enc_utf8)
5374 ScreenLinesUC[off] = 0;
5375#endif
5376 }
5377#ifdef FEAT_SEARCH_EXTRA
5378 if (area_attr == 0)
5379 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005380 /* Use attributes from match with highest priority among
5381 * 'search_hl' and the match list. */
5382 char_attr = search_hl.attr;
5383 cur = wp->w_match_head;
5384 shl_flag = FALSE;
5385 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005386 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005387 if (shl_flag == FALSE
5388 && ((cur != NULL
5389 && cur->priority > SEARCH_HL_PRIORITY)
5390 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005391 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005392 shl = &search_hl;
5393 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005394 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005395 else
5396 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005397 if ((ptr - line) - 1 == (long)shl->startcol
5398 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005399 char_attr = shl->attr;
5400 if (shl != &search_hl && cur != NULL)
5401 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
5404#endif
5405 ScreenAttrs[off] = char_attr;
5406#ifdef FEAT_RIGHTLEFT
5407 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005408 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005410 --off;
5411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 else
5413#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005416 ++off;
5417 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005418 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005419#ifdef FEAT_SYN_HL
5420 eol_hl_off = 1;
5421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
Bram Moolenaar91170f82006-05-05 21:15:17 +00005425 /*
5426 * At end of the text line.
5427 */
5428 if (c == NUL)
5429 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005430#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005431 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5432 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005433 {
5434 /* highlight last char after line */
5435 --col;
5436 --off;
5437 --vcol;
5438 }
5439
Bram Moolenaar1a384422010-07-14 19:53:30 +02005440 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005441 if (wp->w_p_wrap)
5442 v = wp->w_skipcol;
5443 else
5444 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005445
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005446 /* check if line ends before left margin */
5447 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005448 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005449#ifdef FEAT_CONCEAL
5450 /* Get rid of the boguscols now, we want to draw until the right
5451 * edge for 'cursorcolumn'. */
5452 col -= boguscols;
5453 boguscols = 0;
5454#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005455
5456 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005457 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005458
5459 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005460 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5461 && (int)wp->w_virtcol <
5462 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005463 && lnum != wp->w_cursor.lnum)
5464 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005465# ifdef FEAT_RIGHTLEFT
5466 && !wp->w_p_rl
5467# endif
5468 )
5469 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005470 int rightmost_vcol = 0;
5471 int i;
5472
5473 if (wp->w_p_cuc)
5474 rightmost_vcol = wp->w_virtcol;
5475 if (draw_color_col)
5476 /* determine rightmost colorcolumn to possibly draw */
5477 for (i = 0; color_cols[i] >= 0; ++i)
5478 if (rightmost_vcol < color_cols[i])
5479 rightmost_vcol = color_cols[i];
5480
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005481 while (col < W_WIDTH(wp))
5482 {
5483 ScreenLines[off] = ' ';
5484#ifdef FEAT_MBYTE
5485 if (enc_utf8)
5486 ScreenLinesUC[off] = 0;
5487#endif
5488 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005489 if (draw_color_col)
5490 draw_color_col = advance_color_col(VCOL_HLC,
5491 &color_cols);
5492
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005493 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005494 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005495 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005496 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005497 else
5498 ScreenAttrs[off++] = 0;
5499
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005500 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005501 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005502
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005503 ++vcol;
5504 }
5505 }
5506#endif
5507
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005508 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005509 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 row++;
5511
5512 /*
5513 * Update w_cline_height and w_cline_folded if the cursor line was
5514 * updated (saves a call to plines() later).
5515 */
5516 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5517 {
5518 curwin->w_cline_row = startrow;
5519 curwin->w_cline_height = row - startrow;
5520#ifdef FEAT_FOLDING
5521 curwin->w_cline_folded = FALSE;
5522#endif
5523 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5524 }
5525
5526 break;
5527 }
5528
5529 /* line continues beyond line end */
5530 if (lcs_ext
5531 && !wp->w_p_wrap
5532#ifdef FEAT_DIFF
5533 && filler_todo <= 0
5534#endif
5535 && (
5536#ifdef FEAT_RIGHTLEFT
5537 wp->w_p_rl ? col == 0 :
5538#endif
5539 col == W_WIDTH(wp) - 1)
5540 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005541 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5543 {
5544 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005545 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546#ifdef FEAT_MBYTE
5547 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005548 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
5550 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005551 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005552 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
5554 else
5555 mb_utf8 = FALSE;
5556#endif
5557 }
5558
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005559#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005560 /* advance to the next 'colorcolumn' */
5561 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005562 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005563
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005564 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005565 * highlight the cursor position itself.
5566 * Also highlight the 'colorcolumn' if it is different than
5567 * 'cursorcolumn' */
5568 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005569 if (draw_state == WL_LINE && !lnum_in_visual_area
5570 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005571 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005572 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005573 && lnum != wp->w_cursor.lnum)
5574 {
5575 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005576 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005577 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005578 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005579 {
5580 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005581 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005582 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005583 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005584#endif
5585
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 /*
5587 * Store character to be displayed.
5588 * Skip characters that are left of the screen for 'nowrap'.
5589 */
5590 vcol_prev = vcol;
5591 if (draw_state < WL_LINE || n_skip <= 0)
5592 {
5593 /*
5594 * Store the character.
5595 */
5596#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5597 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5598 {
5599 /* A double-wide character is: put first halve in left cell. */
5600 --off;
5601 --col;
5602 }
5603#endif
5604 ScreenLines[off] = c;
5605#ifdef FEAT_MBYTE
5606 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005607 {
5608 if ((mb_c & 0xff00) == 0x8e00)
5609 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 else if (enc_utf8)
5613 {
5614 if (mb_utf8)
5615 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005616 int i;
5617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005619 if ((c & 0xff) == 0)
5620 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005621 for (i = 0; i < Screen_mco; ++i)
5622 {
5623 ScreenLinesC[i][off] = u8cc[i];
5624 if (u8cc[i] == 0)
5625 break;
5626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 }
5628 else
5629 ScreenLinesUC[off] = 0;
5630 }
5631 if (multi_attr)
5632 {
5633 ScreenAttrs[off] = multi_attr;
5634 multi_attr = 0;
5635 }
5636 else
5637#endif
5638 ScreenAttrs[off] = char_attr;
5639
5640#ifdef FEAT_MBYTE
5641 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5642 {
5643 /* Need to fill two screen columns. */
5644 ++off;
5645 ++col;
5646 if (enc_utf8)
5647 /* UTF-8: Put a 0 in the second screen char. */
5648 ScreenLines[off] = 0;
5649 else
5650 /* DBCS: Put second byte in the second screen char. */
5651 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005652 if (draw_state > WL_NR
5653#ifdef FEAT_DIFF
5654 && filler_todo <= 0
5655#endif
5656 )
5657 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 /* When "tocol" is halfway a character, set it to the end of
5659 * the character, otherwise highlighting won't stop. */
5660 if (tocol == vcol)
5661 ++tocol;
5662#ifdef FEAT_RIGHTLEFT
5663 if (wp->w_p_rl)
5664 {
5665 /* now it's time to backup one cell */
5666 --off;
5667 --col;
5668 }
5669#endif
5670 }
5671#endif
5672#ifdef FEAT_RIGHTLEFT
5673 if (wp->w_p_rl)
5674 {
5675 --off;
5676 --col;
5677 }
5678 else
5679#endif
5680 {
5681 ++off;
5682 ++col;
5683 }
5684 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005685#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005686 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005687 {
5688 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005689 ++vcol_off;
5690 if (n_extra > 0)
5691 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005692 if (wp->w_p_wrap)
5693 {
5694 /*
5695 * Special voodoo required if 'wrap' is on.
5696 *
5697 * Advance the column indicator to force the line
5698 * drawing to wrap early. This will make the line
5699 * take up the same screen space when parts are concealed,
5700 * so that cursor line computations aren't messed up.
5701 *
5702 * To avoid the fictitious advance of 'col' causing
5703 * trailing junk to be written out of the screen line
5704 * we are building, 'boguscols' keeps track of the number
5705 * of bad columns we have advanced.
5706 */
5707 if (n_extra > 0)
5708 {
5709 vcol += n_extra;
5710# ifdef FEAT_RIGHTLEFT
5711 if (wp->w_p_rl)
5712 {
5713 col -= n_extra;
5714 boguscols -= n_extra;
5715 }
5716 else
5717# endif
5718 {
5719 col += n_extra;
5720 boguscols += n_extra;
5721 }
5722 n_extra = 0;
5723 n_attr = 0;
5724 }
5725
5726
5727# ifdef FEAT_MBYTE
5728 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5729 {
5730 /* Need to fill two screen columns. */
5731# ifdef FEAT_RIGHTLEFT
5732 if (wp->w_p_rl)
5733 {
5734 --boguscols;
5735 --col;
5736 }
5737 else
5738# endif
5739 {
5740 ++boguscols;
5741 ++col;
5742 }
5743 }
5744# endif
5745
5746# ifdef FEAT_RIGHTLEFT
5747 if (wp->w_p_rl)
5748 {
5749 --boguscols;
5750 --col;
5751 }
5752 else
5753# endif
5754 {
5755 ++boguscols;
5756 ++col;
5757 }
5758 }
5759 else
5760 {
5761 if (n_extra > 0)
5762 {
5763 vcol += n_extra;
5764 n_extra = 0;
5765 n_attr = 0;
5766 }
5767 }
5768
5769 }
5770#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 else
5772 --n_skip;
5773
Bram Moolenaar64486672010-05-16 15:46:46 +02005774 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5775 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005776 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777#ifdef FEAT_DIFF
5778 && filler_todo <= 0
5779#endif
5780 )
5781 ++vcol;
5782
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005783#ifdef FEAT_SYN_HL
5784 if (vcol_save_attr >= 0)
5785 char_attr = vcol_save_attr;
5786#endif
5787
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 /* restore attributes after "predeces" in 'listchars' */
5789 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5790 char_attr = saved_attr3;
5791
5792 /* restore attributes after last 'listchars' or 'number' char */
5793 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5794 char_attr = saved_attr2;
5795
5796 /*
5797 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005798 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 */
5800 if ((
5801#ifdef FEAT_RIGHTLEFT
5802 wp->w_p_rl ? (col < 0) :
5803#endif
5804 (col >= W_WIDTH(wp)))
5805 && (*ptr != NUL
5806#ifdef FEAT_DIFF
5807 || filler_todo > 0
5808#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005809 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5811 )
5812 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005813#ifdef FEAT_CONCEAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005814 screen_line(screen_row, W_WINCOL(wp), col - boguscols,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005815 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005816 boguscols = 0;
5817#else
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005818 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005819 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 ++row;
5822 ++screen_row;
5823
5824 /* When not wrapping and finished diff lines, or when displayed
5825 * '$' and highlighting until last column, break here. */
5826 if ((!wp->w_p_wrap
5827#ifdef FEAT_DIFF
5828 && filler_todo <= 0
5829#endif
5830 ) || lcs_eol_one == -1)
5831 break;
5832
5833 /* When the window is too narrow draw all "@" lines. */
5834 if (draw_state != WL_LINE
5835#ifdef FEAT_DIFF
5836 && filler_todo <= 0
5837#endif
5838 )
5839 {
5840 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005841#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 draw_vsep_win(wp, row);
5843#endif
5844 row = endrow;
5845 }
5846
5847 /* When line got too long for screen break here. */
5848 if (row == endrow)
5849 {
5850 ++row;
5851 break;
5852 }
5853
5854 if (screen_cur_row == screen_row - 1
5855#ifdef FEAT_DIFF
5856 && filler_todo <= 0
5857#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005858#ifdef FEAT_WINDOWS
5859 && W_WIDTH(wp) == Columns
5860#endif
5861 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {
5863 /* Remember that the line wraps, used for modeless copy. */
5864 LineWraps[screen_row - 1] = TRUE;
5865
5866 /*
5867 * Special trick to make copy/paste of wrapped lines work with
5868 * xterm/screen: write an extra character beyond the end of
5869 * the line. This will work with all terminal types
5870 * (regardless of the xn,am settings).
5871 * Only do this on a fast tty.
5872 * Only do this if the cursor is on the current line
5873 * (something has been written in it).
5874 * Don't do this for the GUI.
5875 * Don't do this for double-width characters.
5876 * Don't do this for a window not at the right screen border.
5877 */
5878 if (p_tf
5879#ifdef FEAT_GUI
5880 && !gui.in_use
5881#endif
5882#ifdef FEAT_MBYTE
5883 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005884 && ((*mb_off2cells)(LineOffset[screen_row],
5885 LineOffset[screen_row] + screen_Columns)
5886 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005888 + (int)Columns - 2,
5889 LineOffset[screen_row] + screen_Columns)
5890 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891#endif
5892 )
5893 {
5894 /* First make sure we are at the end of the screen line,
5895 * then output the same character again to let the
5896 * terminal know about the wrap. If the terminal doesn't
5897 * auto-wrap, we overwrite the character. */
5898 if (screen_cur_col != W_WIDTH(wp))
5899 screen_char(LineOffset[screen_row - 1]
5900 + (unsigned)Columns - 1,
5901 screen_row - 1, (int)(Columns - 1));
5902
5903#ifdef FEAT_MBYTE
5904 /* When there is a multi-byte character, just output a
5905 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005906 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5907 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 out_char(' ');
5909 else
5910#endif
5911 out_char(ScreenLines[LineOffset[screen_row - 1]
5912 + (Columns - 1)]);
5913 /* force a redraw of the first char on the next line */
5914 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5915 screen_start(); /* don't know where cursor is now */
5916 }
5917 }
5918
5919 col = 0;
5920 off = (unsigned)(current_ScreenLine - ScreenLines);
5921#ifdef FEAT_RIGHTLEFT
5922 if (wp->w_p_rl)
5923 {
5924 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5925 off += col;
5926 }
5927#endif
5928
5929 /* reset the drawing state for the start of a wrapped line */
5930 draw_state = WL_START;
5931 saved_n_extra = n_extra;
5932 saved_p_extra = p_extra;
5933 saved_c_extra = c_extra;
5934 saved_char_attr = char_attr;
5935 n_extra = 0;
5936 lcs_prec_todo = lcs_prec;
5937#ifdef FEAT_LINEBREAK
5938# ifdef FEAT_DIFF
5939 if (filler_todo <= 0)
5940# endif
5941 need_showbreak = TRUE;
5942#endif
5943#ifdef FEAT_DIFF
5944 --filler_todo;
5945 /* When the filler lines are actually below the last line of the
5946 * file, don't draw the line itself, break here. */
5947 if (filler_todo == 0 && wp->w_botfill)
5948 break;
5949#endif
5950 }
5951
5952 } /* for every character in the line */
5953
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005954#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005955 /* After an empty line check first word for capital. */
5956 if (*skipwhite(line) == NUL)
5957 {
5958 capcol_lnum = lnum + 1;
5959 cap_col = 0;
5960 }
5961#endif
5962
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005963 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 return row;
5965}
5966
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005967#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005968static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005969
5970/*
5971 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005972 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005973 */
5974 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005975comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005976{
5977 int i;
5978
5979 for (i = 0; i < Screen_mco; ++i)
5980 {
5981 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5982 return TRUE;
5983 if (ScreenLinesC[i][off_from] == 0)
5984 break;
5985 }
5986 return FALSE;
5987}
5988#endif
5989
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990/*
5991 * Check whether the given character needs redrawing:
5992 * - the (first byte of the) character is different
5993 * - the attributes are different
5994 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005995 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 */
5997 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005998char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999{
6000 if (cols > 0
6001 && ((ScreenLines[off_from] != ScreenLines[off_to]
6002 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6003
6004#ifdef FEAT_MBYTE
6005 || (enc_dbcs != 0
6006 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6007 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6008 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6009 : (cols > 1 && ScreenLines[off_from + 1]
6010 != ScreenLines[off_to + 1])))
6011 || (enc_utf8
6012 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6013 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006014 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006015 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6016 && ScreenLines[off_from + 1]
6017 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018#endif
6019 ))
6020 return TRUE;
6021 return FALSE;
6022}
6023
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006024#if defined(FEAT_TERMINAL) || defined(PROTO)
6025/*
6026 * Return the index in ScreenLines[] for the current screen line.
6027 */
6028 int
6029screen_get_current_line_off()
6030{
6031 return (int)(current_ScreenLine - ScreenLines);
6032}
6033#endif
6034
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035/*
6036 * Move one "cooked" screen line to the screen, but only the characters that
6037 * have actually changed. Handle insert/delete character.
6038 * "coloff" gives the first column on the screen for this line.
6039 * "endcol" gives the columns where valid characters are.
6040 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6041 * needs to be cleared, negative otherwise.
6042 * "rlflag" is TRUE in a rightleft window:
6043 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6044 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6045 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006046 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006047screen_line(
6048 int row,
6049 int coloff,
6050 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006051 int clear_width,
6052 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 unsigned off_from;
6055 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006056#ifdef FEAT_MBYTE
6057 unsigned max_off_from;
6058 unsigned max_off_to;
6059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006061#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 int hl;
6063#endif
6064 int force = FALSE; /* force update rest of the line */
6065 int redraw_this /* bool: does character need redraw? */
6066#ifdef FEAT_GUI
6067 = TRUE /* For GUI when while-loop empty */
6068#endif
6069 ;
6070 int redraw_next; /* redraw_this for next character */
6071#ifdef FEAT_MBYTE
6072 int clear_next = FALSE;
6073 int char_cells; /* 1: normal char */
6074 /* 2: occupies two display cells */
6075# define CHAR_CELLS char_cells
6076#else
6077# define CHAR_CELLS 1
6078#endif
6079
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006080 /* Check for illegal row and col, just in case. */
6081 if (row >= Rows)
6082 row = Rows - 1;
6083 if (endcol > Columns)
6084 endcol = Columns;
6085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086# ifdef FEAT_CLIPBOARD
6087 clip_may_clear_selection(row, row);
6088# endif
6089
6090 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6091 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006092#ifdef FEAT_MBYTE
6093 max_off_from = off_from + screen_Columns;
6094 max_off_to = LineOffset[row] + screen_Columns;
6095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
6097#ifdef FEAT_RIGHTLEFT
6098 if (rlflag)
6099 {
6100 /* Clear rest first, because it's left of the text. */
6101 if (clear_width > 0)
6102 {
6103 while (col <= endcol && ScreenLines[off_to] == ' '
6104 && ScreenAttrs[off_to] == 0
6105# ifdef FEAT_MBYTE
6106 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6107# endif
6108 )
6109 {
6110 ++off_to;
6111 ++col;
6112 }
6113 if (col <= endcol)
6114 screen_fill(row, row + 1, col + coloff,
6115 endcol + coloff + 1, ' ', ' ', 0);
6116 }
6117 col = endcol + 1;
6118 off_to = LineOffset[row] + col + coloff;
6119 off_from += col;
6120 endcol = (clear_width > 0 ? clear_width : -clear_width);
6121 }
6122#endif /* FEAT_RIGHTLEFT */
6123
6124 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6125
6126 while (col < endcol)
6127 {
6128#ifdef FEAT_MBYTE
6129 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006130 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 else
6132 char_cells = 1;
6133#endif
6134
6135 redraw_this = redraw_next;
6136 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6137 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6138
6139#ifdef FEAT_GUI
6140 /* If the next character was bold, then redraw the current character to
6141 * remove any pixels that might have spilt over into us. This only
6142 * happens in the GUI.
6143 */
6144 if (redraw_next && gui.in_use)
6145 {
6146 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006147 if (hl > HL_ALL)
6148 hl = syn_attr2attr(hl);
6149 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 redraw_this = TRUE;
6151 }
6152#endif
6153
6154 if (redraw_this)
6155 {
6156 /*
6157 * Special handling when 'xs' termcap flag set (hpterm):
6158 * Attributes for characters are stored at the position where the
6159 * cursor is when writing the highlighting code. The
6160 * start-highlighting code must be written with the cursor on the
6161 * first highlighted character. The stop-highlighting code must
6162 * be written with the cursor just after the last highlighted
6163 * character.
6164 * Overwriting a character doesn't remove it's highlighting. Need
6165 * to clear the rest of the line, and force redrawing it
6166 * completely.
6167 */
6168 if ( p_wiv
6169 && !force
6170#ifdef FEAT_GUI
6171 && !gui.in_use
6172#endif
6173 && ScreenAttrs[off_to] != 0
6174 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6175 {
6176 /*
6177 * Need to remove highlighting attributes here.
6178 */
6179 windgoto(row, col + coloff);
6180 out_str(T_CE); /* clear rest of this screen line */
6181 screen_start(); /* don't know where cursor is now */
6182 force = TRUE; /* force redraw of rest of the line */
6183 redraw_next = TRUE; /* or else next char would miss out */
6184
6185 /*
6186 * If the previous character was highlighted, need to stop
6187 * highlighting at this character.
6188 */
6189 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6190 {
6191 screen_attr = ScreenAttrs[off_to - 1];
6192 term_windgoto(row, col + coloff);
6193 screen_stop_highlight();
6194 }
6195 else
6196 screen_attr = 0; /* highlighting has stopped */
6197 }
6198#ifdef FEAT_MBYTE
6199 if (enc_dbcs != 0)
6200 {
6201 /* Check if overwriting a double-byte with a single-byte or
6202 * the other way around requires another character to be
6203 * redrawn. For UTF-8 this isn't needed, because comparing
6204 * ScreenLinesUC[] is sufficient. */
6205 if (char_cells == 1
6206 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006207 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
6209 /* Writing a single-cell character over a double-cell
6210 * character: need to redraw the next cell. */
6211 ScreenLines[off_to + 1] = 0;
6212 redraw_next = TRUE;
6213 }
6214 else if (char_cells == 2
6215 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006216 && (*mb_off2cells)(off_to, max_off_to) == 1
6217 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 {
6219 /* Writing the second half of a double-cell character over
6220 * a double-cell character: need to redraw the second
6221 * cell. */
6222 ScreenLines[off_to + 2] = 0;
6223 redraw_next = TRUE;
6224 }
6225
6226 if (enc_dbcs == DBCS_JPNU)
6227 ScreenLines2[off_to] = ScreenLines2[off_from];
6228 }
6229 /* When writing a single-width character over a double-width
6230 * character and at the end of the redrawn text, need to clear out
6231 * the right halve of the old character.
6232 * Also required when writing the right halve of a double-width
6233 * char over the left halve of an existing one. */
6234 if (has_mbyte && col + char_cells == endcol
6235 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006236 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006238 && (*mb_off2cells)(off_to, max_off_to) == 1
6239 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 clear_next = TRUE;
6241#endif
6242
6243 ScreenLines[off_to] = ScreenLines[off_from];
6244#ifdef FEAT_MBYTE
6245 if (enc_utf8)
6246 {
6247 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6248 if (ScreenLinesUC[off_from] != 0)
6249 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006250 int i;
6251
6252 for (i = 0; i < Screen_mco; ++i)
6253 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255 }
6256 if (char_cells == 2)
6257 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6258#endif
6259
6260#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006261 /* The bold trick makes a single column of pixels appear in the
6262 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 * character should be redrawn too. This happens for our own GUI
6264 * and for some xterms. */
6265 if (
6266# ifdef FEAT_GUI
6267 gui.in_use
6268# endif
6269# if defined(FEAT_GUI) && defined(UNIX)
6270 ||
6271# endif
6272# ifdef UNIX
6273 term_is_xterm
6274# endif
6275 )
6276 {
6277 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006278 if (hl > HL_ALL)
6279 hl = syn_attr2attr(hl);
6280 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 redraw_next = TRUE;
6282 }
6283#endif
6284 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6285#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006286 /* For simplicity set the attributes of second half of a
6287 * double-wide character equal to the first half. */
6288 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006290
6291 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 else
6294#endif
6295 screen_char(off_to, row, col + coloff);
6296 }
6297 else if ( p_wiv
6298#ifdef FEAT_GUI
6299 && !gui.in_use
6300#endif
6301 && col + coloff > 0)
6302 {
6303 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6304 {
6305 /*
6306 * Don't output stop-highlight when moving the cursor, it will
6307 * stop the highlighting when it should continue.
6308 */
6309 screen_attr = 0;
6310 }
6311 else if (screen_attr != 0)
6312 screen_stop_highlight();
6313 }
6314
6315 off_to += CHAR_CELLS;
6316 off_from += CHAR_CELLS;
6317 col += CHAR_CELLS;
6318 }
6319
6320#ifdef FEAT_MBYTE
6321 if (clear_next)
6322 {
6323 /* Clear the second half of a double-wide character of which the left
6324 * half was overwritten with a single-wide character. */
6325 ScreenLines[off_to] = ' ';
6326 if (enc_utf8)
6327 ScreenLinesUC[off_to] = 0;
6328 screen_char(off_to, row, col + coloff);
6329 }
6330#endif
6331
6332 if (clear_width > 0
6333#ifdef FEAT_RIGHTLEFT
6334 && !rlflag
6335#endif
6336 )
6337 {
6338#ifdef FEAT_GUI
6339 int startCol = col;
6340#endif
6341
6342 /* blank out the rest of the line */
6343 while (col < clear_width && ScreenLines[off_to] == ' '
6344 && ScreenAttrs[off_to] == 0
6345#ifdef FEAT_MBYTE
6346 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6347#endif
6348 )
6349 {
6350 ++off_to;
6351 ++col;
6352 }
6353 if (col < clear_width)
6354 {
6355#ifdef FEAT_GUI
6356 /*
6357 * In the GUI, clearing the rest of the line may leave pixels
6358 * behind if the first character cleared was bold. Some bold
6359 * fonts spill over the left. In this case we redraw the previous
6360 * character too. If we didn't skip any blanks above, then we
6361 * only redraw if the character wasn't already redrawn anyway.
6362 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006363 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 {
6365 hl = ScreenAttrs[off_to];
6366 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006367 {
6368 int prev_cells = 1;
6369# ifdef FEAT_MBYTE
6370 if (enc_utf8)
6371 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6372 * that its width is 2. */
6373 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6374 else if (enc_dbcs != 0)
6375 {
6376 /* find previous character by counting from first
6377 * column and get its width. */
6378 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006379 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006380
6381 while (off < off_to)
6382 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006383 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006384 off += prev_cells;
6385 }
6386 }
6387
6388 if (enc_dbcs != 0 && prev_cells > 1)
6389 screen_char_2(off_to - prev_cells, row,
6390 col + coloff - prev_cells);
6391 else
6392# endif
6393 screen_char(off_to - prev_cells, row,
6394 col + coloff - prev_cells);
6395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 }
6397#endif
6398 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6399 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006400#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 off_to += clear_width - col;
6402 col = clear_width;
6403#endif
6404 }
6405 }
6406
6407 if (clear_width > 0)
6408 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006409#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 /* For a window that's left of another, draw the separator char. */
6411 if (col + coloff < Columns)
6412 {
6413 int c;
6414
6415 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006416 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006418 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6419 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420# endif
6421 || ScreenAttrs[off_to] != hl)
6422 {
6423 ScreenLines[off_to] = c;
6424 ScreenAttrs[off_to] = hl;
6425# ifdef FEAT_MBYTE
6426 if (enc_utf8)
6427 {
6428 if (c >= 0x80)
6429 {
6430 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006431 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 }
6433 else
6434 ScreenLinesUC[off_to] = 0;
6435 }
6436# endif
6437 screen_char(off_to, row, col + coloff);
6438 }
6439 }
6440 else
6441#endif
6442 LineWraps[row] = FALSE;
6443 }
6444}
6445
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006446#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006448 * Mirror text "str" for right-left displaying.
6449 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006451 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006452rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
6454 char_u *p1, *p2;
6455 int t;
6456
6457 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6458 {
6459 t = *p1;
6460 *p1 = *p2;
6461 *p2 = t;
6462 }
6463}
6464#endif
6465
6466#if defined(FEAT_WINDOWS) || defined(PROTO)
6467/*
6468 * mark all status lines for redraw; used after first :cd
6469 */
6470 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006471status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472{
6473 win_T *wp;
6474
Bram Moolenaar29323592016-07-24 22:04:11 +02006475 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 if (wp->w_status_height)
6477 {
6478 wp->w_redr_status = TRUE;
6479 redraw_later(VALID);
6480 }
6481}
6482
6483/*
6484 * mark all status lines of the current buffer for redraw
6485 */
6486 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006487status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488{
6489 win_T *wp;
6490
Bram Moolenaar29323592016-07-24 22:04:11 +02006491 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6493 {
6494 wp->w_redr_status = TRUE;
6495 redraw_later(VALID);
6496 }
6497}
6498
6499/*
6500 * Redraw all status lines that need to be redrawn.
6501 */
6502 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006503redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 win_T *wp;
6506
Bram Moolenaar29323592016-07-24 22:04:11 +02006507 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 if (wp->w_redr_status)
6509 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006510 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006511 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512}
6513#endif
6514
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006515#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516/*
6517 * Redraw all status lines at the bottom of frame "frp".
6518 */
6519 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006520win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
6522 if (frp->fr_layout == FR_LEAF)
6523 frp->fr_win->w_redr_status = TRUE;
6524 else if (frp->fr_layout == FR_ROW)
6525 {
6526 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6527 win_redraw_last_status(frp);
6528 }
6529 else /* frp->fr_layout == FR_COL */
6530 {
6531 frp = frp->fr_child;
6532 while (frp->fr_next != NULL)
6533 frp = frp->fr_next;
6534 win_redraw_last_status(frp);
6535 }
6536}
6537#endif
6538
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006539#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540/*
6541 * Draw the verticap separator right of window "wp" starting with line "row".
6542 */
6543 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006544draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
6546 int hl;
6547 int c;
6548
6549 if (wp->w_vsep_width)
6550 {
6551 /* draw the vertical separator right of this window */
6552 c = fillchar_vsep(&hl);
6553 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6554 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6555 c, ' ', hl);
6556 }
6557}
6558#endif
6559
6560#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006561static int status_match_len(expand_T *xp, char_u *s);
6562static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
6564/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006565 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 */
6567 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006568status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
6570 int len = 0;
6571
6572#ifdef FEAT_MENU
6573 int emenu = (xp->xp_context == EXPAND_MENUS
6574 || xp->xp_context == EXPAND_MENUNAMES);
6575
6576 /* Check for menu separators - replace with '|'. */
6577 if (emenu && menu_is_separator(s))
6578 return 1;
6579#endif
6580
6581 while (*s != NUL)
6582 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006583 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006584 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006585 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 }
6587
6588 return len;
6589}
6590
6591/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006592 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006593 * These are backslashes used for escaping. Do show backslashes in help tags.
6594 */
6595 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006596skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006597{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006598 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006599#ifdef FEAT_MENU
6600 || ((xp->xp_context == EXPAND_MENUS
6601 || xp->xp_context == EXPAND_MENUNAMES)
6602 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6603#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006604 )
6605 {
6606#ifndef BACKSLASH_IN_FILENAME
6607 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6608 return 2;
6609#endif
6610 return 1;
6611 }
6612 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006613}
6614
6615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 * Show wildchar matches in the status line.
6617 * Show at least the "match" item.
6618 * We start at item 'first_match' in the list and show all matches that fit.
6619 *
6620 * If inversion is possible we use it. Else '=' characters are used.
6621 */
6622 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006623win_redr_status_matches(
6624 expand_T *xp,
6625 int num_matches,
6626 char_u **matches, /* list of matches */
6627 int match,
6628 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
6630#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6631 int row;
6632 char_u *buf;
6633 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006634 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 int fillchar;
6636 int attr;
6637 int i;
6638 int highlight = TRUE;
6639 char_u *selstart = NULL;
6640 int selstart_col = 0;
6641 char_u *selend = NULL;
6642 static int first_match = 0;
6643 int add_left = FALSE;
6644 char_u *s;
6645#ifdef FEAT_MENU
6646 int emenu;
6647#endif
6648#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6649 int l;
6650#endif
6651
6652 if (matches == NULL) /* interrupted completion? */
6653 return;
6654
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006655#ifdef FEAT_MBYTE
6656 if (has_mbyte)
6657 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6658 else
6659#endif
6660 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 if (buf == NULL)
6662 return;
6663
6664 if (match == -1) /* don't show match but original text */
6665 {
6666 match = 0;
6667 highlight = FALSE;
6668 }
6669 /* count 1 for the ending ">" */
6670 clen = status_match_len(xp, L_MATCH(match)) + 3;
6671 if (match == 0)
6672 first_match = 0;
6673 else if (match < first_match)
6674 {
6675 /* jumping left, as far as we can go */
6676 first_match = match;
6677 add_left = TRUE;
6678 }
6679 else
6680 {
6681 /* check if match fits on the screen */
6682 for (i = first_match; i < match; ++i)
6683 clen += status_match_len(xp, L_MATCH(i)) + 2;
6684 if (first_match > 0)
6685 clen += 2;
6686 /* jumping right, put match at the left */
6687 if ((long)clen > Columns)
6688 {
6689 first_match = match;
6690 /* if showing the last match, we can add some on the left */
6691 clen = 2;
6692 for (i = match; i < num_matches; ++i)
6693 {
6694 clen += status_match_len(xp, L_MATCH(i)) + 2;
6695 if ((long)clen >= Columns)
6696 break;
6697 }
6698 if (i == num_matches)
6699 add_left = TRUE;
6700 }
6701 }
6702 if (add_left)
6703 while (first_match > 0)
6704 {
6705 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6706 if ((long)clen >= Columns)
6707 break;
6708 --first_match;
6709 }
6710
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006711 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
6713 if (first_match == 0)
6714 {
6715 *buf = NUL;
6716 len = 0;
6717 }
6718 else
6719 {
6720 STRCPY(buf, "< ");
6721 len = 2;
6722 }
6723 clen = len;
6724
6725 i = first_match;
6726 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6727 {
6728 if (i == match)
6729 {
6730 selstart = buf + len;
6731 selstart_col = clen;
6732 }
6733
6734 s = L_MATCH(i);
6735 /* Check for menu separators - replace with '|' */
6736#ifdef FEAT_MENU
6737 emenu = (xp->xp_context == EXPAND_MENUS
6738 || xp->xp_context == EXPAND_MENUNAMES);
6739 if (emenu && menu_is_separator(s))
6740 {
6741 STRCPY(buf + len, transchar('|'));
6742 l = (int)STRLEN(buf + len);
6743 len += l;
6744 clen += l;
6745 }
6746 else
6747#endif
6748 for ( ; *s != NUL; ++s)
6749 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006750 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 clen += ptr2cells(s);
6752#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006753 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 {
6755 STRNCPY(buf + len, s, l);
6756 s += l - 1;
6757 len += l;
6758 }
6759 else
6760#endif
6761 {
6762 STRCPY(buf + len, transchar_byte(*s));
6763 len += (int)STRLEN(buf + len);
6764 }
6765 }
6766 if (i == match)
6767 selend = buf + len;
6768
6769 *(buf + len++) = ' ';
6770 *(buf + len++) = ' ';
6771 clen += 2;
6772 if (++i == num_matches)
6773 break;
6774 }
6775
6776 if (i != num_matches)
6777 {
6778 *(buf + len++) = '>';
6779 ++clen;
6780 }
6781
6782 buf[len] = NUL;
6783
6784 row = cmdline_row - 1;
6785 if (row >= 0)
6786 {
6787 if (wild_menu_showing == 0)
6788 {
6789 if (msg_scrolled > 0)
6790 {
6791 /* Put the wildmenu just above the command line. If there is
6792 * no room, scroll the screen one line up. */
6793 if (cmdline_row == Rows - 1)
6794 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006795 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 ++msg_scrolled;
6797 }
6798 else
6799 {
6800 ++cmdline_row;
6801 ++row;
6802 }
6803 wild_menu_showing = WM_SCROLLED;
6804 }
6805 else
6806 {
6807 /* Create status line if needed by setting 'laststatus' to 2.
6808 * Set 'winminheight' to zero to avoid that the window is
6809 * resized. */
6810 if (lastwin->w_status_height == 0)
6811 {
6812 save_p_ls = p_ls;
6813 save_p_wmh = p_wmh;
6814 p_ls = 2;
6815 p_wmh = 0;
6816 last_status(FALSE);
6817 }
6818 wild_menu_showing = WM_SHOWN;
6819 }
6820 }
6821
6822 screen_puts(buf, row, 0, attr);
6823 if (selstart != NULL && highlight)
6824 {
6825 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006826 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 }
6828
6829 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6830 }
6831
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006832#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 win_redraw_last_status(topframe);
6834#else
6835 lastwin->w_redr_status = TRUE;
6836#endif
6837 vim_free(buf);
6838}
6839#endif
6840
6841#if defined(FEAT_WINDOWS) || defined(PROTO)
6842/*
6843 * Redraw the status line of window wp.
6844 *
6845 * If inversion is possible we use it. Else '=' characters are used.
6846 */
6847 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006848win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850 int row;
6851 char_u *p;
6852 int len;
6853 int fillchar;
6854 int attr;
6855 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006856 static int busy = FALSE;
6857
6858 /* It's possible to get here recursively when 'statusline' (indirectly)
6859 * invokes ":redrawstatus". Simply ignore the call then. */
6860 if (busy)
6861 return;
6862 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863
6864 wp->w_redr_status = FALSE;
6865 if (wp->w_status_height == 0)
6866 {
6867 /* no status line, can only be last window */
6868 redraw_cmdline = TRUE;
6869 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006870 else if (!redrawing()
6871#ifdef FEAT_INS_EXPAND
6872 /* don't update status line when popup menu is visible and may be
6873 * drawn over it */
6874 || pum_visible()
6875#endif
6876 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {
6878 /* Don't redraw right now, do it later. */
6879 wp->w_redr_status = TRUE;
6880 }
6881#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006882 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {
6884 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006885 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 }
6887#endif
6888 else
6889 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006890 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006892 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 p = NameBuff;
6894 len = (int)STRLEN(p);
6895
Bram Moolenaard85f2712017-07-28 21:51:57 +02006896 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897#ifdef FEAT_QUICKFIX
6898 || wp->w_p_pvw
6899#endif
6900 || bufIsChanged(wp->w_buffer)
6901 || wp->w_buffer->b_p_ro)
6902 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006903 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006905 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 len += (int)STRLEN(p + len);
6907 }
6908#ifdef FEAT_QUICKFIX
6909 if (wp->w_p_pvw)
6910 {
6911 STRCPY(p + len, _("[Preview]"));
6912 len += (int)STRLEN(p + len);
6913 }
6914#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006915 if (bufIsChanged(wp->w_buffer)
6916#ifdef FEAT_TERMINAL
6917 && !bt_terminal(wp->w_buffer)
6918#endif
6919 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {
6921 STRCPY(p + len, "[+]");
6922 len += 3;
6923 }
6924 if (wp->w_buffer->b_p_ro)
6925 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006926 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006927 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 }
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6931 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6932 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6933 if (this_ru_col <= 1)
6934 {
6935 p = (char_u *)"<"; /* No room for file name! */
6936 len = 1;
6937 }
6938 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939#ifdef FEAT_MBYTE
6940 if (has_mbyte)
6941 {
6942 int clen = 0, i;
6943
6944 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006945 clen = mb_string2cells(p, -1);
6946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 /* Find first character that will fit.
6948 * Going from start to end is much faster for DBCS. */
6949 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006950 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 clen -= (*mb_ptr2cells)(p + i);
6952 len = clen;
6953 if (i > 0)
6954 {
6955 p = p + i - 1;
6956 *p = '<';
6957 ++len;
6958 }
6959
6960 }
6961 else
6962#endif
6963 if (len > this_ru_col - 1)
6964 {
6965 p += len - (this_ru_col - 1);
6966 *p = '<';
6967 len = this_ru_col - 1;
6968 }
6969
6970 row = W_WINROW(wp) + wp->w_height;
6971 screen_puts(p, row, W_WINCOL(wp), attr);
6972 screen_fill(row, row + 1, len + W_WINCOL(wp),
6973 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6974
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006975 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6977 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6978 - 1 + W_WINCOL(wp)), attr);
6979
6980#ifdef FEAT_CMDL_INFO
6981 win_redr_ruler(wp, TRUE);
6982#endif
6983 }
6984
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 /*
6986 * May need to draw the character below the vertical separator.
6987 */
6988 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6989 {
6990 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006991 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 else
6993 fillchar = fillchar_vsep(&attr);
6994 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6995 attr);
6996 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006997 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998}
6999
Bram Moolenaar238a5642006-02-21 22:12:05 +00007000#ifdef FEAT_STL_OPT
7001/*
7002 * Redraw the status line according to 'statusline' and take care of any
7003 * errors encountered.
7004 */
7005 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007006redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007007{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007008 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007009 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007010
7011 /* When called recursively return. This can happen when the statusline
7012 * contains an expression that triggers a redraw. */
7013 if (entered)
7014 return;
7015 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007016
Bram Moolenaara742e082016-04-05 21:10:38 +02007017 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007018 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007019 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007020 {
7021 /* When there is an error disable the statusline, otherwise the
7022 * display is messed up with errors and a redraw triggers the problem
7023 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007024 set_string_option_direct((char_u *)"statusline", -1,
7025 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007026 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007027 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007028 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007029 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007030}
7031#endif
7032
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033/*
7034 * Return TRUE if the status line of window "wp" is connected to the status
7035 * line of the window right of it. If not, then it's a vertical separator.
7036 * Only call if (wp->w_vsep_width != 0).
7037 */
7038 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007039stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
7041 frame_T *fr;
7042
7043 fr = wp->w_frame;
7044 while (fr->fr_parent != NULL)
7045 {
7046 if (fr->fr_parent->fr_layout == FR_COL)
7047 {
7048 if (fr->fr_next != NULL)
7049 break;
7050 }
7051 else
7052 {
7053 if (fr->fr_next != NULL)
7054 return TRUE;
7055 }
7056 fr = fr->fr_parent;
7057 }
7058 return FALSE;
7059}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
7061#endif /* FEAT_WINDOWS */
7062
7063#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
7064/*
7065 * Get the value to show for the language mappings, active 'keymap'.
7066 */
7067 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007068get_keymap_str(
7069 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007070 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007071 char_u *buf, /* buffer for the result */
7072 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
7074 char_u *p;
7075
7076 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7077 return FALSE;
7078
7079 {
7080#ifdef FEAT_EVAL
7081 buf_T *old_curbuf = curbuf;
7082 win_T *old_curwin = curwin;
7083 char_u *s;
7084
7085 curbuf = wp->w_buffer;
7086 curwin = wp;
7087 STRCPY(buf, "b:keymap_name"); /* must be writable */
7088 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007089 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 --emsg_skip;
7091 curbuf = old_curbuf;
7092 curwin = old_curwin;
7093 if (p == NULL || *p == NUL)
7094#endif
7095 {
7096#ifdef FEAT_KEYMAP
7097 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7098 p = wp->w_buffer->b_p_keymap;
7099 else
7100#endif
7101 p = (char_u *)"lang";
7102 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007103 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 buf[0] = NUL;
7105#ifdef FEAT_EVAL
7106 vim_free(s);
7107#endif
7108 }
7109 return buf[0] != NUL;
7110}
7111#endif
7112
7113#if defined(FEAT_STL_OPT) || defined(PROTO)
7114/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007115 * Redraw the status line or ruler of window "wp".
7116 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 */
7118 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007119win_redr_custom(
7120 win_T *wp,
7121 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007123 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 int attr;
7125 int curattr;
7126 int row;
7127 int col = 0;
7128 int maxwidth;
7129 int width;
7130 int n;
7131 int len;
7132 int fillchar;
7133 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007134 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007136 struct stl_hlrec hltab[STL_MAX_ITEM];
7137 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007138 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007139 win_T *ewp;
7140 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141
Bram Moolenaar1d633412013-12-11 15:52:01 +01007142 /* There is a tiny chance that this gets called recursively: When
7143 * redrawing a status line triggers redrawing the ruler or tabline.
7144 * Avoid trouble by not allowing recursion. */
7145 if (entered)
7146 return;
7147 entered = TRUE;
7148
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007150 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007152 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007153 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007154 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007155 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007156 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007157 maxwidth = Columns;
7158# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007159 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007162 else
7163 {
7164 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007165 fillchar = fillchar_status(&attr, wp);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007166 maxwidth = W_WIDTH(wp);
7167
7168 if (draw_ruler)
7169 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007170 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007171 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007172 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007173 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007174 if (*++stl == '-')
7175 stl++;
7176 if (atoi((char *)stl))
7177 while (VIM_ISDIGIT(*stl))
7178 stl++;
7179 if (*stl++ != '(')
7180 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007181 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007182#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007183 col = ru_col - (Columns - W_WIDTH(wp));
7184 if (col < (W_WIDTH(wp) + 1) / 2)
7185 col = (W_WIDTH(wp) + 1) / 2;
7186#else
7187 col = ru_col;
7188 if (col > (Columns + 1) / 2)
7189 col = (Columns + 1) / 2;
7190#endif
7191 maxwidth = W_WIDTH(wp) - col;
7192#ifdef FEAT_WINDOWS
7193 if (!wp->w_status_height)
7194#endif
7195 {
7196 row = Rows - 1;
7197 --maxwidth; /* writing in last column may cause scrolling */
7198 fillchar = ' ';
7199 attr = 0;
7200 }
7201
7202# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007203 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007204# endif
7205 }
7206 else
7207 {
7208 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007209 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007210 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007211 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007212# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007213 use_sandbox = was_set_insecurely((char_u *)"statusline",
7214 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007215# endif
7216 }
7217
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007218#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007219 col += W_WINCOL(wp);
7220#endif
7221 }
7222
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007224 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
Bram Moolenaar61452852011-02-01 18:01:11 +01007226 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7227 * the cursor away and back. */
7228 ewp = wp == NULL ? curwin : wp;
7229 p_crb_save = ewp->w_p_crb;
7230 ewp->w_p_crb = FALSE;
7231
Bram Moolenaar362f3562009-11-03 16:20:34 +00007232 /* Make a copy, because the statusline may include a function call that
7233 * might change the option value and free the memory. */
7234 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007235 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007236 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007237 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007238 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007239 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007241 /* Make all characters printable. */
7242 p = transstr(buf);
7243 if (p != NULL)
7244 {
7245 vim_strncpy(buf, p, sizeof(buf) - 1);
7246 vim_free(p);
7247 }
7248
7249 /* fill up with "fillchar" */
7250 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007251 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {
7253#ifdef FEAT_MBYTE
7254 len += (*mb_char2bytes)(fillchar, buf + len);
7255#else
7256 buf[len++] = fillchar;
7257#endif
7258 ++width;
7259 }
7260 buf[len] = NUL;
7261
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007262 /*
7263 * Draw each snippet with the specified highlighting.
7264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 curattr = attr;
7266 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007267 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007269 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 screen_puts_len(p, len, row, col, curattr);
7271 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007272 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007274 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007276 else if (hltab[n].userhl < 0)
7277 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278#ifdef FEAT_WINDOWS
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007279# ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007280 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7281 && wp->w_status_height != 0)
7282 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007283 else if (wp != NULL && bt_terminal(wp->w_buffer)
7284 && wp->w_status_height != 0)
7285 curattr = highlight_stlterm[hltab[n].userhl - 1];
7286# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007287 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007288 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289#endif
7290 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007291 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 }
7293 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007294
7295 if (wp == NULL)
7296 {
7297 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7298 col = 0;
7299 len = 0;
7300 p = buf;
7301 fillchar = 0;
7302 for (n = 0; tabtab[n].start != NULL; n++)
7303 {
7304 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7305 while (col < len)
7306 TabPageIdxs[col++] = fillchar;
7307 p = tabtab[n].start;
7308 fillchar = tabtab[n].userhl;
7309 }
7310 while (col < Columns)
7311 TabPageIdxs[col++] = fillchar;
7312 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007313
7314theend:
7315 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316}
7317
7318#endif /* FEAT_STL_OPT */
7319
7320/*
7321 * Output a single character directly to the screen and update ScreenLines.
7322 */
7323 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007324screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 char_u buf[MB_MAXBYTES + 1];
7327
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007328#ifdef FEAT_MBYTE
7329 if (has_mbyte)
7330 buf[(*mb_char2bytes)(c, buf)] = NUL;
7331 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007333 {
7334 buf[0] = c;
7335 buf[1] = NUL;
7336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 screen_puts(buf, row, col, attr);
7338}
7339
7340/*
7341 * Get a single character directly from ScreenLines into "bytes[]".
7342 * Also return its attribute in *attrp;
7343 */
7344 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007345screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346{
7347 unsigned off;
7348
7349 /* safety check */
7350 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7351 {
7352 off = LineOffset[row] + col;
7353 *attrp = ScreenAttrs[off];
7354 bytes[0] = ScreenLines[off];
7355 bytes[1] = NUL;
7356
7357#ifdef FEAT_MBYTE
7358 if (enc_utf8 && ScreenLinesUC[off] != 0)
7359 bytes[utfc_char2bytes(off, bytes)] = NUL;
7360 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7361 {
7362 bytes[0] = ScreenLines[off];
7363 bytes[1] = ScreenLines2[off];
7364 bytes[2] = NUL;
7365 }
7366 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7367 {
7368 bytes[1] = ScreenLines[off + 1];
7369 bytes[2] = NUL;
7370 }
7371#endif
7372 }
7373}
7374
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007375#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007376static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007377
7378/*
7379 * Return TRUE if composing characters for screen posn "off" differs from
7380 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007381 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007382 */
7383 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007384screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007385{
7386 int i;
7387
7388 for (i = 0; i < Screen_mco; ++i)
7389 {
7390 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7391 return TRUE;
7392 if (u8cc[i] == 0)
7393 break;
7394 }
7395 return FALSE;
7396}
7397#endif
7398
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399/*
7400 * Put string '*text' on the screen at position 'row' and 'col', with
7401 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7402 * Note: only outputs within one row, message is truncated at screen boundary!
7403 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7404 */
7405 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007406screen_puts(
7407 char_u *text,
7408 int row,
7409 int col,
7410 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411{
7412 screen_puts_len(text, -1, row, col, attr);
7413}
7414
7415/*
7416 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7417 * a NUL.
7418 */
7419 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007420screen_puts_len(
7421 char_u *text,
7422 int textlen,
7423 int row,
7424 int col,
7425 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426{
7427 unsigned off;
7428 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007429 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 int c;
7431#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007432 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 int mbyte_blen = 1;
7434 int mbyte_cells = 1;
7435 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007436 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 int clear_next_cell = FALSE;
7438# ifdef FEAT_ARABIC
7439 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007440 int pc, nc, nc1;
7441 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442# endif
7443#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007444#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7445 int force_redraw_this;
7446 int force_redraw_next = FALSE;
7447#endif
7448 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449
7450 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7451 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007452 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453
Bram Moolenaarc236c162008-07-13 17:41:49 +00007454#ifdef FEAT_MBYTE
7455 /* When drawing over the right halve of a double-wide char clear out the
7456 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007457 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007458# ifdef FEAT_GUI
7459 && !gui.in_use
7460# endif
7461 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007462 {
7463 ScreenLines[off - 1] = ' ';
7464 ScreenAttrs[off - 1] = 0;
7465 if (enc_utf8)
7466 {
7467 ScreenLinesUC[off - 1] = 0;
7468 ScreenLinesC[0][off - 1] = 0;
7469 }
7470 /* redraw the previous cell, make it empty */
7471 screen_char(off - 1, row, col - 1);
7472 /* force the cell at "col" to be redrawn */
7473 force_redraw_next = TRUE;
7474 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007475#endif
7476
Bram Moolenaar367329b2007-08-30 11:53:22 +00007477#ifdef FEAT_MBYTE
7478 max_off = LineOffset[row] + screen_Columns;
7479#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007480 while (col < screen_Columns
7481 && (len < 0 || (int)(ptr - text) < len)
7482 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 {
7484 c = *ptr;
7485#ifdef FEAT_MBYTE
7486 /* check if this is the first byte of a multibyte */
7487 if (has_mbyte)
7488 {
7489 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007490 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007492 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7494 mbyte_cells = 1;
7495 else if (enc_dbcs != 0)
7496 mbyte_cells = mbyte_blen;
7497 else /* enc_utf8 */
7498 {
7499 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007500 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 (int)((text + len) - ptr));
7502 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007503 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007505# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 /* Non-BMP character: display as ? or fullwidth ?. */
7507 if (u8c >= 0x10000)
7508 {
7509 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7510 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007511 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007513# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514# ifdef FEAT_ARABIC
7515 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7516 {
7517 /* Do Arabic shaping. */
7518 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7519 {
7520 /* Past end of string to be displayed. */
7521 nc = NUL;
7522 nc1 = NUL;
7523 }
7524 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007525 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007526 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7527 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007528 nc1 = pcc[0];
7529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 pc = prev_c;
7531 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007532 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 }
7534 else
7535 prev_c = u8c;
7536# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007537 if (col + mbyte_cells > screen_Columns)
7538 {
7539 /* Only 1 cell left, but character requires 2 cells:
7540 * display a '>' in the last column to avoid wrapping. */
7541 c = '>';
7542 mbyte_cells = 1;
7543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 }
7545 }
7546#endif
7547
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007548#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7549 force_redraw_this = force_redraw_next;
7550 force_redraw_next = FALSE;
7551#endif
7552
7553 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554#ifdef FEAT_MBYTE
7555 || (mbyte_cells == 2
7556 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7557 || (enc_dbcs == DBCS_JPNU
7558 && c == 0x8e
7559 && ScreenLines2[off] != ptr[1])
7560 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007561 && (ScreenLinesUC[off] !=
7562 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7563 || (ScreenLinesUC[off] != 0
7564 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565#endif
7566 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007567 || exmode_active;
7568
7569 if (need_redraw
7570#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7571 || force_redraw_this
7572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 )
7574 {
7575#if defined(FEAT_GUI) || defined(UNIX)
7576 /* The bold trick makes a single row of pixels appear in the next
7577 * character. When a bold character is removed, the next
7578 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007579 * and for some xterms. */
7580 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581# ifdef FEAT_GUI
7582 gui.in_use
7583# endif
7584# if defined(FEAT_GUI) && defined(UNIX)
7585 ||
7586# endif
7587# ifdef UNIX
7588 term_is_xterm
7589# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007590 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007592 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007594 if (n > HL_ALL)
7595 n = syn_attr2attr(n);
7596 if (n & HL_BOLD)
7597 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 }
7599#endif
7600#ifdef FEAT_MBYTE
7601 /* When at the end of the text and overwriting a two-cell
7602 * character with a one-cell character, need to clear the next
7603 * cell. Also when overwriting the left halve of a two-cell char
7604 * with the right halve of a two-cell char. Do this only once
7605 * (mb_off2cells() may return 2 on the right halve). */
7606 if (clear_next_cell)
7607 clear_next_cell = FALSE;
7608 else if (has_mbyte
7609 && (len < 0 ? ptr[mbyte_blen] == NUL
7610 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007611 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007613 && (*mb_off2cells)(off, max_off) == 1
7614 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 clear_next_cell = TRUE;
7616
7617 /* Make sure we never leave a second byte of a double-byte behind,
7618 * it confuses mb_off2cells(). */
7619 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007620 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007622 && (*mb_off2cells)(off, max_off) == 1
7623 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 ScreenLines[off + mbyte_blen] = 0;
7625#endif
7626 ScreenLines[off] = c;
7627 ScreenAttrs[off] = attr;
7628#ifdef FEAT_MBYTE
7629 if (enc_utf8)
7630 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007631 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 ScreenLinesUC[off] = 0;
7633 else
7634 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007635 int i;
7636
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007638 for (i = 0; i < Screen_mco; ++i)
7639 {
7640 ScreenLinesC[i][off] = u8cc[i];
7641 if (u8cc[i] == 0)
7642 break;
7643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 }
7645 if (mbyte_cells == 2)
7646 {
7647 ScreenLines[off + 1] = 0;
7648 ScreenAttrs[off + 1] = attr;
7649 }
7650 screen_char(off, row, col);
7651 }
7652 else if (mbyte_cells == 2)
7653 {
7654 ScreenLines[off + 1] = ptr[1];
7655 ScreenAttrs[off + 1] = attr;
7656 screen_char_2(off, row, col);
7657 }
7658 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7659 {
7660 ScreenLines2[off] = ptr[1];
7661 screen_char(off, row, col);
7662 }
7663 else
7664#endif
7665 screen_char(off, row, col);
7666 }
7667#ifdef FEAT_MBYTE
7668 if (has_mbyte)
7669 {
7670 off += mbyte_cells;
7671 col += mbyte_cells;
7672 ptr += mbyte_blen;
7673 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007674 {
7675 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007677 len = -1;
7678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
7680 else
7681#endif
7682 {
7683 ++off;
7684 ++col;
7685 ++ptr;
7686 }
7687 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007688
7689#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7690 /* If we detected the next character needs to be redrawn, but the text
7691 * doesn't extend up to there, update the character here. */
7692 if (force_redraw_next && col < screen_Columns)
7693 {
7694# ifdef FEAT_MBYTE
7695 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7696 screen_char_2(off, row, col);
7697 else
7698# endif
7699 screen_char(off, row, col);
7700 }
7701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702}
7703
7704#ifdef FEAT_SEARCH_EXTRA
7705/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007706 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 */
7708 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007709start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
7711 if (p_hls && !no_hlsearch)
7712 {
7713 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007714 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007715# ifdef FEAT_RELTIME
7716 /* Set the time limit to 'redrawtime'. */
7717 profile_setlimit(p_rdt, &search_hl.tm);
7718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 }
7720}
7721
7722/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007723 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 */
7725 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007726end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 if (search_hl.rm.regprog != NULL)
7729 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007730 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 search_hl.rm.regprog = NULL;
7732 }
7733}
7734
7735/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007736 * Init for calling prepare_search_hl().
7737 */
7738 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007739init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007740{
7741 matchitem_T *cur;
7742
7743 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7744 * match */
7745 cur = wp->w_match_head;
7746 while (cur != NULL)
7747 {
7748 cur->hl.rm = cur->match;
7749 if (cur->hlg_id == 0)
7750 cur->hl.attr = 0;
7751 else
7752 cur->hl.attr = syn_id2attr(cur->hlg_id);
7753 cur->hl.buf = wp->w_buffer;
7754 cur->hl.lnum = 0;
7755 cur->hl.first_lnum = 0;
7756# ifdef FEAT_RELTIME
7757 /* Set the time limit to 'redrawtime'. */
7758 profile_setlimit(p_rdt, &(cur->hl.tm));
7759# endif
7760 cur = cur->next;
7761 }
7762 search_hl.buf = wp->w_buffer;
7763 search_hl.lnum = 0;
7764 search_hl.first_lnum = 0;
7765 /* time limit is set at the toplevel, for all windows */
7766}
7767
7768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 * Advance to the match in window "wp" line "lnum" or past it.
7770 */
7771 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007772prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007774 matchitem_T *cur; /* points to the match list */
7775 match_T *shl; /* points to search_hl or a match */
7776 int shl_flag; /* flag to indicate whether search_hl
7777 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007778 int pos_inprogress; /* marks that position match search is
7779 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 int n;
7781
7782 /*
7783 * When using a multi-line pattern, start searching at the top
7784 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007785 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007787 cur = wp->w_match_head;
7788 shl_flag = FALSE;
7789 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007791 if (shl_flag == FALSE)
7792 {
7793 shl = &search_hl;
7794 shl_flag = TRUE;
7795 }
7796 else
7797 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 if (shl->rm.regprog != NULL
7799 && shl->lnum == 0
7800 && re_multiline(shl->rm.regprog))
7801 {
7802 if (shl->first_lnum == 0)
7803 {
7804# ifdef FEAT_FOLDING
7805 for (shl->first_lnum = lnum;
7806 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7807 if (hasFoldingWin(wp, shl->first_lnum - 1,
7808 NULL, NULL, TRUE, NULL))
7809 break;
7810# else
7811 shl->first_lnum = wp->w_topline;
7812# endif
7813 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007814 if (cur != NULL)
7815 cur->pos.cur = 0;
7816 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007818 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7819 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007821 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7822 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007823 pos_inprogress = cur == NULL || cur->pos.cur == 0
7824 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 if (shl->lnum != 0)
7826 {
7827 shl->first_lnum = shl->lnum
7828 + shl->rm.endpos[0].lnum
7829 - shl->rm.startpos[0].lnum;
7830 n = shl->rm.endpos[0].col;
7831 }
7832 else
7833 {
7834 ++shl->first_lnum;
7835 n = 0;
7836 }
7837 }
7838 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007839 if (shl != &search_hl && cur != NULL)
7840 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
7842}
7843
7844/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007845 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 * Uses shl->buf.
7847 * Sets shl->lnum and shl->rm contents.
7848 * Note: Assumes a previous match is always before "lnum", unless
7849 * shl->lnum is zero.
7850 * Careful: Any pointers for buffer lines will become invalid.
7851 */
7852 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007853next_search_hl(
7854 win_T *win,
7855 match_T *shl, /* points to search_hl or a match */
7856 linenr_T lnum,
7857 colnr_T mincol, /* minimal column for a match */
7858 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860 linenr_T l;
7861 colnr_T matchcol;
7862 long nmatched;
7863
7864 if (shl->lnum != 0)
7865 {
7866 /* Check for three situations:
7867 * 1. If the "lnum" is below a previous match, start a new search.
7868 * 2. If the previous match includes "mincol", use it.
7869 * 3. Continue after the previous match.
7870 */
7871 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7872 if (lnum > l)
7873 shl->lnum = 0;
7874 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7875 return;
7876 }
7877
7878 /*
7879 * Repeat searching for a match until one is found that includes "mincol"
7880 * or none is found in this line.
7881 */
7882 called_emsg = FALSE;
7883 for (;;)
7884 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007885#ifdef FEAT_RELTIME
7886 /* Stop searching after passing the time limit. */
7887 if (profile_passed_limit(&(shl->tm)))
7888 {
7889 shl->lnum = 0; /* no match found in time */
7890 break;
7891 }
7892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 /* Three situations:
7894 * 1. No useful previous match: search from start of line.
7895 * 2. Not Vi compatible or empty match: continue at next character.
7896 * Break the loop if this is beyond the end of the line.
7897 * 3. Vi compatible searching: continue at end of previous match.
7898 */
7899 if (shl->lnum == 0)
7900 matchcol = 0;
7901 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7902 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007903 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007905 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007906
7907 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007908 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007909 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007911 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 shl->lnum = 0;
7913 break;
7914 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007915#ifdef FEAT_MBYTE
7916 if (has_mbyte)
7917 matchcol += mb_ptr2len(ml);
7918 else
7919#endif
7920 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 }
7922 else
7923 matchcol = shl->rm.endpos[0].col;
7924
7925 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007926 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007928 /* Remember whether shl->rm is using a copy of the regprog in
7929 * cur->match. */
7930 int regprog_is_copy = (shl != &search_hl && cur != NULL
7931 && shl == &cur->hl
7932 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007933 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007934
Bram Moolenaarb3414592014-06-17 17:48:32 +02007935 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7936 matchcol,
7937#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007938 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007939#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007940 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007941#endif
7942 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007943 /* Copy the regprog, in case it got freed and recompiled. */
7944 if (regprog_is_copy)
7945 cur->match.regprog = cur->hl.rm.regprog;
7946
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007947 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007948 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007949 /* Error while handling regexp: stop using this regexp. */
7950 if (shl == &search_hl)
7951 {
7952 /* don't free regprog in the match list, it's a copy */
7953 vim_regfree(shl->rm.regprog);
7954 SET_NO_HLSEARCH(TRUE);
7955 }
7956 shl->rm.regprog = NULL;
7957 shl->lnum = 0;
7958 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7959 message */
7960 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007961 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007962 }
7963 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007964 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007965 else
7966 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 if (nmatched == 0)
7968 {
7969 shl->lnum = 0; /* no match found */
7970 break;
7971 }
7972 if (shl->rm.startpos[0].lnum > 0
7973 || shl->rm.startpos[0].col >= mincol
7974 || nmatched > 1
7975 || shl->rm.endpos[0].col > mincol)
7976 {
7977 shl->lnum += shl->rm.startpos[0].lnum;
7978 break; /* useful match found */
7979 }
7980 }
7981}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982
Bram Moolenaar85077472016-10-16 14:35:48 +02007983/*
7984 * If there is a match fill "shl" and return one.
7985 * Return zero otherwise.
7986 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007987 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007988next_search_hl_pos(
7989 match_T *shl, /* points to a match */
7990 linenr_T lnum,
7991 posmatch_T *posmatch, /* match positions */
7992 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007993{
7994 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007995 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007996
Bram Moolenaarb3414592014-06-17 17:48:32 +02007997 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7998 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007999 llpos_T *pos = &posmatch->pos[i];
8000
8001 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008002 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008003 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008004 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008005 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008006 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008007 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008008 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008009 /* if this match comes before the one at "found" then swap
8010 * them */
8011 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008012 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008013 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008014
Bram Moolenaar85077472016-10-16 14:35:48 +02008015 *pos = posmatch->pos[found];
8016 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008017 }
8018 }
8019 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008020 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008021 }
8022 }
8023 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008024 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008025 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008026 colnr_T start = posmatch->pos[found].col == 0
8027 ? 0 : posmatch->pos[found].col - 1;
8028 colnr_T end = posmatch->pos[found].col == 0
8029 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008030
Bram Moolenaar85077472016-10-16 14:35:48 +02008031 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008032 shl->rm.startpos[0].lnum = 0;
8033 shl->rm.startpos[0].col = start;
8034 shl->rm.endpos[0].lnum = 0;
8035 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008036 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008037 posmatch->cur = found + 1;
8038 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008039 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008040 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008041}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008042#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008043
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008045screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046{
8047 attrentry_T *aep = NULL;
8048
8049 screen_attr = attr;
8050 if (full_screen
8051#ifdef WIN3264
8052 && termcap_active
8053#endif
8054 )
8055 {
8056#ifdef FEAT_GUI
8057 if (gui.in_use)
8058 {
8059 char buf[20];
8060
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008061 /* The GUI handles this internally. */
8062 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 OUT_STR(buf);
8064 }
8065 else
8066#endif
8067 {
8068 if (attr > HL_ALL) /* special HL attr. */
8069 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008070 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 aep = syn_cterm_attr2entry(attr);
8072 else
8073 aep = syn_term_attr2entry(attr);
8074 if (aep == NULL) /* did ":syntax clear" */
8075 attr = 0;
8076 else
8077 attr = aep->ae_attr;
8078 }
8079 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8080 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008081 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008082#ifdef FEAT_TERMGUICOLORS
8083 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008084 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008085#endif
8086 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008087#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008088 )
8089#endif
8090 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008091 /* If the Normal FG color has BOLD attribute and the new HL
8092 * has a FG color defined, clear BOLD. */
8093 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8095 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008096 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8097 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 out_str(T_US);
8099 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8100 out_str(T_CZH);
8101 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8102 out_str(T_MR);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008103 if ((attr & HL_STRIKETHROUGH) && T_STS != NULL) /* strike */
8104 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105
8106 /*
8107 * Output the color or start string after bold etc., in case the
8108 * bold etc. override the color setting.
8109 */
8110 if (aep != NULL)
8111 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008112#ifdef FEAT_TERMGUICOLORS
8113 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008115 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008116 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008117 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008118 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 }
8120 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008123 if (t_colors > 1)
8124 {
8125 if (aep->ae_u.cterm.fg_color)
8126 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8127 if (aep->ae_u.cterm.bg_color)
8128 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8129 }
8130 else
8131 {
8132 if (aep->ae_u.term.start != NULL)
8133 out_str(aep->ae_u.term.start);
8134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 }
8136 }
8137 }
8138 }
8139}
8140
8141 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008142screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143{
8144 int do_ME = FALSE; /* output T_ME code */
8145
8146 if (screen_attr != 0
8147#ifdef WIN3264
8148 && termcap_active
8149#endif
8150 )
8151 {
8152#ifdef FEAT_GUI
8153 if (gui.in_use)
8154 {
8155 char buf[20];
8156
8157 /* use internal GUI code */
8158 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8159 OUT_STR(buf);
8160 }
8161 else
8162#endif
8163 {
8164 if (screen_attr > HL_ALL) /* special HL attr. */
8165 {
8166 attrentry_T *aep;
8167
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008168 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {
8170 /*
8171 * Assume that t_me restores the original colors!
8172 */
8173 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008174 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008175#ifdef FEAT_TERMGUICOLORS
8176 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008177 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8178 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008179#endif
8180 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008181#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008182 )
8183#endif
8184 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 do_ME = TRUE;
8186 }
8187 else
8188 {
8189 aep = syn_term_attr2entry(screen_attr);
8190 if (aep != NULL && aep->ae_u.term.stop != NULL)
8191 {
8192 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8193 do_ME = TRUE;
8194 else
8195 out_str(aep->ae_u.term.stop);
8196 }
8197 }
8198 if (aep == NULL) /* did ":syntax clear" */
8199 screen_attr = 0;
8200 else
8201 screen_attr = aep->ae_attr;
8202 }
8203
8204 /*
8205 * Often all ending-codes are equal to T_ME. Avoid outputting the
8206 * same sequence several times.
8207 */
8208 if (screen_attr & HL_STANDOUT)
8209 {
8210 if (STRCMP(T_SE, T_ME) == 0)
8211 do_ME = TRUE;
8212 else
8213 out_str(T_SE);
8214 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008215 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {
8217 if (STRCMP(T_UE, T_ME) == 0)
8218 do_ME = TRUE;
8219 else
8220 out_str(T_UE);
8221 }
8222 if (screen_attr & HL_ITALIC)
8223 {
8224 if (STRCMP(T_CZR, T_ME) == 0)
8225 do_ME = TRUE;
8226 else
8227 out_str(T_CZR);
8228 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008229 if (screen_attr & HL_STRIKETHROUGH)
8230 {
8231 if (STRCMP(T_STE, T_ME) == 0)
8232 do_ME = TRUE;
8233 else
8234 out_str(T_STE);
8235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8237 out_str(T_ME);
8238
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008239#ifdef FEAT_TERMGUICOLORS
8240 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008242 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008243 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008244 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008245 term_bg_rgb_color(cterm_normal_bg_gui_color);
8246 }
8247 else
8248#endif
8249 {
8250 if (t_colors > 1)
8251 {
8252 /* set Normal cterm colors */
8253 if (cterm_normal_fg_color != 0)
8254 term_fg_color(cterm_normal_fg_color - 1);
8255 if (cterm_normal_bg_color != 0)
8256 term_bg_color(cterm_normal_bg_color - 1);
8257 if (cterm_normal_fg_bold)
8258 out_str(T_MD);
8259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 }
8261 }
8262 }
8263 screen_attr = 0;
8264}
8265
8266/*
8267 * Reset the colors for a cterm. Used when leaving Vim.
8268 * The machine specific code may override this again.
8269 */
8270 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008271reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008273 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {
8275 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008276#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008277 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8278 || cterm_normal_bg_gui_color != INVALCOLOR)
8279 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008280#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {
8284 out_str(T_OP);
8285 screen_attr = -1;
8286 }
8287 if (cterm_normal_fg_bold)
8288 {
8289 out_str(T_ME);
8290 screen_attr = -1;
8291 }
8292 }
8293}
8294
8295/*
8296 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8297 * using the attributes from ScreenAttrs["off"].
8298 */
8299 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008300screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301{
8302 int attr;
8303
8304 /* Check for illegal values, just in case (could happen just after
8305 * resizing). */
8306 if (row >= screen_Rows || col >= screen_Columns)
8307 return;
8308
Bram Moolenaar494838a2015-02-10 19:20:37 +01008309 /* Outputting a character in the last cell on the screen may scroll the
8310 * screen up. Only do it when the "xn" termcap property is set, otherwise
8311 * mark the character invalid (update it when scrolled up). */
8312 if (*T_XN == NUL
8313 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314#ifdef FEAT_RIGHTLEFT
8315 /* account for first command-line character in rightleft mode */
8316 && !cmdmsg_rl
8317#endif
8318 )
8319 {
8320 ScreenAttrs[off] = (sattr_T)-1;
8321 return;
8322 }
8323
8324 /*
8325 * Stop highlighting first, so it's easier to move the cursor.
8326 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008327#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 if (screen_char_attr != 0)
8329 attr = screen_char_attr;
8330 else
8331#endif
8332 attr = ScreenAttrs[off];
8333 if (screen_attr != attr)
8334 screen_stop_highlight();
8335
8336 windgoto(row, col);
8337
8338 if (screen_attr != attr)
8339 screen_start_highlight(attr);
8340
8341#ifdef FEAT_MBYTE
8342 if (enc_utf8 && ScreenLinesUC[off] != 0)
8343 {
8344 char_u buf[MB_MAXBYTES + 1];
8345
8346 /* Convert UTF-8 character to bytes and write it. */
8347
8348 buf[utfc_char2bytes(off, buf)] = NUL;
8349
8350 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008351 if (utf_ambiguous_width(ScreenLinesUC[off]))
8352 screen_cur_col = 9999;
8353 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 ++screen_cur_col;
8355 }
8356 else
8357#endif
8358 {
8359#ifdef FEAT_MBYTE
8360 out_flush_check();
8361#endif
8362 out_char(ScreenLines[off]);
8363#ifdef FEAT_MBYTE
8364 /* double-byte character in single-width cell */
8365 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8366 out_char(ScreenLines2[off]);
8367#endif
8368 }
8369
8370 screen_cur_col++;
8371}
8372
8373#ifdef FEAT_MBYTE
8374
8375/*
8376 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8377 * on the screen at position 'row' and 'col'.
8378 * The attributes of the first byte is used for all. This is required to
8379 * output the two bytes of a double-byte character with nothing in between.
8380 */
8381 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008382screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383{
8384 /* Check for illegal values (could be wrong when screen was resized). */
8385 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8386 return;
8387
8388 /* Outputting the last character on the screen may scrollup the screen.
8389 * Don't to it! Mark the character invalid (update it when scrolled up) */
8390 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8391 {
8392 ScreenAttrs[off] = (sattr_T)-1;
8393 return;
8394 }
8395
8396 /* Output the first byte normally (positions the cursor), then write the
8397 * second byte directly. */
8398 screen_char(off, row, col);
8399 out_char(ScreenLines[off + 1]);
8400 ++screen_cur_col;
8401}
8402#endif
8403
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008404#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405/*
8406 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8407 * This uses the contents of ScreenLines[] and doesn't change it.
8408 */
8409 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008410screen_draw_rectangle(
8411 int row,
8412 int col,
8413 int height,
8414 int width,
8415 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416{
8417 int r, c;
8418 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008419#ifdef FEAT_MBYTE
8420 int max_off;
8421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008423 /* Can't use ScreenLines unless initialized */
8424 if (ScreenLines == NULL)
8425 return;
8426
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 if (invert)
8428 screen_char_attr = HL_INVERSE;
8429 for (r = row; r < row + height; ++r)
8430 {
8431 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008432#ifdef FEAT_MBYTE
8433 max_off = off + screen_Columns;
8434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 for (c = col; c < col + width; ++c)
8436 {
8437#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008438 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {
8440 screen_char_2(off + c, r, c);
8441 ++c;
8442 }
8443 else
8444#endif
8445 {
8446 screen_char(off + c, r, c);
8447#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008448 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 ++c;
8450#endif
8451 }
8452 }
8453 }
8454 screen_char_attr = 0;
8455}
8456#endif
8457
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008458#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459/*
8460 * Redraw the characters for a vertically split window.
8461 */
8462 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008463redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
8465 int col;
8466 int width;
8467
8468# ifdef FEAT_CLIPBOARD
8469 clip_may_clear_selection(row, end - 1);
8470# endif
8471
8472 if (wp == NULL)
8473 {
8474 col = 0;
8475 width = Columns;
8476 }
8477 else
8478 {
8479 col = wp->w_wincol;
8480 width = wp->w_width;
8481 }
8482 screen_draw_rectangle(row, col, end - row, width, FALSE);
8483}
8484#endif
8485
8486/*
8487 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8488 * with character 'c1' in first column followed by 'c2' in the other columns.
8489 * Use attributes 'attr'.
8490 */
8491 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008492screen_fill(
8493 int start_row,
8494 int end_row,
8495 int start_col,
8496 int end_col,
8497 int c1,
8498 int c2,
8499 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500{
8501 int row;
8502 int col;
8503 int off;
8504 int end_off;
8505 int did_delete;
8506 int c;
8507 int norm_term;
8508#if defined(FEAT_GUI) || defined(UNIX)
8509 int force_next = FALSE;
8510#endif
8511
8512 if (end_row > screen_Rows) /* safety check */
8513 end_row = screen_Rows;
8514 if (end_col > screen_Columns) /* safety check */
8515 end_col = screen_Columns;
8516 if (ScreenLines == NULL
8517 || start_row >= end_row
8518 || start_col >= end_col) /* nothing to do */
8519 return;
8520
8521 /* it's a "normal" terminal when not in a GUI or cterm */
8522 norm_term = (
8523#ifdef FEAT_GUI
8524 !gui.in_use &&
8525#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008526 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 for (row = start_row; row < end_row; ++row)
8528 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008529#ifdef FEAT_MBYTE
8530 if (has_mbyte
8531# ifdef FEAT_GUI
8532 && !gui.in_use
8533# endif
8534 )
8535 {
8536 /* When drawing over the right halve of a double-wide char clear
8537 * out the left halve. When drawing over the left halve of a
8538 * double wide-char clear out the right halve. Only needed in a
8539 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008540 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008541 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008542 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008543 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008544 }
8545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 /*
8547 * Try to use delete-line termcap code, when no attributes or in a
8548 * "normal" terminal, where a bold/italic space is just a
8549 * space.
8550 */
8551 did_delete = FALSE;
8552 if (c2 == ' '
8553 && end_col == Columns
8554 && can_clear(T_CE)
8555 && (attr == 0
8556 || (norm_term
8557 && attr <= HL_ALL
8558 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8559 {
8560 /*
8561 * check if we really need to clear something
8562 */
8563 col = start_col;
8564 if (c1 != ' ') /* don't clear first char */
8565 ++col;
8566
8567 off = LineOffset[row] + col;
8568 end_off = LineOffset[row] + end_col;
8569
8570 /* skip blanks (used often, keep it fast!) */
8571#ifdef FEAT_MBYTE
8572 if (enc_utf8)
8573 while (off < end_off && ScreenLines[off] == ' '
8574 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8575 ++off;
8576 else
8577#endif
8578 while (off < end_off && ScreenLines[off] == ' '
8579 && ScreenAttrs[off] == 0)
8580 ++off;
8581 if (off < end_off) /* something to be cleared */
8582 {
8583 col = off - LineOffset[row];
8584 screen_stop_highlight();
8585 term_windgoto(row, col);/* clear rest of this screen line */
8586 out_str(T_CE);
8587 screen_start(); /* don't know where cursor is now */
8588 col = end_col - col;
8589 while (col--) /* clear chars in ScreenLines */
8590 {
8591 ScreenLines[off] = ' ';
8592#ifdef FEAT_MBYTE
8593 if (enc_utf8)
8594 ScreenLinesUC[off] = 0;
8595#endif
8596 ScreenAttrs[off] = 0;
8597 ++off;
8598 }
8599 }
8600 did_delete = TRUE; /* the chars are cleared now */
8601 }
8602
8603 off = LineOffset[row] + start_col;
8604 c = c1;
8605 for (col = start_col; col < end_col; ++col)
8606 {
8607 if (ScreenLines[off] != c
8608#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008609 || (enc_utf8 && (int)ScreenLinesUC[off]
8610 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611#endif
8612 || ScreenAttrs[off] != attr
8613#if defined(FEAT_GUI) || defined(UNIX)
8614 || force_next
8615#endif
8616 )
8617 {
8618#if defined(FEAT_GUI) || defined(UNIX)
8619 /* The bold trick may make a single row of pixels appear in
8620 * the next character. When a bold character is removed, the
8621 * next character should be redrawn too. This happens for our
8622 * own GUI and for some xterms. */
8623 if (
8624# ifdef FEAT_GUI
8625 gui.in_use
8626# endif
8627# if defined(FEAT_GUI) && defined(UNIX)
8628 ||
8629# endif
8630# ifdef UNIX
8631 term_is_xterm
8632# endif
8633 )
8634 {
8635 if (ScreenLines[off] != ' '
8636 && (ScreenAttrs[off] > HL_ALL
8637 || ScreenAttrs[off] & HL_BOLD))
8638 force_next = TRUE;
8639 else
8640 force_next = FALSE;
8641 }
8642#endif
8643 ScreenLines[off] = c;
8644#ifdef FEAT_MBYTE
8645 if (enc_utf8)
8646 {
8647 if (c >= 0x80)
8648 {
8649 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008650 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 }
8652 else
8653 ScreenLinesUC[off] = 0;
8654 }
8655#endif
8656 ScreenAttrs[off] = attr;
8657 if (!did_delete || c != ' ')
8658 screen_char(off, row, col);
8659 }
8660 ++off;
8661 if (col == start_col)
8662 {
8663 if (did_delete)
8664 break;
8665 c = c2;
8666 }
8667 }
8668 if (end_col == Columns)
8669 LineWraps[row] = FALSE;
8670 if (row == Rows - 1) /* overwritten the command line */
8671 {
8672 redraw_cmdline = TRUE;
8673 if (c1 == ' ' && c2 == ' ')
8674 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008675 if (start_col == 0)
8676 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 }
8678 }
8679}
8680
8681/*
8682 * Check if there should be a delay. Used before clearing or redrawing the
8683 * screen or the command line.
8684 */
8685 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008686check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
8688 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8689 && !did_wait_return
8690 && emsg_silent == 0)
8691 {
8692 out_flush();
8693 ui_delay(1000L, TRUE);
8694 emsg_on_display = FALSE;
8695 if (check_msg_scroll)
8696 msg_scroll = FALSE;
8697 }
8698}
8699
8700/*
8701 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008702 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 * Returns TRUE if there is a valid screen to write to.
8704 * Returns FALSE when starting up and screen not initialized yet.
8705 */
8706 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008707screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008709 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 return (ScreenLines != NULL);
8711}
8712
8713/*
8714 * Resize the shell to Rows and Columns.
8715 * Allocate ScreenLines[] and associated items.
8716 *
8717 * There may be some time between setting Rows and Columns and (re)allocating
8718 * ScreenLines[]. This happens when starting up and when (manually) changing
8719 * the shell size. Always use screen_Rows and screen_Columns to access items
8720 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8721 * final size of the shell is needed.
8722 */
8723 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008724screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725{
8726 int new_row, old_row;
8727#ifdef FEAT_GUI
8728 int old_Rows;
8729#endif
8730 win_T *wp;
8731 int outofmem = FALSE;
8732 int len;
8733 schar_T *new_ScreenLines;
8734#ifdef FEAT_MBYTE
8735 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008736 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008738 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739#endif
8740 sattr_T *new_ScreenAttrs;
8741 unsigned *new_LineOffset;
8742 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008743#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008744 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008745 tabpage_T *tp;
8746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008748 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008749#ifdef FEAT_AUTOCMD
8750 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008752retry:
8753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 /*
8755 * Allocation of the screen buffers is done only when the size changes and
8756 * when Rows and Columns have been set and we have started doing full
8757 * screen stuff.
8758 */
8759 if ((ScreenLines != NULL
8760 && Rows == screen_Rows
8761 && Columns == screen_Columns
8762#ifdef FEAT_MBYTE
8763 && enc_utf8 == (ScreenLinesUC != NULL)
8764 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008765 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766#endif
8767 )
8768 || Rows == 0
8769 || Columns == 0
8770 || (!full_screen && ScreenLines == NULL))
8771 return;
8772
8773 /*
8774 * It's possible that we produce an out-of-memory message below, which
8775 * will cause this function to be called again. To break the loop, just
8776 * return here.
8777 */
8778 if (entered)
8779 return;
8780 entered = TRUE;
8781
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008782 /*
8783 * Note that the window sizes are updated before reallocating the arrays,
8784 * thus we must not redraw here!
8785 */
8786 ++RedrawingDisabled;
8787
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 win_new_shellsize(); /* fit the windows in the new sized shell */
8789
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 comp_col(); /* recompute columns for shown command and ruler */
8791
8792 /*
8793 * We're changing the size of the screen.
8794 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8795 * - Move lines from the old arrays into the new arrays, clear extra
8796 * lines (unless the screen is going to be cleared).
8797 * - Free the old arrays.
8798 *
8799 * If anything fails, make ScreenLines NULL, so we don't do anything!
8800 * Continuing with the old ScreenLines may result in a crash, because the
8801 * size is wrong.
8802 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008803 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008805#ifdef FEAT_AUTOCMD
8806 if (aucmd_win != NULL)
8807 win_free_lsize(aucmd_win);
8808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809
8810 new_ScreenLines = (schar_T *)lalloc((long_u)(
8811 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8812#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008813 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 if (enc_utf8)
8815 {
8816 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8817 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008818 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008819 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8821 }
8822 if (enc_dbcs == DBCS_JPNU)
8823 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8824 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8825#endif
8826 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8827 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8828 new_LineOffset = (unsigned *)lalloc((long_u)(
8829 Rows * sizeof(unsigned)), FALSE);
8830 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008831#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008832 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008835 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 {
8837 if (win_alloc_lines(wp) == FAIL)
8838 {
8839 outofmem = TRUE;
8840#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008841 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842#endif
8843 }
8844 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008845#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008846 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8847 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008848 outofmem = TRUE;
8849#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008850#ifdef FEAT_WINDOWS
8851give_up:
8852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008854#ifdef FEAT_MBYTE
8855 for (i = 0; i < p_mco; ++i)
8856 if (new_ScreenLinesC[i] == NULL)
8857 break;
8858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 if (new_ScreenLines == NULL
8860#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008861 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8863#endif
8864 || new_ScreenAttrs == NULL
8865 || new_LineOffset == NULL
8866 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008867#ifdef FEAT_WINDOWS
8868 || new_TabPageIdxs == NULL
8869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 || outofmem)
8871 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008872 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008873 {
8874 /* guess the size */
8875 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8876
8877 /* Remember we did this to avoid getting outofmem messages over
8878 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008879 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 vim_free(new_ScreenLines);
8882 new_ScreenLines = NULL;
8883#ifdef FEAT_MBYTE
8884 vim_free(new_ScreenLinesUC);
8885 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008886 for (i = 0; i < p_mco; ++i)
8887 {
8888 vim_free(new_ScreenLinesC[i]);
8889 new_ScreenLinesC[i] = NULL;
8890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 vim_free(new_ScreenLines2);
8892 new_ScreenLines2 = NULL;
8893#endif
8894 vim_free(new_ScreenAttrs);
8895 new_ScreenAttrs = NULL;
8896 vim_free(new_LineOffset);
8897 new_LineOffset = NULL;
8898 vim_free(new_LineWraps);
8899 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008900#ifdef FEAT_WINDOWS
8901 vim_free(new_TabPageIdxs);
8902 new_TabPageIdxs = NULL;
8903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 }
8905 else
8906 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008907 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008908
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 for (new_row = 0; new_row < Rows; ++new_row)
8910 {
8911 new_LineOffset[new_row] = new_row * Columns;
8912 new_LineWraps[new_row] = FALSE;
8913
8914 /*
8915 * If the screen is not going to be cleared, copy as much as
8916 * possible from the old screen to the new one and clear the rest
8917 * (used when resizing the window at the "--more--" prompt or when
8918 * executing an external command, for the GUI).
8919 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008920 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 {
8922 (void)vim_memset(new_ScreenLines + new_row * Columns,
8923 ' ', (size_t)Columns * sizeof(schar_T));
8924#ifdef FEAT_MBYTE
8925 if (enc_utf8)
8926 {
8927 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8928 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008929 for (i = 0; i < p_mco; ++i)
8930 (void)vim_memset(new_ScreenLinesC[i]
8931 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 0, (size_t)Columns * sizeof(u8char_T));
8933 }
8934 if (enc_dbcs == DBCS_JPNU)
8935 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8936 0, (size_t)Columns * sizeof(schar_T));
8937#endif
8938 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8939 0, (size_t)Columns * sizeof(sattr_T));
8940 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008941 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 {
8943 if (screen_Columns < Columns)
8944 len = screen_Columns;
8945 else
8946 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008947#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008948 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008949 * may be invalid now. Also when p_mco changes. */
8950 if (!(enc_utf8 && ScreenLinesUC == NULL)
8951 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008952#endif
8953 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8954 ScreenLines + LineOffset[old_row],
8955 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008957 if (enc_utf8 && ScreenLinesUC != NULL
8958 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 {
8960 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8961 ScreenLinesUC + LineOffset[old_row],
8962 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008963 for (i = 0; i < p_mco; ++i)
8964 mch_memmove(new_ScreenLinesC[i]
8965 + new_LineOffset[new_row],
8966 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 (size_t)len * sizeof(u8char_T));
8968 }
8969 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8970 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8971 ScreenLines2 + LineOffset[old_row],
8972 (size_t)len * sizeof(schar_T));
8973#endif
8974 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8975 ScreenAttrs + LineOffset[old_row],
8976 (size_t)len * sizeof(sattr_T));
8977 }
8978 }
8979 }
8980 /* Use the last line of the screen for the current line. */
8981 current_ScreenLine = new_ScreenLines + Rows * Columns;
8982 }
8983
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008984 free_screenlines();
8985
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 ScreenLines = new_ScreenLines;
8987#ifdef FEAT_MBYTE
8988 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008989 for (i = 0; i < p_mco; ++i)
8990 ScreenLinesC[i] = new_ScreenLinesC[i];
8991 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 ScreenLines2 = new_ScreenLines2;
8993#endif
8994 ScreenAttrs = new_ScreenAttrs;
8995 LineOffset = new_LineOffset;
8996 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008997#ifdef FEAT_WINDOWS
8998 TabPageIdxs = new_TabPageIdxs;
8999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000
9001 /* It's important that screen_Rows and screen_Columns reflect the actual
9002 * size of ScreenLines[]. Set them before calling anything. */
9003#ifdef FEAT_GUI
9004 old_Rows = screen_Rows;
9005#endif
9006 screen_Rows = Rows;
9007 screen_Columns = Columns;
9008
9009 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009010 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 screenclear2();
9012
9013#ifdef FEAT_GUI
9014 else if (gui.in_use
9015 && !gui.starting
9016 && ScreenLines != NULL
9017 && old_Rows != Rows)
9018 {
9019 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9020 /*
9021 * Adjust the position of the cursor, for when executing an external
9022 * command.
9023 */
9024 if (msg_row >= Rows) /* Rows got smaller */
9025 msg_row = Rows - 1; /* put cursor at last row */
9026 else if (Rows > old_Rows) /* Rows got bigger */
9027 msg_row += Rows - old_Rows; /* put cursor in same place */
9028 if (msg_col >= Columns) /* Columns got smaller */
9029 msg_col = Columns - 1; /* put cursor at last column */
9030 }
9031#endif
9032
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009034 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009035
9036#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009037 /*
9038 * Do not apply autocommands more than 3 times to avoid an endless loop
9039 * in case applying autocommands always changes Rows or Columns.
9040 */
9041 if (starting == 0 && ++retry_count <= 3)
9042 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009043 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009044 /* In rare cases, autocommands may have altered Rows or Columns,
9045 * jump back to check if we need to allocate the screen again. */
9046 goto retry;
9047 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049}
9050
9051 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009052free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009053{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009054#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009055 int i;
9056
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009057 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009058 for (i = 0; i < Screen_mco; ++i)
9059 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009060 vim_free(ScreenLines2);
9061#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009062 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009063 vim_free(ScreenAttrs);
9064 vim_free(LineOffset);
9065 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009066#ifdef FEAT_WINDOWS
9067 vim_free(TabPageIdxs);
9068#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009069}
9070
9071 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009072screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073{
9074 check_for_delay(FALSE);
9075 screenalloc(FALSE); /* allocate screen buffers if size changed */
9076 screenclear2(); /* clear the screen */
9077}
9078
9079 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009080screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082 int i;
9083
9084 if (starting == NO_SCREEN || ScreenLines == NULL
9085#ifdef FEAT_GUI
9086 || (gui.in_use && gui.starting)
9087#endif
9088 )
9089 return;
9090
9091#ifdef FEAT_GUI
9092 if (!gui.in_use)
9093#endif
9094 screen_attr = -1; /* force setting the Normal colors */
9095 screen_stop_highlight(); /* don't want highlighting here */
9096
9097#ifdef FEAT_CLIPBOARD
9098 /* disable selection without redrawing it */
9099 clip_scroll_selection(9999);
9100#endif
9101
9102 /* blank out ScreenLines */
9103 for (i = 0; i < Rows; ++i)
9104 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009105 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 LineWraps[i] = FALSE;
9107 }
9108
9109 if (can_clear(T_CL))
9110 {
9111 out_str(T_CL); /* clear the display */
9112 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009113 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 }
9115 else
9116 {
9117 /* can't clear the screen, mark all chars with invalid attributes */
9118 for (i = 0; i < Rows; ++i)
9119 lineinvalid(LineOffset[i], (int)Columns);
9120 clear_cmdline = TRUE;
9121 }
9122
9123 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9124
9125 win_rest_invalid(firstwin);
9126 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009127#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009128 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 if (must_redraw == CLEAR) /* no need to clear again */
9131 must_redraw = NOT_VALID;
9132 compute_cmdrow();
9133 msg_row = cmdline_row; /* put cursor on last line for messages */
9134 msg_col = 0;
9135 screen_start(); /* don't know where cursor is now */
9136 msg_scrolled = 0; /* can't scroll back */
9137 msg_didany = FALSE;
9138 msg_didout = FALSE;
9139}
9140
9141/*
9142 * Clear one line in ScreenLines.
9143 */
9144 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009145lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
9147 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9148#ifdef FEAT_MBYTE
9149 if (enc_utf8)
9150 (void)vim_memset(ScreenLinesUC + off, 0,
9151 (size_t)width * sizeof(u8char_T));
9152#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009153 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154}
9155
9156/*
9157 * Mark one line in ScreenLines invalid by setting the attributes to an
9158 * invalid value.
9159 */
9160 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009161lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162{
9163 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9164}
9165
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009166#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167/*
9168 * Copy part of a Screenline for vertically split window "wp".
9169 */
9170 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009171linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172{
9173 unsigned off_to = LineOffset[to] + wp->w_wincol;
9174 unsigned off_from = LineOffset[from] + wp->w_wincol;
9175
9176 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9177 wp->w_width * sizeof(schar_T));
9178# ifdef FEAT_MBYTE
9179 if (enc_utf8)
9180 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009181 int i;
9182
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9184 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009185 for (i = 0; i < p_mco; ++i)
9186 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9187 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 }
9189 if (enc_dbcs == DBCS_JPNU)
9190 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9191 wp->w_width * sizeof(schar_T));
9192# endif
9193 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9194 wp->w_width * sizeof(sattr_T));
9195}
9196#endif
9197
9198/*
9199 * Return TRUE if clearing with term string "p" would work.
9200 * It can't work when the string is empty or it won't set the right background.
9201 */
9202 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009203can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204{
9205 return (*p != NUL && (t_colors <= 1
9206#ifdef FEAT_GUI
9207 || gui.in_use
9208#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009209#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009210 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009211 || (!p_tgc && cterm_normal_bg_color == 0)
9212#else
9213 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009214#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009215 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216}
9217
9218/*
9219 * Reset cursor position. Use whenever cursor was moved because of outputting
9220 * something directly to the screen (shell commands) or a terminal control
9221 * code.
9222 */
9223 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009224screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225{
9226 screen_cur_row = screen_cur_col = 9999;
9227}
9228
9229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 * Move the cursor to position "row","col" in the screen.
9231 * This tries to find the most efficient way to move, minimizing the number of
9232 * characters sent to the terminal.
9233 */
9234 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009235windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009237 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 int i;
9239 int plan;
9240 int cost;
9241 int wouldbe_col;
9242 int noinvcurs;
9243 char_u *bs;
9244 int goto_cost;
9245 int attr;
9246
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009247#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9249
9250#define PLAN_LE 1
9251#define PLAN_CR 2
9252#define PLAN_NL 3
9253#define PLAN_WRITE 4
9254 /* Can't use ScreenLines unless initialized */
9255 if (ScreenLines == NULL)
9256 return;
9257
9258 if (col != screen_cur_col || row != screen_cur_row)
9259 {
9260 /* Check for valid position. */
9261 if (row < 0) /* window without text lines? */
9262 row = 0;
9263 if (row >= screen_Rows)
9264 row = screen_Rows - 1;
9265 if (col >= screen_Columns)
9266 col = screen_Columns - 1;
9267
9268 /* check if no cursor movement is allowed in highlight mode */
9269 if (screen_attr && *T_MS == NUL)
9270 noinvcurs = HIGHL_COST;
9271 else
9272 noinvcurs = 0;
9273 goto_cost = GOTO_COST + noinvcurs;
9274
9275 /*
9276 * Plan how to do the positioning:
9277 * 1. Use CR to move it to column 0, same row.
9278 * 2. Use T_LE to move it a few columns to the left.
9279 * 3. Use NL to move a few lines down, column 0.
9280 * 4. Move a few columns to the right with T_ND or by writing chars.
9281 *
9282 * Don't do this if the cursor went beyond the last column, the cursor
9283 * position is unknown then (some terminals wrap, some don't )
9284 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009285 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 * characters to move the cursor to the right.
9287 */
9288 if (row >= screen_cur_row && screen_cur_col < Columns)
9289 {
9290 /*
9291 * If the cursor is in the same row, bigger col, we can use CR
9292 * or T_LE.
9293 */
9294 bs = NULL; /* init for GCC */
9295 attr = screen_attr;
9296 if (row == screen_cur_row && col < screen_cur_col)
9297 {
9298 /* "le" is preferred over "bc", because "bc" is obsolete */
9299 if (*T_LE)
9300 bs = T_LE; /* "cursor left" */
9301 else
9302 bs = T_BC; /* "backspace character (old) */
9303 if (*bs)
9304 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9305 else
9306 cost = 999;
9307 if (col + 1 < cost) /* using CR is less characters */
9308 {
9309 plan = PLAN_CR;
9310 wouldbe_col = 0;
9311 cost = 1; /* CR is just one character */
9312 }
9313 else
9314 {
9315 plan = PLAN_LE;
9316 wouldbe_col = col;
9317 }
9318 if (noinvcurs) /* will stop highlighting */
9319 {
9320 cost += noinvcurs;
9321 attr = 0;
9322 }
9323 }
9324
9325 /*
9326 * If the cursor is above where we want to be, we can use CR LF.
9327 */
9328 else if (row > screen_cur_row)
9329 {
9330 plan = PLAN_NL;
9331 wouldbe_col = 0;
9332 cost = (row - screen_cur_row) * 2; /* CR LF */
9333 if (noinvcurs) /* will stop highlighting */
9334 {
9335 cost += noinvcurs;
9336 attr = 0;
9337 }
9338 }
9339
9340 /*
9341 * If the cursor is in the same row, smaller col, just use write.
9342 */
9343 else
9344 {
9345 plan = PLAN_WRITE;
9346 wouldbe_col = screen_cur_col;
9347 cost = 0;
9348 }
9349
9350 /*
9351 * Check if any characters that need to be written have the
9352 * correct attributes. Also avoid UTF-8 characters.
9353 */
9354 i = col - wouldbe_col;
9355 if (i > 0)
9356 cost += i;
9357 if (cost < goto_cost && i > 0)
9358 {
9359 /*
9360 * Check if the attributes are correct without additionally
9361 * stopping highlighting.
9362 */
9363 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9364 while (i && *p++ == attr)
9365 --i;
9366 if (i != 0)
9367 {
9368 /*
9369 * Try if it works when highlighting is stopped here.
9370 */
9371 if (*--p == 0)
9372 {
9373 cost += noinvcurs;
9374 while (i && *p++ == 0)
9375 --i;
9376 }
9377 if (i != 0)
9378 cost = 999; /* different attributes, don't do it */
9379 }
9380#ifdef FEAT_MBYTE
9381 if (enc_utf8)
9382 {
9383 /* Don't use an UTF-8 char for positioning, it's slow. */
9384 for (i = wouldbe_col; i < col; ++i)
9385 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9386 {
9387 cost = 999;
9388 break;
9389 }
9390 }
9391#endif
9392 }
9393
9394 /*
9395 * We can do it without term_windgoto()!
9396 */
9397 if (cost < goto_cost)
9398 {
9399 if (plan == PLAN_LE)
9400 {
9401 if (noinvcurs)
9402 screen_stop_highlight();
9403 while (screen_cur_col > col)
9404 {
9405 out_str(bs);
9406 --screen_cur_col;
9407 }
9408 }
9409 else if (plan == PLAN_CR)
9410 {
9411 if (noinvcurs)
9412 screen_stop_highlight();
9413 out_char('\r');
9414 screen_cur_col = 0;
9415 }
9416 else if (plan == PLAN_NL)
9417 {
9418 if (noinvcurs)
9419 screen_stop_highlight();
9420 while (screen_cur_row < row)
9421 {
9422 out_char('\n');
9423 ++screen_cur_row;
9424 }
9425 screen_cur_col = 0;
9426 }
9427
9428 i = col - screen_cur_col;
9429 if (i > 0)
9430 {
9431 /*
9432 * Use cursor-right if it's one character only. Avoids
9433 * removing a line of pixels from the last bold char, when
9434 * using the bold trick in the GUI.
9435 */
9436 if (T_ND[0] != NUL && T_ND[1] == NUL)
9437 {
9438 while (i-- > 0)
9439 out_char(*T_ND);
9440 }
9441 else
9442 {
9443 int off;
9444
9445 off = LineOffset[row] + screen_cur_col;
9446 while (i-- > 0)
9447 {
9448 if (ScreenAttrs[off] != screen_attr)
9449 screen_stop_highlight();
9450#ifdef FEAT_MBYTE
9451 out_flush_check();
9452#endif
9453 out_char(ScreenLines[off]);
9454#ifdef FEAT_MBYTE
9455 if (enc_dbcs == DBCS_JPNU
9456 && ScreenLines[off] == 0x8e)
9457 out_char(ScreenLines2[off]);
9458#endif
9459 ++off;
9460 }
9461 }
9462 }
9463 }
9464 }
9465 else
9466 cost = 999;
9467
9468 if (cost >= goto_cost)
9469 {
9470 if (noinvcurs)
9471 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009472 if (row == screen_cur_row && (col > screen_cur_col)
9473 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 term_cursor_right(col - screen_cur_col);
9475 else
9476 term_windgoto(row, col);
9477 }
9478 screen_cur_row = row;
9479 screen_cur_col = col;
9480 }
9481}
9482
9483/*
9484 * Set cursor to its position in the current window.
9485 */
9486 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009487setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
9489 if (redrawing())
9490 {
9491 validate_cursor();
9492 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9493 W_WINCOL(curwin) + (
9494#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009495 /* With 'rightleft' set and the cursor on a double-wide
9496 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9498# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009499 (has_mbyte
9500 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9501 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502# endif
9503 1)) :
9504#endif
9505 curwin->w_wcol));
9506 }
9507}
9508
9509
9510/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009511 * Insert 'line_count' lines at 'row' in window 'wp'.
9512 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9513 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 * scrolling.
9515 * Returns FAIL if the lines are not inserted, OK for success.
9516 */
9517 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009518win_ins_lines(
9519 win_T *wp,
9520 int row,
9521 int line_count,
9522 int invalid,
9523 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524{
9525 int did_delete;
9526 int nextrow;
9527 int lastrow;
9528 int retval;
9529
9530 if (invalid)
9531 wp->w_lines_valid = 0;
9532
9533 if (wp->w_height < 5)
9534 return FAIL;
9535
9536 if (line_count > wp->w_height - row)
9537 line_count = wp->w_height - row;
9538
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009539 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 if (retval != MAYBE)
9541 return retval;
9542
9543 /*
9544 * If there is a next window or a status line, we first try to delete the
9545 * lines at the bottom to avoid messing what is after the window.
9546 * If this fails and there are following windows, don't do anything to avoid
9547 * messing up those windows, better just redraw.
9548 */
9549 did_delete = FALSE;
9550#ifdef FEAT_WINDOWS
9551 if (wp->w_next != NULL || wp->w_status_height)
9552 {
9553 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009554 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 did_delete = TRUE;
9556 else if (wp->w_next)
9557 return FAIL;
9558 }
9559#endif
9560 /*
9561 * if no lines deleted, blank the lines that will end up below the window
9562 */
9563 if (!did_delete)
9564 {
9565#ifdef FEAT_WINDOWS
9566 wp->w_redr_status = TRUE;
9567#endif
9568 redraw_cmdline = TRUE;
9569 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9570 lastrow = nextrow + line_count;
9571 if (lastrow > Rows)
9572 lastrow = Rows;
9573 screen_fill(nextrow - line_count, lastrow - line_count,
9574 W_WINCOL(wp), (int)W_ENDCOL(wp),
9575 ' ', ' ', 0);
9576 }
9577
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009578 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 == FAIL)
9580 {
9581 /* deletion will have messed up other windows */
9582 if (did_delete)
9583 {
9584#ifdef FEAT_WINDOWS
9585 wp->w_redr_status = TRUE;
9586#endif
9587 win_rest_invalid(W_NEXT(wp));
9588 }
9589 return FAIL;
9590 }
9591
9592 return OK;
9593}
9594
9595/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009596 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9598 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9599 * scrolling
9600 * Return OK for success, FAIL if the lines are not deleted.
9601 */
9602 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009603win_del_lines(
9604 win_T *wp,
9605 int row,
9606 int line_count,
9607 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009608 int mayclear,
9609 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
9611 int retval;
9612
9613 if (invalid)
9614 wp->w_lines_valid = 0;
9615
9616 if (line_count > wp->w_height - row)
9617 line_count = wp->w_height - row;
9618
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009619 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 if (retval != MAYBE)
9621 return retval;
9622
9623 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009624 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 return FAIL;
9626
9627#ifdef FEAT_WINDOWS
9628 /*
9629 * If there are windows or status lines below, try to put them at the
9630 * correct place. If we can't do that, they have to be redrawn.
9631 */
9632 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9633 {
9634 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009635 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 {
9637 wp->w_redr_status = TRUE;
9638 win_rest_invalid(wp->w_next);
9639 }
9640 }
9641 /*
9642 * If this is the last window and there is no status line, redraw the
9643 * command line later.
9644 */
9645 else
9646#endif
9647 redraw_cmdline = TRUE;
9648 return OK;
9649}
9650
9651/*
9652 * Common code for win_ins_lines() and win_del_lines().
9653 * Returns OK or FAIL when the work has been done.
9654 * Returns MAYBE when not finished yet.
9655 */
9656 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009657win_do_lines(
9658 win_T *wp,
9659 int row,
9660 int line_count,
9661 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009662 int del,
9663 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665 int retval;
9666
9667 if (!redrawing() || line_count <= 0)
9668 return FAIL;
9669
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009670 /* When inserting lines would result in loss of command output, just redraw
9671 * the lines. */
9672 if (no_win_do_lines_ins && !del)
9673 return FAIL;
9674
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 /* only a few lines left: redraw is faster */
9676 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009677#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 && wp->w_width == Columns
9679#endif
9680 )
9681 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009682 if (!no_win_do_lines_ins)
9683 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 return FAIL;
9685 }
9686
9687 /*
9688 * Delete all remaining lines
9689 */
9690 if (row + line_count >= wp->w_height)
9691 {
9692 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9693 W_WINCOL(wp), (int)W_ENDCOL(wp),
9694 ' ', ' ', 0);
9695 return OK;
9696 }
9697
9698 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009699 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009701 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009703 if (!no_win_do_lines_ins)
9704 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705
9706 /*
9707 * If the terminal can set a scroll region, use that.
9708 * Always do this in a vertically split window. This will redraw from
9709 * ScreenLines[] when t_CV isn't defined. That's faster than using
9710 * win_line().
9711 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009712 * a character in the lower right corner of the scroll region may cause a
9713 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 */
9715 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009716#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 || W_WIDTH(wp) != Columns
9718#endif
9719 )
9720 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009721#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9723#endif
9724 scroll_region_set(wp, row);
9725 if (del)
9726 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009727 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 else
9729 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009730 wp->w_height - row, clear_attr, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009731#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9733#endif
9734 scroll_region_reset();
9735 return retval;
9736 }
9737
9738#ifdef FEAT_WINDOWS
9739 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9740 return FAIL;
9741#endif
9742
9743 return MAYBE;
9744}
9745
9746/*
9747 * window 'wp' and everything after it is messed up, mark it for redraw
9748 */
9749 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009750win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751{
9752#ifdef FEAT_WINDOWS
9753 while (wp != NULL)
9754#else
9755 if (wp != NULL)
9756#endif
9757 {
9758 redraw_win_later(wp, NOT_VALID);
9759#ifdef FEAT_WINDOWS
9760 wp->w_redr_status = TRUE;
9761 wp = wp->w_next;
9762#endif
9763 }
9764 redraw_cmdline = TRUE;
9765}
9766
9767/*
9768 * The rest of the routines in this file perform screen manipulations. The
9769 * given operation is performed physically on the screen. The corresponding
9770 * change is also made to the internal screen image. In this way, the editor
9771 * anticipates the effect of editing changes on the appearance of the screen.
9772 * That way, when we call screenupdate a complete redraw isn't usually
9773 * necessary. Another advantage is that we can keep adding code to anticipate
9774 * screen changes, and in the meantime, everything still works.
9775 */
9776
9777/*
9778 * types for inserting or deleting lines
9779 */
9780#define USE_T_CAL 1
9781#define USE_T_CDL 2
9782#define USE_T_AL 3
9783#define USE_T_CE 4
9784#define USE_T_DL 5
9785#define USE_T_SR 6
9786#define USE_NL 7
9787#define USE_T_CD 8
9788#define USE_REDRAW 9
9789
9790/*
9791 * insert lines on the screen and update ScreenLines[]
9792 * 'end' is the line after the scrolled part. Normally it is Rows.
9793 * When scrolling region used 'off' is the offset from the top for the region.
9794 * 'row' and 'end' are relative to the start of the region.
9795 *
9796 * return FAIL for failure, OK for success.
9797 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009798 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009799screen_ins_lines(
9800 int off,
9801 int row,
9802 int line_count,
9803 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009804 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009805 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806{
9807 int i;
9808 int j;
9809 unsigned temp;
9810 int cursor_row;
9811 int type;
9812 int result_empty;
9813 int can_ce = can_clear(T_CE);
9814
9815 /*
9816 * FAIL if
9817 * - there is no valid screen
9818 * - the screen has to be redrawn completely
9819 * - the line count is less than one
9820 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009821 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009823 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9824#ifdef FEAT_CLIPBOARD
9825 || (clip_star.state != SELECT_CLEARED
9826 && redrawing_for_callback > 0)
9827#endif
9828 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 return FAIL;
9830
9831 /*
9832 * There are seven ways to insert lines:
9833 * 0. When in a vertically split window and t_CV isn't set, redraw the
9834 * characters from ScreenLines[].
9835 * 1. Use T_CD (clear to end of display) if it exists and the result of
9836 * the insert is just empty lines
9837 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9838 * present or line_count > 1. It looks better if we do all the inserts
9839 * at once.
9840 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9841 * insert is just empty lines and T_CE is not present or line_count >
9842 * 1.
9843 * 4. Use T_AL (insert line) if it exists.
9844 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9845 * just empty lines.
9846 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9847 * just empty lines.
9848 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9849 * the 'da' flag is not set or we have clear line capability.
9850 * 8. redraw the characters from ScreenLines[].
9851 *
9852 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9853 * the scrollbar for the window. It does have insert line, use that if it
9854 * exists.
9855 */
9856 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009857#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9859 type = USE_REDRAW;
9860 else
9861#endif
9862 if (can_clear(T_CD) && result_empty)
9863 type = USE_T_CD;
9864 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9865 type = USE_T_CAL;
9866 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9867 type = USE_T_CDL;
9868 else if (*T_AL != NUL)
9869 type = USE_T_AL;
9870 else if (can_ce && result_empty)
9871 type = USE_T_CE;
9872 else if (*T_DL != NUL && result_empty)
9873 type = USE_T_DL;
9874 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9875 type = USE_T_SR;
9876 else
9877 return FAIL;
9878
9879 /*
9880 * For clearing the lines screen_del_lines() is used. This will also take
9881 * care of t_db if necessary.
9882 */
9883 if (type == USE_T_CD || type == USE_T_CDL ||
9884 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009885 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886
9887 /*
9888 * If text is retained below the screen, first clear or delete as many
9889 * lines at the bottom of the window as are about to be inserted so that
9890 * the deleted lines won't later surface during a screen_del_lines.
9891 */
9892 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009893 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894
9895#ifdef FEAT_CLIPBOARD
9896 /* Remove a modeless selection when inserting lines halfway the screen
9897 * or not the full width of the screen. */
9898 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009899# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 || (wp != NULL && wp->w_width != Columns)
9901# endif
9902 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009903 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 else
9905 clip_scroll_selection(-line_count);
9906#endif
9907
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908#ifdef FEAT_GUI
9909 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9910 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009911 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912#endif
9913
9914 if (*T_CCS != NUL) /* cursor relative to region */
9915 cursor_row = row;
9916 else
9917 cursor_row = row + off;
9918
9919 /*
9920 * Shift LineOffset[] line_count down to reflect the inserted lines.
9921 * Clear the inserted lines in ScreenLines[].
9922 */
9923 row += off;
9924 end += off;
9925 for (i = 0; i < line_count; ++i)
9926 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009927#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 if (wp != NULL && wp->w_width != Columns)
9929 {
9930 /* need to copy part of a line */
9931 j = end - 1 - i;
9932 while ((j -= line_count) >= row)
9933 linecopy(j + line_count, j, wp);
9934 j += line_count;
9935 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009936 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9937 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 else
9939 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9940 LineWraps[j] = FALSE;
9941 }
9942 else
9943#endif
9944 {
9945 j = end - 1 - i;
9946 temp = LineOffset[j];
9947 while ((j -= line_count) >= row)
9948 {
9949 LineOffset[j + line_count] = LineOffset[j];
9950 LineWraps[j + line_count] = LineWraps[j];
9951 }
9952 LineOffset[j + line_count] = temp;
9953 LineWraps[j + line_count] = FALSE;
9954 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009955 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 else
9957 lineinvalid(temp, (int)Columns);
9958 }
9959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960
9961 screen_stop_highlight();
9962 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009963 if (clear_attr != 0)
9964 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009966#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 /* redraw the characters */
9968 if (type == USE_REDRAW)
9969 redraw_block(row, end, wp);
9970 else
9971#endif
9972 if (type == USE_T_CAL)
9973 {
9974 term_append_lines(line_count);
9975 screen_start(); /* don't know where cursor is now */
9976 }
9977 else
9978 {
9979 for (i = 0; i < line_count; i++)
9980 {
9981 if (type == USE_T_AL)
9982 {
9983 if (i && cursor_row != 0)
9984 windgoto(cursor_row, 0);
9985 out_str(T_AL);
9986 }
9987 else /* type == USE_T_SR */
9988 out_str(T_SR);
9989 screen_start(); /* don't know where cursor is now */
9990 }
9991 }
9992
9993 /*
9994 * With scroll-reverse and 'da' flag set we need to clear the lines that
9995 * have been scrolled down into the region.
9996 */
9997 if (type == USE_T_SR && *T_DA)
9998 {
9999 for (i = 0; i < line_count; ++i)
10000 {
10001 windgoto(off + i, 0);
10002 out_str(T_CE);
10003 screen_start(); /* don't know where cursor is now */
10004 }
10005 }
10006
10007#ifdef FEAT_GUI
10008 gui_can_update_cursor();
10009 if (gui.in_use)
10010 out_flush(); /* always flush after a scroll */
10011#endif
10012 return OK;
10013}
10014
10015/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010016 * Delete lines on the screen and update ScreenLines[].
10017 * "end" is the line after the scrolled part. Normally it is Rows.
10018 * When scrolling region used "off" is the offset from the top for the region.
10019 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 *
10021 * Return OK for success, FAIL if the lines are not deleted.
10022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010024screen_del_lines(
10025 int off,
10026 int row,
10027 int line_count,
10028 int end,
10029 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010030 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010031 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032{
10033 int j;
10034 int i;
10035 unsigned temp;
10036 int cursor_row;
10037 int cursor_end;
10038 int result_empty; /* result is empty until end of region */
10039 int can_delete; /* deleting line codes can be used */
10040 int type;
10041
10042 /*
10043 * FAIL if
10044 * - there is no valid screen
10045 * - the screen has to be redrawn completely
10046 * - the line count is less than one
10047 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010048 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010050 if (!screen_valid(TRUE) || line_count <= 0
10051 || (!force && line_count > p_ttyscroll)
10052#ifdef FEAT_CLIPBOARD
10053 || (clip_star.state != SELECT_CLEARED
10054 && redrawing_for_callback > 0)
10055#endif
10056 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 return FAIL;
10058
10059 /*
10060 * Check if the rest of the current region will become empty.
10061 */
10062 result_empty = row + line_count >= end;
10063
10064 /*
10065 * We can delete lines only when 'db' flag not set or when 'ce' option
10066 * available.
10067 */
10068 can_delete = (*T_DB == NUL || can_clear(T_CE));
10069
10070 /*
10071 * There are six ways to delete lines:
10072 * 0. When in a vertically split window and t_CV isn't set, redraw the
10073 * characters from ScreenLines[].
10074 * 1. Use T_CD if it exists and the result is empty.
10075 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10076 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10077 * none of the other ways work.
10078 * 4. Use T_CE (erase line) if the result is empty.
10079 * 5. Use T_DL (delete line) if it exists.
10080 * 6. redraw the characters from ScreenLines[].
10081 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010082#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10084 type = USE_REDRAW;
10085 else
10086#endif
10087 if (can_clear(T_CD) && result_empty)
10088 type = USE_T_CD;
10089#if defined(__BEOS__) && defined(BEOS_DR8)
10090 /*
10091 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10092 * its internal termcap... this works okay for tests which test *T_DB !=
10093 * NUL. It has the disadvantage that the user cannot use any :set t_*
10094 * command to get T_DB (back) to empty_option, only :set term=... will do
10095 * the trick...
10096 * Anyway, this hack will hopefully go away with the next OS release.
10097 * (Olaf Seibert)
10098 */
10099 else if (row == 0 && T_DB == empty_option
10100 && (line_count == 1 || *T_CDL == NUL))
10101#else
10102 else if (row == 0 && (
10103#ifndef AMIGA
10104 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10105 * up, so use delete-line command */
10106 line_count == 1 ||
10107#endif
10108 *T_CDL == NUL))
10109#endif
10110 type = USE_NL;
10111 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10112 type = USE_T_CDL;
10113 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010114#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 && (wp == NULL || wp->w_width == Columns)
10116#endif
10117 )
10118 type = USE_T_CE;
10119 else if (*T_DL != NUL && can_delete)
10120 type = USE_T_DL;
10121 else if (*T_CDL != NUL && can_delete)
10122 type = USE_T_CDL;
10123 else
10124 return FAIL;
10125
10126#ifdef FEAT_CLIPBOARD
10127 /* Remove a modeless selection when deleting lines halfway the screen or
10128 * not the full width of the screen. */
10129 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010130# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 || (wp != NULL && wp->w_width != Columns)
10132# endif
10133 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010134 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 else
10136 clip_scroll_selection(line_count);
10137#endif
10138
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139#ifdef FEAT_GUI
10140 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10141 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010142 gui_dont_update_cursor(gui.cursor_row >= row + off
10143 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144#endif
10145
10146 if (*T_CCS != NUL) /* cursor relative to region */
10147 {
10148 cursor_row = row;
10149 cursor_end = end;
10150 }
10151 else
10152 {
10153 cursor_row = row + off;
10154 cursor_end = end + off;
10155 }
10156
10157 /*
10158 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10159 * Clear the inserted lines in ScreenLines[].
10160 */
10161 row += off;
10162 end += off;
10163 for (i = 0; i < line_count; ++i)
10164 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010165#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 if (wp != NULL && wp->w_width != Columns)
10167 {
10168 /* need to copy part of a line */
10169 j = row + i;
10170 while ((j += line_count) <= end - 1)
10171 linecopy(j - line_count, j, wp);
10172 j -= line_count;
10173 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010174 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10175 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 else
10177 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10178 LineWraps[j] = FALSE;
10179 }
10180 else
10181#endif
10182 {
10183 /* whole width, moving the line pointers is faster */
10184 j = row + i;
10185 temp = LineOffset[j];
10186 while ((j += line_count) <= end - 1)
10187 {
10188 LineOffset[j - line_count] = LineOffset[j];
10189 LineWraps[j - line_count] = LineWraps[j];
10190 }
10191 LineOffset[j - line_count] = temp;
10192 LineWraps[j - line_count] = FALSE;
10193 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010194 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 else
10196 lineinvalid(temp, (int)Columns);
10197 }
10198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010200 if (screen_attr != clear_attr)
10201 screen_stop_highlight();
10202 if (clear_attr != 0)
10203 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010205#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 /* redraw the characters */
10207 if (type == USE_REDRAW)
10208 redraw_block(row, end, wp);
10209 else
10210#endif
10211 if (type == USE_T_CD) /* delete the lines */
10212 {
10213 windgoto(cursor_row, 0);
10214 out_str(T_CD);
10215 screen_start(); /* don't know where cursor is now */
10216 }
10217 else if (type == USE_T_CDL)
10218 {
10219 windgoto(cursor_row, 0);
10220 term_delete_lines(line_count);
10221 screen_start(); /* don't know where cursor is now */
10222 }
10223 /*
10224 * Deleting lines at top of the screen or scroll region: Just scroll
10225 * the whole screen (scroll region) up by outputting newlines on the
10226 * last line.
10227 */
10228 else if (type == USE_NL)
10229 {
10230 windgoto(cursor_end - 1, 0);
10231 for (i = line_count; --i >= 0; )
10232 out_char('\n'); /* cursor will remain on same line */
10233 }
10234 else
10235 {
10236 for (i = line_count; --i >= 0; )
10237 {
10238 if (type == USE_T_DL)
10239 {
10240 windgoto(cursor_row, 0);
10241 out_str(T_DL); /* delete a line */
10242 }
10243 else /* type == USE_T_CE */
10244 {
10245 windgoto(cursor_row + i, 0);
10246 out_str(T_CE); /* erase a line */
10247 }
10248 screen_start(); /* don't know where cursor is now */
10249 }
10250 }
10251
10252 /*
10253 * If the 'db' flag is set, we need to clear the lines that have been
10254 * scrolled up at the bottom of the region.
10255 */
10256 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10257 {
10258 for (i = line_count; i > 0; --i)
10259 {
10260 windgoto(cursor_end - i, 0);
10261 out_str(T_CE); /* erase a line */
10262 screen_start(); /* don't know where cursor is now */
10263 }
10264 }
10265
10266#ifdef FEAT_GUI
10267 gui_can_update_cursor();
10268 if (gui.in_use)
10269 out_flush(); /* always flush after a scroll */
10270#endif
10271
10272 return OK;
10273}
10274
10275/*
10276 * show the current mode and ruler
10277 *
10278 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10279 * If clear_cmdline is FALSE there may be a message there that needs to be
10280 * cleared only if a mode is shown.
10281 * Return the length of the message (0 if no message).
10282 */
10283 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010284showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285{
10286 int need_clear;
10287 int length = 0;
10288 int do_mode;
10289 int attr;
10290 int nwr_save;
10291#ifdef FEAT_INS_EXPAND
10292 int sub_attr;
10293#endif
10294
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010295 do_mode = ((p_smd && msg_silent == 0)
10296 && ((State & INSERT)
10297 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010298 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 if (do_mode || Recording)
10300 {
10301 /*
10302 * Don't show mode right now, when not redrawing or inside a mapping.
10303 * Call char_avail() only when we are going to show something, because
10304 * it takes a bit of time.
10305 */
10306 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10307 {
10308 redraw_cmdline = TRUE; /* show mode later */
10309 return 0;
10310 }
10311
10312 nwr_save = need_wait_return;
10313
10314 /* wait a bit before overwriting an important message */
10315 check_for_delay(FALSE);
10316
10317 /* if the cmdline is more than one line high, erase top lines */
10318 need_clear = clear_cmdline;
10319 if (clear_cmdline && cmdline_row < Rows - 1)
10320 msg_clr_cmdline(); /* will reset clear_cmdline */
10321
10322 /* Position on the last line in the window, column 0 */
10323 msg_pos_mode();
10324 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010325 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 if (do_mode)
10327 {
10328 MSG_PUTS_ATTR("--", attr);
10329#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010330 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010331# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010332 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010333# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010334 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010335# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010336 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010337# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 MSG_PUTS_ATTR(" IM", attr);
10339# else
10340 MSG_PUTS_ATTR(" XIM", attr);
10341# endif
10342#endif
10343#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10344 if (gui.in_use)
10345 {
10346 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010347 {
10348 /* HANGUL */
10349 if (enc_utf8)
10350 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10351 else
10352 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 }
10355#endif
10356#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010357 /* CTRL-X in Insert mode */
10358 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 {
10360 /* These messages can get long, avoid a wrap in a narrow
10361 * window. Prefer showing edit_submode_extra. */
10362 length = (Rows - msg_row) * Columns - 3;
10363 if (edit_submode_extra != NULL)
10364 length -= vim_strsize(edit_submode_extra);
10365 if (length > 0)
10366 {
10367 if (edit_submode_pre != NULL)
10368 length -= vim_strsize(edit_submode_pre);
10369 if (length - vim_strsize(edit_submode) > 0)
10370 {
10371 if (edit_submode_pre != NULL)
10372 msg_puts_attr(edit_submode_pre, attr);
10373 msg_puts_attr(edit_submode, attr);
10374 }
10375 if (edit_submode_extra != NULL)
10376 {
10377 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10378 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010379 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 else
10381 sub_attr = attr;
10382 msg_puts_attr(edit_submode_extra, sub_attr);
10383 }
10384 }
10385 length = 0;
10386 }
10387 else
10388#endif
10389 {
10390#ifdef FEAT_VREPLACE
10391 if (State & VREPLACE_FLAG)
10392 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10393 else
10394#endif
10395 if (State & REPLACE_FLAG)
10396 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10397 else if (State & INSERT)
10398 {
10399#ifdef FEAT_RIGHTLEFT
10400 if (p_ri)
10401 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10402#endif
10403 MSG_PUTS_ATTR(_(" INSERT"), attr);
10404 }
10405 else if (restart_edit == 'I')
10406 MSG_PUTS_ATTR(_(" (insert)"), attr);
10407 else if (restart_edit == 'R')
10408 MSG_PUTS_ATTR(_(" (replace)"), attr);
10409 else if (restart_edit == 'V')
10410 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10411#ifdef FEAT_RIGHTLEFT
10412 if (p_hkmap)
10413 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10414# ifdef FEAT_FKMAP
10415 if (p_fkmap)
10416 MSG_PUTS_ATTR(farsi_text_5, attr);
10417# endif
10418#endif
10419#ifdef FEAT_KEYMAP
10420 if (State & LANGMAP)
10421 {
10422# ifdef FEAT_ARABIC
10423 if (curwin->w_p_arab)
10424 MSG_PUTS_ATTR(_(" Arabic"), attr);
10425 else
10426# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010427 if (get_keymap_str(curwin, (char_u *)" (%s)",
10428 NameBuff, MAXPATHL))
10429 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 }
10431#endif
10432 if ((State & INSERT) && p_paste)
10433 MSG_PUTS_ATTR(_(" (paste)"), attr);
10434
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 if (VIsual_active)
10436 {
10437 char *p;
10438
10439 /* Don't concatenate separate words to avoid translation
10440 * problems. */
10441 switch ((VIsual_select ? 4 : 0)
10442 + (VIsual_mode == Ctrl_V) * 2
10443 + (VIsual_mode == 'V'))
10444 {
10445 case 0: p = N_(" VISUAL"); break;
10446 case 1: p = N_(" VISUAL LINE"); break;
10447 case 2: p = N_(" VISUAL BLOCK"); break;
10448 case 4: p = N_(" SELECT"); break;
10449 case 5: p = N_(" SELECT LINE"); break;
10450 default: p = N_(" SELECT BLOCK"); break;
10451 }
10452 MSG_PUTS_ATTR(_(p), attr);
10453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 MSG_PUTS_ATTR(" --", attr);
10455 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010456
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 need_clear = TRUE;
10458 }
10459 if (Recording
10460#ifdef FEAT_INS_EXPAND
10461 && edit_submode == NULL /* otherwise it gets too long */
10462#endif
10463 )
10464 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010465 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 need_clear = TRUE;
10467 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010468
10469 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 if (need_clear || clear_cmdline)
10471 msg_clr_eos();
10472 msg_didout = FALSE; /* overwrite this message */
10473 length = msg_col;
10474 msg_col = 0;
10475 need_wait_return = nwr_save; /* never ask for hit-return for this */
10476 }
10477 else if (clear_cmdline && msg_silent == 0)
10478 /* Clear the whole command line. Will reset "clear_cmdline". */
10479 msg_clr_cmdline();
10480
10481#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 /* In Visual mode the size of the selected area must be redrawn. */
10483 if (VIsual_active)
10484 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485
10486 /* If the last window has no status line, the ruler is after the mode
10487 * message and must be redrawn */
10488 if (redrawing()
10489# ifdef FEAT_WINDOWS
10490 && lastwin->w_status_height == 0
10491# endif
10492 )
10493 win_redr_ruler(lastwin, TRUE);
10494#endif
10495 redraw_cmdline = FALSE;
10496 clear_cmdline = FALSE;
10497
10498 return length;
10499}
10500
10501/*
10502 * Position for a mode message.
10503 */
10504 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010505msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506{
10507 msg_col = 0;
10508 msg_row = Rows - 1;
10509}
10510
10511/*
10512 * Delete mode message. Used when ESC is typed which is expected to end
10513 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010514 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 */
10516 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010517unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518{
10519 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010520 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 */
10522 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10523 redraw_cmdline = TRUE; /* delete mode later */
10524 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010525 clearmode();
10526}
10527
10528/*
10529 * Clear the mode message.
10530 */
10531 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010532clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010533{
10534 msg_pos_mode();
10535 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010536 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010537 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538}
10539
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010540 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010541recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010542{
10543 MSG_PUTS_ATTR(_("recording"), attr);
10544 if (!shortmess(SHM_RECORDING))
10545 {
10546 char_u s[4];
10547 sprintf((char *)s, " @%c", Recording);
10548 MSG_PUTS_ATTR(s, attr);
10549 }
10550}
10551
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010552#if defined(FEAT_WINDOWS)
10553/*
10554 * Draw the tab pages line at the top of the Vim window.
10555 */
10556 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010557draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010558{
10559 int tabcount = 0;
10560 tabpage_T *tp;
10561 int tabwidth;
10562 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010563 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010564 int attr;
10565 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010566 win_T *cwp;
10567 int wincount;
10568 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010569 int c;
10570 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010571 int attr_sel = HL_ATTR(HLF_TPS);
10572 int attr_nosel = HL_ATTR(HLF_TP);
10573 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010574 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010575 int room;
10576 int use_sep_chars = (t_colors < 8
10577#ifdef FEAT_GUI
10578 && !gui.in_use
10579#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010580#ifdef FEAT_TERMGUICOLORS
10581 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010582#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010583 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010584
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010585 if (ScreenLines == NULL)
10586 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010587 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010588
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010589#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010590 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010591 if (gui_use_tabline())
10592 {
10593 gui_update_tabline();
10594 return;
10595 }
10596#endif
10597
10598 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010599 return;
10600
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010601#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010602
10603 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10604 for (scol = 0; scol < Columns; ++scol)
10605 TabPageIdxs[scol] = 0;
10606
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010607 /* Use the 'tabline' option if it's set. */
10608 if (*p_tal != NUL)
10609 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010610 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010611
10612 /* Check for an error. If there is one we would loop in redrawing the
10613 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010614 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010615 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010616 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010617 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010618 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010619 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010620 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010621 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010622#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010623 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010624 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010625 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010626
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10628 if (tabwidth < 6)
10629 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010630
Bram Moolenaar238a5642006-02-21 22:12:05 +000010631 attr = attr_nosel;
10632 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010633 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010634 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10635 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010636 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010637 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010638
Bram Moolenaar238a5642006-02-21 22:12:05 +000010639 if (tp->tp_topframe == topframe)
10640 attr = attr_sel;
10641 if (use_sep_chars && col > 0)
10642 screen_putchar('|', 0, col++, attr);
10643
10644 if (tp->tp_topframe != topframe)
10645 attr = attr_nosel;
10646
10647 screen_putchar(' ', 0, col++, attr);
10648
10649 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010650 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010651 cwp = curwin;
10652 wp = firstwin;
10653 }
10654 else
10655 {
10656 cwp = tp->tp_curwin;
10657 wp = tp->tp_firstwin;
10658 }
10659
10660 modified = FALSE;
10661 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10662 if (bufIsChanged(wp->w_buffer))
10663 modified = TRUE;
10664 if (modified || wincount > 1)
10665 {
10666 if (wincount > 1)
10667 {
10668 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010669 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010670 if (col + len >= Columns - 3)
10671 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010672 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010673#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010674 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010675#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010676 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010677#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010678 );
10679 col += len;
10680 }
10681 if (modified)
10682 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10683 screen_putchar(' ', 0, col++, attr);
10684 }
10685
10686 room = scol - col + tabwidth - 1;
10687 if (room > 0)
10688 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010689 /* Get buffer name in NameBuff[] */
10690 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010691 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010692 len = vim_strsize(NameBuff);
10693 p = NameBuff;
10694#ifdef FEAT_MBYTE
10695 if (has_mbyte)
10696 while (len > room)
10697 {
10698 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010699 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010700 }
10701 else
10702#endif
10703 if (len > room)
10704 {
10705 p += len - room;
10706 len = room;
10707 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010708 if (len > Columns - col - 1)
10709 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010710
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010711 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010712 col += len;
10713 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010714 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010715
10716 /* Store the tab page number in TabPageIdxs[], so that
10717 * jump_to_mouse() knows where each one is. */
10718 ++tabcount;
10719 while (scol < col)
10720 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010721 }
10722
Bram Moolenaar238a5642006-02-21 22:12:05 +000010723 if (use_sep_chars)
10724 c = '_';
10725 else
10726 c = ' ';
10727 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010728
10729 /* Put an "X" for closing the current tab if there are several. */
10730 if (first_tabpage->tp_next != NULL)
10731 {
10732 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10733 TabPageIdxs[Columns - 1] = -999;
10734 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010735 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010736
10737 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10738 * set. */
10739 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010740}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010741
10742/*
10743 * Get buffer name for "buf" into NameBuff[].
10744 * Takes care of special buffer names and translates special characters.
10745 */
10746 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010747get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010748{
10749 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010750 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010751 else
10752 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10753 trans_characters(NameBuff, MAXPATHL);
10754}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010755#endif
10756
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10758/*
10759 * Get the character to use in a status line. Get its attributes in "*attr".
10760 */
10761 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010762fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763{
10764 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010765
10766#ifdef FEAT_TERMINAL
10767 if (bt_terminal(wp->w_buffer))
10768 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010769 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010770 {
10771 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010772 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010773 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010774 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010775 {
10776 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010777 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010778 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010779 }
10780 else
10781#endif
10782 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010784 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 fill = fill_stl;
10786 }
10787 else
10788 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010789 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 fill = fill_stlnc;
10791 }
10792 /* Use fill when there is highlighting, and highlighting of current
10793 * window differs, or the fillchars differ, or this is not the
10794 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010795 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010796 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 || (fill_stl != fill_stlnc)))
10798 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010799 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 return '^';
10801 return '=';
10802}
10803#endif
10804
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010805#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806/*
10807 * Get the character to use in a separator between vertically split windows.
10808 * Get its attributes in "*attr".
10809 */
10810 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010811fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010813 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 if (*attr == 0 && fill_vert == ' ')
10815 return '|';
10816 else
10817 return fill_vert;
10818}
10819#endif
10820
10821/*
10822 * Return TRUE if redrawing should currently be done.
10823 */
10824 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010825redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010827#ifdef FEAT_EVAL
10828 if (disable_redraw_for_testing)
10829 return 0;
10830 else
10831#endif
10832 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10834}
10835
10836/*
10837 * Return TRUE if printing messages should currently be done.
10838 */
10839 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010840messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841{
10842 return (!(p_lz && char_avail() && !KeyTyped));
10843}
10844
10845/*
10846 * Show current status info in ruler and various other places
10847 * If always is FALSE, only show ruler if position has changed.
10848 */
10849 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010850showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851{
10852 if (!always && !redrawing())
10853 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010854#ifdef FEAT_INS_EXPAND
10855 if (pum_visible())
10856 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010857# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010858 /* Don't redraw right now, do it later. */
10859 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010860# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010861 return;
10862 }
10863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010865 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010866 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010867 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 else
10870#endif
10871#ifdef FEAT_CMDL_INFO
10872 win_redr_ruler(curwin, always);
10873#endif
10874
10875#ifdef FEAT_TITLE
10876 if (need_maketitle
10877# ifdef FEAT_STL_OPT
10878 || (p_icon && (stl_syntax & STL_IN_ICON))
10879 || (p_title && (stl_syntax & STL_IN_TITLE))
10880# endif
10881 )
10882 maketitle();
10883#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010884#ifdef FEAT_WINDOWS
10885 /* Redraw the tab pages line if needed. */
10886 if (redraw_tabline)
10887 draw_tabline();
10888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889}
10890
10891#ifdef FEAT_CMDL_INFO
10892 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010893win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010895#define RULER_BUF_LEN 70
10896 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 int row;
10898 int fillchar;
10899 int attr;
10900 int empty_line = FALSE;
10901 colnr_T virtcol;
10902 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010903 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010905#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 int this_ru_col;
10907 int off = 0;
10908 int width = Columns;
10909# define WITH_OFF(x) x
10910# define WITH_WIDTH(x) x
10911#else
10912# define WITH_OFF(x) 0
10913# define WITH_WIDTH(x) Columns
10914# define this_ru_col ru_col
10915#endif
10916
10917 /* If 'ruler' off or redrawing disabled, don't do anything */
10918 if (!p_ru)
10919 return;
10920
10921 /*
10922 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10923 * after deleting lines, before cursor.lnum is corrected.
10924 */
10925 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10926 return;
10927
10928#ifdef FEAT_INS_EXPAND
10929 /* Don't draw the ruler while doing insert-completion, it might overwrite
10930 * the (long) mode message. */
10931# ifdef FEAT_WINDOWS
10932 if (wp == lastwin && lastwin->w_status_height == 0)
10933# endif
10934 if (edit_submode != NULL)
10935 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010936 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10937 if (pum_visible())
10938 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939#endif
10940
10941#ifdef FEAT_STL_OPT
10942 if (*p_ruf)
10943 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010944 int save_called_emsg = called_emsg;
10945
10946 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010948 if (called_emsg)
10949 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010950 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010951 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 return;
10953 }
10954#endif
10955
10956 /*
10957 * Check if not in Insert mode and the line is empty (will show "0-1").
10958 */
10959 if (!(State & INSERT)
10960 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10961 empty_line = TRUE;
10962
10963 /*
10964 * Only draw the ruler when something changed.
10965 */
10966 validate_virtcol_win(wp);
10967 if ( redraw_cmdline
10968 || always
10969 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10970 || wp->w_cursor.col != wp->w_ru_cursor.col
10971 || wp->w_virtcol != wp->w_ru_virtcol
10972#ifdef FEAT_VIRTUALEDIT
10973 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10974#endif
10975 || wp->w_topline != wp->w_ru_topline
10976 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10977#ifdef FEAT_DIFF
10978 || wp->w_topfill != wp->w_ru_topfill
10979#endif
10980 || empty_line != wp->w_ru_empty)
10981 {
10982 cursor_off();
10983#ifdef FEAT_WINDOWS
10984 if (wp->w_status_height)
10985 {
10986 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010987 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 off = W_WINCOL(wp);
10989 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 }
10991 else
10992#endif
10993 {
10994 row = Rows - 1;
10995 fillchar = ' ';
10996 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010997#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 width = Columns;
10999 off = 0;
11000#endif
11001 }
11002
11003 /* In list mode virtcol needs to be recomputed */
11004 virtcol = wp->w_virtcol;
11005 if (wp->w_p_list && lcs_tab1 == NUL)
11006 {
11007 wp->w_p_list = FALSE;
11008 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11009 wp->w_p_list = TRUE;
11010 }
11011
11012 /*
11013 * Some sprintfs return the length, some return a pointer.
11014 * To avoid portability problems we use strlen() here.
11015 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011016 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11018 ? 0L
11019 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011020 len = STRLEN(buffer);
11021 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11023 (int)virtcol + 1);
11024
11025 /*
11026 * Add a "50%" if there is room for it.
11027 * On the last line, don't print in the last column (scrolls the
11028 * screen up on some terminals).
11029 */
11030 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011031 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 o = i + vim_strsize(buffer + i + 1);
11033#ifdef FEAT_WINDOWS
11034 if (wp->w_status_height == 0) /* can't use last char of screen */
11035#endif
11036 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010011037#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 this_ru_col = ru_col - (Columns - width);
11039 if (this_ru_col < 0)
11040 this_ru_col = 0;
11041#endif
11042 /* Never use more than half the window/screen width, leave the other
11043 * half for the filename. */
11044 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
11045 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
11046 if (this_ru_col + o < WITH_WIDTH(width))
11047 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011048 /* need at least 3 chars left for get_rel_pos() + NUL */
11049 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 {
11051#ifdef FEAT_MBYTE
11052 if (has_mbyte)
11053 i += (*mb_char2bytes)(fillchar, buffer + i);
11054 else
11055#endif
11056 buffer[i++] = fillchar;
11057 ++o;
11058 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011059 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 }
11061 /* Truncate at window boundary. */
11062#ifdef FEAT_MBYTE
11063 if (has_mbyte)
11064 {
11065 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011066 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 {
11068 o += (*mb_ptr2cells)(buffer + i);
11069 if (this_ru_col + o > WITH_WIDTH(width))
11070 {
11071 buffer[i] = NUL;
11072 break;
11073 }
11074 }
11075 }
11076 else
11077#endif
11078 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
11079 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
11080
11081 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
11082 i = redraw_cmdline;
11083 screen_fill(row, row + 1,
11084 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
11085 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
11086 fillchar, fillchar, attr);
11087 /* don't redraw the cmdline because of showing the ruler */
11088 redraw_cmdline = i;
11089 wp->w_ru_cursor = wp->w_cursor;
11090 wp->w_ru_virtcol = wp->w_virtcol;
11091 wp->w_ru_empty = empty_line;
11092 wp->w_ru_topline = wp->w_topline;
11093 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11094#ifdef FEAT_DIFF
11095 wp->w_ru_topfill = wp->w_topfill;
11096#endif
11097 }
11098}
11099#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011100
11101#if defined(FEAT_LINEBREAK) || defined(PROTO)
11102/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011103 * Return the width of the 'number' and 'relativenumber' column.
11104 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011105 * Otherwise it depends on 'numberwidth' and the line count.
11106 */
11107 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011108number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011109{
11110 int n;
11111 linenr_T lnum;
11112
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011113 if (wp->w_p_rnu && !wp->w_p_nu)
11114 /* cursor line shows "0" */
11115 lnum = wp->w_height;
11116 else
11117 /* cursor line shows absolute line number */
11118 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011119
Bram Moolenaar6b314672015-03-20 15:42:10 +010011120 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011121 return wp->w_nrwidth_width;
11122 wp->w_nrwidth_line_count = lnum;
11123
11124 n = 0;
11125 do
11126 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011127 lnum /= 10;
11128 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011129 } while (lnum > 0);
11130
11131 /* 'numberwidth' gives the minimal width plus one */
11132 if (n < wp->w_p_nuw - 1)
11133 n = wp->w_p_nuw - 1;
11134
11135 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011136 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011137 return n;
11138}
11139#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011140
11141/*
11142 * Return the current cursor column. This is the actual position on the
11143 * screen. First column is 0.
11144 */
11145 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011146screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011147{
11148 return screen_cur_col;
11149}
11150
11151/*
11152 * Return the current cursor row. This is the actual position on the screen.
11153 * First row is 0.
11154 */
11155 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011156screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011157{
11158 return screen_cur_row;
11159}