blob: ca7b6583608ee0b0784deae9be371849f3636dc6 [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;
3142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
3144 /* draw_state: items that are drawn in sequence: */
3145#define WL_START 0 /* nothing done yet */
3146#ifdef FEAT_CMDWIN
3147# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3148#else
3149# define WL_CMDLINE WL_START
3150#endif
3151#ifdef FEAT_FOLDING
3152# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3153#else
3154# define WL_FOLD WL_CMDLINE
3155#endif
3156#ifdef FEAT_SIGNS
3157# define WL_SIGN WL_FOLD + 1 /* column for signs */
3158#else
3159# define WL_SIGN WL_FOLD /* column for signs */
3160#endif
3161#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003162#ifdef FEAT_LINEBREAK
3163# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003165# define WL_BRI WL_NR
3166#endif
3167#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3168# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3169#else
3170# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171#endif
3172#define WL_LINE WL_SBR + 1 /* text in the line */
3173 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003174#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 int feedback_col = 0;
3176 int feedback_old_attr = -1;
3177#endif
3178
Bram Moolenaar860cae12010-06-05 23:22:07 +02003179#ifdef FEAT_CONCEAL
3180 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003181 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003182 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003183 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003184 int is_concealing = FALSE;
3185 int boguscols = 0; /* nonexistent columns added to force
3186 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003187 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003188 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003189 int match_conc = 0; /* cchar for match functions */
3190 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003191 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003192# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003193# define FIX_FOR_BOGUSCOLS \
3194 { \
3195 n_extra += vcol_off; \
3196 vcol -= vcol_off; \
3197 vcol_off = 0; \
3198 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003199 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003200 boguscols = 0; \
3201 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003202#else
3203# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
3206 if (startrow > endrow) /* past the end already! */
3207 return startrow;
3208
3209 row = startrow;
3210 screen_row = row + W_WINROW(wp);
3211
3212 /*
3213 * To speed up the loop below, set extra_check when there is linebreak,
3214 * trailing white space and/or syntax processing to be done.
3215 */
3216#ifdef FEAT_LINEBREAK
3217 extra_check = wp->w_p_lbr;
3218#else
3219 extra_check = 0;
3220#endif
3221#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003222 if (syntax_present(wp) && !wp->w_s->b_syn_error
3223# ifdef SYN_TIME_LIMIT
3224 && !wp->w_s->b_syn_slow
3225# endif
3226 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
3228 /* Prepare for syntax highlighting in this line. When there is an
3229 * error, stop syntax highlighting. */
3230 save_did_emsg = did_emsg;
3231 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003232 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003234 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 else
3236 {
3237 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003238#ifdef SYN_TIME_LIMIT
3239 if (!wp->w_s->b_syn_slow)
3240#endif
3241 {
3242 has_syntax = TRUE;
3243 extra_check = TRUE;
3244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 }
3246 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003247
3248 /* Check for columns to display for 'colorcolumn'. */
3249 color_cols = wp->w_p_cc_cols;
3250 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003251 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003252#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003253
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003254#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003255 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003256 {
3257 extra_check = TRUE;
3258 get_term_attr = TRUE;
3259 }
3260#endif
3261
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003262#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003263 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003264 && *wp->w_s->b_p_spl != NUL
3265 && wp->w_s->b_langp.ga_len > 0
3266 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003267 {
3268 /* Prepare for spell checking. */
3269 has_spell = TRUE;
3270 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003271
3272 /* Get the start of the next line, so that words that wrap to the next
3273 * line are found too: "et<line-break>al.".
3274 * Trick: skip a few chars for C/shell/Vim comments */
3275 nextline[SPWORDLEN] = NUL;
3276 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3277 {
3278 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3279 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3280 }
3281
3282 /* When a word wrapped from the previous line the start of the current
3283 * line is valid. */
3284 if (lnum == checked_lnum)
3285 cur_checked_col = checked_col;
3286 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003287
3288 /* When there was a sentence end in the previous line may require a
3289 * word starting with capital in this line. In line 1 always check
3290 * the first word. */
3291 if (lnum != capcol_lnum)
3292 cap_col = -1;
3293 if (lnum == 1)
3294 cap_col = 0;
3295 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297#endif
3298
3299 /*
3300 * handle visual active in this window
3301 */
3302 fromcol = -10;
3303 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3305 {
3306 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003307 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 top = &curwin->w_cursor;
3310 bot = &VIsual;
3311 }
3312 else /* Visual is before curwin->w_cursor */
3313 {
3314 top = &VIsual;
3315 bot = &curwin->w_cursor;
3316 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003317 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (VIsual_mode == Ctrl_V) /* block mode */
3319 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003320 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 {
3322 fromcol = wp->w_old_cursor_fcol;
3323 tocol = wp->w_old_cursor_lcol;
3324 }
3325 }
3326 else /* non-block mode */
3327 {
3328 if (lnum > top->lnum && lnum <= bot->lnum)
3329 fromcol = 0;
3330 else if (lnum == top->lnum)
3331 {
3332 if (VIsual_mode == 'V') /* linewise */
3333 fromcol = 0;
3334 else
3335 {
3336 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3337 if (gchar_pos(top) == NUL)
3338 tocol = fromcol + 1;
3339 }
3340 }
3341 if (VIsual_mode != 'V' && lnum == bot->lnum)
3342 {
3343 if (*p_sel == 'e' && bot->col == 0
3344#ifdef FEAT_VIRTUALEDIT
3345 && bot->coladd == 0
3346#endif
3347 )
3348 {
3349 fromcol = -10;
3350 tocol = MAXCOL;
3351 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003352 else if (bot->col == MAXCOL)
3353 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 else
3355 {
3356 pos = *bot;
3357 if (*p_sel == 'e')
3358 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3359 else
3360 {
3361 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3362 ++tocol;
3363 }
3364 }
3365 }
3366 }
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 /* Check if the character under the cursor should not be inverted */
3369 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003370#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 )
3374 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
3376 /* if inverting in this line set area_highlighting */
3377 if (fromcol >= 0)
3378 {
3379 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003380 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003382 if ((clip_star.available && !clip_star.owned
3383 && clip_isautosel_star())
3384 || (clip_plus.available && !clip_plus.owned
3385 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003386 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387#endif
3388 }
3389 }
3390
3391 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003392 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003394 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 && wp == curwin
3396 && lnum >= curwin->w_cursor.lnum
3397 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3398 {
3399 if (lnum == curwin->w_cursor.lnum)
3400 getvcol(curwin, &(curwin->w_cursor),
3401 (colnr_T *)&fromcol, NULL, NULL);
3402 else
3403 fromcol = 0;
3404 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3405 {
3406 pos.lnum = lnum;
3407 pos.col = search_match_endcol;
3408 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3409 }
3410 else
3411 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003412 /* do at least one character; happens when past end of line */
3413 if (fromcol == tocol)
3414 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003416 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
3418
3419#ifdef FEAT_DIFF
3420 filler_lines = diff_check(wp, lnum);
3421 if (filler_lines < 0)
3422 {
3423 if (filler_lines == -1)
3424 {
3425 if (diff_find_change(wp, lnum, &change_start, &change_end))
3426 diff_hlf = HLF_ADD; /* added line */
3427 else if (change_start == 0)
3428 diff_hlf = HLF_TXD; /* changed text */
3429 else
3430 diff_hlf = HLF_CHD; /* changed line */
3431 }
3432 else
3433 diff_hlf = HLF_ADD; /* added line */
3434 filler_lines = 0;
3435 area_highlighting = TRUE;
3436 }
3437 if (lnum == wp->w_topline)
3438 filler_lines = wp->w_topfill;
3439 filler_todo = filler_lines;
3440#endif
3441
3442#ifdef LINE_ATTR
3443# ifdef FEAT_SIGNS
3444 /* If this line has a sign with line highlighting set line_attr. */
3445 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3446 if (v != 0)
3447 line_attr = sign_get_attr((int)v, TRUE);
3448# endif
3449# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3450 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003451 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003452 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453# endif
3454 if (line_attr != 0)
3455 area_highlighting = TRUE;
3456#endif
3457
3458 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3459 ptr = line;
3460
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003461#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003462 if (has_spell)
3463 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003464 /* For checking first word with a capital skip white space. */
3465 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003466 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003467
Bram Moolenaar30abd282005-06-22 22:35:10 +00003468 /* To be able to spell-check over line boundaries copy the end of the
3469 * current line into nextline[]. Above the start of the next line was
3470 * copied to nextline[SPWORDLEN]. */
3471 if (nextline[SPWORDLEN] == NUL)
3472 {
3473 /* No next line or it is empty. */
3474 nextlinecol = MAXCOL;
3475 nextline_idx = 0;
3476 }
3477 else
3478 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003479 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003480 if (v < SPWORDLEN)
3481 {
3482 /* Short line, use it completely and append the start of the
3483 * next line. */
3484 nextlinecol = 0;
3485 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003486 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003487 nextline_idx = v + 1;
3488 }
3489 else
3490 {
3491 /* Long line, use only the last SPWORDLEN bytes. */
3492 nextlinecol = v - SPWORDLEN;
3493 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3494 nextline_idx = SPWORDLEN + 1;
3495 }
3496 }
3497 }
3498#endif
3499
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003500 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003502 if (lcs_space || lcs_trail)
3503 extra_check = TRUE;
3504 /* find start of trailing whitespace */
3505 if (lcs_trail)
3506 {
3507 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003508 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003509 --trailcol;
3510 trailcol += (colnr_T) (ptr - line);
3511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
3513
3514 /*
3515 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3516 * first character to be displayed.
3517 */
3518 if (wp->w_p_wrap)
3519 v = wp->w_skipcol;
3520 else
3521 v = wp->w_leftcol;
3522 if (v > 0)
3523 {
3524#ifdef FEAT_MBYTE
3525 char_u *prev_ptr = ptr;
3526#endif
3527 while (vcol < v && *ptr != NUL)
3528 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003529 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 vcol += c;
3531#ifdef FEAT_MBYTE
3532 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003534 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003537 /* When:
3538 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003539 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003540 * - 'virtualedit' is set, or
3541 * - the visual mode is active,
3542 * the end of the line may be before the start of the displayed part.
3543 */
3544 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003545#ifdef FEAT_SYN_HL
3546 wp->w_p_cuc || draw_color_col ||
3547#endif
3548#ifdef FEAT_VIRTUALEDIT
3549 virtual_active() ||
3550#endif
3551 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
3556 /* Handle a character that's not completely on the screen: Put ptr at
3557 * that character but skip the first few screen characters. */
3558 if (vcol > v)
3559 {
3560 vcol -= c;
3561#ifdef FEAT_MBYTE
3562 ptr = prev_ptr;
3563#else
3564 --ptr;
3565#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003566 /* If the character fits on the screen, don't need to skip it.
3567 * Except for a TAB. */
3568 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003569#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003570 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003571#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003572 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003573 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575
3576 /*
3577 * Adjust for when the inverted text is before the screen,
3578 * and when the start of the inverted text is before the screen.
3579 */
3580 if (tocol <= vcol)
3581 fromcol = 0;
3582 else if (fromcol >= 0 && fromcol < vcol)
3583 fromcol = vcol;
3584
3585#ifdef FEAT_LINEBREAK
3586 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3587 if (wp->w_p_wrap)
3588 need_showbreak = TRUE;
3589#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003590#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003591 /* When spell checking a word we need to figure out the start of the
3592 * word and if it's badly spelled or not. */
3593 if (has_spell)
3594 {
3595 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003596 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003597 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003598
3599 pos = wp->w_cursor;
3600 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003601 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003602 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003603
3604 /* spell_move_to() may call ml_get() and make "line" invalid */
3605 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3606 ptr = line + linecol;
3607
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003608 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003609 {
3610 /* no bad word found at line start, don't check until end of a
3611 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003612 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003613 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003614 }
3615 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003616 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003617 /* bad word found, use attributes until end of word */
3618 word_end = wp->w_cursor.col + len + 1;
3619
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003620 /* Turn index into actual attributes. */
3621 if (spell_hlf != HLF_COUNT)
3622 spell_attr = highlight_attr[spell_hlf];
3623 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003624 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003625
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003626# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003627 /* Need to restart syntax highlighting for this line. */
3628 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003629 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003630# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003631 }
3632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
3634
3635 /*
3636 * Correct highlighting for cursor that can't be disabled.
3637 * Avoids having to check this for each character.
3638 */
3639 if (fromcol >= 0)
3640 {
3641 if (noinvcur)
3642 {
3643 if ((colnr_T)fromcol == wp->w_virtcol)
3644 {
3645 /* highlighting starts at cursor, let it start just after the
3646 * cursor */
3647 fromcol_prev = fromcol;
3648 fromcol = -1;
3649 }
3650 else if ((colnr_T)fromcol < wp->w_virtcol)
3651 /* restart highlighting after the cursor */
3652 fromcol_prev = wp->w_virtcol;
3653 }
3654 if (fromcol >= tocol)
3655 fromcol = -1;
3656 }
3657
3658#ifdef FEAT_SEARCH_EXTRA
3659 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003660 * Handle highlighting the last used search pattern and matches.
3661 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003663 cur = wp->w_match_head;
3664 shl_flag = FALSE;
3665 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003667 if (shl_flag == FALSE)
3668 {
3669 shl = &search_hl;
3670 shl_flag = TRUE;
3671 }
3672 else
3673 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003674 shl->startcol = MAXCOL;
3675 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003677 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003678 v = (long)(ptr - line);
3679 if (cur != NULL)
3680 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003681 next_search_hl(wp, shl, lnum, (colnr_T)v,
3682 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003683
3684 /* Need to get the line again, a multi-line regexp may have made it
3685 * invalid. */
3686 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3687 ptr = line + v;
3688
3689 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003691 if (shl->lnum == lnum)
3692 shl->startcol = shl->rm.startpos[0].col;
3693 else
3694 shl->startcol = 0;
3695 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3696 - shl->rm.startpos[0].lnum)
3697 shl->endcol = shl->rm.endpos[0].col;
3698 else
3699 shl->endcol = MAXCOL;
3700 /* Highlight one character for an empty match. */
3701 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003704 if (has_mbyte && line[shl->endcol] != NUL)
3705 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3706 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003708 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003710 if ((long)shl->startcol < v) /* match at leftcol */
3711 {
3712 shl->attr_cur = shl->attr;
3713 search_attr = shl->attr;
3714 }
3715 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003717 if (shl != &search_hl && cur != NULL)
3718 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720#endif
3721
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003722#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003723 /* Cursor line highlighting for 'cursorline' in the current window. Not
3724 * when Visual mode is active, because it's not clear what is selected
3725 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003726 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003727 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003728 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003729 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003730 area_highlighting = TRUE;
3731 }
3732#endif
3733
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003734 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 col = 0;
3736#ifdef FEAT_RIGHTLEFT
3737 if (wp->w_p_rl)
3738 {
3739 /* Rightleft window: process the text in the normal direction, but put
3740 * it in current_ScreenLine[] from right to left. Start at the
3741 * rightmost column of the window. */
3742 col = W_WIDTH(wp) - 1;
3743 off += col;
3744 }
3745#endif
3746
3747 /*
3748 * Repeat for the whole displayed line.
3749 */
3750 for (;;)
3751 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003752#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003753 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 /* Skip this quickly when working on the text. */
3756 if (draw_state != WL_LINE)
3757 {
3758#ifdef FEAT_CMDWIN
3759 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3760 {
3761 draw_state = WL_CMDLINE;
3762 if (cmdwin_type != 0 && wp == curwin)
3763 {
3764 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003766 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003767 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769 }
3770#endif
3771
3772#ifdef FEAT_FOLDING
3773 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3774 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003775 int fdc = compute_foldcolumn(wp, 0);
3776
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003778 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003780 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003781 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003782 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003783 p_extra_free = alloc(12 + 1);
3784
3785 if (p_extra_free != NULL)
3786 {
3787 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3788 n_extra = fdc;
3789 p_extra_free[n_extra] = NUL;
3790 p_extra = p_extra_free;
3791 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003792 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795 }
3796#endif
3797
3798#ifdef FEAT_SIGNS
3799 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3800 {
3801 draw_state = WL_SIGN;
3802 /* Show the sign column when there are any signs in this
3803 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003804 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003806 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003808 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809# endif
3810
3811 /* Draw two cells with the sign value or blank. */
3812 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003813 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 n_extra = 2;
3815
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003816 if (row == startrow
3817#ifdef FEAT_DIFF
3818 + filler_lines && filler_todo <= 0
3819#endif
3820 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
3822 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3823 SIGN_TEXT);
3824# ifdef FEAT_SIGN_ICONS
3825 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3826 SIGN_ICON);
3827 if (gui.in_use && icon_sign != 0)
3828 {
3829 /* Use the image in this position. */
3830 c_extra = SIGN_BYTE;
3831# ifdef FEAT_NETBEANS_INTG
3832 if (buf_signcount(wp->w_buffer, lnum) > 1)
3833 c_extra = MULTISIGN_BYTE;
3834# endif
3835 char_attr = icon_sign;
3836 }
3837 else
3838# endif
3839 if (text_sign != 0)
3840 {
3841 p_extra = sign_get_text(text_sign);
3842 if (p_extra != NULL)
3843 {
3844 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003845 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
3847 char_attr = sign_get_attr(text_sign, FALSE);
3848 }
3849 }
3850 }
3851 }
3852#endif
3853
3854 if (draw_state == WL_NR - 1 && n_extra == 0)
3855 {
3856 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003857 /* Display the absolute or relative line number. After the
3858 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3859 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 && (row == startrow
3861#ifdef FEAT_DIFF
3862 + filler_lines
3863#endif
3864 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3865 {
3866 /* Draw the line number (empty space after wrapping). */
3867 if (row == startrow
3868#ifdef FEAT_DIFF
3869 + filler_lines
3870#endif
3871 )
3872 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003873 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003874 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003875
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003876 if (wp->w_p_nu && !wp->w_p_rnu)
3877 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003878 num = (long)lnum;
3879 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003880 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003881 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003882 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003883 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003884 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003885 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003886 num = lnum;
3887 fmt = "%-*ld ";
3888 }
3889 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003890
Bram Moolenaar700e7342013-01-30 12:31:36 +01003891 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003892 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (wp->w_skipcol > 0)
3894 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3895 *p_extra = '-';
3896#ifdef FEAT_RIGHTLEFT
3897 if (wp->w_p_rl) /* reverse line numbers */
3898 rl_mirror(extra);
3899#endif
3900 p_extra = extra;
3901 c_extra = NUL;
3902 }
3903 else
3904 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003905 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003906 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003907#ifdef FEAT_SYN_HL
3908 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003909 * the current line differently.
3910 * TODO: Can we use CursorLine instead of CursorLineNr
3911 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003912 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003913 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003914 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917 }
3918
Bram Moolenaar597a4222014-06-25 14:39:50 +02003919#ifdef FEAT_LINEBREAK
3920 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3921 && n_extra == 0 && *p_sbr != NUL)
3922 /* draw indent after showbreak value */
3923 draw_state = WL_BRI;
3924 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3925 /* After the showbreak, draw the breakindent */
3926 draw_state = WL_BRI - 1;
3927
3928 /* draw 'breakindent': indent wrapped text accordingly */
3929 if (draw_state == WL_BRI - 1 && n_extra == 0)
3930 {
3931 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003932 /* if need_showbreak is set, breakindent also applies */
3933 if (wp->w_p_bri && n_extra == 0
3934 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003935# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003936 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003937# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003938 )
3939 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003940 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003941# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003942 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003943 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003944 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003945# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003946 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3947 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003948 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003949# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003950 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003951# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003952 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003953 c_extra = ' ';
3954 n_extra = get_breakindent_win(wp,
3955 ml_get_buf(wp->w_buffer, lnum, FALSE));
3956 /* Correct end of highlighted area for 'breakindent',
3957 * required when 'linebreak' is also set. */
3958 if (tocol == vcol)
3959 tocol += n_extra;
3960 }
3961 }
3962#endif
3963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3965 if (draw_state == WL_SBR - 1 && n_extra == 0)
3966 {
3967 draw_state = WL_SBR;
3968# ifdef FEAT_DIFF
3969 if (filler_todo > 0)
3970 {
3971 /* Draw "deleted" diff line(s). */
3972 if (char2cells(fill_diff) > 1)
3973 c_extra = '-';
3974 else
3975 c_extra = fill_diff;
3976# ifdef FEAT_RIGHTLEFT
3977 if (wp->w_p_rl)
3978 n_extra = col + 1;
3979 else
3980# endif
3981 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003982 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
3984# endif
3985# ifdef FEAT_LINEBREAK
3986 if (*p_sbr != NUL && need_showbreak)
3987 {
3988 /* Draw 'showbreak' at the start of each broken line. */
3989 p_extra = p_sbr;
3990 c_extra = NUL;
3991 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003992 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003994 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 /* Correct end of highlighted area for 'showbreak',
3996 * required when 'linebreak' is also set. */
3997 if (tocol == vcol)
3998 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003999#ifdef FEAT_SYN_HL
4000 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004001 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004002 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004003 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
4006# endif
4007 }
4008#endif
4009
4010 if (draw_state == WL_LINE - 1 && n_extra == 0)
4011 {
4012 draw_state = WL_LINE;
4013 if (saved_n_extra)
4014 {
4015 /* Continue item from end of wrapped line. */
4016 n_extra = saved_n_extra;
4017 c_extra = saved_c_extra;
4018 p_extra = saved_p_extra;
4019 char_attr = saved_char_attr;
4020 }
4021 else
4022 char_attr = 0;
4023 }
4024 }
4025
4026 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004027 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004028 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029#ifdef FEAT_DIFF
4030 && filler_todo <= 0
4031#endif
4032 )
4033 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004034 screen_line(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004035 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004036 /* Pretend we have finished updating the window. Except when
4037 * 'cursorcolumn' is set. */
4038#ifdef FEAT_SYN_HL
4039 if (wp->w_p_cuc)
4040 row = wp->w_cline_row + wp->w_cline_height;
4041 else
4042#endif
4043 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 break;
4045 }
4046
4047 if (draw_state == WL_LINE && area_highlighting)
4048 {
4049 /* handle Visual or match highlighting in this line */
4050 if (vcol == fromcol
4051#ifdef FEAT_MBYTE
4052 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4053 && (*mb_ptr2cells)(ptr) > 1)
4054#endif
4055 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004056 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 && vcol < tocol))
4058 area_attr = attr; /* start highlighting */
4059 else if (area_attr != 0
4060 && (vcol == tocol
4061 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064#ifdef FEAT_SEARCH_EXTRA
4065 if (!n_extra)
4066 {
4067 /*
4068 * Check for start/end of search pattern match.
4069 * After end, check for start/end of next match.
4070 * When another match, have to check for start again.
4071 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004072 * Do this for 'search_hl' and the match list (ordered by
4073 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004075 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004076 cur = wp->w_match_head;
4077 shl_flag = FALSE;
4078 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004080 if (shl_flag == FALSE
4081 && ((cur != NULL
4082 && cur->priority > SEARCH_HL_PRIORITY)
4083 || cur == NULL))
4084 {
4085 shl = &search_hl;
4086 shl_flag = TRUE;
4087 }
4088 else
4089 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004090 if (cur != NULL)
4091 cur->pos.cur = 0;
4092 pos_inprogress = TRUE;
4093 while (shl->rm.regprog != NULL
4094 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004096 if (shl->startcol != MAXCOL
4097 && v >= (long)shl->startcol
4098 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004100#ifdef FEAT_MBYTE
4101 int tmp_col = v + MB_PTR2LEN(ptr);
4102
4103 if (shl->endcol < tmp_col)
4104 shl->endcol = tmp_col;
4105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004107#ifdef FEAT_CONCEAL
4108 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4109 == cur->hlg_id)
4110 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004111 has_match_conc =
4112 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004113 match_conc = cur->conceal_char;
4114 }
4115 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004116 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004119 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
4121 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004122 next_search_hl(wp, shl, lnum, (colnr_T)v,
4123 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004124 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004125 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
4127 /* Need to get the line again, a multi-line regexp
4128 * may have made it invalid. */
4129 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4130 ptr = line + v;
4131
4132 if (shl->lnum == lnum)
4133 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004134 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004136 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004138 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004140 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
4142 /* highlight empty match, try again after
4143 * it */
4144#ifdef FEAT_MBYTE
4145 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004146 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004147 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 else
4149#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004150 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152
4153 /* Loop to check if the match starts at the
4154 * current position */
4155 continue;
4156 }
4157 }
4158 break;
4159 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004160 if (shl != &search_hl && cur != NULL)
4161 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004163
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004164 /* Use attributes from match with highest priority among
4165 * 'search_hl' and the match list. */
4166 search_attr = search_hl.attr_cur;
4167 cur = wp->w_match_head;
4168 shl_flag = FALSE;
4169 while (cur != NULL || shl_flag == FALSE)
4170 {
4171 if (shl_flag == FALSE
4172 && ((cur != NULL
4173 && cur->priority > SEARCH_HL_PRIORITY)
4174 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004175 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004176 shl = &search_hl;
4177 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004178 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004179 else
4180 shl = &cur->hl;
4181 if (shl->attr_cur != 0)
4182 search_attr = shl->attr_cur;
4183 if (shl != &search_hl && cur != NULL)
4184 cur = cur->next;
4185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187#endif
4188
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004190 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004192 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4193 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004195 if (diff_hlf == HLF_TXD && ptr - line > change_end
4196 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004198 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004199 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004200 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004203
4204 /* Decide which of the highlight attributes to use. */
4205 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004206#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004207 if (area_attr != 0)
4208 char_attr = hl_combine_attr(line_attr, area_attr);
4209 else if (search_attr != 0)
4210 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004211 /* Use line_attr when not in the Visual or 'incsearch' area
4212 * (area_attr may be 0 when "noinvcur" is set). */
4213 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004214 || vcol < fromcol || vcol_prev < fromcol_prev
4215 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004216 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004217#else
4218 if (area_attr != 0)
4219 char_attr = area_attr;
4220 else if (search_attr != 0)
4221 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004222#endif
4223 else
4224 {
4225 attr_pri = FALSE;
4226#ifdef FEAT_SYN_HL
4227 if (has_syntax)
4228 char_attr = syntax_attr;
4229 else
4230#endif
4231 char_attr = 0;
4232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234
4235 /*
4236 * Get the next character to put on the screen.
4237 */
4238 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004239 * The "p_extra" points to the extra stuff that is inserted to
4240 * represent special characters (non-printable stuff) and other
4241 * things. When all characters are the same, c_extra is used.
4242 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4243 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4245 */
4246 if (n_extra > 0)
4247 {
4248 if (c_extra != NUL)
4249 {
4250 c = c_extra;
4251#ifdef FEAT_MBYTE
4252 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004253 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004256 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004257 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259 else
4260 mb_utf8 = FALSE;
4261#endif
4262 }
4263 else
4264 {
4265 c = *p_extra;
4266#ifdef FEAT_MBYTE
4267 if (has_mbyte)
4268 {
4269 mb_c = c;
4270 if (enc_utf8)
4271 {
4272 /* If the UTF-8 character is more than one byte:
4273 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004274 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 mb_utf8 = FALSE;
4276 if (mb_l > n_extra)
4277 mb_l = 1;
4278 else if (mb_l > 1)
4279 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004280 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004282 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 }
4285 else
4286 {
4287 /* if this is a DBCS character, put it in "mb_c" */
4288 mb_l = MB_BYTE2LEN(c);
4289 if (mb_l >= n_extra)
4290 mb_l = 1;
4291 else if (mb_l > 1)
4292 mb_c = (c << 8) + p_extra[1];
4293 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004294 if (mb_l == 0) /* at the NUL at end-of-line */
4295 mb_l = 1;
4296
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 /* If a double-width char doesn't fit display a '>' in the
4298 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004299 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300# ifdef FEAT_RIGHTLEFT
4301 wp->w_p_rl ? (col <= 0) :
4302# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004303 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 && (*mb_char2cells)(mb_c) == 2)
4305 {
4306 c = '>';
4307 mb_c = c;
4308 mb_l = 1;
4309 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004310 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 /* put the pointer back to output the double-width
4312 * character at the start of the next line. */
4313 ++n_extra;
4314 --p_extra;
4315 }
4316 else
4317 {
4318 n_extra -= mb_l - 1;
4319 p_extra += mb_l - 1;
4320 }
4321 }
4322#endif
4323 ++p_extra;
4324 }
4325 --n_extra;
4326 }
4327 else
4328 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004329#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004330 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004331#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004332
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004333 if (p_extra_free != NULL)
4334 {
4335 vim_free(p_extra_free);
4336 p_extra_free = NULL;
4337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 /*
4339 * Get a character from the line itself.
4340 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004341 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004342#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004343 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345#ifdef FEAT_MBYTE
4346 if (has_mbyte)
4347 {
4348 mb_c = c;
4349 if (enc_utf8)
4350 {
4351 /* If the UTF-8 character is more than one byte: Decode it
4352 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004353 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 mb_utf8 = FALSE;
4355 if (mb_l > 1)
4356 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004357 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 /* Overlong encoded ASCII or ASCII with composing char
4359 * is displayed normally, except a NUL. */
4360 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004361 {
4362 c = mb_c;
4363# ifdef FEAT_LINEBREAK
4364 c0 = mb_c;
4365# endif
4366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004368
4369 /* At start of the line we can have a composing char.
4370 * Draw it as a space with a composing char. */
4371 if (utf_iscomposing(mb_c))
4372 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004373 int i;
4374
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004375 for (i = Screen_mco - 1; i > 0; --i)
4376 u8cc[i] = u8cc[i - 1];
4377 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004378 mb_c = ' ';
4379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
4381
4382 if ((mb_l == 1 && c >= 0x80)
4383 || (mb_l >= 1 && mb_c == 0)
4384 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004385# ifdef UNICODE16
4386 || mb_c >= 0x10000
4387# endif
4388 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 {
4390 /*
4391 * Illegal UTF-8 byte: display as <xx>.
4392 * Non-BMP character : display as ? or fullwidth ?.
4393 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004394# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
4398 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004399# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 if (wp->w_p_rl) /* reverse */
4401 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004404# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 else if (utf_char2cells(mb_c) != 2)
4406 STRCPY(extra, "?");
4407 else
4408 /* 0xff1f in UTF-8: full-width '?' */
4409 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004410# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
4412 p_extra = extra;
4413 c = *p_extra;
4414 mb_c = mb_ptr2char_adv(&p_extra);
4415 mb_utf8 = (c >= 0x80);
4416 n_extra = (int)STRLEN(p_extra);
4417 c_extra = NUL;
4418 if (area_attr == 0 && search_attr == 0)
4419 {
4420 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004421 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 saved_attr2 = char_attr; /* save current attr */
4423 }
4424 }
4425 else if (mb_l == 0) /* at the NUL at end-of-line */
4426 mb_l = 1;
4427#ifdef FEAT_ARABIC
4428 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4429 {
4430 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004431 int pc, pc1, nc;
4432 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
4434 /* The idea of what is the previous and next
4435 * character depends on 'rightleft'. */
4436 if (wp->w_p_rl)
4437 {
4438 pc = prev_c;
4439 pc1 = prev_c1;
4440 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004441 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443 else
4444 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004445 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004447 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 prev_c = mb_c;
4450
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004451 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453 else
4454 prev_c = mb_c;
4455#endif
4456 }
4457 else /* enc_dbcs */
4458 {
4459 mb_l = MB_BYTE2LEN(c);
4460 if (mb_l == 0) /* at the NUL at end-of-line */
4461 mb_l = 1;
4462 else if (mb_l > 1)
4463 {
4464 /* We assume a second byte below 32 is illegal.
4465 * Hopefully this is OK for all double-byte encodings!
4466 */
4467 if (ptr[1] >= 32)
4468 mb_c = (c << 8) + ptr[1];
4469 else
4470 {
4471 if (ptr[1] == NUL)
4472 {
4473 /* head byte at end of line */
4474 mb_l = 1;
4475 transchar_nonprint(extra, c);
4476 }
4477 else
4478 {
4479 /* illegal tail byte */
4480 mb_l = 2;
4481 STRCPY(extra, "XX");
4482 }
4483 p_extra = extra;
4484 n_extra = (int)STRLEN(extra) - 1;
4485 c_extra = NUL;
4486 c = *p_extra++;
4487 if (area_attr == 0 && search_attr == 0)
4488 {
4489 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004490 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 saved_attr2 = char_attr; /* save current attr */
4492 }
4493 mb_c = c;
4494 }
4495 }
4496 }
4497 /* If a double-width char doesn't fit display a '>' in the
4498 * last column; the character is displayed at the start of the
4499 * next line. */
4500 if ((
4501# ifdef FEAT_RIGHTLEFT
4502 wp->w_p_rl ? (col <= 0) :
4503# endif
4504 (col >= W_WIDTH(wp) - 1))
4505 && (*mb_char2cells)(mb_c) == 2)
4506 {
4507 c = '>';
4508 mb_c = c;
4509 mb_utf8 = FALSE;
4510 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004511 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 /* Put pointer back so that the character will be
4513 * displayed at the start of the next line. */
4514 --ptr;
4515 }
4516 else if (*ptr != NUL)
4517 ptr += mb_l - 1;
4518
4519 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004520 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004521 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004522 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004525 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 c = ' ';
4527 if (area_attr == 0 && search_attr == 0)
4528 {
4529 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004530 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 saved_attr2 = char_attr; /* save current attr */
4532 }
4533 mb_c = c;
4534 mb_utf8 = FALSE;
4535 mb_l = 1;
4536 }
4537
4538 }
4539#endif
4540 ++ptr;
4541
4542 if (extra_check)
4543 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004544#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004545 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004546#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004547
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004548#ifdef FEAT_TERMINAL
4549 if (get_term_attr)
4550 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004551 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004552
4553 if (!attr_pri)
4554 char_attr = syntax_attr;
4555 else
4556 char_attr = hl_combine_attr(syntax_attr, char_attr);
4557 }
4558#endif
4559
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004560#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 /* Get syntax attribute, unless still at the start of the line
4562 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004563 v = (long)(ptr - line);
4564 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 {
4566 /* Get the syntax attribute for the character. If there
4567 * is an error, disable syntax highlighting. */
4568 save_did_emsg = did_emsg;
4569 did_emsg = FALSE;
4570
Bram Moolenaar217ad922005-03-20 22:37:15 +00004571 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004572# ifdef FEAT_SPELL
4573 has_spell ? &can_spell :
4574# endif
4575 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
4577 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004578 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004579 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004580 has_syntax = FALSE;
4581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 else
4583 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004584#ifdef SYN_TIME_LIMIT
4585 if (wp->w_s->b_syn_slow)
4586 has_syntax = FALSE;
4587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588
4589 /* Need to get the line again, a multi-line regexp may
4590 * have made it invalid. */
4591 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4592 ptr = line + v;
4593
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004594 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004596 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004597 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004598# ifdef FEAT_CONCEAL
4599 /* no concealing past the end of the line, it interferes
4600 * with line highlighting */
4601 if (c == NUL)
4602 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004603 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004604 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004607#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004608
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004609#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004610 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004611 * Only do this when there is no syntax highlighting, the
4612 * @Spell cluster is not used or the current syntax item
4613 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004614 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004615 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004616 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004617# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004618 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004619 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004620# endif
4621 if (c != 0 && (
4622# ifdef FEAT_SYN_HL
4623 !has_syntax ||
4624# endif
4625 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004626 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004627 char_u *prev_ptr, *p;
4628 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004629 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004630# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004631 if (has_mbyte)
4632 {
4633 prev_ptr = ptr - mb_l;
4634 v -= mb_l - 1;
4635 }
4636 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004637# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004638 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004639
4640 /* Use nextline[] if possible, it has the start of the
4641 * next line concatenated. */
4642 if ((prev_ptr - line) - nextlinecol >= 0)
4643 p = nextline + (prev_ptr - line) - nextlinecol;
4644 else
4645 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004646 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004647 len = spell_check(wp, p, &spell_hlf, &cap_col,
4648 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004649 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004650
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004651 /* In Insert mode only highlight a word that
4652 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004653 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004654 && (State & INSERT) != 0
4655 && wp->w_cursor.lnum == lnum
4656 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004657 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004658 && wp->w_cursor.col < (colnr_T)word_end)
4659 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004660 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004661 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004662 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004663
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004664 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004665 && (p - nextline) + len > nextline_idx)
4666 {
4667 /* Remember that the good word continues at the
4668 * start of the next line. */
4669 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004670 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004671 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004672
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004673 /* Turn index into actual attributes. */
4674 if (spell_hlf != HLF_COUNT)
4675 spell_attr = highlight_attr[spell_hlf];
4676
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004677 if (cap_col > 0)
4678 {
4679 if (p != prev_ptr
4680 && (p - nextline) + cap_col >= nextline_idx)
4681 {
4682 /* Remember that the word in the next line
4683 * must start with a capital. */
4684 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004685 cap_col = (int)((p - nextline) + cap_col
4686 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004687 }
4688 else
4689 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004690 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004691 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004692 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004693 }
4694 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004695 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004696 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004697 char_attr = hl_combine_attr(char_attr, spell_attr);
4698 else
4699 char_attr = hl_combine_attr(spell_attr, char_attr);
4700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701#endif
4702#ifdef FEAT_LINEBREAK
4703 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004704 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004706 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004707 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004709# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004710 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004711# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004712 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004714 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004716 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004717
Bram Moolenaar597a4222014-06-25 14:39:50 +02004718 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004719 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004720 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004721 if (c == TAB && n_extra + col > W_WIDTH(wp))
4722 n_extra = (int)wp->w_buffer->b_p_ts
4723 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4724
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004725# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004726 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004727# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004729# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004730 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004731 {
4732#ifdef FEAT_CONCEAL
4733 if (c == TAB)
4734 /* See "Tab alignment" below. */
4735 FIX_FOR_BOGUSCOLS;
4736#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004737 if (!wp->w_p_list)
4738 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
4741#endif
4742
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004743 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4744 */
4745 if (wp->w_p_list
4746 && (((c == 160
4747#ifdef FEAT_MBYTE
4748 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4749#endif
4750 ) && lcs_nbsp)
4751 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4752 {
4753 c = (c == ' ') ? lcs_space : lcs_nbsp;
4754 if (area_attr == 0 && search_attr == 0)
4755 {
4756 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004757 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004758 saved_attr2 = char_attr; /* save current attr */
4759 }
4760#ifdef FEAT_MBYTE
4761 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004762 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004763 {
4764 mb_utf8 = TRUE;
4765 u8cc[0] = 0;
4766 c = 0xc0;
4767 }
4768 else
4769 mb_utf8 = FALSE;
4770#endif
4771 }
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4774 {
4775 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004776 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
4778 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004779 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 saved_attr2 = char_attr; /* save current attr */
4781 }
4782#ifdef FEAT_MBYTE
4783 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004784 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004787 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004788 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790 else
4791 mb_utf8 = FALSE;
4792#endif
4793 }
4794 }
4795
4796 /*
4797 * Handling of non-printable characters.
4798 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004799 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
4801 /*
4802 * when getting a character from the file, we may have to
4803 * turn it into something else on the way to putting it
4804 * into "ScreenLines".
4805 */
4806 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4807 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004808 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004809 long vcol_adjusted = vcol; /* removed showbreak length */
4810#ifdef FEAT_LINEBREAK
4811 /* only adjust the tab_len, when at the first column
4812 * after the showbreak value was drawn */
4813 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4814 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004817 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004818 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4819
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004820#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004821 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004822#endif
4823 /* tab amount depends on current column */
4824 n_extra = tab_len;
4825#ifdef FEAT_LINEBREAK
4826 else
4827 {
4828 char_u *p;
4829 int len = n_extra;
4830 int i;
4831 int saved_nextra = n_extra;
4832
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004833#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004834 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004835 /* there are characters to conceal */
4836 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004837 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4838 */
4839 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4840 && n_extra > tab_len)
4841 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004842#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004843
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004844 /* if n_extra > 0, it gives the number of chars, to
4845 * use for a tab, else we need to calculate the width
4846 * for a tab */
4847#ifdef FEAT_MBYTE
4848 len = (tab_len * mb_char2len(lcs_tab2));
4849 if (n_extra > 0)
4850 len += n_extra - tab_len;
4851#endif
4852 c = lcs_tab1;
4853 p = alloc((unsigned)(len + 1));
4854 vim_memset(p, ' ', len);
4855 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004856 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004857 p_extra_free = p;
4858 for (i = 0; i < tab_len; i++)
4859 {
4860#ifdef FEAT_MBYTE
4861 mb_char2bytes(lcs_tab2, p);
4862 p += mb_char2len(lcs_tab2);
4863 n_extra += mb_char2len(lcs_tab2)
4864 - (saved_nextra > 0 ? 1 : 0);
4865#else
4866 p[i] = lcs_tab2;
4867#endif
4868 }
4869 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004870#ifdef FEAT_CONCEAL
4871 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4872 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004873 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004874 n_extra -= vcol_off;
4875#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004876 }
4877#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004878#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004879 {
4880 int vc_saved = vcol_off;
4881
4882 /* Tab alignment should be identical regardless of
4883 * 'conceallevel' value. So tab compensates of all
4884 * previous concealed characters, and thus resets
4885 * vcol_off and boguscols accumulated so far in the
4886 * line. Note that the tab can be longer than
4887 * 'tabstop' when there are concealed characters. */
4888 FIX_FOR_BOGUSCOLS;
4889
4890 /* Make sure, the highlighting for the tab char will be
4891 * correctly set further below (effectively reverts the
4892 * FIX_FOR_BOGSUCOLS macro */
4893 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004894 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004895 tab_len += vc_saved;
4896 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898#ifdef FEAT_MBYTE
4899 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4900#endif
4901 if (wp->w_p_list)
4902 {
4903 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004904#ifdef FEAT_LINEBREAK
4905 if (wp->w_p_lbr)
4906 c_extra = NUL; /* using p_extra from above */
4907 else
4908#endif
4909 c_extra = lcs_tab2;
4910 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004911 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 saved_attr2 = char_attr; /* save current attr */
4913#ifdef FEAT_MBYTE
4914 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004915 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
4917 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004918 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004919 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
4921#endif
4922 }
4923 else
4924 {
4925 c_extra = ' ';
4926 c = ' ';
4927 }
4928 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004929 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004930 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004931 || ((fromcol >= 0 || fromcol_prev >= 0)
4932 && tocol > vcol
4933 && VIsual_mode != Ctrl_V
4934 && (
4935# ifdef FEAT_RIGHTLEFT
4936 wp->w_p_rl ? (col >= 0) :
4937# endif
4938 (col < W_WIDTH(wp)))
4939 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004940 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004941 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004942 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004944 /* Display a '$' after the line or highlight an extra
4945 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4947 /* For a diff line the highlighting continues after the
4948 * "$". */
4949 if (
4950# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004951 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952# ifdef LINE_ATTR
4953 &&
4954# endif
4955# endif
4956# ifdef LINE_ATTR
4957 line_attr == 0
4958# endif
4959 )
4960#endif
4961 {
4962#ifdef FEAT_VIRTUALEDIT
4963 /* In virtualedit, visual selections may extend
4964 * beyond end of line. */
4965 if (area_highlighting && virtual_active()
4966 && tocol != MAXCOL && vcol < tocol)
4967 n_extra = 0;
4968 else
4969#endif
4970 {
4971 p_extra = at_end_str;
4972 n_extra = 1;
4973 c_extra = NUL;
4974 }
4975 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004976 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004977 c = lcs_eol;
4978 else
4979 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 lcs_eol_one = -1;
4981 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004982 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004984 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 n_attr = 1;
4986 }
4987#ifdef FEAT_MBYTE
4988 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004989 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
4991 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004992 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004993 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
4995 else
4996 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4997#endif
4998 }
4999 else if (c != NUL)
5000 {
5001 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005002 if (n_extra == 0)
5003 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004#ifdef FEAT_RIGHTLEFT
5005 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5006 rl_mirror(p_extra); /* reverse "<12>" */
5007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005009#ifdef FEAT_LINEBREAK
5010 if (wp->w_p_lbr)
5011 {
5012 char_u *p;
5013
5014 c = *p_extra;
5015 p = alloc((unsigned)n_extra + 1);
5016 vim_memset(p, ' ', n_extra);
5017 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5018 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005019 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005020 p_extra_free = p_extra = p;
5021 }
5022 else
5023#endif
5024 {
5025 n_extra = byte2cells(c) - 1;
5026 c = *p_extra++;
5027 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005028 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 {
5030 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005031 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 saved_attr2 = char_attr; /* save current attr */
5033 }
5034#ifdef FEAT_MBYTE
5035 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5036#endif
5037 }
5038#ifdef FEAT_VIRTUALEDIT
5039 else if (VIsual_active
5040 && (VIsual_mode == Ctrl_V
5041 || VIsual_mode == 'v')
5042 && virtual_active()
5043 && tocol != MAXCOL
5044 && vcol < tocol
5045 && (
5046# ifdef FEAT_RIGHTLEFT
5047 wp->w_p_rl ? (col >= 0) :
5048# endif
5049 (col < W_WIDTH(wp))))
5050 {
5051 c = ' ';
5052 --ptr; /* put it back at the NUL */
5053 }
5054#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005055#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 else if ((
5057# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005058 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 ) && (
5062# ifdef FEAT_RIGHTLEFT
5063 wp->w_p_rl ? (col >= 0) :
5064# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005065 (col
5066# ifdef FEAT_CONCEAL
5067 - boguscols
5068# endif
5069 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
5071 /* Highlight until the right side of the window */
5072 c = ' ';
5073 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005074
5075 /* Remember we do the char for line highlighting. */
5076 ++did_line_attr;
5077
5078 /* don't do search HL for the rest of the line */
5079 if (line_attr != 0 && char_attr == search_attr && col > 0)
5080 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081# ifdef FEAT_DIFF
5082 if (diff_hlf == HLF_TXD)
5083 {
5084 diff_hlf = HLF_CHD;
5085 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005086 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005087 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005088 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5089 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005090 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093# endif
5094 }
5095#endif
5096 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005097
5098#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005099 if ( wp->w_p_cole > 0
5100 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005101 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005102 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005103 && !(lnum_in_visual_area
5104 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005105 {
5106 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005107 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005108 && (syn_get_sub_char() != NUL || match_conc
5109 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005110 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005111 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005112 /* First time at this concealed item: display one
5113 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005114 if (match_conc)
5115 c = match_conc;
5116 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005117 c = syn_get_sub_char();
5118 else if (lcs_conceal != NUL)
5119 c = lcs_conceal;
5120 else
5121 c = ' ';
5122
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005123 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005124
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005125 if (n_extra > 0)
5126 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005127 vcol += n_extra;
5128 if (wp->w_p_wrap && n_extra > 0)
5129 {
5130# ifdef FEAT_RIGHTLEFT
5131 if (wp->w_p_rl)
5132 {
5133 col -= n_extra;
5134 boguscols -= n_extra;
5135 }
5136 else
5137# endif
5138 {
5139 boguscols += n_extra;
5140 col += n_extra;
5141 }
5142 }
5143 n_extra = 0;
5144 n_attr = 0;
5145 }
5146 else if (n_skip == 0)
5147 {
5148 is_concealing = TRUE;
5149 n_skip = 1;
5150 }
5151# ifdef FEAT_MBYTE
5152 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005153 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005154 {
5155 mb_utf8 = TRUE;
5156 u8cc[0] = 0;
5157 c = 0xc0;
5158 }
5159 else
5160 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5161# endif
5162 }
5163 else
5164 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005165 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005166 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005167 }
5168#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005171#ifdef FEAT_CONCEAL
5172 /* In the cursor line and we may be concealing characters: correct
5173 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005174 if (!did_wcol && draw_state == WL_LINE
5175 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005176 && conceal_cursor_line(wp)
5177 && (int)wp->w_virtcol <= vcol + n_skip)
5178 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005179# ifdef FEAT_RIGHTLEFT
5180 if (wp->w_p_rl)
5181 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5182 else
5183# endif
5184 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005185 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005186 did_wcol = TRUE;
5187 }
5188#endif
5189
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 /* Don't override visual selection highlighting. */
5191 if (n_attr > 0
5192 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005193 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 char_attr = extra_attr;
5195
Bram Moolenaar81695252004-12-29 20:58:21 +00005196#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 /* XIM don't send preedit_start and preedit_end, but they send
5198 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5199 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005200 if (p_imst == IM_ON_THE_SPOT
5201 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005202 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 && (State & INSERT)
5204 && !p_imdisable
5205 && im_is_preediting()
5206 && draw_state == WL_LINE)
5207 {
5208 colnr_T tcol;
5209
5210 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005211 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 else
5213 tcol = preedit_end_col;
5214 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5215 {
5216 if (feedback_old_attr < 0)
5217 {
5218 feedback_col = 0;
5219 feedback_old_attr = char_attr;
5220 }
5221 char_attr = im_get_feedback_attr(feedback_col);
5222 if (char_attr < 0)
5223 char_attr = feedback_old_attr;
5224 feedback_col++;
5225 }
5226 else if (feedback_old_attr >= 0)
5227 {
5228 char_attr = feedback_old_attr;
5229 feedback_old_attr = -1;
5230 feedback_col = 0;
5231 }
5232 }
5233#endif
5234 /*
5235 * Handle the case where we are in column 0 but not on the first
5236 * character of the line and the user wants us to show us a
5237 * special character (via 'listchars' option "precedes:<char>".
5238 */
5239 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005240 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5242#ifdef FEAT_DIFF
5243 && filler_todo <= 0
5244#endif
5245 && draw_state > WL_NR
5246 && c != NUL)
5247 {
5248 c = lcs_prec;
5249 lcs_prec_todo = NUL;
5250#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005251 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5252 {
5253 /* Double-width character being overwritten by the "precedes"
5254 * character, need to fill up half the character. */
5255 c_extra = MB_FILLER_CHAR;
5256 n_extra = 1;
5257 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005258 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005261 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
5263 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005264 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005265 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
5267 else
5268 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5269#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005270 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 {
5272 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005273 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 n_attr3 = 1;
5275 }
5276 }
5277
5278 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005279 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005281 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005282#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005283 || did_line_attr == 1
5284#endif
5285 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005287#ifdef FEAT_SEARCH_EXTRA
5288 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005289
5290 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005291 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005292 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005293#endif
5294
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005295 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 * highlight match at end of line. If it's beyond the last
5297 * char on the screen, just overwrite that one (tricky!) Not
5298 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005299#ifdef FEAT_SEARCH_EXTRA
5300 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005301 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005302 prevcol_hl_flag = TRUE;
5303 else
5304 {
5305 cur = wp->w_match_head;
5306 while (cur != NULL)
5307 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005308 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005309 {
5310 prevcol_hl_flag = TRUE;
5311 break;
5312 }
5313 cur = cur->next;
5314 }
5315 }
5316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005318 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005319 && (VIsual_mode != Ctrl_V
5320 || lnum == VIsual.lnum
5321 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005322 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323#ifdef FEAT_SEARCH_EXTRA
5324 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005325 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005326# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005327 && did_line_attr <= 1
5328# endif
5329 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330#endif
5331 ))
5332 {
5333 int n = 0;
5334
5335#ifdef FEAT_RIGHTLEFT
5336 if (wp->w_p_rl)
5337 {
5338 if (col < 0)
5339 n = 1;
5340 }
5341 else
5342#endif
5343 {
5344 if (col >= W_WIDTH(wp))
5345 n = -1;
5346 }
5347 if (n != 0)
5348 {
5349 /* At the window boundary, highlight the last character
5350 * instead (better than nothing). */
5351 off += n;
5352 col += n;
5353 }
5354 else
5355 {
5356 /* Add a blank character to highlight. */
5357 ScreenLines[off] = ' ';
5358#ifdef FEAT_MBYTE
5359 if (enc_utf8)
5360 ScreenLinesUC[off] = 0;
5361#endif
5362 }
5363#ifdef FEAT_SEARCH_EXTRA
5364 if (area_attr == 0)
5365 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005366 /* Use attributes from match with highest priority among
5367 * 'search_hl' and the match list. */
5368 char_attr = search_hl.attr;
5369 cur = wp->w_match_head;
5370 shl_flag = FALSE;
5371 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005372 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005373 if (shl_flag == FALSE
5374 && ((cur != NULL
5375 && cur->priority > SEARCH_HL_PRIORITY)
5376 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005377 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005378 shl = &search_hl;
5379 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005380 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005381 else
5382 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005383 if ((ptr - line) - 1 == (long)shl->startcol
5384 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005385 char_attr = shl->attr;
5386 if (shl != &search_hl && cur != NULL)
5387 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 }
5390#endif
5391 ScreenAttrs[off] = char_attr;
5392#ifdef FEAT_RIGHTLEFT
5393 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005396 --off;
5397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 else
5399#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005402 ++off;
5403 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005404 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005405#ifdef FEAT_SYN_HL
5406 eol_hl_off = 1;
5407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
Bram Moolenaar91170f82006-05-05 21:15:17 +00005411 /*
5412 * At end of the text line.
5413 */
5414 if (c == NUL)
5415 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005416#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005417 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5418 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005419 {
5420 /* highlight last char after line */
5421 --col;
5422 --off;
5423 --vcol;
5424 }
5425
Bram Moolenaar1a384422010-07-14 19:53:30 +02005426 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005427 if (wp->w_p_wrap)
5428 v = wp->w_skipcol;
5429 else
5430 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005431
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005432 /* check if line ends before left margin */
5433 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005434 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005435#ifdef FEAT_CONCEAL
5436 /* Get rid of the boguscols now, we want to draw until the right
5437 * edge for 'cursorcolumn'. */
5438 col -= boguscols;
5439 boguscols = 0;
5440#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005441
5442 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005443 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005444
5445 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005446 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5447 && (int)wp->w_virtcol <
5448 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005449 && lnum != wp->w_cursor.lnum)
5450 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005451# ifdef FEAT_RIGHTLEFT
5452 && !wp->w_p_rl
5453# endif
5454 )
5455 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005456 int rightmost_vcol = 0;
5457 int i;
5458
5459 if (wp->w_p_cuc)
5460 rightmost_vcol = wp->w_virtcol;
5461 if (draw_color_col)
5462 /* determine rightmost colorcolumn to possibly draw */
5463 for (i = 0; color_cols[i] >= 0; ++i)
5464 if (rightmost_vcol < color_cols[i])
5465 rightmost_vcol = color_cols[i];
5466
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005467 while (col < W_WIDTH(wp))
5468 {
5469 ScreenLines[off] = ' ';
5470#ifdef FEAT_MBYTE
5471 if (enc_utf8)
5472 ScreenLinesUC[off] = 0;
5473#endif
5474 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005475 if (draw_color_col)
5476 draw_color_col = advance_color_col(VCOL_HLC,
5477 &color_cols);
5478
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005479 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005480 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005481 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005482 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005483 else
5484 ScreenAttrs[off++] = 0;
5485
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005486 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005487 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005488
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005489 ++vcol;
5490 }
5491 }
5492#endif
5493
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005494 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005495 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 row++;
5497
5498 /*
5499 * Update w_cline_height and w_cline_folded if the cursor line was
5500 * updated (saves a call to plines() later).
5501 */
5502 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5503 {
5504 curwin->w_cline_row = startrow;
5505 curwin->w_cline_height = row - startrow;
5506#ifdef FEAT_FOLDING
5507 curwin->w_cline_folded = FALSE;
5508#endif
5509 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5510 }
5511
5512 break;
5513 }
5514
5515 /* line continues beyond line end */
5516 if (lcs_ext
5517 && !wp->w_p_wrap
5518#ifdef FEAT_DIFF
5519 && filler_todo <= 0
5520#endif
5521 && (
5522#ifdef FEAT_RIGHTLEFT
5523 wp->w_p_rl ? col == 0 :
5524#endif
5525 col == W_WIDTH(wp) - 1)
5526 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005527 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5529 {
5530 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005531 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532#ifdef FEAT_MBYTE
5533 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005534 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
5536 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005537 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005538 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 }
5540 else
5541 mb_utf8 = FALSE;
5542#endif
5543 }
5544
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005545#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005546 /* advance to the next 'colorcolumn' */
5547 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005548 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005549
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005550 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005551 * highlight the cursor position itself.
5552 * Also highlight the 'colorcolumn' if it is different than
5553 * 'cursorcolumn' */
5554 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005555 if (draw_state == WL_LINE && !lnum_in_visual_area
5556 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005557 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005558 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005559 && lnum != wp->w_cursor.lnum)
5560 {
5561 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005562 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005563 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005564 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005565 {
5566 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005567 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005568 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005569 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005570#endif
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 /*
5573 * Store character to be displayed.
5574 * Skip characters that are left of the screen for 'nowrap'.
5575 */
5576 vcol_prev = vcol;
5577 if (draw_state < WL_LINE || n_skip <= 0)
5578 {
5579 /*
5580 * Store the character.
5581 */
5582#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5583 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5584 {
5585 /* A double-wide character is: put first halve in left cell. */
5586 --off;
5587 --col;
5588 }
5589#endif
5590 ScreenLines[off] = c;
5591#ifdef FEAT_MBYTE
5592 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005593 {
5594 if ((mb_c & 0xff00) == 0x8e00)
5595 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 else if (enc_utf8)
5599 {
5600 if (mb_utf8)
5601 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005602 int i;
5603
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005605 if ((c & 0xff) == 0)
5606 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005607 for (i = 0; i < Screen_mco; ++i)
5608 {
5609 ScreenLinesC[i][off] = u8cc[i];
5610 if (u8cc[i] == 0)
5611 break;
5612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614 else
5615 ScreenLinesUC[off] = 0;
5616 }
5617 if (multi_attr)
5618 {
5619 ScreenAttrs[off] = multi_attr;
5620 multi_attr = 0;
5621 }
5622 else
5623#endif
5624 ScreenAttrs[off] = char_attr;
5625
5626#ifdef FEAT_MBYTE
5627 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5628 {
5629 /* Need to fill two screen columns. */
5630 ++off;
5631 ++col;
5632 if (enc_utf8)
5633 /* UTF-8: Put a 0 in the second screen char. */
5634 ScreenLines[off] = 0;
5635 else
5636 /* DBCS: Put second byte in the second screen char. */
5637 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005638 if (draw_state > WL_NR
5639#ifdef FEAT_DIFF
5640 && filler_todo <= 0
5641#endif
5642 )
5643 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 /* When "tocol" is halfway a character, set it to the end of
5645 * the character, otherwise highlighting won't stop. */
5646 if (tocol == vcol)
5647 ++tocol;
5648#ifdef FEAT_RIGHTLEFT
5649 if (wp->w_p_rl)
5650 {
5651 /* now it's time to backup one cell */
5652 --off;
5653 --col;
5654 }
5655#endif
5656 }
5657#endif
5658#ifdef FEAT_RIGHTLEFT
5659 if (wp->w_p_rl)
5660 {
5661 --off;
5662 --col;
5663 }
5664 else
5665#endif
5666 {
5667 ++off;
5668 ++col;
5669 }
5670 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005671#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005672 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005673 {
5674 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005675 ++vcol_off;
5676 if (n_extra > 0)
5677 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005678 if (wp->w_p_wrap)
5679 {
5680 /*
5681 * Special voodoo required if 'wrap' is on.
5682 *
5683 * Advance the column indicator to force the line
5684 * drawing to wrap early. This will make the line
5685 * take up the same screen space when parts are concealed,
5686 * so that cursor line computations aren't messed up.
5687 *
5688 * To avoid the fictitious advance of 'col' causing
5689 * trailing junk to be written out of the screen line
5690 * we are building, 'boguscols' keeps track of the number
5691 * of bad columns we have advanced.
5692 */
5693 if (n_extra > 0)
5694 {
5695 vcol += n_extra;
5696# ifdef FEAT_RIGHTLEFT
5697 if (wp->w_p_rl)
5698 {
5699 col -= n_extra;
5700 boguscols -= n_extra;
5701 }
5702 else
5703# endif
5704 {
5705 col += n_extra;
5706 boguscols += n_extra;
5707 }
5708 n_extra = 0;
5709 n_attr = 0;
5710 }
5711
5712
5713# ifdef FEAT_MBYTE
5714 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5715 {
5716 /* Need to fill two screen columns. */
5717# ifdef FEAT_RIGHTLEFT
5718 if (wp->w_p_rl)
5719 {
5720 --boguscols;
5721 --col;
5722 }
5723 else
5724# endif
5725 {
5726 ++boguscols;
5727 ++col;
5728 }
5729 }
5730# endif
5731
5732# ifdef FEAT_RIGHTLEFT
5733 if (wp->w_p_rl)
5734 {
5735 --boguscols;
5736 --col;
5737 }
5738 else
5739# endif
5740 {
5741 ++boguscols;
5742 ++col;
5743 }
5744 }
5745 else
5746 {
5747 if (n_extra > 0)
5748 {
5749 vcol += n_extra;
5750 n_extra = 0;
5751 n_attr = 0;
5752 }
5753 }
5754
5755 }
5756#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 else
5758 --n_skip;
5759
Bram Moolenaar64486672010-05-16 15:46:46 +02005760 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5761 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005762 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763#ifdef FEAT_DIFF
5764 && filler_todo <= 0
5765#endif
5766 )
5767 ++vcol;
5768
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005769#ifdef FEAT_SYN_HL
5770 if (vcol_save_attr >= 0)
5771 char_attr = vcol_save_attr;
5772#endif
5773
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 /* restore attributes after "predeces" in 'listchars' */
5775 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5776 char_attr = saved_attr3;
5777
5778 /* restore attributes after last 'listchars' or 'number' char */
5779 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5780 char_attr = saved_attr2;
5781
5782 /*
5783 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005784 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 */
5786 if ((
5787#ifdef FEAT_RIGHTLEFT
5788 wp->w_p_rl ? (col < 0) :
5789#endif
5790 (col >= W_WIDTH(wp)))
5791 && (*ptr != NUL
5792#ifdef FEAT_DIFF
5793 || filler_todo > 0
5794#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005795 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5797 )
5798 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005799#ifdef FEAT_CONCEAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005800 screen_line(screen_row, W_WINCOL(wp), col - boguscols,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005801 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005802 boguscols = 0;
5803#else
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005804 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005805 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 ++row;
5808 ++screen_row;
5809
5810 /* When not wrapping and finished diff lines, or when displayed
5811 * '$' and highlighting until last column, break here. */
5812 if ((!wp->w_p_wrap
5813#ifdef FEAT_DIFF
5814 && filler_todo <= 0
5815#endif
5816 ) || lcs_eol_one == -1)
5817 break;
5818
5819 /* When the window is too narrow draw all "@" lines. */
5820 if (draw_state != WL_LINE
5821#ifdef FEAT_DIFF
5822 && filler_todo <= 0
5823#endif
5824 )
5825 {
5826 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005827#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 draw_vsep_win(wp, row);
5829#endif
5830 row = endrow;
5831 }
5832
5833 /* When line got too long for screen break here. */
5834 if (row == endrow)
5835 {
5836 ++row;
5837 break;
5838 }
5839
5840 if (screen_cur_row == screen_row - 1
5841#ifdef FEAT_DIFF
5842 && filler_todo <= 0
5843#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005844#ifdef FEAT_WINDOWS
5845 && W_WIDTH(wp) == Columns
5846#endif
5847 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
5849 /* Remember that the line wraps, used for modeless copy. */
5850 LineWraps[screen_row - 1] = TRUE;
5851
5852 /*
5853 * Special trick to make copy/paste of wrapped lines work with
5854 * xterm/screen: write an extra character beyond the end of
5855 * the line. This will work with all terminal types
5856 * (regardless of the xn,am settings).
5857 * Only do this on a fast tty.
5858 * Only do this if the cursor is on the current line
5859 * (something has been written in it).
5860 * Don't do this for the GUI.
5861 * Don't do this for double-width characters.
5862 * Don't do this for a window not at the right screen border.
5863 */
5864 if (p_tf
5865#ifdef FEAT_GUI
5866 && !gui.in_use
5867#endif
5868#ifdef FEAT_MBYTE
5869 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005870 && ((*mb_off2cells)(LineOffset[screen_row],
5871 LineOffset[screen_row] + screen_Columns)
5872 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005874 + (int)Columns - 2,
5875 LineOffset[screen_row] + screen_Columns)
5876 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877#endif
5878 )
5879 {
5880 /* First make sure we are at the end of the screen line,
5881 * then output the same character again to let the
5882 * terminal know about the wrap. If the terminal doesn't
5883 * auto-wrap, we overwrite the character. */
5884 if (screen_cur_col != W_WIDTH(wp))
5885 screen_char(LineOffset[screen_row - 1]
5886 + (unsigned)Columns - 1,
5887 screen_row - 1, (int)(Columns - 1));
5888
5889#ifdef FEAT_MBYTE
5890 /* When there is a multi-byte character, just output a
5891 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005892 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5893 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 out_char(' ');
5895 else
5896#endif
5897 out_char(ScreenLines[LineOffset[screen_row - 1]
5898 + (Columns - 1)]);
5899 /* force a redraw of the first char on the next line */
5900 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5901 screen_start(); /* don't know where cursor is now */
5902 }
5903 }
5904
5905 col = 0;
5906 off = (unsigned)(current_ScreenLine - ScreenLines);
5907#ifdef FEAT_RIGHTLEFT
5908 if (wp->w_p_rl)
5909 {
5910 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5911 off += col;
5912 }
5913#endif
5914
5915 /* reset the drawing state for the start of a wrapped line */
5916 draw_state = WL_START;
5917 saved_n_extra = n_extra;
5918 saved_p_extra = p_extra;
5919 saved_c_extra = c_extra;
5920 saved_char_attr = char_attr;
5921 n_extra = 0;
5922 lcs_prec_todo = lcs_prec;
5923#ifdef FEAT_LINEBREAK
5924# ifdef FEAT_DIFF
5925 if (filler_todo <= 0)
5926# endif
5927 need_showbreak = TRUE;
5928#endif
5929#ifdef FEAT_DIFF
5930 --filler_todo;
5931 /* When the filler lines are actually below the last line of the
5932 * file, don't draw the line itself, break here. */
5933 if (filler_todo == 0 && wp->w_botfill)
5934 break;
5935#endif
5936 }
5937
5938 } /* for every character in the line */
5939
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005940#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005941 /* After an empty line check first word for capital. */
5942 if (*skipwhite(line) == NUL)
5943 {
5944 capcol_lnum = lnum + 1;
5945 cap_col = 0;
5946 }
5947#endif
5948
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005949 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 return row;
5951}
5952
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005953#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005954static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005955
5956/*
5957 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005958 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005959 */
5960 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005961comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005962{
5963 int i;
5964
5965 for (i = 0; i < Screen_mco; ++i)
5966 {
5967 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5968 return TRUE;
5969 if (ScreenLinesC[i][off_from] == 0)
5970 break;
5971 }
5972 return FALSE;
5973}
5974#endif
5975
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976/*
5977 * Check whether the given character needs redrawing:
5978 * - the (first byte of the) character is different
5979 * - the attributes are different
5980 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005981 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 */
5983 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005984char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985{
5986 if (cols > 0
5987 && ((ScreenLines[off_from] != ScreenLines[off_to]
5988 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5989
5990#ifdef FEAT_MBYTE
5991 || (enc_dbcs != 0
5992 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5993 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5994 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5995 : (cols > 1 && ScreenLines[off_from + 1]
5996 != ScreenLines[off_to + 1])))
5997 || (enc_utf8
5998 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5999 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006000 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006001 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6002 && ScreenLines[off_from + 1]
6003 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004#endif
6005 ))
6006 return TRUE;
6007 return FALSE;
6008}
6009
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006010#if defined(FEAT_TERMINAL) || defined(PROTO)
6011/*
6012 * Return the index in ScreenLines[] for the current screen line.
6013 */
6014 int
6015screen_get_current_line_off()
6016{
6017 return (int)(current_ScreenLine - ScreenLines);
6018}
6019#endif
6020
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021/*
6022 * Move one "cooked" screen line to the screen, but only the characters that
6023 * have actually changed. Handle insert/delete character.
6024 * "coloff" gives the first column on the screen for this line.
6025 * "endcol" gives the columns where valid characters are.
6026 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6027 * needs to be cleared, negative otherwise.
6028 * "rlflag" is TRUE in a rightleft window:
6029 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6030 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6031 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006032 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006033screen_line(
6034 int row,
6035 int coloff,
6036 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006037 int clear_width,
6038 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
6040 unsigned off_from;
6041 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006042#ifdef FEAT_MBYTE
6043 unsigned max_off_from;
6044 unsigned max_off_to;
6045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006047#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 int hl;
6049#endif
6050 int force = FALSE; /* force update rest of the line */
6051 int redraw_this /* bool: does character need redraw? */
6052#ifdef FEAT_GUI
6053 = TRUE /* For GUI when while-loop empty */
6054#endif
6055 ;
6056 int redraw_next; /* redraw_this for next character */
6057#ifdef FEAT_MBYTE
6058 int clear_next = FALSE;
6059 int char_cells; /* 1: normal char */
6060 /* 2: occupies two display cells */
6061# define CHAR_CELLS char_cells
6062#else
6063# define CHAR_CELLS 1
6064#endif
6065
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006066 /* Check for illegal row and col, just in case. */
6067 if (row >= Rows)
6068 row = Rows - 1;
6069 if (endcol > Columns)
6070 endcol = Columns;
6071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072# ifdef FEAT_CLIPBOARD
6073 clip_may_clear_selection(row, row);
6074# endif
6075
6076 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6077 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006078#ifdef FEAT_MBYTE
6079 max_off_from = off_from + screen_Columns;
6080 max_off_to = LineOffset[row] + screen_Columns;
6081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082
6083#ifdef FEAT_RIGHTLEFT
6084 if (rlflag)
6085 {
6086 /* Clear rest first, because it's left of the text. */
6087 if (clear_width > 0)
6088 {
6089 while (col <= endcol && ScreenLines[off_to] == ' '
6090 && ScreenAttrs[off_to] == 0
6091# ifdef FEAT_MBYTE
6092 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6093# endif
6094 )
6095 {
6096 ++off_to;
6097 ++col;
6098 }
6099 if (col <= endcol)
6100 screen_fill(row, row + 1, col + coloff,
6101 endcol + coloff + 1, ' ', ' ', 0);
6102 }
6103 col = endcol + 1;
6104 off_to = LineOffset[row] + col + coloff;
6105 off_from += col;
6106 endcol = (clear_width > 0 ? clear_width : -clear_width);
6107 }
6108#endif /* FEAT_RIGHTLEFT */
6109
6110 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6111
6112 while (col < endcol)
6113 {
6114#ifdef FEAT_MBYTE
6115 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006116 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 else
6118 char_cells = 1;
6119#endif
6120
6121 redraw_this = redraw_next;
6122 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6123 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6124
6125#ifdef FEAT_GUI
6126 /* If the next character was bold, then redraw the current character to
6127 * remove any pixels that might have spilt over into us. This only
6128 * happens in the GUI.
6129 */
6130 if (redraw_next && gui.in_use)
6131 {
6132 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006133 if (hl > HL_ALL)
6134 hl = syn_attr2attr(hl);
6135 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 redraw_this = TRUE;
6137 }
6138#endif
6139
6140 if (redraw_this)
6141 {
6142 /*
6143 * Special handling when 'xs' termcap flag set (hpterm):
6144 * Attributes for characters are stored at the position where the
6145 * cursor is when writing the highlighting code. The
6146 * start-highlighting code must be written with the cursor on the
6147 * first highlighted character. The stop-highlighting code must
6148 * be written with the cursor just after the last highlighted
6149 * character.
6150 * Overwriting a character doesn't remove it's highlighting. Need
6151 * to clear the rest of the line, and force redrawing it
6152 * completely.
6153 */
6154 if ( p_wiv
6155 && !force
6156#ifdef FEAT_GUI
6157 && !gui.in_use
6158#endif
6159 && ScreenAttrs[off_to] != 0
6160 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6161 {
6162 /*
6163 * Need to remove highlighting attributes here.
6164 */
6165 windgoto(row, col + coloff);
6166 out_str(T_CE); /* clear rest of this screen line */
6167 screen_start(); /* don't know where cursor is now */
6168 force = TRUE; /* force redraw of rest of the line */
6169 redraw_next = TRUE; /* or else next char would miss out */
6170
6171 /*
6172 * If the previous character was highlighted, need to stop
6173 * highlighting at this character.
6174 */
6175 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6176 {
6177 screen_attr = ScreenAttrs[off_to - 1];
6178 term_windgoto(row, col + coloff);
6179 screen_stop_highlight();
6180 }
6181 else
6182 screen_attr = 0; /* highlighting has stopped */
6183 }
6184#ifdef FEAT_MBYTE
6185 if (enc_dbcs != 0)
6186 {
6187 /* Check if overwriting a double-byte with a single-byte or
6188 * the other way around requires another character to be
6189 * redrawn. For UTF-8 this isn't needed, because comparing
6190 * ScreenLinesUC[] is sufficient. */
6191 if (char_cells == 1
6192 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006193 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 {
6195 /* Writing a single-cell character over a double-cell
6196 * character: need to redraw the next cell. */
6197 ScreenLines[off_to + 1] = 0;
6198 redraw_next = TRUE;
6199 }
6200 else if (char_cells == 2
6201 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006202 && (*mb_off2cells)(off_to, max_off_to) == 1
6203 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 {
6205 /* Writing the second half of a double-cell character over
6206 * a double-cell character: need to redraw the second
6207 * cell. */
6208 ScreenLines[off_to + 2] = 0;
6209 redraw_next = TRUE;
6210 }
6211
6212 if (enc_dbcs == DBCS_JPNU)
6213 ScreenLines2[off_to] = ScreenLines2[off_from];
6214 }
6215 /* When writing a single-width character over a double-width
6216 * character and at the end of the redrawn text, need to clear out
6217 * the right halve of the old character.
6218 * Also required when writing the right halve of a double-width
6219 * char over the left halve of an existing one. */
6220 if (has_mbyte && col + char_cells == endcol
6221 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006222 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006224 && (*mb_off2cells)(off_to, max_off_to) == 1
6225 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 clear_next = TRUE;
6227#endif
6228
6229 ScreenLines[off_to] = ScreenLines[off_from];
6230#ifdef FEAT_MBYTE
6231 if (enc_utf8)
6232 {
6233 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6234 if (ScreenLinesUC[off_from] != 0)
6235 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006236 int i;
6237
6238 for (i = 0; i < Screen_mco; ++i)
6239 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 }
6241 }
6242 if (char_cells == 2)
6243 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6244#endif
6245
6246#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006247 /* The bold trick makes a single column of pixels appear in the
6248 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 * character should be redrawn too. This happens for our own GUI
6250 * and for some xterms. */
6251 if (
6252# ifdef FEAT_GUI
6253 gui.in_use
6254# endif
6255# if defined(FEAT_GUI) && defined(UNIX)
6256 ||
6257# endif
6258# ifdef UNIX
6259 term_is_xterm
6260# endif
6261 )
6262 {
6263 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006264 if (hl > HL_ALL)
6265 hl = syn_attr2attr(hl);
6266 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 redraw_next = TRUE;
6268 }
6269#endif
6270 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6271#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006272 /* For simplicity set the attributes of second half of a
6273 * double-wide character equal to the first half. */
6274 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006276
6277 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 else
6280#endif
6281 screen_char(off_to, row, col + coloff);
6282 }
6283 else if ( p_wiv
6284#ifdef FEAT_GUI
6285 && !gui.in_use
6286#endif
6287 && col + coloff > 0)
6288 {
6289 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6290 {
6291 /*
6292 * Don't output stop-highlight when moving the cursor, it will
6293 * stop the highlighting when it should continue.
6294 */
6295 screen_attr = 0;
6296 }
6297 else if (screen_attr != 0)
6298 screen_stop_highlight();
6299 }
6300
6301 off_to += CHAR_CELLS;
6302 off_from += CHAR_CELLS;
6303 col += CHAR_CELLS;
6304 }
6305
6306#ifdef FEAT_MBYTE
6307 if (clear_next)
6308 {
6309 /* Clear the second half of a double-wide character of which the left
6310 * half was overwritten with a single-wide character. */
6311 ScreenLines[off_to] = ' ';
6312 if (enc_utf8)
6313 ScreenLinesUC[off_to] = 0;
6314 screen_char(off_to, row, col + coloff);
6315 }
6316#endif
6317
6318 if (clear_width > 0
6319#ifdef FEAT_RIGHTLEFT
6320 && !rlflag
6321#endif
6322 )
6323 {
6324#ifdef FEAT_GUI
6325 int startCol = col;
6326#endif
6327
6328 /* blank out the rest of the line */
6329 while (col < clear_width && ScreenLines[off_to] == ' '
6330 && ScreenAttrs[off_to] == 0
6331#ifdef FEAT_MBYTE
6332 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6333#endif
6334 )
6335 {
6336 ++off_to;
6337 ++col;
6338 }
6339 if (col < clear_width)
6340 {
6341#ifdef FEAT_GUI
6342 /*
6343 * In the GUI, clearing the rest of the line may leave pixels
6344 * behind if the first character cleared was bold. Some bold
6345 * fonts spill over the left. In this case we redraw the previous
6346 * character too. If we didn't skip any blanks above, then we
6347 * only redraw if the character wasn't already redrawn anyway.
6348 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006349 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 {
6351 hl = ScreenAttrs[off_to];
6352 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006353 {
6354 int prev_cells = 1;
6355# ifdef FEAT_MBYTE
6356 if (enc_utf8)
6357 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6358 * that its width is 2. */
6359 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6360 else if (enc_dbcs != 0)
6361 {
6362 /* find previous character by counting from first
6363 * column and get its width. */
6364 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006365 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006366
6367 while (off < off_to)
6368 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006369 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006370 off += prev_cells;
6371 }
6372 }
6373
6374 if (enc_dbcs != 0 && prev_cells > 1)
6375 screen_char_2(off_to - prev_cells, row,
6376 col + coloff - prev_cells);
6377 else
6378# endif
6379 screen_char(off_to - prev_cells, row,
6380 col + coloff - prev_cells);
6381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 }
6383#endif
6384 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6385 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006386#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 off_to += clear_width - col;
6388 col = clear_width;
6389#endif
6390 }
6391 }
6392
6393 if (clear_width > 0)
6394 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006395#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 /* For a window that's left of another, draw the separator char. */
6397 if (col + coloff < Columns)
6398 {
6399 int c;
6400
6401 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006402 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006404 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6405 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406# endif
6407 || ScreenAttrs[off_to] != hl)
6408 {
6409 ScreenLines[off_to] = c;
6410 ScreenAttrs[off_to] = hl;
6411# ifdef FEAT_MBYTE
6412 if (enc_utf8)
6413 {
6414 if (c >= 0x80)
6415 {
6416 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006417 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419 else
6420 ScreenLinesUC[off_to] = 0;
6421 }
6422# endif
6423 screen_char(off_to, row, col + coloff);
6424 }
6425 }
6426 else
6427#endif
6428 LineWraps[row] = FALSE;
6429 }
6430}
6431
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006432#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006434 * Mirror text "str" for right-left displaying.
6435 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006437 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006438rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
6440 char_u *p1, *p2;
6441 int t;
6442
6443 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6444 {
6445 t = *p1;
6446 *p1 = *p2;
6447 *p2 = t;
6448 }
6449}
6450#endif
6451
6452#if defined(FEAT_WINDOWS) || defined(PROTO)
6453/*
6454 * mark all status lines for redraw; used after first :cd
6455 */
6456 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006457status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
6459 win_T *wp;
6460
Bram Moolenaar29323592016-07-24 22:04:11 +02006461 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 if (wp->w_status_height)
6463 {
6464 wp->w_redr_status = TRUE;
6465 redraw_later(VALID);
6466 }
6467}
6468
6469/*
6470 * mark all status lines of the current buffer for redraw
6471 */
6472 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006473status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
6475 win_T *wp;
6476
Bram Moolenaar29323592016-07-24 22:04:11 +02006477 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6479 {
6480 wp->w_redr_status = TRUE;
6481 redraw_later(VALID);
6482 }
6483}
6484
6485/*
6486 * Redraw all status lines that need to be redrawn.
6487 */
6488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006489redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
6491 win_T *wp;
6492
Bram Moolenaar29323592016-07-24 22:04:11 +02006493 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 if (wp->w_redr_status)
6495 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006496 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006497 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498}
6499#endif
6500
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006501#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502/*
6503 * Redraw all status lines at the bottom of frame "frp".
6504 */
6505 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006506win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 if (frp->fr_layout == FR_LEAF)
6509 frp->fr_win->w_redr_status = TRUE;
6510 else if (frp->fr_layout == FR_ROW)
6511 {
6512 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6513 win_redraw_last_status(frp);
6514 }
6515 else /* frp->fr_layout == FR_COL */
6516 {
6517 frp = frp->fr_child;
6518 while (frp->fr_next != NULL)
6519 frp = frp->fr_next;
6520 win_redraw_last_status(frp);
6521 }
6522}
6523#endif
6524
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006525#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526/*
6527 * Draw the verticap separator right of window "wp" starting with line "row".
6528 */
6529 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006530draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
6532 int hl;
6533 int c;
6534
6535 if (wp->w_vsep_width)
6536 {
6537 /* draw the vertical separator right of this window */
6538 c = fillchar_vsep(&hl);
6539 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6540 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6541 c, ' ', hl);
6542 }
6543}
6544#endif
6545
6546#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006547static int status_match_len(expand_T *xp, char_u *s);
6548static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
6550/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006551 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 */
6553 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006554status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 int len = 0;
6557
6558#ifdef FEAT_MENU
6559 int emenu = (xp->xp_context == EXPAND_MENUS
6560 || xp->xp_context == EXPAND_MENUNAMES);
6561
6562 /* Check for menu separators - replace with '|'. */
6563 if (emenu && menu_is_separator(s))
6564 return 1;
6565#endif
6566
6567 while (*s != NUL)
6568 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006569 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006570 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006571 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 }
6573
6574 return len;
6575}
6576
6577/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006578 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006579 * These are backslashes used for escaping. Do show backslashes in help tags.
6580 */
6581 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006582skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006583{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006584 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006585#ifdef FEAT_MENU
6586 || ((xp->xp_context == EXPAND_MENUS
6587 || xp->xp_context == EXPAND_MENUNAMES)
6588 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6589#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006590 )
6591 {
6592#ifndef BACKSLASH_IN_FILENAME
6593 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6594 return 2;
6595#endif
6596 return 1;
6597 }
6598 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006599}
6600
6601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 * Show wildchar matches in the status line.
6603 * Show at least the "match" item.
6604 * We start at item 'first_match' in the list and show all matches that fit.
6605 *
6606 * If inversion is possible we use it. Else '=' characters are used.
6607 */
6608 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006609win_redr_status_matches(
6610 expand_T *xp,
6611 int num_matches,
6612 char_u **matches, /* list of matches */
6613 int match,
6614 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6617 int row;
6618 char_u *buf;
6619 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006620 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 int fillchar;
6622 int attr;
6623 int i;
6624 int highlight = TRUE;
6625 char_u *selstart = NULL;
6626 int selstart_col = 0;
6627 char_u *selend = NULL;
6628 static int first_match = 0;
6629 int add_left = FALSE;
6630 char_u *s;
6631#ifdef FEAT_MENU
6632 int emenu;
6633#endif
6634#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6635 int l;
6636#endif
6637
6638 if (matches == NULL) /* interrupted completion? */
6639 return;
6640
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006641#ifdef FEAT_MBYTE
6642 if (has_mbyte)
6643 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6644 else
6645#endif
6646 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 if (buf == NULL)
6648 return;
6649
6650 if (match == -1) /* don't show match but original text */
6651 {
6652 match = 0;
6653 highlight = FALSE;
6654 }
6655 /* count 1 for the ending ">" */
6656 clen = status_match_len(xp, L_MATCH(match)) + 3;
6657 if (match == 0)
6658 first_match = 0;
6659 else if (match < first_match)
6660 {
6661 /* jumping left, as far as we can go */
6662 first_match = match;
6663 add_left = TRUE;
6664 }
6665 else
6666 {
6667 /* check if match fits on the screen */
6668 for (i = first_match; i < match; ++i)
6669 clen += status_match_len(xp, L_MATCH(i)) + 2;
6670 if (first_match > 0)
6671 clen += 2;
6672 /* jumping right, put match at the left */
6673 if ((long)clen > Columns)
6674 {
6675 first_match = match;
6676 /* if showing the last match, we can add some on the left */
6677 clen = 2;
6678 for (i = match; i < num_matches; ++i)
6679 {
6680 clen += status_match_len(xp, L_MATCH(i)) + 2;
6681 if ((long)clen >= Columns)
6682 break;
6683 }
6684 if (i == num_matches)
6685 add_left = TRUE;
6686 }
6687 }
6688 if (add_left)
6689 while (first_match > 0)
6690 {
6691 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6692 if ((long)clen >= Columns)
6693 break;
6694 --first_match;
6695 }
6696
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006697 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
6699 if (first_match == 0)
6700 {
6701 *buf = NUL;
6702 len = 0;
6703 }
6704 else
6705 {
6706 STRCPY(buf, "< ");
6707 len = 2;
6708 }
6709 clen = len;
6710
6711 i = first_match;
6712 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6713 {
6714 if (i == match)
6715 {
6716 selstart = buf + len;
6717 selstart_col = clen;
6718 }
6719
6720 s = L_MATCH(i);
6721 /* Check for menu separators - replace with '|' */
6722#ifdef FEAT_MENU
6723 emenu = (xp->xp_context == EXPAND_MENUS
6724 || xp->xp_context == EXPAND_MENUNAMES);
6725 if (emenu && menu_is_separator(s))
6726 {
6727 STRCPY(buf + len, transchar('|'));
6728 l = (int)STRLEN(buf + len);
6729 len += l;
6730 clen += l;
6731 }
6732 else
6733#endif
6734 for ( ; *s != NUL; ++s)
6735 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006736 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 clen += ptr2cells(s);
6738#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006739 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {
6741 STRNCPY(buf + len, s, l);
6742 s += l - 1;
6743 len += l;
6744 }
6745 else
6746#endif
6747 {
6748 STRCPY(buf + len, transchar_byte(*s));
6749 len += (int)STRLEN(buf + len);
6750 }
6751 }
6752 if (i == match)
6753 selend = buf + len;
6754
6755 *(buf + len++) = ' ';
6756 *(buf + len++) = ' ';
6757 clen += 2;
6758 if (++i == num_matches)
6759 break;
6760 }
6761
6762 if (i != num_matches)
6763 {
6764 *(buf + len++) = '>';
6765 ++clen;
6766 }
6767
6768 buf[len] = NUL;
6769
6770 row = cmdline_row - 1;
6771 if (row >= 0)
6772 {
6773 if (wild_menu_showing == 0)
6774 {
6775 if (msg_scrolled > 0)
6776 {
6777 /* Put the wildmenu just above the command line. If there is
6778 * no room, scroll the screen one line up. */
6779 if (cmdline_row == Rows - 1)
6780 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006781 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 ++msg_scrolled;
6783 }
6784 else
6785 {
6786 ++cmdline_row;
6787 ++row;
6788 }
6789 wild_menu_showing = WM_SCROLLED;
6790 }
6791 else
6792 {
6793 /* Create status line if needed by setting 'laststatus' to 2.
6794 * Set 'winminheight' to zero to avoid that the window is
6795 * resized. */
6796 if (lastwin->w_status_height == 0)
6797 {
6798 save_p_ls = p_ls;
6799 save_p_wmh = p_wmh;
6800 p_ls = 2;
6801 p_wmh = 0;
6802 last_status(FALSE);
6803 }
6804 wild_menu_showing = WM_SHOWN;
6805 }
6806 }
6807
6808 screen_puts(buf, row, 0, attr);
6809 if (selstart != NULL && highlight)
6810 {
6811 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006812 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 }
6814
6815 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6816 }
6817
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006818#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 win_redraw_last_status(topframe);
6820#else
6821 lastwin->w_redr_status = TRUE;
6822#endif
6823 vim_free(buf);
6824}
6825#endif
6826
6827#if defined(FEAT_WINDOWS) || defined(PROTO)
6828/*
6829 * Redraw the status line of window wp.
6830 *
6831 * If inversion is possible we use it. Else '=' characters are used.
6832 */
6833 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006834win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835{
6836 int row;
6837 char_u *p;
6838 int len;
6839 int fillchar;
6840 int attr;
6841 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006842 static int busy = FALSE;
6843
6844 /* It's possible to get here recursively when 'statusline' (indirectly)
6845 * invokes ":redrawstatus". Simply ignore the call then. */
6846 if (busy)
6847 return;
6848 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
6850 wp->w_redr_status = FALSE;
6851 if (wp->w_status_height == 0)
6852 {
6853 /* no status line, can only be last window */
6854 redraw_cmdline = TRUE;
6855 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006856 else if (!redrawing()
6857#ifdef FEAT_INS_EXPAND
6858 /* don't update status line when popup menu is visible and may be
6859 * drawn over it */
6860 || pum_visible()
6861#endif
6862 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {
6864 /* Don't redraw right now, do it later. */
6865 wp->w_redr_status = TRUE;
6866 }
6867#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006868 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 {
6870 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006871 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 }
6873#endif
6874 else
6875 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006876 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006878 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 p = NameBuff;
6880 len = (int)STRLEN(p);
6881
Bram Moolenaard85f2712017-07-28 21:51:57 +02006882 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883#ifdef FEAT_QUICKFIX
6884 || wp->w_p_pvw
6885#endif
6886 || bufIsChanged(wp->w_buffer)
6887 || wp->w_buffer->b_p_ro)
6888 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006889 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006891 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 len += (int)STRLEN(p + len);
6893 }
6894#ifdef FEAT_QUICKFIX
6895 if (wp->w_p_pvw)
6896 {
6897 STRCPY(p + len, _("[Preview]"));
6898 len += (int)STRLEN(p + len);
6899 }
6900#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006901 if (bufIsChanged(wp->w_buffer)
6902#ifdef FEAT_TERMINAL
6903 && !bt_terminal(wp->w_buffer)
6904#endif
6905 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {
6907 STRCPY(p + len, "[+]");
6908 len += 3;
6909 }
6910 if (wp->w_buffer->b_p_ro)
6911 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006912 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006913 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 }
6915
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6917 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6918 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6919 if (this_ru_col <= 1)
6920 {
6921 p = (char_u *)"<"; /* No room for file name! */
6922 len = 1;
6923 }
6924 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925#ifdef FEAT_MBYTE
6926 if (has_mbyte)
6927 {
6928 int clen = 0, i;
6929
6930 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006931 clen = mb_string2cells(p, -1);
6932
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 /* Find first character that will fit.
6934 * Going from start to end is much faster for DBCS. */
6935 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006936 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 clen -= (*mb_ptr2cells)(p + i);
6938 len = clen;
6939 if (i > 0)
6940 {
6941 p = p + i - 1;
6942 *p = '<';
6943 ++len;
6944 }
6945
6946 }
6947 else
6948#endif
6949 if (len > this_ru_col - 1)
6950 {
6951 p += len - (this_ru_col - 1);
6952 *p = '<';
6953 len = this_ru_col - 1;
6954 }
6955
6956 row = W_WINROW(wp) + wp->w_height;
6957 screen_puts(p, row, W_WINCOL(wp), attr);
6958 screen_fill(row, row + 1, len + W_WINCOL(wp),
6959 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6960
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006961 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6963 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6964 - 1 + W_WINCOL(wp)), attr);
6965
6966#ifdef FEAT_CMDL_INFO
6967 win_redr_ruler(wp, TRUE);
6968#endif
6969 }
6970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 /*
6972 * May need to draw the character below the vertical separator.
6973 */
6974 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6975 {
6976 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006977 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 else
6979 fillchar = fillchar_vsep(&attr);
6980 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6981 attr);
6982 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006983 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984}
6985
Bram Moolenaar238a5642006-02-21 22:12:05 +00006986#ifdef FEAT_STL_OPT
6987/*
6988 * Redraw the status line according to 'statusline' and take care of any
6989 * errors encountered.
6990 */
6991 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006992redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006993{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006994 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006995 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006996
6997 /* When called recursively return. This can happen when the statusline
6998 * contains an expression that triggers a redraw. */
6999 if (entered)
7000 return;
7001 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007002
Bram Moolenaara742e082016-04-05 21:10:38 +02007003 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007004 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007005 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007006 {
7007 /* When there is an error disable the statusline, otherwise the
7008 * display is messed up with errors and a redraw triggers the problem
7009 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007010 set_string_option_direct((char_u *)"statusline", -1,
7011 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007012 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007013 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007014 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007015 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007016}
7017#endif
7018
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019/*
7020 * Return TRUE if the status line of window "wp" is connected to the status
7021 * line of the window right of it. If not, then it's a vertical separator.
7022 * Only call if (wp->w_vsep_width != 0).
7023 */
7024 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007025stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026{
7027 frame_T *fr;
7028
7029 fr = wp->w_frame;
7030 while (fr->fr_parent != NULL)
7031 {
7032 if (fr->fr_parent->fr_layout == FR_COL)
7033 {
7034 if (fr->fr_next != NULL)
7035 break;
7036 }
7037 else
7038 {
7039 if (fr->fr_next != NULL)
7040 return TRUE;
7041 }
7042 fr = fr->fr_parent;
7043 }
7044 return FALSE;
7045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
7047#endif /* FEAT_WINDOWS */
7048
7049#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
7050/*
7051 * Get the value to show for the language mappings, active 'keymap'.
7052 */
7053 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007054get_keymap_str(
7055 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007056 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007057 char_u *buf, /* buffer for the result */
7058 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059{
7060 char_u *p;
7061
7062 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7063 return FALSE;
7064
7065 {
7066#ifdef FEAT_EVAL
7067 buf_T *old_curbuf = curbuf;
7068 win_T *old_curwin = curwin;
7069 char_u *s;
7070
7071 curbuf = wp->w_buffer;
7072 curwin = wp;
7073 STRCPY(buf, "b:keymap_name"); /* must be writable */
7074 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007075 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 --emsg_skip;
7077 curbuf = old_curbuf;
7078 curwin = old_curwin;
7079 if (p == NULL || *p == NUL)
7080#endif
7081 {
7082#ifdef FEAT_KEYMAP
7083 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7084 p = wp->w_buffer->b_p_keymap;
7085 else
7086#endif
7087 p = (char_u *)"lang";
7088 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007089 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 buf[0] = NUL;
7091#ifdef FEAT_EVAL
7092 vim_free(s);
7093#endif
7094 }
7095 return buf[0] != NUL;
7096}
7097#endif
7098
7099#if defined(FEAT_STL_OPT) || defined(PROTO)
7100/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007101 * Redraw the status line or ruler of window "wp".
7102 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 */
7104 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007105win_redr_custom(
7106 win_T *wp,
7107 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007109 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 int attr;
7111 int curattr;
7112 int row;
7113 int col = 0;
7114 int maxwidth;
7115 int width;
7116 int n;
7117 int len;
7118 int fillchar;
7119 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007120 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007122 struct stl_hlrec hltab[STL_MAX_ITEM];
7123 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007124 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007125 win_T *ewp;
7126 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127
Bram Moolenaar1d633412013-12-11 15:52:01 +01007128 /* There is a tiny chance that this gets called recursively: When
7129 * redrawing a status line triggers redrawing the ruler or tabline.
7130 * Avoid trouble by not allowing recursion. */
7131 if (entered)
7132 return;
7133 entered = TRUE;
7134
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007136 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007138 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007139 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007140 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007141 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007142 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007143 maxwidth = Columns;
7144# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007145 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007146# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007148 else
7149 {
7150 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007151 fillchar = fillchar_status(&attr, wp);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007152 maxwidth = W_WIDTH(wp);
7153
7154 if (draw_ruler)
7155 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007156 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007157 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007158 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007159 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007160 if (*++stl == '-')
7161 stl++;
7162 if (atoi((char *)stl))
7163 while (VIM_ISDIGIT(*stl))
7164 stl++;
7165 if (*stl++ != '(')
7166 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007167 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007168#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007169 col = ru_col - (Columns - W_WIDTH(wp));
7170 if (col < (W_WIDTH(wp) + 1) / 2)
7171 col = (W_WIDTH(wp) + 1) / 2;
7172#else
7173 col = ru_col;
7174 if (col > (Columns + 1) / 2)
7175 col = (Columns + 1) / 2;
7176#endif
7177 maxwidth = W_WIDTH(wp) - col;
7178#ifdef FEAT_WINDOWS
7179 if (!wp->w_status_height)
7180#endif
7181 {
7182 row = Rows - 1;
7183 --maxwidth; /* writing in last column may cause scrolling */
7184 fillchar = ' ';
7185 attr = 0;
7186 }
7187
7188# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007189 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007190# endif
7191 }
7192 else
7193 {
7194 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007195 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007196 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007197 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007198# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007199 use_sandbox = was_set_insecurely((char_u *)"statusline",
7200 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007201# endif
7202 }
7203
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007204#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007205 col += W_WINCOL(wp);
7206#endif
7207 }
7208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007210 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211
Bram Moolenaar61452852011-02-01 18:01:11 +01007212 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7213 * the cursor away and back. */
7214 ewp = wp == NULL ? curwin : wp;
7215 p_crb_save = ewp->w_p_crb;
7216 ewp->w_p_crb = FALSE;
7217
Bram Moolenaar362f3562009-11-03 16:20:34 +00007218 /* Make a copy, because the statusline may include a function call that
7219 * might change the option value and free the memory. */
7220 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007221 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007222 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007223 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007224 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007225 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007227 /* Make all characters printable. */
7228 p = transstr(buf);
7229 if (p != NULL)
7230 {
7231 vim_strncpy(buf, p, sizeof(buf) - 1);
7232 vim_free(p);
7233 }
7234
7235 /* fill up with "fillchar" */
7236 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007237 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 {
7239#ifdef FEAT_MBYTE
7240 len += (*mb_char2bytes)(fillchar, buf + len);
7241#else
7242 buf[len++] = fillchar;
7243#endif
7244 ++width;
7245 }
7246 buf[len] = NUL;
7247
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007248 /*
7249 * Draw each snippet with the specified highlighting.
7250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 curattr = attr;
7252 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007253 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007255 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 screen_puts_len(p, len, row, col, curattr);
7257 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007258 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007260 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007262 else if (hltab[n].userhl < 0)
7263 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264#ifdef FEAT_WINDOWS
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007265# ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007266 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7267 && wp->w_status_height != 0)
7268 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007269 else if (wp != NULL && bt_terminal(wp->w_buffer)
7270 && wp->w_status_height != 0)
7271 curattr = highlight_stlterm[hltab[n].userhl - 1];
7272# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007273 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007274 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275#endif
7276 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007277 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 }
7279 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007280
7281 if (wp == NULL)
7282 {
7283 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7284 col = 0;
7285 len = 0;
7286 p = buf;
7287 fillchar = 0;
7288 for (n = 0; tabtab[n].start != NULL; n++)
7289 {
7290 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7291 while (col < len)
7292 TabPageIdxs[col++] = fillchar;
7293 p = tabtab[n].start;
7294 fillchar = tabtab[n].userhl;
7295 }
7296 while (col < Columns)
7297 TabPageIdxs[col++] = fillchar;
7298 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007299
7300theend:
7301 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302}
7303
7304#endif /* FEAT_STL_OPT */
7305
7306/*
7307 * Output a single character directly to the screen and update ScreenLines.
7308 */
7309 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007310screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 char_u buf[MB_MAXBYTES + 1];
7313
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007314#ifdef FEAT_MBYTE
7315 if (has_mbyte)
7316 buf[(*mb_char2bytes)(c, buf)] = NUL;
7317 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007319 {
7320 buf[0] = c;
7321 buf[1] = NUL;
7322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 screen_puts(buf, row, col, attr);
7324}
7325
7326/*
7327 * Get a single character directly from ScreenLines into "bytes[]".
7328 * Also return its attribute in *attrp;
7329 */
7330 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007331screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
7333 unsigned off;
7334
7335 /* safety check */
7336 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7337 {
7338 off = LineOffset[row] + col;
7339 *attrp = ScreenAttrs[off];
7340 bytes[0] = ScreenLines[off];
7341 bytes[1] = NUL;
7342
7343#ifdef FEAT_MBYTE
7344 if (enc_utf8 && ScreenLinesUC[off] != 0)
7345 bytes[utfc_char2bytes(off, bytes)] = NUL;
7346 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7347 {
7348 bytes[0] = ScreenLines[off];
7349 bytes[1] = ScreenLines2[off];
7350 bytes[2] = NUL;
7351 }
7352 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7353 {
7354 bytes[1] = ScreenLines[off + 1];
7355 bytes[2] = NUL;
7356 }
7357#endif
7358 }
7359}
7360
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007361#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007362static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007363
7364/*
7365 * Return TRUE if composing characters for screen posn "off" differs from
7366 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007367 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007368 */
7369 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007370screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007371{
7372 int i;
7373
7374 for (i = 0; i < Screen_mco; ++i)
7375 {
7376 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7377 return TRUE;
7378 if (u8cc[i] == 0)
7379 break;
7380 }
7381 return FALSE;
7382}
7383#endif
7384
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385/*
7386 * Put string '*text' on the screen at position 'row' and 'col', with
7387 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7388 * Note: only outputs within one row, message is truncated at screen boundary!
7389 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7390 */
7391 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007392screen_puts(
7393 char_u *text,
7394 int row,
7395 int col,
7396 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
7398 screen_puts_len(text, -1, row, col, attr);
7399}
7400
7401/*
7402 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7403 * a NUL.
7404 */
7405 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007406screen_puts_len(
7407 char_u *text,
7408 int textlen,
7409 int row,
7410 int col,
7411 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
7413 unsigned off;
7414 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007415 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 int c;
7417#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007418 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 int mbyte_blen = 1;
7420 int mbyte_cells = 1;
7421 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007422 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 int clear_next_cell = FALSE;
7424# ifdef FEAT_ARABIC
7425 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007426 int pc, nc, nc1;
7427 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428# endif
7429#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007430#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7431 int force_redraw_this;
7432 int force_redraw_next = FALSE;
7433#endif
7434 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435
7436 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7437 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007438 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439
Bram Moolenaarc236c162008-07-13 17:41:49 +00007440#ifdef FEAT_MBYTE
7441 /* When drawing over the right halve of a double-wide char clear out the
7442 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007443 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007444# ifdef FEAT_GUI
7445 && !gui.in_use
7446# endif
7447 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007448 {
7449 ScreenLines[off - 1] = ' ';
7450 ScreenAttrs[off - 1] = 0;
7451 if (enc_utf8)
7452 {
7453 ScreenLinesUC[off - 1] = 0;
7454 ScreenLinesC[0][off - 1] = 0;
7455 }
7456 /* redraw the previous cell, make it empty */
7457 screen_char(off - 1, row, col - 1);
7458 /* force the cell at "col" to be redrawn */
7459 force_redraw_next = TRUE;
7460 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007461#endif
7462
Bram Moolenaar367329b2007-08-30 11:53:22 +00007463#ifdef FEAT_MBYTE
7464 max_off = LineOffset[row] + screen_Columns;
7465#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007466 while (col < screen_Columns
7467 && (len < 0 || (int)(ptr - text) < len)
7468 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 {
7470 c = *ptr;
7471#ifdef FEAT_MBYTE
7472 /* check if this is the first byte of a multibyte */
7473 if (has_mbyte)
7474 {
7475 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007476 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007478 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7480 mbyte_cells = 1;
7481 else if (enc_dbcs != 0)
7482 mbyte_cells = mbyte_blen;
7483 else /* enc_utf8 */
7484 {
7485 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007486 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 (int)((text + len) - ptr));
7488 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007489 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007491# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 /* Non-BMP character: display as ? or fullwidth ?. */
7493 if (u8c >= 0x10000)
7494 {
7495 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7496 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007497 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500# ifdef FEAT_ARABIC
7501 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7502 {
7503 /* Do Arabic shaping. */
7504 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7505 {
7506 /* Past end of string to be displayed. */
7507 nc = NUL;
7508 nc1 = NUL;
7509 }
7510 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007511 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007512 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7513 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007514 nc1 = pcc[0];
7515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 pc = prev_c;
7517 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007518 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 }
7520 else
7521 prev_c = u8c;
7522# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007523 if (col + mbyte_cells > screen_Columns)
7524 {
7525 /* Only 1 cell left, but character requires 2 cells:
7526 * display a '>' in the last column to avoid wrapping. */
7527 c = '>';
7528 mbyte_cells = 1;
7529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 }
7531 }
7532#endif
7533
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007534#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7535 force_redraw_this = force_redraw_next;
7536 force_redraw_next = FALSE;
7537#endif
7538
7539 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540#ifdef FEAT_MBYTE
7541 || (mbyte_cells == 2
7542 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7543 || (enc_dbcs == DBCS_JPNU
7544 && c == 0x8e
7545 && ScreenLines2[off] != ptr[1])
7546 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007547 && (ScreenLinesUC[off] !=
7548 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7549 || (ScreenLinesUC[off] != 0
7550 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551#endif
7552 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007553 || exmode_active;
7554
7555 if (need_redraw
7556#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7557 || force_redraw_this
7558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 )
7560 {
7561#if defined(FEAT_GUI) || defined(UNIX)
7562 /* The bold trick makes a single row of pixels appear in the next
7563 * character. When a bold character is removed, the next
7564 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007565 * and for some xterms. */
7566 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567# ifdef FEAT_GUI
7568 gui.in_use
7569# endif
7570# if defined(FEAT_GUI) && defined(UNIX)
7571 ||
7572# endif
7573# ifdef UNIX
7574 term_is_xterm
7575# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007576 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007578 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007580 if (n > HL_ALL)
7581 n = syn_attr2attr(n);
7582 if (n & HL_BOLD)
7583 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 }
7585#endif
7586#ifdef FEAT_MBYTE
7587 /* When at the end of the text and overwriting a two-cell
7588 * character with a one-cell character, need to clear the next
7589 * cell. Also when overwriting the left halve of a two-cell char
7590 * with the right halve of a two-cell char. Do this only once
7591 * (mb_off2cells() may return 2 on the right halve). */
7592 if (clear_next_cell)
7593 clear_next_cell = FALSE;
7594 else if (has_mbyte
7595 && (len < 0 ? ptr[mbyte_blen] == NUL
7596 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007597 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007599 && (*mb_off2cells)(off, max_off) == 1
7600 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 clear_next_cell = TRUE;
7602
7603 /* Make sure we never leave a second byte of a double-byte behind,
7604 * it confuses mb_off2cells(). */
7605 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007606 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007608 && (*mb_off2cells)(off, max_off) == 1
7609 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 ScreenLines[off + mbyte_blen] = 0;
7611#endif
7612 ScreenLines[off] = c;
7613 ScreenAttrs[off] = attr;
7614#ifdef FEAT_MBYTE
7615 if (enc_utf8)
7616 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007617 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 ScreenLinesUC[off] = 0;
7619 else
7620 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007621 int i;
7622
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007624 for (i = 0; i < Screen_mco; ++i)
7625 {
7626 ScreenLinesC[i][off] = u8cc[i];
7627 if (u8cc[i] == 0)
7628 break;
7629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 }
7631 if (mbyte_cells == 2)
7632 {
7633 ScreenLines[off + 1] = 0;
7634 ScreenAttrs[off + 1] = attr;
7635 }
7636 screen_char(off, row, col);
7637 }
7638 else if (mbyte_cells == 2)
7639 {
7640 ScreenLines[off + 1] = ptr[1];
7641 ScreenAttrs[off + 1] = attr;
7642 screen_char_2(off, row, col);
7643 }
7644 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7645 {
7646 ScreenLines2[off] = ptr[1];
7647 screen_char(off, row, col);
7648 }
7649 else
7650#endif
7651 screen_char(off, row, col);
7652 }
7653#ifdef FEAT_MBYTE
7654 if (has_mbyte)
7655 {
7656 off += mbyte_cells;
7657 col += mbyte_cells;
7658 ptr += mbyte_blen;
7659 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007660 {
7661 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007663 len = -1;
7664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 }
7666 else
7667#endif
7668 {
7669 ++off;
7670 ++col;
7671 ++ptr;
7672 }
7673 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007674
7675#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7676 /* If we detected the next character needs to be redrawn, but the text
7677 * doesn't extend up to there, update the character here. */
7678 if (force_redraw_next && col < screen_Columns)
7679 {
7680# ifdef FEAT_MBYTE
7681 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7682 screen_char_2(off, row, col);
7683 else
7684# endif
7685 screen_char(off, row, col);
7686 }
7687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688}
7689
7690#ifdef FEAT_SEARCH_EXTRA
7691/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007692 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 */
7694 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007695start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
7697 if (p_hls && !no_hlsearch)
7698 {
7699 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007700 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007701# ifdef FEAT_RELTIME
7702 /* Set the time limit to 'redrawtime'. */
7703 profile_setlimit(p_rdt, &search_hl.tm);
7704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 }
7706}
7707
7708/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007709 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 */
7711 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007712end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
7714 if (search_hl.rm.regprog != NULL)
7715 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007716 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 search_hl.rm.regprog = NULL;
7718 }
7719}
7720
7721/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007722 * Init for calling prepare_search_hl().
7723 */
7724 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007725init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007726{
7727 matchitem_T *cur;
7728
7729 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7730 * match */
7731 cur = wp->w_match_head;
7732 while (cur != NULL)
7733 {
7734 cur->hl.rm = cur->match;
7735 if (cur->hlg_id == 0)
7736 cur->hl.attr = 0;
7737 else
7738 cur->hl.attr = syn_id2attr(cur->hlg_id);
7739 cur->hl.buf = wp->w_buffer;
7740 cur->hl.lnum = 0;
7741 cur->hl.first_lnum = 0;
7742# ifdef FEAT_RELTIME
7743 /* Set the time limit to 'redrawtime'. */
7744 profile_setlimit(p_rdt, &(cur->hl.tm));
7745# endif
7746 cur = cur->next;
7747 }
7748 search_hl.buf = wp->w_buffer;
7749 search_hl.lnum = 0;
7750 search_hl.first_lnum = 0;
7751 /* time limit is set at the toplevel, for all windows */
7752}
7753
7754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 * Advance to the match in window "wp" line "lnum" or past it.
7756 */
7757 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007758prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007760 matchitem_T *cur; /* points to the match list */
7761 match_T *shl; /* points to search_hl or a match */
7762 int shl_flag; /* flag to indicate whether search_hl
7763 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007764 int pos_inprogress; /* marks that position match search is
7765 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 int n;
7767
7768 /*
7769 * When using a multi-line pattern, start searching at the top
7770 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007771 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007773 cur = wp->w_match_head;
7774 shl_flag = FALSE;
7775 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007777 if (shl_flag == FALSE)
7778 {
7779 shl = &search_hl;
7780 shl_flag = TRUE;
7781 }
7782 else
7783 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 if (shl->rm.regprog != NULL
7785 && shl->lnum == 0
7786 && re_multiline(shl->rm.regprog))
7787 {
7788 if (shl->first_lnum == 0)
7789 {
7790# ifdef FEAT_FOLDING
7791 for (shl->first_lnum = lnum;
7792 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7793 if (hasFoldingWin(wp, shl->first_lnum - 1,
7794 NULL, NULL, TRUE, NULL))
7795 break;
7796# else
7797 shl->first_lnum = wp->w_topline;
7798# endif
7799 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007800 if (cur != NULL)
7801 cur->pos.cur = 0;
7802 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007804 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7805 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007807 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7808 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007809 pos_inprogress = cur == NULL || cur->pos.cur == 0
7810 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 if (shl->lnum != 0)
7812 {
7813 shl->first_lnum = shl->lnum
7814 + shl->rm.endpos[0].lnum
7815 - shl->rm.startpos[0].lnum;
7816 n = shl->rm.endpos[0].col;
7817 }
7818 else
7819 {
7820 ++shl->first_lnum;
7821 n = 0;
7822 }
7823 }
7824 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007825 if (shl != &search_hl && cur != NULL)
7826 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 }
7828}
7829
7830/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007831 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 * Uses shl->buf.
7833 * Sets shl->lnum and shl->rm contents.
7834 * Note: Assumes a previous match is always before "lnum", unless
7835 * shl->lnum is zero.
7836 * Careful: Any pointers for buffer lines will become invalid.
7837 */
7838 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007839next_search_hl(
7840 win_T *win,
7841 match_T *shl, /* points to search_hl or a match */
7842 linenr_T lnum,
7843 colnr_T mincol, /* minimal column for a match */
7844 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846 linenr_T l;
7847 colnr_T matchcol;
7848 long nmatched;
7849
7850 if (shl->lnum != 0)
7851 {
7852 /* Check for three situations:
7853 * 1. If the "lnum" is below a previous match, start a new search.
7854 * 2. If the previous match includes "mincol", use it.
7855 * 3. Continue after the previous match.
7856 */
7857 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7858 if (lnum > l)
7859 shl->lnum = 0;
7860 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7861 return;
7862 }
7863
7864 /*
7865 * Repeat searching for a match until one is found that includes "mincol"
7866 * or none is found in this line.
7867 */
7868 called_emsg = FALSE;
7869 for (;;)
7870 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007871#ifdef FEAT_RELTIME
7872 /* Stop searching after passing the time limit. */
7873 if (profile_passed_limit(&(shl->tm)))
7874 {
7875 shl->lnum = 0; /* no match found in time */
7876 break;
7877 }
7878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 /* Three situations:
7880 * 1. No useful previous match: search from start of line.
7881 * 2. Not Vi compatible or empty match: continue at next character.
7882 * Break the loop if this is beyond the end of the line.
7883 * 3. Vi compatible searching: continue at end of previous match.
7884 */
7885 if (shl->lnum == 0)
7886 matchcol = 0;
7887 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7888 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007889 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007891 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007892
7893 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007894 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007895 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007897 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 shl->lnum = 0;
7899 break;
7900 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007901#ifdef FEAT_MBYTE
7902 if (has_mbyte)
7903 matchcol += mb_ptr2len(ml);
7904 else
7905#endif
7906 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 }
7908 else
7909 matchcol = shl->rm.endpos[0].col;
7910
7911 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007912 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007914 /* Remember whether shl->rm is using a copy of the regprog in
7915 * cur->match. */
7916 int regprog_is_copy = (shl != &search_hl && cur != NULL
7917 && shl == &cur->hl
7918 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007919 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007920
Bram Moolenaarb3414592014-06-17 17:48:32 +02007921 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7922 matchcol,
7923#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007924 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007925#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007926 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007927#endif
7928 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007929 /* Copy the regprog, in case it got freed and recompiled. */
7930 if (regprog_is_copy)
7931 cur->match.regprog = cur->hl.rm.regprog;
7932
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007933 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007934 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007935 /* Error while handling regexp: stop using this regexp. */
7936 if (shl == &search_hl)
7937 {
7938 /* don't free regprog in the match list, it's a copy */
7939 vim_regfree(shl->rm.regprog);
7940 SET_NO_HLSEARCH(TRUE);
7941 }
7942 shl->rm.regprog = NULL;
7943 shl->lnum = 0;
7944 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7945 message */
7946 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007947 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007948 }
7949 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007950 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007951 else
7952 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 if (nmatched == 0)
7954 {
7955 shl->lnum = 0; /* no match found */
7956 break;
7957 }
7958 if (shl->rm.startpos[0].lnum > 0
7959 || shl->rm.startpos[0].col >= mincol
7960 || nmatched > 1
7961 || shl->rm.endpos[0].col > mincol)
7962 {
7963 shl->lnum += shl->rm.startpos[0].lnum;
7964 break; /* useful match found */
7965 }
7966 }
7967}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968
Bram Moolenaar85077472016-10-16 14:35:48 +02007969/*
7970 * If there is a match fill "shl" and return one.
7971 * Return zero otherwise.
7972 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007973 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007974next_search_hl_pos(
7975 match_T *shl, /* points to a match */
7976 linenr_T lnum,
7977 posmatch_T *posmatch, /* match positions */
7978 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007979{
7980 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007981 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007982
Bram Moolenaarb3414592014-06-17 17:48:32 +02007983 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7984 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007985 llpos_T *pos = &posmatch->pos[i];
7986
7987 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007988 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007989 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007991 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007992 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007993 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007994 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007995 /* if this match comes before the one at "found" then swap
7996 * them */
7997 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007998 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007999 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008000
Bram Moolenaar85077472016-10-16 14:35:48 +02008001 *pos = posmatch->pos[found];
8002 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008003 }
8004 }
8005 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008006 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008007 }
8008 }
8009 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008010 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008011 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008012 colnr_T start = posmatch->pos[found].col == 0
8013 ? 0 : posmatch->pos[found].col - 1;
8014 colnr_T end = posmatch->pos[found].col == 0
8015 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008016
Bram Moolenaar85077472016-10-16 14:35:48 +02008017 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008018 shl->rm.startpos[0].lnum = 0;
8019 shl->rm.startpos[0].col = start;
8020 shl->rm.endpos[0].lnum = 0;
8021 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008022 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008023 posmatch->cur = found + 1;
8024 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008025 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008026 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008027}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008028#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008029
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008031screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032{
8033 attrentry_T *aep = NULL;
8034
8035 screen_attr = attr;
8036 if (full_screen
8037#ifdef WIN3264
8038 && termcap_active
8039#endif
8040 )
8041 {
8042#ifdef FEAT_GUI
8043 if (gui.in_use)
8044 {
8045 char buf[20];
8046
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008047 /* The GUI handles this internally. */
8048 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 OUT_STR(buf);
8050 }
8051 else
8052#endif
8053 {
8054 if (attr > HL_ALL) /* special HL attr. */
8055 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008056 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 aep = syn_cterm_attr2entry(attr);
8058 else
8059 aep = syn_term_attr2entry(attr);
8060 if (aep == NULL) /* did ":syntax clear" */
8061 attr = 0;
8062 else
8063 attr = aep->ae_attr;
8064 }
8065 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8066 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008067 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008068#ifdef FEAT_TERMGUICOLORS
8069 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008070 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008071#endif
8072 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008073#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008074 )
8075#endif
8076 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008077 /* If the Normal FG color has BOLD attribute and the new HL
8078 * has a FG color defined, clear BOLD. */
8079 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8081 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008082 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8083 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 out_str(T_US);
8085 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8086 out_str(T_CZH);
8087 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8088 out_str(T_MR);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008089 if ((attr & HL_STRIKETHROUGH) && T_STS != NULL) /* strike */
8090 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091
8092 /*
8093 * Output the color or start string after bold etc., in case the
8094 * bold etc. override the color setting.
8095 */
8096 if (aep != NULL)
8097 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008098#ifdef FEAT_TERMGUICOLORS
8099 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008101 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008102 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008103 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008104 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 }
8106 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008109 if (t_colors > 1)
8110 {
8111 if (aep->ae_u.cterm.fg_color)
8112 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8113 if (aep->ae_u.cterm.bg_color)
8114 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8115 }
8116 else
8117 {
8118 if (aep->ae_u.term.start != NULL)
8119 out_str(aep->ae_u.term.start);
8120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 }
8122 }
8123 }
8124 }
8125}
8126
8127 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008128screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129{
8130 int do_ME = FALSE; /* output T_ME code */
8131
8132 if (screen_attr != 0
8133#ifdef WIN3264
8134 && termcap_active
8135#endif
8136 )
8137 {
8138#ifdef FEAT_GUI
8139 if (gui.in_use)
8140 {
8141 char buf[20];
8142
8143 /* use internal GUI code */
8144 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8145 OUT_STR(buf);
8146 }
8147 else
8148#endif
8149 {
8150 if (screen_attr > HL_ALL) /* special HL attr. */
8151 {
8152 attrentry_T *aep;
8153
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008154 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {
8156 /*
8157 * Assume that t_me restores the original colors!
8158 */
8159 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008160 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008161#ifdef FEAT_TERMGUICOLORS
8162 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008163 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8164 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008165#endif
8166 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008167#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008168 )
8169#endif
8170 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 do_ME = TRUE;
8172 }
8173 else
8174 {
8175 aep = syn_term_attr2entry(screen_attr);
8176 if (aep != NULL && aep->ae_u.term.stop != NULL)
8177 {
8178 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8179 do_ME = TRUE;
8180 else
8181 out_str(aep->ae_u.term.stop);
8182 }
8183 }
8184 if (aep == NULL) /* did ":syntax clear" */
8185 screen_attr = 0;
8186 else
8187 screen_attr = aep->ae_attr;
8188 }
8189
8190 /*
8191 * Often all ending-codes are equal to T_ME. Avoid outputting the
8192 * same sequence several times.
8193 */
8194 if (screen_attr & HL_STANDOUT)
8195 {
8196 if (STRCMP(T_SE, T_ME) == 0)
8197 do_ME = TRUE;
8198 else
8199 out_str(T_SE);
8200 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008201 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {
8203 if (STRCMP(T_UE, T_ME) == 0)
8204 do_ME = TRUE;
8205 else
8206 out_str(T_UE);
8207 }
8208 if (screen_attr & HL_ITALIC)
8209 {
8210 if (STRCMP(T_CZR, T_ME) == 0)
8211 do_ME = TRUE;
8212 else
8213 out_str(T_CZR);
8214 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008215 if (screen_attr & HL_STRIKETHROUGH)
8216 {
8217 if (STRCMP(T_STE, T_ME) == 0)
8218 do_ME = TRUE;
8219 else
8220 out_str(T_STE);
8221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8223 out_str(T_ME);
8224
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008225#ifdef FEAT_TERMGUICOLORS
8226 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008228 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008229 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008230 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008231 term_bg_rgb_color(cterm_normal_bg_gui_color);
8232 }
8233 else
8234#endif
8235 {
8236 if (t_colors > 1)
8237 {
8238 /* set Normal cterm colors */
8239 if (cterm_normal_fg_color != 0)
8240 term_fg_color(cterm_normal_fg_color - 1);
8241 if (cterm_normal_bg_color != 0)
8242 term_bg_color(cterm_normal_bg_color - 1);
8243 if (cterm_normal_fg_bold)
8244 out_str(T_MD);
8245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 }
8247 }
8248 }
8249 screen_attr = 0;
8250}
8251
8252/*
8253 * Reset the colors for a cterm. Used when leaving Vim.
8254 * The machine specific code may override this again.
8255 */
8256 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008257reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008259 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 {
8261 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008262#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008263 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8264 || cterm_normal_bg_gui_color != INVALCOLOR)
8265 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008266#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {
8270 out_str(T_OP);
8271 screen_attr = -1;
8272 }
8273 if (cterm_normal_fg_bold)
8274 {
8275 out_str(T_ME);
8276 screen_attr = -1;
8277 }
8278 }
8279}
8280
8281/*
8282 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8283 * using the attributes from ScreenAttrs["off"].
8284 */
8285 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008286screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287{
8288 int attr;
8289
8290 /* Check for illegal values, just in case (could happen just after
8291 * resizing). */
8292 if (row >= screen_Rows || col >= screen_Columns)
8293 return;
8294
Bram Moolenaar494838a2015-02-10 19:20:37 +01008295 /* Outputting a character in the last cell on the screen may scroll the
8296 * screen up. Only do it when the "xn" termcap property is set, otherwise
8297 * mark the character invalid (update it when scrolled up). */
8298 if (*T_XN == NUL
8299 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300#ifdef FEAT_RIGHTLEFT
8301 /* account for first command-line character in rightleft mode */
8302 && !cmdmsg_rl
8303#endif
8304 )
8305 {
8306 ScreenAttrs[off] = (sattr_T)-1;
8307 return;
8308 }
8309
8310 /*
8311 * Stop highlighting first, so it's easier to move the cursor.
8312 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008313#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 if (screen_char_attr != 0)
8315 attr = screen_char_attr;
8316 else
8317#endif
8318 attr = ScreenAttrs[off];
8319 if (screen_attr != attr)
8320 screen_stop_highlight();
8321
8322 windgoto(row, col);
8323
8324 if (screen_attr != attr)
8325 screen_start_highlight(attr);
8326
8327#ifdef FEAT_MBYTE
8328 if (enc_utf8 && ScreenLinesUC[off] != 0)
8329 {
8330 char_u buf[MB_MAXBYTES + 1];
8331
8332 /* Convert UTF-8 character to bytes and write it. */
8333
8334 buf[utfc_char2bytes(off, buf)] = NUL;
8335
8336 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008337 if (utf_ambiguous_width(ScreenLinesUC[off]))
8338 screen_cur_col = 9999;
8339 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 ++screen_cur_col;
8341 }
8342 else
8343#endif
8344 {
8345#ifdef FEAT_MBYTE
8346 out_flush_check();
8347#endif
8348 out_char(ScreenLines[off]);
8349#ifdef FEAT_MBYTE
8350 /* double-byte character in single-width cell */
8351 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8352 out_char(ScreenLines2[off]);
8353#endif
8354 }
8355
8356 screen_cur_col++;
8357}
8358
8359#ifdef FEAT_MBYTE
8360
8361/*
8362 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8363 * on the screen at position 'row' and 'col'.
8364 * The attributes of the first byte is used for all. This is required to
8365 * output the two bytes of a double-byte character with nothing in between.
8366 */
8367 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008368screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369{
8370 /* Check for illegal values (could be wrong when screen was resized). */
8371 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8372 return;
8373
8374 /* Outputting the last character on the screen may scrollup the screen.
8375 * Don't to it! Mark the character invalid (update it when scrolled up) */
8376 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8377 {
8378 ScreenAttrs[off] = (sattr_T)-1;
8379 return;
8380 }
8381
8382 /* Output the first byte normally (positions the cursor), then write the
8383 * second byte directly. */
8384 screen_char(off, row, col);
8385 out_char(ScreenLines[off + 1]);
8386 ++screen_cur_col;
8387}
8388#endif
8389
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008390#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391/*
8392 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8393 * This uses the contents of ScreenLines[] and doesn't change it.
8394 */
8395 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008396screen_draw_rectangle(
8397 int row,
8398 int col,
8399 int height,
8400 int width,
8401 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403 int r, c;
8404 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008405#ifdef FEAT_MBYTE
8406 int max_off;
8407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008409 /* Can't use ScreenLines unless initialized */
8410 if (ScreenLines == NULL)
8411 return;
8412
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 if (invert)
8414 screen_char_attr = HL_INVERSE;
8415 for (r = row; r < row + height; ++r)
8416 {
8417 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008418#ifdef FEAT_MBYTE
8419 max_off = off + screen_Columns;
8420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 for (c = col; c < col + width; ++c)
8422 {
8423#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008424 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {
8426 screen_char_2(off + c, r, c);
8427 ++c;
8428 }
8429 else
8430#endif
8431 {
8432 screen_char(off + c, r, c);
8433#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008434 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 ++c;
8436#endif
8437 }
8438 }
8439 }
8440 screen_char_attr = 0;
8441}
8442#endif
8443
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008444#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445/*
8446 * Redraw the characters for a vertically split window.
8447 */
8448 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008449redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
8451 int col;
8452 int width;
8453
8454# ifdef FEAT_CLIPBOARD
8455 clip_may_clear_selection(row, end - 1);
8456# endif
8457
8458 if (wp == NULL)
8459 {
8460 col = 0;
8461 width = Columns;
8462 }
8463 else
8464 {
8465 col = wp->w_wincol;
8466 width = wp->w_width;
8467 }
8468 screen_draw_rectangle(row, col, end - row, width, FALSE);
8469}
8470#endif
8471
8472/*
8473 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8474 * with character 'c1' in first column followed by 'c2' in the other columns.
8475 * Use attributes 'attr'.
8476 */
8477 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008478screen_fill(
8479 int start_row,
8480 int end_row,
8481 int start_col,
8482 int end_col,
8483 int c1,
8484 int c2,
8485 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
8487 int row;
8488 int col;
8489 int off;
8490 int end_off;
8491 int did_delete;
8492 int c;
8493 int norm_term;
8494#if defined(FEAT_GUI) || defined(UNIX)
8495 int force_next = FALSE;
8496#endif
8497
8498 if (end_row > screen_Rows) /* safety check */
8499 end_row = screen_Rows;
8500 if (end_col > screen_Columns) /* safety check */
8501 end_col = screen_Columns;
8502 if (ScreenLines == NULL
8503 || start_row >= end_row
8504 || start_col >= end_col) /* nothing to do */
8505 return;
8506
8507 /* it's a "normal" terminal when not in a GUI or cterm */
8508 norm_term = (
8509#ifdef FEAT_GUI
8510 !gui.in_use &&
8511#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008512 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 for (row = start_row; row < end_row; ++row)
8514 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008515#ifdef FEAT_MBYTE
8516 if (has_mbyte
8517# ifdef FEAT_GUI
8518 && !gui.in_use
8519# endif
8520 )
8521 {
8522 /* When drawing over the right halve of a double-wide char clear
8523 * out the left halve. When drawing over the left halve of a
8524 * double wide-char clear out the right halve. Only needed in a
8525 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008526 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008527 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008528 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008529 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008530 }
8531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 /*
8533 * Try to use delete-line termcap code, when no attributes or in a
8534 * "normal" terminal, where a bold/italic space is just a
8535 * space.
8536 */
8537 did_delete = FALSE;
8538 if (c2 == ' '
8539 && end_col == Columns
8540 && can_clear(T_CE)
8541 && (attr == 0
8542 || (norm_term
8543 && attr <= HL_ALL
8544 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8545 {
8546 /*
8547 * check if we really need to clear something
8548 */
8549 col = start_col;
8550 if (c1 != ' ') /* don't clear first char */
8551 ++col;
8552
8553 off = LineOffset[row] + col;
8554 end_off = LineOffset[row] + end_col;
8555
8556 /* skip blanks (used often, keep it fast!) */
8557#ifdef FEAT_MBYTE
8558 if (enc_utf8)
8559 while (off < end_off && ScreenLines[off] == ' '
8560 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8561 ++off;
8562 else
8563#endif
8564 while (off < end_off && ScreenLines[off] == ' '
8565 && ScreenAttrs[off] == 0)
8566 ++off;
8567 if (off < end_off) /* something to be cleared */
8568 {
8569 col = off - LineOffset[row];
8570 screen_stop_highlight();
8571 term_windgoto(row, col);/* clear rest of this screen line */
8572 out_str(T_CE);
8573 screen_start(); /* don't know where cursor is now */
8574 col = end_col - col;
8575 while (col--) /* clear chars in ScreenLines */
8576 {
8577 ScreenLines[off] = ' ';
8578#ifdef FEAT_MBYTE
8579 if (enc_utf8)
8580 ScreenLinesUC[off] = 0;
8581#endif
8582 ScreenAttrs[off] = 0;
8583 ++off;
8584 }
8585 }
8586 did_delete = TRUE; /* the chars are cleared now */
8587 }
8588
8589 off = LineOffset[row] + start_col;
8590 c = c1;
8591 for (col = start_col; col < end_col; ++col)
8592 {
8593 if (ScreenLines[off] != c
8594#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008595 || (enc_utf8 && (int)ScreenLinesUC[off]
8596 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597#endif
8598 || ScreenAttrs[off] != attr
8599#if defined(FEAT_GUI) || defined(UNIX)
8600 || force_next
8601#endif
8602 )
8603 {
8604#if defined(FEAT_GUI) || defined(UNIX)
8605 /* The bold trick may make a single row of pixels appear in
8606 * the next character. When a bold character is removed, the
8607 * next character should be redrawn too. This happens for our
8608 * own GUI and for some xterms. */
8609 if (
8610# ifdef FEAT_GUI
8611 gui.in_use
8612# endif
8613# if defined(FEAT_GUI) && defined(UNIX)
8614 ||
8615# endif
8616# ifdef UNIX
8617 term_is_xterm
8618# endif
8619 )
8620 {
8621 if (ScreenLines[off] != ' '
8622 && (ScreenAttrs[off] > HL_ALL
8623 || ScreenAttrs[off] & HL_BOLD))
8624 force_next = TRUE;
8625 else
8626 force_next = FALSE;
8627 }
8628#endif
8629 ScreenLines[off] = c;
8630#ifdef FEAT_MBYTE
8631 if (enc_utf8)
8632 {
8633 if (c >= 0x80)
8634 {
8635 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008636 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 }
8638 else
8639 ScreenLinesUC[off] = 0;
8640 }
8641#endif
8642 ScreenAttrs[off] = attr;
8643 if (!did_delete || c != ' ')
8644 screen_char(off, row, col);
8645 }
8646 ++off;
8647 if (col == start_col)
8648 {
8649 if (did_delete)
8650 break;
8651 c = c2;
8652 }
8653 }
8654 if (end_col == Columns)
8655 LineWraps[row] = FALSE;
8656 if (row == Rows - 1) /* overwritten the command line */
8657 {
8658 redraw_cmdline = TRUE;
8659 if (c1 == ' ' && c2 == ' ')
8660 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008661 if (start_col == 0)
8662 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 }
8664 }
8665}
8666
8667/*
8668 * Check if there should be a delay. Used before clearing or redrawing the
8669 * screen or the command line.
8670 */
8671 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008672check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673{
8674 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8675 && !did_wait_return
8676 && emsg_silent == 0)
8677 {
8678 out_flush();
8679 ui_delay(1000L, TRUE);
8680 emsg_on_display = FALSE;
8681 if (check_msg_scroll)
8682 msg_scroll = FALSE;
8683 }
8684}
8685
8686/*
8687 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008688 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 * Returns TRUE if there is a valid screen to write to.
8690 * Returns FALSE when starting up and screen not initialized yet.
8691 */
8692 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008693screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008695 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 return (ScreenLines != NULL);
8697}
8698
8699/*
8700 * Resize the shell to Rows and Columns.
8701 * Allocate ScreenLines[] and associated items.
8702 *
8703 * There may be some time between setting Rows and Columns and (re)allocating
8704 * ScreenLines[]. This happens when starting up and when (manually) changing
8705 * the shell size. Always use screen_Rows and screen_Columns to access items
8706 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8707 * final size of the shell is needed.
8708 */
8709 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008710screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711{
8712 int new_row, old_row;
8713#ifdef FEAT_GUI
8714 int old_Rows;
8715#endif
8716 win_T *wp;
8717 int outofmem = FALSE;
8718 int len;
8719 schar_T *new_ScreenLines;
8720#ifdef FEAT_MBYTE
8721 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008722 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008724 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725#endif
8726 sattr_T *new_ScreenAttrs;
8727 unsigned *new_LineOffset;
8728 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008729#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008730 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008731 tabpage_T *tp;
8732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008734 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008735#ifdef FEAT_AUTOCMD
8736 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008738retry:
8739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 /*
8741 * Allocation of the screen buffers is done only when the size changes and
8742 * when Rows and Columns have been set and we have started doing full
8743 * screen stuff.
8744 */
8745 if ((ScreenLines != NULL
8746 && Rows == screen_Rows
8747 && Columns == screen_Columns
8748#ifdef FEAT_MBYTE
8749 && enc_utf8 == (ScreenLinesUC != NULL)
8750 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008751 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752#endif
8753 )
8754 || Rows == 0
8755 || Columns == 0
8756 || (!full_screen && ScreenLines == NULL))
8757 return;
8758
8759 /*
8760 * It's possible that we produce an out-of-memory message below, which
8761 * will cause this function to be called again. To break the loop, just
8762 * return here.
8763 */
8764 if (entered)
8765 return;
8766 entered = TRUE;
8767
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008768 /*
8769 * Note that the window sizes are updated before reallocating the arrays,
8770 * thus we must not redraw here!
8771 */
8772 ++RedrawingDisabled;
8773
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 win_new_shellsize(); /* fit the windows in the new sized shell */
8775
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 comp_col(); /* recompute columns for shown command and ruler */
8777
8778 /*
8779 * We're changing the size of the screen.
8780 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8781 * - Move lines from the old arrays into the new arrays, clear extra
8782 * lines (unless the screen is going to be cleared).
8783 * - Free the old arrays.
8784 *
8785 * If anything fails, make ScreenLines NULL, so we don't do anything!
8786 * Continuing with the old ScreenLines may result in a crash, because the
8787 * size is wrong.
8788 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008789 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008791#ifdef FEAT_AUTOCMD
8792 if (aucmd_win != NULL)
8793 win_free_lsize(aucmd_win);
8794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795
8796 new_ScreenLines = (schar_T *)lalloc((long_u)(
8797 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8798#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008799 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 if (enc_utf8)
8801 {
8802 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8803 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008804 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008805 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8807 }
8808 if (enc_dbcs == DBCS_JPNU)
8809 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8810 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8811#endif
8812 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8813 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8814 new_LineOffset = (unsigned *)lalloc((long_u)(
8815 Rows * sizeof(unsigned)), FALSE);
8816 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008817#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008818 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008821 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 {
8823 if (win_alloc_lines(wp) == FAIL)
8824 {
8825 outofmem = TRUE;
8826#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008827 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828#endif
8829 }
8830 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008831#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008832 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8833 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008834 outofmem = TRUE;
8835#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008836#ifdef FEAT_WINDOWS
8837give_up:
8838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008840#ifdef FEAT_MBYTE
8841 for (i = 0; i < p_mco; ++i)
8842 if (new_ScreenLinesC[i] == NULL)
8843 break;
8844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 if (new_ScreenLines == NULL
8846#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008847 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8849#endif
8850 || new_ScreenAttrs == NULL
8851 || new_LineOffset == NULL
8852 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008853#ifdef FEAT_WINDOWS
8854 || new_TabPageIdxs == NULL
8855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 || outofmem)
8857 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008858 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008859 {
8860 /* guess the size */
8861 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8862
8863 /* Remember we did this to avoid getting outofmem messages over
8864 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008865 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 vim_free(new_ScreenLines);
8868 new_ScreenLines = NULL;
8869#ifdef FEAT_MBYTE
8870 vim_free(new_ScreenLinesUC);
8871 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008872 for (i = 0; i < p_mco; ++i)
8873 {
8874 vim_free(new_ScreenLinesC[i]);
8875 new_ScreenLinesC[i] = NULL;
8876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 vim_free(new_ScreenLines2);
8878 new_ScreenLines2 = NULL;
8879#endif
8880 vim_free(new_ScreenAttrs);
8881 new_ScreenAttrs = NULL;
8882 vim_free(new_LineOffset);
8883 new_LineOffset = NULL;
8884 vim_free(new_LineWraps);
8885 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008886#ifdef FEAT_WINDOWS
8887 vim_free(new_TabPageIdxs);
8888 new_TabPageIdxs = NULL;
8889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 }
8891 else
8892 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008893 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008894
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 for (new_row = 0; new_row < Rows; ++new_row)
8896 {
8897 new_LineOffset[new_row] = new_row * Columns;
8898 new_LineWraps[new_row] = FALSE;
8899
8900 /*
8901 * If the screen is not going to be cleared, copy as much as
8902 * possible from the old screen to the new one and clear the rest
8903 * (used when resizing the window at the "--more--" prompt or when
8904 * executing an external command, for the GUI).
8905 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008906 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 {
8908 (void)vim_memset(new_ScreenLines + new_row * Columns,
8909 ' ', (size_t)Columns * sizeof(schar_T));
8910#ifdef FEAT_MBYTE
8911 if (enc_utf8)
8912 {
8913 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8914 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008915 for (i = 0; i < p_mco; ++i)
8916 (void)vim_memset(new_ScreenLinesC[i]
8917 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 0, (size_t)Columns * sizeof(u8char_T));
8919 }
8920 if (enc_dbcs == DBCS_JPNU)
8921 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8922 0, (size_t)Columns * sizeof(schar_T));
8923#endif
8924 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8925 0, (size_t)Columns * sizeof(sattr_T));
8926 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008927 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 {
8929 if (screen_Columns < Columns)
8930 len = screen_Columns;
8931 else
8932 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008933#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008934 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008935 * may be invalid now. Also when p_mco changes. */
8936 if (!(enc_utf8 && ScreenLinesUC == NULL)
8937 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008938#endif
8939 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8940 ScreenLines + LineOffset[old_row],
8941 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008943 if (enc_utf8 && ScreenLinesUC != NULL
8944 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 {
8946 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8947 ScreenLinesUC + LineOffset[old_row],
8948 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008949 for (i = 0; i < p_mco; ++i)
8950 mch_memmove(new_ScreenLinesC[i]
8951 + new_LineOffset[new_row],
8952 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 (size_t)len * sizeof(u8char_T));
8954 }
8955 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8956 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8957 ScreenLines2 + LineOffset[old_row],
8958 (size_t)len * sizeof(schar_T));
8959#endif
8960 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8961 ScreenAttrs + LineOffset[old_row],
8962 (size_t)len * sizeof(sattr_T));
8963 }
8964 }
8965 }
8966 /* Use the last line of the screen for the current line. */
8967 current_ScreenLine = new_ScreenLines + Rows * Columns;
8968 }
8969
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008970 free_screenlines();
8971
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 ScreenLines = new_ScreenLines;
8973#ifdef FEAT_MBYTE
8974 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008975 for (i = 0; i < p_mco; ++i)
8976 ScreenLinesC[i] = new_ScreenLinesC[i];
8977 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 ScreenLines2 = new_ScreenLines2;
8979#endif
8980 ScreenAttrs = new_ScreenAttrs;
8981 LineOffset = new_LineOffset;
8982 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008983#ifdef FEAT_WINDOWS
8984 TabPageIdxs = new_TabPageIdxs;
8985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986
8987 /* It's important that screen_Rows and screen_Columns reflect the actual
8988 * size of ScreenLines[]. Set them before calling anything. */
8989#ifdef FEAT_GUI
8990 old_Rows = screen_Rows;
8991#endif
8992 screen_Rows = Rows;
8993 screen_Columns = Columns;
8994
8995 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008996 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 screenclear2();
8998
8999#ifdef FEAT_GUI
9000 else if (gui.in_use
9001 && !gui.starting
9002 && ScreenLines != NULL
9003 && old_Rows != Rows)
9004 {
9005 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9006 /*
9007 * Adjust the position of the cursor, for when executing an external
9008 * command.
9009 */
9010 if (msg_row >= Rows) /* Rows got smaller */
9011 msg_row = Rows - 1; /* put cursor at last row */
9012 else if (Rows > old_Rows) /* Rows got bigger */
9013 msg_row += Rows - old_Rows; /* put cursor in same place */
9014 if (msg_col >= Columns) /* Columns got smaller */
9015 msg_col = Columns - 1; /* put cursor at last column */
9016 }
9017#endif
9018
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009020 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009021
9022#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009023 /*
9024 * Do not apply autocommands more than 3 times to avoid an endless loop
9025 * in case applying autocommands always changes Rows or Columns.
9026 */
9027 if (starting == 0 && ++retry_count <= 3)
9028 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009029 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009030 /* In rare cases, autocommands may have altered Rows or Columns,
9031 * jump back to check if we need to allocate the screen again. */
9032 goto retry;
9033 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035}
9036
9037 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009038free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009039{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009040#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009041 int i;
9042
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009043 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009044 for (i = 0; i < Screen_mco; ++i)
9045 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009046 vim_free(ScreenLines2);
9047#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009048 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009049 vim_free(ScreenAttrs);
9050 vim_free(LineOffset);
9051 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009052#ifdef FEAT_WINDOWS
9053 vim_free(TabPageIdxs);
9054#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009055}
9056
9057 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009058screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
9060 check_for_delay(FALSE);
9061 screenalloc(FALSE); /* allocate screen buffers if size changed */
9062 screenclear2(); /* clear the screen */
9063}
9064
9065 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009066screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
9068 int i;
9069
9070 if (starting == NO_SCREEN || ScreenLines == NULL
9071#ifdef FEAT_GUI
9072 || (gui.in_use && gui.starting)
9073#endif
9074 )
9075 return;
9076
9077#ifdef FEAT_GUI
9078 if (!gui.in_use)
9079#endif
9080 screen_attr = -1; /* force setting the Normal colors */
9081 screen_stop_highlight(); /* don't want highlighting here */
9082
9083#ifdef FEAT_CLIPBOARD
9084 /* disable selection without redrawing it */
9085 clip_scroll_selection(9999);
9086#endif
9087
9088 /* blank out ScreenLines */
9089 for (i = 0; i < Rows; ++i)
9090 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009091 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 LineWraps[i] = FALSE;
9093 }
9094
9095 if (can_clear(T_CL))
9096 {
9097 out_str(T_CL); /* clear the display */
9098 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009099 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 }
9101 else
9102 {
9103 /* can't clear the screen, mark all chars with invalid attributes */
9104 for (i = 0; i < Rows; ++i)
9105 lineinvalid(LineOffset[i], (int)Columns);
9106 clear_cmdline = TRUE;
9107 }
9108
9109 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9110
9111 win_rest_invalid(firstwin);
9112 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009113#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009114 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 if (must_redraw == CLEAR) /* no need to clear again */
9117 must_redraw = NOT_VALID;
9118 compute_cmdrow();
9119 msg_row = cmdline_row; /* put cursor on last line for messages */
9120 msg_col = 0;
9121 screen_start(); /* don't know where cursor is now */
9122 msg_scrolled = 0; /* can't scroll back */
9123 msg_didany = FALSE;
9124 msg_didout = FALSE;
9125}
9126
9127/*
9128 * Clear one line in ScreenLines.
9129 */
9130 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009131lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
9133 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9134#ifdef FEAT_MBYTE
9135 if (enc_utf8)
9136 (void)vim_memset(ScreenLinesUC + off, 0,
9137 (size_t)width * sizeof(u8char_T));
9138#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009139 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140}
9141
9142/*
9143 * Mark one line in ScreenLines invalid by setting the attributes to an
9144 * invalid value.
9145 */
9146 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009147lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148{
9149 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9150}
9151
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009152#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153/*
9154 * Copy part of a Screenline for vertically split window "wp".
9155 */
9156 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009157linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158{
9159 unsigned off_to = LineOffset[to] + wp->w_wincol;
9160 unsigned off_from = LineOffset[from] + wp->w_wincol;
9161
9162 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9163 wp->w_width * sizeof(schar_T));
9164# ifdef FEAT_MBYTE
9165 if (enc_utf8)
9166 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009167 int i;
9168
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9170 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009171 for (i = 0; i < p_mco; ++i)
9172 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9173 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 }
9175 if (enc_dbcs == DBCS_JPNU)
9176 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9177 wp->w_width * sizeof(schar_T));
9178# endif
9179 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9180 wp->w_width * sizeof(sattr_T));
9181}
9182#endif
9183
9184/*
9185 * Return TRUE if clearing with term string "p" would work.
9186 * It can't work when the string is empty or it won't set the right background.
9187 */
9188 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009189can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190{
9191 return (*p != NUL && (t_colors <= 1
9192#ifdef FEAT_GUI
9193 || gui.in_use
9194#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009195#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009196 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009197 || (!p_tgc && cterm_normal_bg_color == 0)
9198#else
9199 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009200#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009201 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202}
9203
9204/*
9205 * Reset cursor position. Use whenever cursor was moved because of outputting
9206 * something directly to the screen (shell commands) or a terminal control
9207 * code.
9208 */
9209 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009210screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211{
9212 screen_cur_row = screen_cur_col = 9999;
9213}
9214
9215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 * Move the cursor to position "row","col" in the screen.
9217 * This tries to find the most efficient way to move, minimizing the number of
9218 * characters sent to the terminal.
9219 */
9220 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009221windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009223 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 int i;
9225 int plan;
9226 int cost;
9227 int wouldbe_col;
9228 int noinvcurs;
9229 char_u *bs;
9230 int goto_cost;
9231 int attr;
9232
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009233#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9235
9236#define PLAN_LE 1
9237#define PLAN_CR 2
9238#define PLAN_NL 3
9239#define PLAN_WRITE 4
9240 /* Can't use ScreenLines unless initialized */
9241 if (ScreenLines == NULL)
9242 return;
9243
9244 if (col != screen_cur_col || row != screen_cur_row)
9245 {
9246 /* Check for valid position. */
9247 if (row < 0) /* window without text lines? */
9248 row = 0;
9249 if (row >= screen_Rows)
9250 row = screen_Rows - 1;
9251 if (col >= screen_Columns)
9252 col = screen_Columns - 1;
9253
9254 /* check if no cursor movement is allowed in highlight mode */
9255 if (screen_attr && *T_MS == NUL)
9256 noinvcurs = HIGHL_COST;
9257 else
9258 noinvcurs = 0;
9259 goto_cost = GOTO_COST + noinvcurs;
9260
9261 /*
9262 * Plan how to do the positioning:
9263 * 1. Use CR to move it to column 0, same row.
9264 * 2. Use T_LE to move it a few columns to the left.
9265 * 3. Use NL to move a few lines down, column 0.
9266 * 4. Move a few columns to the right with T_ND or by writing chars.
9267 *
9268 * Don't do this if the cursor went beyond the last column, the cursor
9269 * position is unknown then (some terminals wrap, some don't )
9270 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009271 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 * characters to move the cursor to the right.
9273 */
9274 if (row >= screen_cur_row && screen_cur_col < Columns)
9275 {
9276 /*
9277 * If the cursor is in the same row, bigger col, we can use CR
9278 * or T_LE.
9279 */
9280 bs = NULL; /* init for GCC */
9281 attr = screen_attr;
9282 if (row == screen_cur_row && col < screen_cur_col)
9283 {
9284 /* "le" is preferred over "bc", because "bc" is obsolete */
9285 if (*T_LE)
9286 bs = T_LE; /* "cursor left" */
9287 else
9288 bs = T_BC; /* "backspace character (old) */
9289 if (*bs)
9290 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9291 else
9292 cost = 999;
9293 if (col + 1 < cost) /* using CR is less characters */
9294 {
9295 plan = PLAN_CR;
9296 wouldbe_col = 0;
9297 cost = 1; /* CR is just one character */
9298 }
9299 else
9300 {
9301 plan = PLAN_LE;
9302 wouldbe_col = col;
9303 }
9304 if (noinvcurs) /* will stop highlighting */
9305 {
9306 cost += noinvcurs;
9307 attr = 0;
9308 }
9309 }
9310
9311 /*
9312 * If the cursor is above where we want to be, we can use CR LF.
9313 */
9314 else if (row > screen_cur_row)
9315 {
9316 plan = PLAN_NL;
9317 wouldbe_col = 0;
9318 cost = (row - screen_cur_row) * 2; /* CR LF */
9319 if (noinvcurs) /* will stop highlighting */
9320 {
9321 cost += noinvcurs;
9322 attr = 0;
9323 }
9324 }
9325
9326 /*
9327 * If the cursor is in the same row, smaller col, just use write.
9328 */
9329 else
9330 {
9331 plan = PLAN_WRITE;
9332 wouldbe_col = screen_cur_col;
9333 cost = 0;
9334 }
9335
9336 /*
9337 * Check if any characters that need to be written have the
9338 * correct attributes. Also avoid UTF-8 characters.
9339 */
9340 i = col - wouldbe_col;
9341 if (i > 0)
9342 cost += i;
9343 if (cost < goto_cost && i > 0)
9344 {
9345 /*
9346 * Check if the attributes are correct without additionally
9347 * stopping highlighting.
9348 */
9349 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9350 while (i && *p++ == attr)
9351 --i;
9352 if (i != 0)
9353 {
9354 /*
9355 * Try if it works when highlighting is stopped here.
9356 */
9357 if (*--p == 0)
9358 {
9359 cost += noinvcurs;
9360 while (i && *p++ == 0)
9361 --i;
9362 }
9363 if (i != 0)
9364 cost = 999; /* different attributes, don't do it */
9365 }
9366#ifdef FEAT_MBYTE
9367 if (enc_utf8)
9368 {
9369 /* Don't use an UTF-8 char for positioning, it's slow. */
9370 for (i = wouldbe_col; i < col; ++i)
9371 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9372 {
9373 cost = 999;
9374 break;
9375 }
9376 }
9377#endif
9378 }
9379
9380 /*
9381 * We can do it without term_windgoto()!
9382 */
9383 if (cost < goto_cost)
9384 {
9385 if (plan == PLAN_LE)
9386 {
9387 if (noinvcurs)
9388 screen_stop_highlight();
9389 while (screen_cur_col > col)
9390 {
9391 out_str(bs);
9392 --screen_cur_col;
9393 }
9394 }
9395 else if (plan == PLAN_CR)
9396 {
9397 if (noinvcurs)
9398 screen_stop_highlight();
9399 out_char('\r');
9400 screen_cur_col = 0;
9401 }
9402 else if (plan == PLAN_NL)
9403 {
9404 if (noinvcurs)
9405 screen_stop_highlight();
9406 while (screen_cur_row < row)
9407 {
9408 out_char('\n');
9409 ++screen_cur_row;
9410 }
9411 screen_cur_col = 0;
9412 }
9413
9414 i = col - screen_cur_col;
9415 if (i > 0)
9416 {
9417 /*
9418 * Use cursor-right if it's one character only. Avoids
9419 * removing a line of pixels from the last bold char, when
9420 * using the bold trick in the GUI.
9421 */
9422 if (T_ND[0] != NUL && T_ND[1] == NUL)
9423 {
9424 while (i-- > 0)
9425 out_char(*T_ND);
9426 }
9427 else
9428 {
9429 int off;
9430
9431 off = LineOffset[row] + screen_cur_col;
9432 while (i-- > 0)
9433 {
9434 if (ScreenAttrs[off] != screen_attr)
9435 screen_stop_highlight();
9436#ifdef FEAT_MBYTE
9437 out_flush_check();
9438#endif
9439 out_char(ScreenLines[off]);
9440#ifdef FEAT_MBYTE
9441 if (enc_dbcs == DBCS_JPNU
9442 && ScreenLines[off] == 0x8e)
9443 out_char(ScreenLines2[off]);
9444#endif
9445 ++off;
9446 }
9447 }
9448 }
9449 }
9450 }
9451 else
9452 cost = 999;
9453
9454 if (cost >= goto_cost)
9455 {
9456 if (noinvcurs)
9457 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009458 if (row == screen_cur_row && (col > screen_cur_col)
9459 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 term_cursor_right(col - screen_cur_col);
9461 else
9462 term_windgoto(row, col);
9463 }
9464 screen_cur_row = row;
9465 screen_cur_col = col;
9466 }
9467}
9468
9469/*
9470 * Set cursor to its position in the current window.
9471 */
9472 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009473setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474{
9475 if (redrawing())
9476 {
9477 validate_cursor();
9478 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9479 W_WINCOL(curwin) + (
9480#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009481 /* With 'rightleft' set and the cursor on a double-wide
9482 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9484# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009485 (has_mbyte
9486 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9487 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488# endif
9489 1)) :
9490#endif
9491 curwin->w_wcol));
9492 }
9493}
9494
9495
9496/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009497 * Insert 'line_count' lines at 'row' in window 'wp'.
9498 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9499 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 * scrolling.
9501 * Returns FAIL if the lines are not inserted, OK for success.
9502 */
9503 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009504win_ins_lines(
9505 win_T *wp,
9506 int row,
9507 int line_count,
9508 int invalid,
9509 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510{
9511 int did_delete;
9512 int nextrow;
9513 int lastrow;
9514 int retval;
9515
9516 if (invalid)
9517 wp->w_lines_valid = 0;
9518
9519 if (wp->w_height < 5)
9520 return FAIL;
9521
9522 if (line_count > wp->w_height - row)
9523 line_count = wp->w_height - row;
9524
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009525 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 if (retval != MAYBE)
9527 return retval;
9528
9529 /*
9530 * If there is a next window or a status line, we first try to delete the
9531 * lines at the bottom to avoid messing what is after the window.
9532 * If this fails and there are following windows, don't do anything to avoid
9533 * messing up those windows, better just redraw.
9534 */
9535 did_delete = FALSE;
9536#ifdef FEAT_WINDOWS
9537 if (wp->w_next != NULL || wp->w_status_height)
9538 {
9539 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009540 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 did_delete = TRUE;
9542 else if (wp->w_next)
9543 return FAIL;
9544 }
9545#endif
9546 /*
9547 * if no lines deleted, blank the lines that will end up below the window
9548 */
9549 if (!did_delete)
9550 {
9551#ifdef FEAT_WINDOWS
9552 wp->w_redr_status = TRUE;
9553#endif
9554 redraw_cmdline = TRUE;
9555 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9556 lastrow = nextrow + line_count;
9557 if (lastrow > Rows)
9558 lastrow = Rows;
9559 screen_fill(nextrow - line_count, lastrow - line_count,
9560 W_WINCOL(wp), (int)W_ENDCOL(wp),
9561 ' ', ' ', 0);
9562 }
9563
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009564 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 == FAIL)
9566 {
9567 /* deletion will have messed up other windows */
9568 if (did_delete)
9569 {
9570#ifdef FEAT_WINDOWS
9571 wp->w_redr_status = TRUE;
9572#endif
9573 win_rest_invalid(W_NEXT(wp));
9574 }
9575 return FAIL;
9576 }
9577
9578 return OK;
9579}
9580
9581/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009582 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9584 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9585 * scrolling
9586 * Return OK for success, FAIL if the lines are not deleted.
9587 */
9588 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009589win_del_lines(
9590 win_T *wp,
9591 int row,
9592 int line_count,
9593 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009594 int mayclear,
9595 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
9597 int retval;
9598
9599 if (invalid)
9600 wp->w_lines_valid = 0;
9601
9602 if (line_count > wp->w_height - row)
9603 line_count = wp->w_height - row;
9604
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009605 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 if (retval != MAYBE)
9607 return retval;
9608
9609 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009610 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 return FAIL;
9612
9613#ifdef FEAT_WINDOWS
9614 /*
9615 * If there are windows or status lines below, try to put them at the
9616 * correct place. If we can't do that, they have to be redrawn.
9617 */
9618 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9619 {
9620 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009621 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 {
9623 wp->w_redr_status = TRUE;
9624 win_rest_invalid(wp->w_next);
9625 }
9626 }
9627 /*
9628 * If this is the last window and there is no status line, redraw the
9629 * command line later.
9630 */
9631 else
9632#endif
9633 redraw_cmdline = TRUE;
9634 return OK;
9635}
9636
9637/*
9638 * Common code for win_ins_lines() and win_del_lines().
9639 * Returns OK or FAIL when the work has been done.
9640 * Returns MAYBE when not finished yet.
9641 */
9642 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009643win_do_lines(
9644 win_T *wp,
9645 int row,
9646 int line_count,
9647 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009648 int del,
9649 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650{
9651 int retval;
9652
9653 if (!redrawing() || line_count <= 0)
9654 return FAIL;
9655
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009656 /* When inserting lines would result in loss of command output, just redraw
9657 * the lines. */
9658 if (no_win_do_lines_ins && !del)
9659 return FAIL;
9660
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 /* only a few lines left: redraw is faster */
9662 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009663#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 && wp->w_width == Columns
9665#endif
9666 )
9667 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009668 if (!no_win_do_lines_ins)
9669 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 return FAIL;
9671 }
9672
9673 /*
9674 * Delete all remaining lines
9675 */
9676 if (row + line_count >= wp->w_height)
9677 {
9678 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9679 W_WINCOL(wp), (int)W_ENDCOL(wp),
9680 ' ', ' ', 0);
9681 return OK;
9682 }
9683
9684 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009685 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009687 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009689 if (!no_win_do_lines_ins)
9690 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691
9692 /*
9693 * If the terminal can set a scroll region, use that.
9694 * Always do this in a vertically split window. This will redraw from
9695 * ScreenLines[] when t_CV isn't defined. That's faster than using
9696 * win_line().
9697 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009698 * a character in the lower right corner of the scroll region may cause a
9699 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 */
9701 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009702#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 || W_WIDTH(wp) != Columns
9704#endif
9705 )
9706 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009707#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9709#endif
9710 scroll_region_set(wp, row);
9711 if (del)
9712 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009713 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 else
9715 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009716 wp->w_height - row, clear_attr, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009717#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9719#endif
9720 scroll_region_reset();
9721 return retval;
9722 }
9723
9724#ifdef FEAT_WINDOWS
9725 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9726 return FAIL;
9727#endif
9728
9729 return MAYBE;
9730}
9731
9732/*
9733 * window 'wp' and everything after it is messed up, mark it for redraw
9734 */
9735 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009736win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737{
9738#ifdef FEAT_WINDOWS
9739 while (wp != NULL)
9740#else
9741 if (wp != NULL)
9742#endif
9743 {
9744 redraw_win_later(wp, NOT_VALID);
9745#ifdef FEAT_WINDOWS
9746 wp->w_redr_status = TRUE;
9747 wp = wp->w_next;
9748#endif
9749 }
9750 redraw_cmdline = TRUE;
9751}
9752
9753/*
9754 * The rest of the routines in this file perform screen manipulations. The
9755 * given operation is performed physically on the screen. The corresponding
9756 * change is also made to the internal screen image. In this way, the editor
9757 * anticipates the effect of editing changes on the appearance of the screen.
9758 * That way, when we call screenupdate a complete redraw isn't usually
9759 * necessary. Another advantage is that we can keep adding code to anticipate
9760 * screen changes, and in the meantime, everything still works.
9761 */
9762
9763/*
9764 * types for inserting or deleting lines
9765 */
9766#define USE_T_CAL 1
9767#define USE_T_CDL 2
9768#define USE_T_AL 3
9769#define USE_T_CE 4
9770#define USE_T_DL 5
9771#define USE_T_SR 6
9772#define USE_NL 7
9773#define USE_T_CD 8
9774#define USE_REDRAW 9
9775
9776/*
9777 * insert lines on the screen and update ScreenLines[]
9778 * 'end' is the line after the scrolled part. Normally it is Rows.
9779 * When scrolling region used 'off' is the offset from the top for the region.
9780 * 'row' and 'end' are relative to the start of the region.
9781 *
9782 * return FAIL for failure, OK for success.
9783 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009784 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009785screen_ins_lines(
9786 int off,
9787 int row,
9788 int line_count,
9789 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009790 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009791 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792{
9793 int i;
9794 int j;
9795 unsigned temp;
9796 int cursor_row;
9797 int type;
9798 int result_empty;
9799 int can_ce = can_clear(T_CE);
9800
9801 /*
9802 * FAIL if
9803 * - there is no valid screen
9804 * - the screen has to be redrawn completely
9805 * - the line count is less than one
9806 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009807 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009809 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9810#ifdef FEAT_CLIPBOARD
9811 || (clip_star.state != SELECT_CLEARED
9812 && redrawing_for_callback > 0)
9813#endif
9814 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 return FAIL;
9816
9817 /*
9818 * There are seven ways to insert lines:
9819 * 0. When in a vertically split window and t_CV isn't set, redraw the
9820 * characters from ScreenLines[].
9821 * 1. Use T_CD (clear to end of display) if it exists and the result of
9822 * the insert is just empty lines
9823 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9824 * present or line_count > 1. It looks better if we do all the inserts
9825 * at once.
9826 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9827 * insert is just empty lines and T_CE is not present or line_count >
9828 * 1.
9829 * 4. Use T_AL (insert line) if it exists.
9830 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9831 * just empty lines.
9832 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9833 * just empty lines.
9834 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9835 * the 'da' flag is not set or we have clear line capability.
9836 * 8. redraw the characters from ScreenLines[].
9837 *
9838 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9839 * the scrollbar for the window. It does have insert line, use that if it
9840 * exists.
9841 */
9842 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009843#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9845 type = USE_REDRAW;
9846 else
9847#endif
9848 if (can_clear(T_CD) && result_empty)
9849 type = USE_T_CD;
9850 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9851 type = USE_T_CAL;
9852 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9853 type = USE_T_CDL;
9854 else if (*T_AL != NUL)
9855 type = USE_T_AL;
9856 else if (can_ce && result_empty)
9857 type = USE_T_CE;
9858 else if (*T_DL != NUL && result_empty)
9859 type = USE_T_DL;
9860 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9861 type = USE_T_SR;
9862 else
9863 return FAIL;
9864
9865 /*
9866 * For clearing the lines screen_del_lines() is used. This will also take
9867 * care of t_db if necessary.
9868 */
9869 if (type == USE_T_CD || type == USE_T_CDL ||
9870 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009871 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872
9873 /*
9874 * If text is retained below the screen, first clear or delete as many
9875 * lines at the bottom of the window as are about to be inserted so that
9876 * the deleted lines won't later surface during a screen_del_lines.
9877 */
9878 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009879 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880
9881#ifdef FEAT_CLIPBOARD
9882 /* Remove a modeless selection when inserting lines halfway the screen
9883 * or not the full width of the screen. */
9884 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009885# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 || (wp != NULL && wp->w_width != Columns)
9887# endif
9888 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009889 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 else
9891 clip_scroll_selection(-line_count);
9892#endif
9893
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894#ifdef FEAT_GUI
9895 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9896 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009897 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898#endif
9899
9900 if (*T_CCS != NUL) /* cursor relative to region */
9901 cursor_row = row;
9902 else
9903 cursor_row = row + off;
9904
9905 /*
9906 * Shift LineOffset[] line_count down to reflect the inserted lines.
9907 * Clear the inserted lines in ScreenLines[].
9908 */
9909 row += off;
9910 end += off;
9911 for (i = 0; i < line_count; ++i)
9912 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009913#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 if (wp != NULL && wp->w_width != Columns)
9915 {
9916 /* need to copy part of a line */
9917 j = end - 1 - i;
9918 while ((j -= line_count) >= row)
9919 linecopy(j + line_count, j, wp);
9920 j += line_count;
9921 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009922 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9923 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 else
9925 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9926 LineWraps[j] = FALSE;
9927 }
9928 else
9929#endif
9930 {
9931 j = end - 1 - i;
9932 temp = LineOffset[j];
9933 while ((j -= line_count) >= row)
9934 {
9935 LineOffset[j + line_count] = LineOffset[j];
9936 LineWraps[j + line_count] = LineWraps[j];
9937 }
9938 LineOffset[j + line_count] = temp;
9939 LineWraps[j + line_count] = FALSE;
9940 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009941 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 else
9943 lineinvalid(temp, (int)Columns);
9944 }
9945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946
9947 screen_stop_highlight();
9948 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009949 if (clear_attr != 0)
9950 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009952#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 /* redraw the characters */
9954 if (type == USE_REDRAW)
9955 redraw_block(row, end, wp);
9956 else
9957#endif
9958 if (type == USE_T_CAL)
9959 {
9960 term_append_lines(line_count);
9961 screen_start(); /* don't know where cursor is now */
9962 }
9963 else
9964 {
9965 for (i = 0; i < line_count; i++)
9966 {
9967 if (type == USE_T_AL)
9968 {
9969 if (i && cursor_row != 0)
9970 windgoto(cursor_row, 0);
9971 out_str(T_AL);
9972 }
9973 else /* type == USE_T_SR */
9974 out_str(T_SR);
9975 screen_start(); /* don't know where cursor is now */
9976 }
9977 }
9978
9979 /*
9980 * With scroll-reverse and 'da' flag set we need to clear the lines that
9981 * have been scrolled down into the region.
9982 */
9983 if (type == USE_T_SR && *T_DA)
9984 {
9985 for (i = 0; i < line_count; ++i)
9986 {
9987 windgoto(off + i, 0);
9988 out_str(T_CE);
9989 screen_start(); /* don't know where cursor is now */
9990 }
9991 }
9992
9993#ifdef FEAT_GUI
9994 gui_can_update_cursor();
9995 if (gui.in_use)
9996 out_flush(); /* always flush after a scroll */
9997#endif
9998 return OK;
9999}
10000
10001/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010002 * Delete lines on the screen and update ScreenLines[].
10003 * "end" is the line after the scrolled part. Normally it is Rows.
10004 * When scrolling region used "off" is the offset from the top for the region.
10005 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 *
10007 * Return OK for success, FAIL if the lines are not deleted.
10008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010010screen_del_lines(
10011 int off,
10012 int row,
10013 int line_count,
10014 int end,
10015 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010016 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010017 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018{
10019 int j;
10020 int i;
10021 unsigned temp;
10022 int cursor_row;
10023 int cursor_end;
10024 int result_empty; /* result is empty until end of region */
10025 int can_delete; /* deleting line codes can be used */
10026 int type;
10027
10028 /*
10029 * FAIL if
10030 * - there is no valid screen
10031 * - the screen has to be redrawn completely
10032 * - the line count is less than one
10033 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010034 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010036 if (!screen_valid(TRUE) || line_count <= 0
10037 || (!force && line_count > p_ttyscroll)
10038#ifdef FEAT_CLIPBOARD
10039 || (clip_star.state != SELECT_CLEARED
10040 && redrawing_for_callback > 0)
10041#endif
10042 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 return FAIL;
10044
10045 /*
10046 * Check if the rest of the current region will become empty.
10047 */
10048 result_empty = row + line_count >= end;
10049
10050 /*
10051 * We can delete lines only when 'db' flag not set or when 'ce' option
10052 * available.
10053 */
10054 can_delete = (*T_DB == NUL || can_clear(T_CE));
10055
10056 /*
10057 * There are six ways to delete lines:
10058 * 0. When in a vertically split window and t_CV isn't set, redraw the
10059 * characters from ScreenLines[].
10060 * 1. Use T_CD if it exists and the result is empty.
10061 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10062 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10063 * none of the other ways work.
10064 * 4. Use T_CE (erase line) if the result is empty.
10065 * 5. Use T_DL (delete line) if it exists.
10066 * 6. redraw the characters from ScreenLines[].
10067 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010068#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10070 type = USE_REDRAW;
10071 else
10072#endif
10073 if (can_clear(T_CD) && result_empty)
10074 type = USE_T_CD;
10075#if defined(__BEOS__) && defined(BEOS_DR8)
10076 /*
10077 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10078 * its internal termcap... this works okay for tests which test *T_DB !=
10079 * NUL. It has the disadvantage that the user cannot use any :set t_*
10080 * command to get T_DB (back) to empty_option, only :set term=... will do
10081 * the trick...
10082 * Anyway, this hack will hopefully go away with the next OS release.
10083 * (Olaf Seibert)
10084 */
10085 else if (row == 0 && T_DB == empty_option
10086 && (line_count == 1 || *T_CDL == NUL))
10087#else
10088 else if (row == 0 && (
10089#ifndef AMIGA
10090 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10091 * up, so use delete-line command */
10092 line_count == 1 ||
10093#endif
10094 *T_CDL == NUL))
10095#endif
10096 type = USE_NL;
10097 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10098 type = USE_T_CDL;
10099 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010100#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 && (wp == NULL || wp->w_width == Columns)
10102#endif
10103 )
10104 type = USE_T_CE;
10105 else if (*T_DL != NUL && can_delete)
10106 type = USE_T_DL;
10107 else if (*T_CDL != NUL && can_delete)
10108 type = USE_T_CDL;
10109 else
10110 return FAIL;
10111
10112#ifdef FEAT_CLIPBOARD
10113 /* Remove a modeless selection when deleting lines halfway the screen or
10114 * not the full width of the screen. */
10115 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010116# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 || (wp != NULL && wp->w_width != Columns)
10118# endif
10119 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010120 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 else
10122 clip_scroll_selection(line_count);
10123#endif
10124
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125#ifdef FEAT_GUI
10126 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10127 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010128 gui_dont_update_cursor(gui.cursor_row >= row + off
10129 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130#endif
10131
10132 if (*T_CCS != NUL) /* cursor relative to region */
10133 {
10134 cursor_row = row;
10135 cursor_end = end;
10136 }
10137 else
10138 {
10139 cursor_row = row + off;
10140 cursor_end = end + off;
10141 }
10142
10143 /*
10144 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10145 * Clear the inserted lines in ScreenLines[].
10146 */
10147 row += off;
10148 end += off;
10149 for (i = 0; i < line_count; ++i)
10150 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010151#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 if (wp != NULL && wp->w_width != Columns)
10153 {
10154 /* need to copy part of a line */
10155 j = row + i;
10156 while ((j += line_count) <= end - 1)
10157 linecopy(j - line_count, j, wp);
10158 j -= line_count;
10159 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010160 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10161 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 else
10163 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10164 LineWraps[j] = FALSE;
10165 }
10166 else
10167#endif
10168 {
10169 /* whole width, moving the line pointers is faster */
10170 j = row + i;
10171 temp = LineOffset[j];
10172 while ((j += line_count) <= end - 1)
10173 {
10174 LineOffset[j - line_count] = LineOffset[j];
10175 LineWraps[j - line_count] = LineWraps[j];
10176 }
10177 LineOffset[j - line_count] = temp;
10178 LineWraps[j - line_count] = FALSE;
10179 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010180 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 else
10182 lineinvalid(temp, (int)Columns);
10183 }
10184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010186 if (screen_attr != clear_attr)
10187 screen_stop_highlight();
10188 if (clear_attr != 0)
10189 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010191#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 /* redraw the characters */
10193 if (type == USE_REDRAW)
10194 redraw_block(row, end, wp);
10195 else
10196#endif
10197 if (type == USE_T_CD) /* delete the lines */
10198 {
10199 windgoto(cursor_row, 0);
10200 out_str(T_CD);
10201 screen_start(); /* don't know where cursor is now */
10202 }
10203 else if (type == USE_T_CDL)
10204 {
10205 windgoto(cursor_row, 0);
10206 term_delete_lines(line_count);
10207 screen_start(); /* don't know where cursor is now */
10208 }
10209 /*
10210 * Deleting lines at top of the screen or scroll region: Just scroll
10211 * the whole screen (scroll region) up by outputting newlines on the
10212 * last line.
10213 */
10214 else if (type == USE_NL)
10215 {
10216 windgoto(cursor_end - 1, 0);
10217 for (i = line_count; --i >= 0; )
10218 out_char('\n'); /* cursor will remain on same line */
10219 }
10220 else
10221 {
10222 for (i = line_count; --i >= 0; )
10223 {
10224 if (type == USE_T_DL)
10225 {
10226 windgoto(cursor_row, 0);
10227 out_str(T_DL); /* delete a line */
10228 }
10229 else /* type == USE_T_CE */
10230 {
10231 windgoto(cursor_row + i, 0);
10232 out_str(T_CE); /* erase a line */
10233 }
10234 screen_start(); /* don't know where cursor is now */
10235 }
10236 }
10237
10238 /*
10239 * If the 'db' flag is set, we need to clear the lines that have been
10240 * scrolled up at the bottom of the region.
10241 */
10242 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10243 {
10244 for (i = line_count; i > 0; --i)
10245 {
10246 windgoto(cursor_end - i, 0);
10247 out_str(T_CE); /* erase a line */
10248 screen_start(); /* don't know where cursor is now */
10249 }
10250 }
10251
10252#ifdef FEAT_GUI
10253 gui_can_update_cursor();
10254 if (gui.in_use)
10255 out_flush(); /* always flush after a scroll */
10256#endif
10257
10258 return OK;
10259}
10260
10261/*
10262 * show the current mode and ruler
10263 *
10264 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10265 * If clear_cmdline is FALSE there may be a message there that needs to be
10266 * cleared only if a mode is shown.
10267 * Return the length of the message (0 if no message).
10268 */
10269 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010270showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271{
10272 int need_clear;
10273 int length = 0;
10274 int do_mode;
10275 int attr;
10276 int nwr_save;
10277#ifdef FEAT_INS_EXPAND
10278 int sub_attr;
10279#endif
10280
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010281 do_mode = ((p_smd && msg_silent == 0)
10282 && ((State & INSERT)
10283 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010284 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 if (do_mode || Recording)
10286 {
10287 /*
10288 * Don't show mode right now, when not redrawing or inside a mapping.
10289 * Call char_avail() only when we are going to show something, because
10290 * it takes a bit of time.
10291 */
10292 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10293 {
10294 redraw_cmdline = TRUE; /* show mode later */
10295 return 0;
10296 }
10297
10298 nwr_save = need_wait_return;
10299
10300 /* wait a bit before overwriting an important message */
10301 check_for_delay(FALSE);
10302
10303 /* if the cmdline is more than one line high, erase top lines */
10304 need_clear = clear_cmdline;
10305 if (clear_cmdline && cmdline_row < Rows - 1)
10306 msg_clr_cmdline(); /* will reset clear_cmdline */
10307
10308 /* Position on the last line in the window, column 0 */
10309 msg_pos_mode();
10310 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010311 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 if (do_mode)
10313 {
10314 MSG_PUTS_ATTR("--", attr);
10315#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010316 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010317# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010318 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010319# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010320 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010321# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010322 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010323# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 MSG_PUTS_ATTR(" IM", attr);
10325# else
10326 MSG_PUTS_ATTR(" XIM", attr);
10327# endif
10328#endif
10329#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10330 if (gui.in_use)
10331 {
10332 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010333 {
10334 /* HANGUL */
10335 if (enc_utf8)
10336 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10337 else
10338 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 }
10341#endif
10342#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010343 /* CTRL-X in Insert mode */
10344 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 {
10346 /* These messages can get long, avoid a wrap in a narrow
10347 * window. Prefer showing edit_submode_extra. */
10348 length = (Rows - msg_row) * Columns - 3;
10349 if (edit_submode_extra != NULL)
10350 length -= vim_strsize(edit_submode_extra);
10351 if (length > 0)
10352 {
10353 if (edit_submode_pre != NULL)
10354 length -= vim_strsize(edit_submode_pre);
10355 if (length - vim_strsize(edit_submode) > 0)
10356 {
10357 if (edit_submode_pre != NULL)
10358 msg_puts_attr(edit_submode_pre, attr);
10359 msg_puts_attr(edit_submode, attr);
10360 }
10361 if (edit_submode_extra != NULL)
10362 {
10363 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10364 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010365 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 else
10367 sub_attr = attr;
10368 msg_puts_attr(edit_submode_extra, sub_attr);
10369 }
10370 }
10371 length = 0;
10372 }
10373 else
10374#endif
10375 {
10376#ifdef FEAT_VREPLACE
10377 if (State & VREPLACE_FLAG)
10378 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10379 else
10380#endif
10381 if (State & REPLACE_FLAG)
10382 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10383 else if (State & INSERT)
10384 {
10385#ifdef FEAT_RIGHTLEFT
10386 if (p_ri)
10387 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10388#endif
10389 MSG_PUTS_ATTR(_(" INSERT"), attr);
10390 }
10391 else if (restart_edit == 'I')
10392 MSG_PUTS_ATTR(_(" (insert)"), attr);
10393 else if (restart_edit == 'R')
10394 MSG_PUTS_ATTR(_(" (replace)"), attr);
10395 else if (restart_edit == 'V')
10396 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10397#ifdef FEAT_RIGHTLEFT
10398 if (p_hkmap)
10399 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10400# ifdef FEAT_FKMAP
10401 if (p_fkmap)
10402 MSG_PUTS_ATTR(farsi_text_5, attr);
10403# endif
10404#endif
10405#ifdef FEAT_KEYMAP
10406 if (State & LANGMAP)
10407 {
10408# ifdef FEAT_ARABIC
10409 if (curwin->w_p_arab)
10410 MSG_PUTS_ATTR(_(" Arabic"), attr);
10411 else
10412# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010413 if (get_keymap_str(curwin, (char_u *)" (%s)",
10414 NameBuff, MAXPATHL))
10415 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 }
10417#endif
10418 if ((State & INSERT) && p_paste)
10419 MSG_PUTS_ATTR(_(" (paste)"), attr);
10420
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 if (VIsual_active)
10422 {
10423 char *p;
10424
10425 /* Don't concatenate separate words to avoid translation
10426 * problems. */
10427 switch ((VIsual_select ? 4 : 0)
10428 + (VIsual_mode == Ctrl_V) * 2
10429 + (VIsual_mode == 'V'))
10430 {
10431 case 0: p = N_(" VISUAL"); break;
10432 case 1: p = N_(" VISUAL LINE"); break;
10433 case 2: p = N_(" VISUAL BLOCK"); break;
10434 case 4: p = N_(" SELECT"); break;
10435 case 5: p = N_(" SELECT LINE"); break;
10436 default: p = N_(" SELECT BLOCK"); break;
10437 }
10438 MSG_PUTS_ATTR(_(p), attr);
10439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 MSG_PUTS_ATTR(" --", attr);
10441 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010442
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 need_clear = TRUE;
10444 }
10445 if (Recording
10446#ifdef FEAT_INS_EXPAND
10447 && edit_submode == NULL /* otherwise it gets too long */
10448#endif
10449 )
10450 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010451 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 need_clear = TRUE;
10453 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010454
10455 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 if (need_clear || clear_cmdline)
10457 msg_clr_eos();
10458 msg_didout = FALSE; /* overwrite this message */
10459 length = msg_col;
10460 msg_col = 0;
10461 need_wait_return = nwr_save; /* never ask for hit-return for this */
10462 }
10463 else if (clear_cmdline && msg_silent == 0)
10464 /* Clear the whole command line. Will reset "clear_cmdline". */
10465 msg_clr_cmdline();
10466
10467#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 /* In Visual mode the size of the selected area must be redrawn. */
10469 if (VIsual_active)
10470 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471
10472 /* If the last window has no status line, the ruler is after the mode
10473 * message and must be redrawn */
10474 if (redrawing()
10475# ifdef FEAT_WINDOWS
10476 && lastwin->w_status_height == 0
10477# endif
10478 )
10479 win_redr_ruler(lastwin, TRUE);
10480#endif
10481 redraw_cmdline = FALSE;
10482 clear_cmdline = FALSE;
10483
10484 return length;
10485}
10486
10487/*
10488 * Position for a mode message.
10489 */
10490 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010491msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492{
10493 msg_col = 0;
10494 msg_row = Rows - 1;
10495}
10496
10497/*
10498 * Delete mode message. Used when ESC is typed which is expected to end
10499 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010500 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 */
10502 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010503unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504{
10505 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010506 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 */
10508 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10509 redraw_cmdline = TRUE; /* delete mode later */
10510 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010511 clearmode();
10512}
10513
10514/*
10515 * Clear the mode message.
10516 */
10517 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010518clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010519{
10520 msg_pos_mode();
10521 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010522 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010523 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524}
10525
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010526 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010527recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010528{
10529 MSG_PUTS_ATTR(_("recording"), attr);
10530 if (!shortmess(SHM_RECORDING))
10531 {
10532 char_u s[4];
10533 sprintf((char *)s, " @%c", Recording);
10534 MSG_PUTS_ATTR(s, attr);
10535 }
10536}
10537
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010538#if defined(FEAT_WINDOWS)
10539/*
10540 * Draw the tab pages line at the top of the Vim window.
10541 */
10542 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010543draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010544{
10545 int tabcount = 0;
10546 tabpage_T *tp;
10547 int tabwidth;
10548 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010549 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010550 int attr;
10551 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010552 win_T *cwp;
10553 int wincount;
10554 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010555 int c;
10556 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010557 int attr_sel = HL_ATTR(HLF_TPS);
10558 int attr_nosel = HL_ATTR(HLF_TP);
10559 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010560 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010561 int room;
10562 int use_sep_chars = (t_colors < 8
10563#ifdef FEAT_GUI
10564 && !gui.in_use
10565#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010566#ifdef FEAT_TERMGUICOLORS
10567 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010568#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010569 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010570
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010571 if (ScreenLines == NULL)
10572 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010573 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010574
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010575#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010576 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010577 if (gui_use_tabline())
10578 {
10579 gui_update_tabline();
10580 return;
10581 }
10582#endif
10583
10584 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010585 return;
10586
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010587#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010588
10589 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10590 for (scol = 0; scol < Columns; ++scol)
10591 TabPageIdxs[scol] = 0;
10592
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010593 /* Use the 'tabline' option if it's set. */
10594 if (*p_tal != NUL)
10595 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010596 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010597
10598 /* Check for an error. If there is one we would loop in redrawing the
10599 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010600 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010601 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010602 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010603 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010604 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010605 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010606 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010607 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010608#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010609 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010610 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010611 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010612
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10614 if (tabwidth < 6)
10615 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010616
Bram Moolenaar238a5642006-02-21 22:12:05 +000010617 attr = attr_nosel;
10618 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010619 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010620 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10621 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010622 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010623 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010624
Bram Moolenaar238a5642006-02-21 22:12:05 +000010625 if (tp->tp_topframe == topframe)
10626 attr = attr_sel;
10627 if (use_sep_chars && col > 0)
10628 screen_putchar('|', 0, col++, attr);
10629
10630 if (tp->tp_topframe != topframe)
10631 attr = attr_nosel;
10632
10633 screen_putchar(' ', 0, col++, attr);
10634
10635 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010636 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010637 cwp = curwin;
10638 wp = firstwin;
10639 }
10640 else
10641 {
10642 cwp = tp->tp_curwin;
10643 wp = tp->tp_firstwin;
10644 }
10645
10646 modified = FALSE;
10647 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10648 if (bufIsChanged(wp->w_buffer))
10649 modified = TRUE;
10650 if (modified || wincount > 1)
10651 {
10652 if (wincount > 1)
10653 {
10654 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010655 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010656 if (col + len >= Columns - 3)
10657 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010658 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010659#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010660 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010661#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010662 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010663#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010664 );
10665 col += len;
10666 }
10667 if (modified)
10668 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10669 screen_putchar(' ', 0, col++, attr);
10670 }
10671
10672 room = scol - col + tabwidth - 1;
10673 if (room > 0)
10674 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010675 /* Get buffer name in NameBuff[] */
10676 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010677 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010678 len = vim_strsize(NameBuff);
10679 p = NameBuff;
10680#ifdef FEAT_MBYTE
10681 if (has_mbyte)
10682 while (len > room)
10683 {
10684 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010685 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010686 }
10687 else
10688#endif
10689 if (len > room)
10690 {
10691 p += len - room;
10692 len = room;
10693 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010694 if (len > Columns - col - 1)
10695 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010696
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010697 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010698 col += len;
10699 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010700 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010701
10702 /* Store the tab page number in TabPageIdxs[], so that
10703 * jump_to_mouse() knows where each one is. */
10704 ++tabcount;
10705 while (scol < col)
10706 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010707 }
10708
Bram Moolenaar238a5642006-02-21 22:12:05 +000010709 if (use_sep_chars)
10710 c = '_';
10711 else
10712 c = ' ';
10713 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010714
10715 /* Put an "X" for closing the current tab if there are several. */
10716 if (first_tabpage->tp_next != NULL)
10717 {
10718 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10719 TabPageIdxs[Columns - 1] = -999;
10720 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010721 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010722
10723 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10724 * set. */
10725 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010726}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010727
10728/*
10729 * Get buffer name for "buf" into NameBuff[].
10730 * Takes care of special buffer names and translates special characters.
10731 */
10732 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010733get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010734{
10735 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010736 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010737 else
10738 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10739 trans_characters(NameBuff, MAXPATHL);
10740}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010741#endif
10742
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10744/*
10745 * Get the character to use in a status line. Get its attributes in "*attr".
10746 */
10747 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010748fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749{
10750 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010751
10752#ifdef FEAT_TERMINAL
10753 if (bt_terminal(wp->w_buffer))
10754 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010755 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010756 {
10757 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010758 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010759 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010760 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010761 {
10762 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010763 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010764 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010765 }
10766 else
10767#endif
10768 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010770 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 fill = fill_stl;
10772 }
10773 else
10774 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010775 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 fill = fill_stlnc;
10777 }
10778 /* Use fill when there is highlighting, and highlighting of current
10779 * window differs, or the fillchars differ, or this is not the
10780 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010781 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010782 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 || (fill_stl != fill_stlnc)))
10784 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010785 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 return '^';
10787 return '=';
10788}
10789#endif
10790
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010791#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792/*
10793 * Get the character to use in a separator between vertically split windows.
10794 * Get its attributes in "*attr".
10795 */
10796 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010797fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010799 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 if (*attr == 0 && fill_vert == ' ')
10801 return '|';
10802 else
10803 return fill_vert;
10804}
10805#endif
10806
10807/*
10808 * Return TRUE if redrawing should currently be done.
10809 */
10810 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010811redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010813#ifdef FEAT_EVAL
10814 if (disable_redraw_for_testing)
10815 return 0;
10816 else
10817#endif
10818 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10820}
10821
10822/*
10823 * Return TRUE if printing messages should currently be done.
10824 */
10825 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010826messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827{
10828 return (!(p_lz && char_avail() && !KeyTyped));
10829}
10830
10831/*
10832 * Show current status info in ruler and various other places
10833 * If always is FALSE, only show ruler if position has changed.
10834 */
10835 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010836showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837{
10838 if (!always && !redrawing())
10839 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010840#ifdef FEAT_INS_EXPAND
10841 if (pum_visible())
10842 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010843# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010844 /* Don't redraw right now, do it later. */
10845 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010846# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010847 return;
10848 }
10849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010851 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010852 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010853 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 else
10856#endif
10857#ifdef FEAT_CMDL_INFO
10858 win_redr_ruler(curwin, always);
10859#endif
10860
10861#ifdef FEAT_TITLE
10862 if (need_maketitle
10863# ifdef FEAT_STL_OPT
10864 || (p_icon && (stl_syntax & STL_IN_ICON))
10865 || (p_title && (stl_syntax & STL_IN_TITLE))
10866# endif
10867 )
10868 maketitle();
10869#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010870#ifdef FEAT_WINDOWS
10871 /* Redraw the tab pages line if needed. */
10872 if (redraw_tabline)
10873 draw_tabline();
10874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875}
10876
10877#ifdef FEAT_CMDL_INFO
10878 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010879win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010881#define RULER_BUF_LEN 70
10882 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 int row;
10884 int fillchar;
10885 int attr;
10886 int empty_line = FALSE;
10887 colnr_T virtcol;
10888 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010889 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010891#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 int this_ru_col;
10893 int off = 0;
10894 int width = Columns;
10895# define WITH_OFF(x) x
10896# define WITH_WIDTH(x) x
10897#else
10898# define WITH_OFF(x) 0
10899# define WITH_WIDTH(x) Columns
10900# define this_ru_col ru_col
10901#endif
10902
10903 /* If 'ruler' off or redrawing disabled, don't do anything */
10904 if (!p_ru)
10905 return;
10906
10907 /*
10908 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10909 * after deleting lines, before cursor.lnum is corrected.
10910 */
10911 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10912 return;
10913
10914#ifdef FEAT_INS_EXPAND
10915 /* Don't draw the ruler while doing insert-completion, it might overwrite
10916 * the (long) mode message. */
10917# ifdef FEAT_WINDOWS
10918 if (wp == lastwin && lastwin->w_status_height == 0)
10919# endif
10920 if (edit_submode != NULL)
10921 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010922 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10923 if (pum_visible())
10924 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925#endif
10926
10927#ifdef FEAT_STL_OPT
10928 if (*p_ruf)
10929 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010930 int save_called_emsg = called_emsg;
10931
10932 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010934 if (called_emsg)
10935 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010936 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010937 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 return;
10939 }
10940#endif
10941
10942 /*
10943 * Check if not in Insert mode and the line is empty (will show "0-1").
10944 */
10945 if (!(State & INSERT)
10946 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10947 empty_line = TRUE;
10948
10949 /*
10950 * Only draw the ruler when something changed.
10951 */
10952 validate_virtcol_win(wp);
10953 if ( redraw_cmdline
10954 || always
10955 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10956 || wp->w_cursor.col != wp->w_ru_cursor.col
10957 || wp->w_virtcol != wp->w_ru_virtcol
10958#ifdef FEAT_VIRTUALEDIT
10959 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10960#endif
10961 || wp->w_topline != wp->w_ru_topline
10962 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10963#ifdef FEAT_DIFF
10964 || wp->w_topfill != wp->w_ru_topfill
10965#endif
10966 || empty_line != wp->w_ru_empty)
10967 {
10968 cursor_off();
10969#ifdef FEAT_WINDOWS
10970 if (wp->w_status_height)
10971 {
10972 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010973 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 off = W_WINCOL(wp);
10975 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 }
10977 else
10978#endif
10979 {
10980 row = Rows - 1;
10981 fillchar = ' ';
10982 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010983#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 width = Columns;
10985 off = 0;
10986#endif
10987 }
10988
10989 /* In list mode virtcol needs to be recomputed */
10990 virtcol = wp->w_virtcol;
10991 if (wp->w_p_list && lcs_tab1 == NUL)
10992 {
10993 wp->w_p_list = FALSE;
10994 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10995 wp->w_p_list = TRUE;
10996 }
10997
10998 /*
10999 * Some sprintfs return the length, some return a pointer.
11000 * To avoid portability problems we use strlen() here.
11001 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011002 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11004 ? 0L
11005 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011006 len = STRLEN(buffer);
11007 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11009 (int)virtcol + 1);
11010
11011 /*
11012 * Add a "50%" if there is room for it.
11013 * On the last line, don't print in the last column (scrolls the
11014 * screen up on some terminals).
11015 */
11016 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011017 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 o = i + vim_strsize(buffer + i + 1);
11019#ifdef FEAT_WINDOWS
11020 if (wp->w_status_height == 0) /* can't use last char of screen */
11021#endif
11022 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010011023#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 this_ru_col = ru_col - (Columns - width);
11025 if (this_ru_col < 0)
11026 this_ru_col = 0;
11027#endif
11028 /* Never use more than half the window/screen width, leave the other
11029 * half for the filename. */
11030 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
11031 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
11032 if (this_ru_col + o < WITH_WIDTH(width))
11033 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011034 /* need at least 3 chars left for get_rel_pos() + NUL */
11035 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 {
11037#ifdef FEAT_MBYTE
11038 if (has_mbyte)
11039 i += (*mb_char2bytes)(fillchar, buffer + i);
11040 else
11041#endif
11042 buffer[i++] = fillchar;
11043 ++o;
11044 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011045 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 }
11047 /* Truncate at window boundary. */
11048#ifdef FEAT_MBYTE
11049 if (has_mbyte)
11050 {
11051 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011052 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 {
11054 o += (*mb_ptr2cells)(buffer + i);
11055 if (this_ru_col + o > WITH_WIDTH(width))
11056 {
11057 buffer[i] = NUL;
11058 break;
11059 }
11060 }
11061 }
11062 else
11063#endif
11064 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
11065 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
11066
11067 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
11068 i = redraw_cmdline;
11069 screen_fill(row, row + 1,
11070 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
11071 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
11072 fillchar, fillchar, attr);
11073 /* don't redraw the cmdline because of showing the ruler */
11074 redraw_cmdline = i;
11075 wp->w_ru_cursor = wp->w_cursor;
11076 wp->w_ru_virtcol = wp->w_virtcol;
11077 wp->w_ru_empty = empty_line;
11078 wp->w_ru_topline = wp->w_topline;
11079 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11080#ifdef FEAT_DIFF
11081 wp->w_ru_topfill = wp->w_topfill;
11082#endif
11083 }
11084}
11085#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011086
11087#if defined(FEAT_LINEBREAK) || defined(PROTO)
11088/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011089 * Return the width of the 'number' and 'relativenumber' column.
11090 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011091 * Otherwise it depends on 'numberwidth' and the line count.
11092 */
11093 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011094number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011095{
11096 int n;
11097 linenr_T lnum;
11098
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011099 if (wp->w_p_rnu && !wp->w_p_nu)
11100 /* cursor line shows "0" */
11101 lnum = wp->w_height;
11102 else
11103 /* cursor line shows absolute line number */
11104 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011105
Bram Moolenaar6b314672015-03-20 15:42:10 +010011106 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011107 return wp->w_nrwidth_width;
11108 wp->w_nrwidth_line_count = lnum;
11109
11110 n = 0;
11111 do
11112 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011113 lnum /= 10;
11114 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011115 } while (lnum > 0);
11116
11117 /* 'numberwidth' gives the minimal width plus one */
11118 if (n < wp->w_p_nuw - 1)
11119 n = wp->w_p_nuw - 1;
11120
11121 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011122 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011123 return n;
11124}
11125#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011126
11127/*
11128 * Return the current cursor column. This is the actual position on the
11129 * screen. First column is 0.
11130 */
11131 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011132screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011133{
11134 return screen_cur_col;
11135}
11136
11137/*
11138 * Return the current cursor row. This is the actual position on the screen.
11139 * First row is 0.
11140 */
11141 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011142screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011143{
11144 return screen_cur_row;
11145}