blob: 6a7284bb46aa6b6fe0af9d632ff97b25c28382f9 [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
115/*
116 * Buffer for one screen line (characters and attributes).
117 */
118static schar_T *current_ScreenLine;
119
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static void win_update(win_T *wp);
121static 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 +0000122#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
124static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
125static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static int win_line(win_T *, linenr_T, int, int, int nochange);
128static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_RIGHTLEFT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void screen_line(int row, int coloff, int endcol, int clear_width, int rlflag);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
132#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void screen_line(int row, int coloff, int endcol, int clear_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
135#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100136#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200143# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void start_search_hl(void);
145static void end_search_hl(void);
146static void init_search_hl(win_T *wp);
147static void prepare_search_hl(win_T *wp, linenr_T lnum);
148static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
149static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_start_highlight(int attr);
152static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void screenclear2(void);
157static void lineclear(unsigned off, int width);
158static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100159#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void linecopy(int to, int from, win_T *wp);
161static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
164static void win_rest_invalid(win_T *wp);
165static void msg_pos_mode(void);
166static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_status(int *attr, int is_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100173#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100180static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif
182
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100183#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/* Ugly global: overrule attribute used by screen_char() */
185static int screen_char_attr = 0;
186#endif
187
188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
191 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 if (wp->w_redr_type < type)
205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100251redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 redraw_buf_later(curbuf, type);
254}
255
256 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100257redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 win_T *wp;
260
261 FOR_ALL_WINDOWS(wp)
262 {
263 if (wp->w_buffer == buf)
264 redraw_win_later(wp, type);
265 }
266}
267
268/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200269 * Redraw as soon as possible. When the command line is not scrolled redraw
270 * right away and restore what was on the command line.
271 * Return a code indicating what happened.
272 */
273 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100274redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200275{
276 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200277 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200278 int r;
279 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200280 schar_T *screenline; /* copy from ScreenLines[] */
281 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200282#ifdef FEAT_MBYTE
283 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200284 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200285 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200286 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200287#endif
288
289 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200290 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200291 return ret;
292
293 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200296 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200298 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299 if (screenline == NULL || screenattr == NULL)
300 ret = 2;
301#ifdef FEAT_MBYTE
302 if (enc_utf8)
303 {
304 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200305 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306 if (screenlineUC == NULL)
307 ret = 2;
308 for (i = 0; i < p_mco; ++i)
309 {
310 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 if (screenlineC[i] == NULL)
313 ret = 2;
314 }
315 }
316 if (enc_dbcs == DBCS_JPNU)
317 {
318 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 if (screenline2 == NULL)
321 ret = 2;
322 }
323#endif
324
325 if (ret != 2)
326 {
327 /* Save the text displayed in the command line area. */
328 for (r = 0; r < rows; ++r)
329 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200331 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200332 (size_t)cols * sizeof(schar_T));
333 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200334 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336#ifdef FEAT_MBYTE
337 if (enc_utf8)
338 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200339 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200343 mch_memmove(screenlineC[i] + r * cols,
344 ScreenLinesC[i] + LineOffset[cmdline_row + r],
345 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200346 }
347 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200348 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200349 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351#endif
352 }
353
354 update_screen(0);
355 ret = 3;
356
357 if (must_redraw == 0)
358 {
359 int off = (int)(current_ScreenLine - ScreenLines);
360
361 /* Restore the text displayed in the command line area. */
362 for (r = 0; r < rows; ++r)
363 {
364 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 screenline + r * cols,
366 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 screenattr + r * cols,
369 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370#ifdef FEAT_MBYTE
371 if (enc_utf8)
372 {
373 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 screenlineUC + r * cols,
375 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 for (i = 0; i < p_mco; ++i)
377 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 screenlineC[i] + r * cols,
379 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 }
381 if (enc_dbcs == DBCS_JPNU)
382 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200383 screenline2 + r * cols,
384 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200385#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200386 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387 }
388 ret = 4;
389 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 }
391
392 vim_free(screenline);
393 vim_free(screenattr);
394#ifdef FEAT_MBYTE
395 if (enc_utf8)
396 {
397 vim_free(screenlineUC);
398 for (i = 0; i < p_mco; ++i)
399 vim_free(screenlineC[i]);
400 }
401 if (enc_dbcs == DBCS_JPNU)
402 vim_free(screenline2);
403#endif
404
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200405 /* Show the intro message when appropriate. */
406 maybe_intro_message();
407
408 setcursor();
409
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 return ret;
411}
412
413/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100414 * Invoked after an asynchronous callback is called.
415 * If an echo command was used the cursor needs to be put back where
416 * it belongs. If highlighting was changed a redraw is needed.
417 */
418 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200419redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100420{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100421 if (State == HITRETURN || State == ASKMORE)
422 ; /* do nothing */
423 else if (State & CMDLINE)
424 redrawcmdline();
Bram Moolenaar144445d2016-07-08 21:41:54 +0200425 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100426 {
427 update_screen(0);
428 setcursor();
429 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100430 cursor_on();
431 out_flush();
432#ifdef FEAT_GUI
433 if (gui.in_use)
434 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200435 /* Don't update the cursor when it is blinking and off to avoid
436 * flicker. */
437 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200438 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439 gui_mch_flush();
440 }
441#endif
442}
443
444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 * Changed something in the current window, at buffer line "lnum", that
446 * requires that line and possibly other lines to be redrawn.
447 * Used when entering/leaving Insert mode with the cursor on a folded line.
448 * Used to remove the "$" from a change command.
449 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
450 * may become invalid and the whole window will have to be redrawn.
451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100453redrawWinline(
454 linenr_T lnum,
455 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457#ifdef FEAT_FOLDING
458 int i;
459#endif
460
461 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
462 curwin->w_redraw_top = lnum;
463 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
464 curwin->w_redraw_bot = lnum;
465 redraw_later(VALID);
466
467#ifdef FEAT_FOLDING
468 if (invalid)
469 {
470 /* A w_lines[] entry for this lnum has become invalid. */
471 i = find_wl_entry(curwin, lnum);
472 if (i >= 0)
473 curwin->w_lines[i].wl_valid = FALSE;
474 }
475#endif
476}
477
478/*
479 * update all windows that are editing the current buffer
480 */
481 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100482update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
484 redraw_curbuf_later(type);
485 update_screen(type);
486}
487
488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 * Based on the current value of curwin->w_topline, transfer a screenfull
490 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
491 */
492 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100493update_screen(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494{
495 win_T *wp;
496 static int did_intro = FALSE;
497#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
498 int did_one;
499#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200500#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200501 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200502 int gui_cursor_col;
503 int gui_cursor_row;
504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000506 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 if (!screen_valid(TRUE))
508 return;
509
510 if (must_redraw)
511 {
512 if (type < must_redraw) /* use maximal type */
513 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000514
515 /* must_redraw is reset here, so that when we run into some weird
516 * reason to redraw while busy redrawing (e.g., asynchronous
517 * scrolling), or update_topline() in win_update() will cause a
518 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 must_redraw = 0;
520 }
521
522 /* Need to update w_lines[]. */
523 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
524 type = NOT_VALID;
525
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000526 /* Postpone the redrawing when it's not needed and when being called
527 * recursively. */
528 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
530 redraw_later(type); /* remember type for next time */
531 must_redraw = type;
532 if (type > INVERTED_ALL)
533 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
534 return;
535 }
536
537 updating_screen = TRUE;
538#ifdef FEAT_SYN_HL
539 ++display_tick; /* let syntax code know we're in a next round of
540 * display updating */
541#endif
542
543 /*
544 * if the screen was scrolled up when displaying a message, scroll it down
545 */
546 if (msg_scrolled)
547 {
548 clear_cmdline = TRUE;
549 if (msg_scrolled > Rows - 5) /* clearing is faster */
550 type = CLEAR;
551 else if (type != CLEAR)
552 {
553 check_for_delay(FALSE);
554 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
555 type = CLEAR;
556 FOR_ALL_WINDOWS(wp)
557 {
558 if (W_WINROW(wp) < msg_scrolled)
559 {
560 if (W_WINROW(wp) + wp->w_height > msg_scrolled
561 && wp->w_redr_type < REDRAW_TOP
562 && wp->w_lines_valid > 0
563 && wp->w_topline == wp->w_lines[0].wl_lnum)
564 {
565 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
566 wp->w_redr_type = REDRAW_TOP;
567 }
568 else
569 {
570 wp->w_redr_type = NOT_VALID;
571#ifdef FEAT_WINDOWS
572 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
573 <= msg_scrolled)
574 wp->w_redr_status = TRUE;
575#endif
576 }
577 }
578 }
579 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000580#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000581 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 }
584 msg_scrolled = 0;
585 need_wait_return = FALSE;
586 }
587
588 /* reset cmdline_row now (may have been changed temporarily) */
589 compute_cmdrow();
590
591 /* Check for changed highlighting */
592 if (need_highlight_changed)
593 highlight_changed();
594
595 if (type == CLEAR) /* first clear screen */
596 {
597 screenclear(); /* will reset clear_cmdline */
598 type = NOT_VALID;
599 }
600
601 if (clear_cmdline) /* going to clear cmdline (done below) */
602 check_for_delay(FALSE);
603
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000604#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200605 /* Force redraw when width of 'number' or 'relativenumber' column
606 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000607 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200608 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
609 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000610 curwin->w_redr_type = NOT_VALID;
611#endif
612
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 /*
614 * Only start redrawing if there is really something to do.
615 */
616 if (type == INVERTED)
617 update_curswant();
618 if (curwin->w_redr_type < type
619 && !((type == VALID
620 && curwin->w_lines[0].wl_valid
621#ifdef FEAT_DIFF
622 && curwin->w_topfill == curwin->w_old_topfill
623 && curwin->w_botfill == curwin->w_old_botfill
624#endif
625 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000627 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
629 && curwin->w_old_visual_mode == VIsual_mode
630 && (curwin->w_valid & VALID_VIRTCOL)
631 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 ))
633 curwin->w_redr_type = type;
634
Bram Moolenaar5a305422006-04-28 22:38:25 +0000635#ifdef FEAT_WINDOWS
636 /* Redraw the tab pages line if needed. */
637 if (redraw_tabline || type >= NOT_VALID)
638 draw_tabline();
639#endif
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#ifdef FEAT_SYN_HL
642 /*
643 * Correct stored syntax highlighting info for changes in each displayed
644 * buffer. Each buffer must only be done once.
645 */
646 FOR_ALL_WINDOWS(wp)
647 {
648 if (wp->w_buffer->b_mod_set)
649 {
650# ifdef FEAT_WINDOWS
651 win_T *wwp;
652
653 /* Check if we already did this buffer. */
654 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
655 if (wwp->w_buffer == wp->w_buffer)
656 break;
657# endif
658 if (
659# ifdef FEAT_WINDOWS
660 wwp == wp &&
661# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200662 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 syn_stack_apply_changes(wp->w_buffer);
664 }
665 }
666#endif
667
668 /*
669 * Go from top to bottom through the windows, redrawing the ones that need
670 * it.
671 */
672#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
673 did_one = FALSE;
674#endif
675#ifdef FEAT_SEARCH_EXTRA
676 search_hl.rm.regprog = NULL;
677#endif
678 FOR_ALL_WINDOWS(wp)
679 {
680 if (wp->w_redr_type != 0)
681 {
682 cursor_off();
683#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
684 if (!did_one)
685 {
686 did_one = TRUE;
687# ifdef FEAT_SEARCH_EXTRA
688 start_search_hl();
689# endif
690# ifdef FEAT_CLIPBOARD
691 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200692 if (clip_star.available && clip_isautosel_star())
693 clip_update_selection(&clip_star);
694 if (clip_plus.available && clip_isautosel_plus())
695 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696# endif
697#ifdef FEAT_GUI
698 /* Remove the cursor before starting to do anything, because
699 * scrolling may make it difficult to redraw the text under
700 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200701 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200702 {
703 gui_cursor_col = gui.cursor_col;
704 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200706 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708#endif
709 }
710#endif
711 win_update(wp);
712 }
713
714#ifdef FEAT_WINDOWS
715 /* redraw status line after the window to minimize cursor movement */
716 if (wp->w_redr_status)
717 {
718 cursor_off();
719 win_redr_status(wp);
720 }
721#endif
722 }
723#if defined(FEAT_SEARCH_EXTRA)
724 end_search_hl();
725#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100726#ifdef FEAT_INS_EXPAND
727 /* May need to redraw the popup menu. */
728 if (pum_visible())
729 pum_redraw();
730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
732#ifdef FEAT_WINDOWS
733 /* Reset b_mod_set flags. Going through all windows is probably faster
734 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200735 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 wp->w_buffer->b_mod_set = FALSE;
737#else
738 curbuf->b_mod_set = FALSE;
739#endif
740
741 updating_screen = FALSE;
742#ifdef FEAT_GUI
743 gui_may_resize_shell();
744#endif
745
746 /* Clear or redraw the command line. Done last, because scrolling may
747 * mess up the command line. */
748 if (clear_cmdline || redraw_cmdline)
749 showmode();
750
751 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200752 if (!did_intro)
753 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 did_intro = TRUE;
755
756#ifdef FEAT_GUI
757 /* Redraw the cursor and update the scrollbars when all screen updating is
758 * done. */
759 if (gui.in_use)
760 {
761 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200762 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200763 {
764 /* Put the GUI position where the cursor was, gui_update_cursor()
765 * uses that. */
766 gui.col = gui_cursor_col;
767 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200768# ifdef FEAT_MBYTE
769 gui.col = mb_fix_col(gui.col, gui.row);
770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200772 screen_cur_col = gui.col;
773 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 gui_update_scrollbars(FALSE);
776 }
777#endif
778}
779
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100780#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
781/*
782 * Prepare for updating one or more windows.
783 * Caller must check for "updating_screen" already set to avoid recursiveness.
784 */
785 static void
786update_prepare(void)
787{
788 cursor_off();
789 updating_screen = TRUE;
790#ifdef FEAT_GUI
791 /* Remove the cursor before starting to do anything, because scrolling may
792 * make it difficult to redraw the text under it. */
793 if (gui.in_use)
794 gui_undraw_cursor();
795#endif
796#ifdef FEAT_SEARCH_EXTRA
797 start_search_hl();
798#endif
799}
800
801/*
802 * Finish updating one or more windows.
803 */
804 static void
805update_finish(void)
806{
807 if (redraw_cmdline)
808 showmode();
809
810# ifdef FEAT_SEARCH_EXTRA
811 end_search_hl();
812# endif
813
814 updating_screen = FALSE;
815
816# ifdef FEAT_GUI
817 gui_may_resize_shell();
818
819 /* Redraw the cursor and update the scrollbars when all screen updating is
820 * done. */
821 if (gui.in_use)
822 {
823 out_flush(); /* required before updating the cursor */
824 gui_update_cursor(FALSE, FALSE);
825 gui_update_scrollbars(FALSE);
826 }
827# endif
828}
829#endif
830
Bram Moolenaar860cae12010-06-05 23:22:07 +0200831#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200832/*
833 * Return TRUE if the cursor line in window "wp" may be concealed, according
834 * to the 'concealcursor' option.
835 */
836 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100837conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200838{
839 int c;
840
841 if (*wp->w_p_cocu == NUL)
842 return FALSE;
843 if (get_real_state() & VISUAL)
844 c = 'v';
845 else if (State & INSERT)
846 c = 'i';
847 else if (State & NORMAL)
848 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200849 else if (State & CMDLINE)
850 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200851 else
852 return FALSE;
853 return vim_strchr(wp->w_p_cocu, c) != NULL;
854}
855
856/*
857 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
858 */
859 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100860conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200861{
862 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
863 {
864 need_cursor_line_redraw = TRUE;
865 /* Need to recompute cursor column, e.g., when starting Visual mode
866 * without concealing. */
867 curs_columns(TRUE);
868 }
869}
870
Bram Moolenaar860cae12010-06-05 23:22:07 +0200871 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100872update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200873{
874 int row;
875 int j;
876
Bram Moolenaar908be432016-05-24 10:51:30 +0200877 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100878 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200879 return;
880
Bram Moolenaar860cae12010-06-05 23:22:07 +0200881 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200882 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200883 {
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100884 update_prepare();
885
Bram Moolenaar860cae12010-06-05 23:22:07 +0200886 row = 0;
887 for (j = 0; j < wp->w_lines_valid; ++j)
888 {
889 if (lnum == wp->w_lines[j].wl_lnum)
890 {
891 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200892# ifdef FEAT_SEARCH_EXTRA
893 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200894 start_search_hl();
895 prepare_search_hl(wp, lnum);
896# endif
897 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
898# if defined(FEAT_SEARCH_EXTRA)
899 end_search_hl();
900# endif
901 break;
902 }
903 row += wp->w_lines[j].wl_size;
904 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100905
906 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200907 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200908 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909}
910#endif
911
912#if defined(FEAT_SIGNS) || defined(PROTO)
913 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100914update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915{
916 win_T *wp;
917 int doit = FALSE;
918
919# ifdef FEAT_FOLDING
920 win_foldinfo.fi_level = 0;
921# endif
922
923 /* update/delete a specific mark */
924 FOR_ALL_WINDOWS(wp)
925 {
926 if (buf != NULL && lnum > 0)
927 {
928 if (wp->w_buffer == buf && lnum >= wp->w_topline
929 && lnum < wp->w_botline)
930 {
931 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
932 wp->w_redraw_top = lnum;
933 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
934 wp->w_redraw_bot = lnum;
935 redraw_win_later(wp, VALID);
936 }
937 }
938 else
939 redraw_win_later(wp, VALID);
940 if (wp->w_redr_type != 0)
941 doit = TRUE;
942 }
943
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100944 /* Return when there is nothing to do, screen updating is already
945 * happening (recursive call) or still starting up. */
946 if (!doit || updating_screen
947#ifdef FEAT_GUI
948 || gui.starting
949#endif
950 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 return;
952
953 /* update all windows that need updating */
954 update_prepare();
955
956# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +0200957 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 {
959 if (wp->w_redr_type != 0)
960 win_update(wp);
961 if (wp->w_redr_status)
962 win_redr_status(wp);
963 }
964# else
965 if (curwin->w_redr_type != 0)
966 win_update(curwin);
967# endif
968
969 update_finish();
970}
971#endif
972
973
974#if defined(FEAT_GUI) || defined(PROTO)
975/*
976 * Update a single window, its status line and maybe the command line msg.
977 * Used for the GUI scrollbar.
978 */
979 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100980updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000982 /* return if already busy updating */
983 if (updating_screen)
984 return;
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 update_prepare();
987
988#ifdef FEAT_CLIPBOARD
989 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200990 if (clip_star.available && clip_isautosel_star())
991 clip_update_selection(&clip_star);
992 if (clip_plus.available && clip_isautosel_plus())
993 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000999 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001000 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001001 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (wp->w_redr_status
1004# ifdef FEAT_CMDL_INFO
1005 || p_ru
1006# endif
1007# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001008 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009# endif
1010 )
1011 win_redr_status(wp);
1012#endif
1013
1014 update_finish();
1015}
1016#endif
1017
1018/*
1019 * Update a single window.
1020 *
1021 * This may cause the windows below it also to be redrawn (when clearing the
1022 * screen or scrolling lines).
1023 *
1024 * How the window is redrawn depends on wp->w_redr_type. Each type also
1025 * implies the one below it.
1026 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001027 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1029 * INVERTED redraw the changed part of the Visual area
1030 * INVERTED_ALL redraw the whole Visual area
1031 * VALID 1. scroll up/down to adjust for a changed w_topline
1032 * 2. update lines at the top when scrolled down
1033 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001034 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 * b_mod_top and b_mod_bot.
1036 * - if wp->w_redraw_top non-zero, redraw lines between
1037 * wp->w_redraw_top and wp->w_redr_bot.
1038 * - continue redrawing when syntax status is invalid.
1039 * 4. if scrolled up, update lines at the bottom.
1040 * This results in three areas that may need updating:
1041 * top: from first row to top_end (when scrolled down)
1042 * mid: from mid_start to mid_end (update inversion or changed text)
1043 * bot: from bot_start to last row (when scrolled up)
1044 */
1045 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001046win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
1048 buf_T *buf = wp->w_buffer;
1049 int type;
1050 int top_end = 0; /* Below last row of the top area that needs
1051 updating. 0 when no top area updating. */
1052 int mid_start = 999;/* first row of the mid area that needs
1053 updating. 999 when no mid area updating. */
1054 int mid_end = 0; /* Below last row of the mid area that needs
1055 updating. 0 when no mid area updating. */
1056 int bot_start = 999;/* first row of the bot area that needs
1057 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 int scrolled_down = FALSE; /* TRUE when scrolled down when
1059 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001061 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 int top_to_mod = FALSE; /* redraw above mod_top */
1063#endif
1064
1065 int row; /* current window row to display */
1066 linenr_T lnum; /* current buffer lnum to display */
1067 int idx; /* current index in w_lines[] */
1068 int srow; /* starting row of the current line */
1069
1070 int eof = FALSE; /* if TRUE, we hit the end of the file */
1071 int didline = FALSE; /* if TRUE, we finished the last line */
1072 int i;
1073 long j;
1074 static int recursive = FALSE; /* being called recursively */
1075 int old_botline = wp->w_botline;
1076#ifdef FEAT_FOLDING
1077 long fold_count;
1078#endif
1079#ifdef FEAT_SYN_HL
1080 /* remember what happened to the previous line, to know if
1081 * check_visual_highlight() can be used */
1082#define DID_NONE 1 /* didn't update a line */
1083#define DID_LINE 2 /* updated a normal line */
1084#define DID_FOLD 3 /* updated a folded line */
1085 int did_update = DID_NONE;
1086 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1087#endif
1088 linenr_T mod_top = 0;
1089 linenr_T mod_bot = 0;
1090#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1091 int save_got_int;
1092#endif
1093
1094 type = wp->w_redr_type;
1095
1096 if (type == NOT_VALID)
1097 {
1098#ifdef FEAT_WINDOWS
1099 wp->w_redr_status = TRUE;
1100#endif
1101 wp->w_lines_valid = 0;
1102 }
1103
1104 /* Window is zero-height: nothing to draw. */
1105 if (wp->w_height == 0)
1106 {
1107 wp->w_redr_type = 0;
1108 return;
1109 }
1110
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001111#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 /* Window is zero-width: Only need to draw the separator. */
1113 if (wp->w_width == 0)
1114 {
1115 /* draw the vertical separator right of this window */
1116 draw_vsep_win(wp, 0);
1117 wp->w_redr_type = 0;
1118 return;
1119 }
1120#endif
1121
1122#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001123 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124#endif
1125
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001126#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001127 /* Force redraw when width of 'number' or 'relativenumber' column
1128 * changes. */
1129 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001130 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001131 {
1132 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001133 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001134 }
1135 else
1136#endif
1137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1139 {
1140 /*
1141 * When there are both inserted/deleted lines and specific lines to be
1142 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1143 * everything (only happens when redrawing is off for while).
1144 */
1145 type = NOT_VALID;
1146 }
1147 else
1148 {
1149 /*
1150 * Set mod_top to the first line that needs displaying because of
1151 * changes. Set mod_bot to the first line after the changes.
1152 */
1153 mod_top = wp->w_redraw_top;
1154 if (wp->w_redraw_bot != 0)
1155 mod_bot = wp->w_redraw_bot + 1;
1156 else
1157 mod_bot = 0;
1158 wp->w_redraw_top = 0; /* reset for next time */
1159 wp->w_redraw_bot = 0;
1160 if (buf->b_mod_set)
1161 {
1162 if (mod_top == 0 || mod_top > buf->b_mod_top)
1163 {
1164 mod_top = buf->b_mod_top;
1165#ifdef FEAT_SYN_HL
1166 /* Need to redraw lines above the change that may be included
1167 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001168 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001170 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (mod_top < 1)
1172 mod_top = 1;
1173 }
1174#endif
1175 }
1176 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1177 mod_bot = buf->b_mod_bot;
1178
1179#ifdef FEAT_SEARCH_EXTRA
1180 /* When 'hlsearch' is on and using a multi-line search pattern, a
1181 * change in one line may make the Search highlighting in a
1182 * previous line invalid. Simple solution: redraw all visible
1183 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001184 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001186 if (search_hl.rm.regprog != NULL
1187 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001189 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001190 {
1191 cur = wp->w_match_head;
1192 while (cur != NULL)
1193 {
1194 if (cur->match.regprog != NULL
1195 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001196 {
1197 top_to_mod = TRUE;
1198 break;
1199 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001200 cur = cur->next;
1201 }
1202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#endif
1204 }
1205#ifdef FEAT_FOLDING
1206 if (mod_top != 0 && hasAnyFolding(wp))
1207 {
1208 linenr_T lnumt, lnumb;
1209
1210 /*
1211 * A change in a line can cause lines above it to become folded or
1212 * unfolded. Find the top most buffer line that may be affected.
1213 * If the line was previously folded and displayed, get the first
1214 * line of that fold. If the line is folded now, get the first
1215 * folded line. Use the minimum of these two.
1216 */
1217
1218 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1219 * the line below it. If there is no valid entry, use w_topline.
1220 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1221 * to this line. If there is no valid entry, use MAXLNUM. */
1222 lnumt = wp->w_topline;
1223 lnumb = MAXLNUM;
1224 for (i = 0; i < wp->w_lines_valid; ++i)
1225 if (wp->w_lines[i].wl_valid)
1226 {
1227 if (wp->w_lines[i].wl_lastlnum < mod_top)
1228 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1229 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1230 {
1231 lnumb = wp->w_lines[i].wl_lnum;
1232 /* When there is a fold column it might need updating
1233 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001234 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 ++lnumb;
1236 }
1237 }
1238
1239 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1240 if (mod_top > lnumt)
1241 mod_top = lnumt;
1242
1243 /* Now do the same for the bottom line (one above mod_bot). */
1244 --mod_bot;
1245 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1246 ++mod_bot;
1247 if (mod_bot < lnumb)
1248 mod_bot = lnumb;
1249 }
1250#endif
1251
1252 /* When a change starts above w_topline and the end is below
1253 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001254 * If the end of the change is above w_topline: do like no change was
1255 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (mod_top != 0 && mod_top < wp->w_topline)
1257 {
1258 if (mod_bot > wp->w_topline)
1259 mod_top = wp->w_topline;
1260#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001261 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 top_end = 1;
1263#endif
1264 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001265
1266 /* When line numbers are displayed need to redraw all lines below
1267 * inserted/deleted lines. */
1268 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1269 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271
1272 /*
1273 * When only displaying the lines at the top, set top_end. Used when
1274 * window has scrolled down for msg_scrolled.
1275 */
1276 if (type == REDRAW_TOP)
1277 {
1278 j = 0;
1279 for (i = 0; i < wp->w_lines_valid; ++i)
1280 {
1281 j += wp->w_lines[i].wl_size;
1282 if (j >= wp->w_upd_rows)
1283 {
1284 top_end = j;
1285 break;
1286 }
1287 }
1288 if (top_end == 0)
1289 /* not found (cannot happen?): redraw everything */
1290 type = NOT_VALID;
1291 else
1292 /* top area defined, the rest is VALID */
1293 type = VALID;
1294 }
1295
Bram Moolenaar367329b2007-08-30 11:53:22 +00001296 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001297 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1298 * non-zero and thus not FALSE) will indicate that screenclear() was not
1299 * called. */
1300 if (screen_cleared)
1301 screen_cleared = MAYBE;
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 /*
1304 * If there are no changes on the screen that require a complete redraw,
1305 * handle three cases:
1306 * 1: we are off the top of the screen by a few lines: scroll down
1307 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1308 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1309 * w_lines[] that needs updating.
1310 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001311 if ((type == VALID || type == SOME_VALID
1312 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313#ifdef FEAT_DIFF
1314 && !wp->w_botfill && !wp->w_old_botfill
1315#endif
1316 )
1317 {
1318 if (mod_top != 0 && wp->w_topline == mod_top)
1319 {
1320 /*
1321 * w_topline is the first changed line, the scrolling will be done
1322 * further down.
1323 */
1324 }
1325 else if (wp->w_lines[0].wl_valid
1326 && (wp->w_topline < wp->w_lines[0].wl_lnum
1327#ifdef FEAT_DIFF
1328 || (wp->w_topline == wp->w_lines[0].wl_lnum
1329 && wp->w_topfill > wp->w_old_topfill)
1330#endif
1331 ))
1332 {
1333 /*
1334 * New topline is above old topline: May scroll down.
1335 */
1336#ifdef FEAT_FOLDING
1337 if (hasAnyFolding(wp))
1338 {
1339 linenr_T ln;
1340
1341 /* count the number of lines we are off, counting a sequence
1342 * of folded lines as one */
1343 j = 0;
1344 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1345 {
1346 ++j;
1347 if (j >= wp->w_height - 2)
1348 break;
1349 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1350 }
1351 }
1352 else
1353#endif
1354 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1355 if (j < wp->w_height - 2) /* not too far off */
1356 {
1357 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1358#ifdef FEAT_DIFF
1359 /* insert extra lines for previously invisible filler lines */
1360 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1361 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1362 - wp->w_old_topfill;
1363#endif
1364 if (i < wp->w_height - 2) /* less than a screen off */
1365 {
1366 /*
1367 * Try to insert the correct number of lines.
1368 * If not the last window, delete the lines at the bottom.
1369 * win_ins_lines may fail when the terminal can't do it.
1370 */
1371 if (i > 0)
1372 check_for_delay(FALSE);
1373 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1374 {
1375 if (wp->w_lines_valid != 0)
1376 {
1377 /* Need to update rows that are new, stop at the
1378 * first one that scrolled down. */
1379 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382 /* Move the entries that were scrolled, disable
1383 * the entries for the lines to be redrawn. */
1384 if ((wp->w_lines_valid += j) > wp->w_height)
1385 wp->w_lines_valid = wp->w_height;
1386 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1387 wp->w_lines[idx] = wp->w_lines[idx - j];
1388 while (idx >= 0)
1389 wp->w_lines[idx--].wl_valid = FALSE;
1390 }
1391 }
1392 else
1393 mid_start = 0; /* redraw all lines */
1394 }
1395 else
1396 mid_start = 0; /* redraw all lines */
1397 }
1398 else
1399 mid_start = 0; /* redraw all lines */
1400 }
1401 else
1402 {
1403 /*
1404 * New topline is at or below old topline: May scroll up.
1405 * When topline didn't change, find first entry in w_lines[] that
1406 * needs updating.
1407 */
1408
1409 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1410 j = -1;
1411 row = 0;
1412 for (i = 0; i < wp->w_lines_valid; i++)
1413 {
1414 if (wp->w_lines[i].wl_valid
1415 && wp->w_lines[i].wl_lnum == wp->w_topline)
1416 {
1417 j = i;
1418 break;
1419 }
1420 row += wp->w_lines[i].wl_size;
1421 }
1422 if (j == -1)
1423 {
1424 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1425 * lines */
1426 mid_start = 0;
1427 }
1428 else
1429 {
1430 /*
1431 * Try to delete the correct number of lines.
1432 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1433 */
1434#ifdef FEAT_DIFF
1435 /* If the topline didn't change, delete old filler lines,
1436 * otherwise delete filler lines of the new topline... */
1437 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1438 row += wp->w_old_topfill;
1439 else
1440 row += diff_check_fill(wp, wp->w_topline);
1441 /* ... but don't delete new filler lines. */
1442 row -= wp->w_topfill;
1443#endif
1444 if (row > 0)
1445 {
1446 check_for_delay(FALSE);
1447 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1448 bot_start = wp->w_height - row;
1449 else
1450 mid_start = 0; /* redraw all lines */
1451 }
1452 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1453 {
1454 /*
1455 * Skip the lines (below the deleted lines) that are still
1456 * valid and don't need redrawing. Copy their info
1457 * upwards, to compensate for the deleted lines. Set
1458 * bot_start to the first row that needs redrawing.
1459 */
1460 bot_start = 0;
1461 idx = 0;
1462 for (;;)
1463 {
1464 wp->w_lines[idx] = wp->w_lines[j];
1465 /* stop at line that didn't fit, unless it is still
1466 * valid (no lines deleted) */
1467 if (row > 0 && bot_start + row
1468 + (int)wp->w_lines[j].wl_size > wp->w_height)
1469 {
1470 wp->w_lines_valid = idx + 1;
1471 break;
1472 }
1473 bot_start += wp->w_lines[idx++].wl_size;
1474
1475 /* stop at the last valid entry in w_lines[].wl_size */
1476 if (++j >= wp->w_lines_valid)
1477 {
1478 wp->w_lines_valid = idx;
1479 break;
1480 }
1481 }
1482#ifdef FEAT_DIFF
1483 /* Correct the first entry for filler lines at the top
1484 * when it won't get updated below. */
1485 if (wp->w_p_diff && bot_start > 0)
1486 wp->w_lines[0].wl_size =
1487 plines_win_nofill(wp, wp->w_topline, TRUE)
1488 + wp->w_topfill;
1489#endif
1490 }
1491 }
1492 }
1493
1494 /* When starting redraw in the first line, redraw all lines. When
1495 * there is only one window it's probably faster to clear the screen
1496 * first. */
1497 if (mid_start == 0)
1498 {
1499 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001500 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001501 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001502 /* Clear the screen when it was not done by win_del_lines() or
1503 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1504 * then. */
1505 if (screen_cleared != TRUE)
1506 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001507#ifdef FEAT_WINDOWS
1508 /* The screen was cleared, redraw the tab pages line. */
1509 if (redraw_tabline)
1510 draw_tabline();
1511#endif
1512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001514
1515 /* When win_del_lines() or win_ins_lines() caused the screen to be
1516 * cleared (only happens for the first window) or when screenclear()
1517 * was called directly above, "must_redraw" will have been set to
1518 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1519 if (screen_cleared == TRUE)
1520 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 }
1522 else
1523 {
1524 /* Not VALID or INVERTED: redraw all lines. */
1525 mid_start = 0;
1526 mid_end = wp->w_height;
1527 }
1528
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001529 if (type == SOME_VALID)
1530 {
1531 /* SOME_VALID: redraw all lines. */
1532 mid_start = 0;
1533 mid_end = wp->w_height;
1534 type = NOT_VALID;
1535 }
1536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 /* check if we are updating or removing the inverted part */
1538 if ((VIsual_active && buf == curwin->w_buffer)
1539 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1540 {
1541 linenr_T from, to;
1542
1543 if (VIsual_active)
1544 {
1545 if (VIsual_active
1546 && (VIsual_mode != wp->w_old_visual_mode
1547 || type == INVERTED_ALL))
1548 {
1549 /*
1550 * If the type of Visual selection changed, redraw the whole
1551 * selection. Also when the ownership of the X selection is
1552 * gained or lost.
1553 */
1554 if (curwin->w_cursor.lnum < VIsual.lnum)
1555 {
1556 from = curwin->w_cursor.lnum;
1557 to = VIsual.lnum;
1558 }
1559 else
1560 {
1561 from = VIsual.lnum;
1562 to = curwin->w_cursor.lnum;
1563 }
1564 /* redraw more when the cursor moved as well */
1565 if (wp->w_old_cursor_lnum < from)
1566 from = wp->w_old_cursor_lnum;
1567 if (wp->w_old_cursor_lnum > to)
1568 to = wp->w_old_cursor_lnum;
1569 if (wp->w_old_visual_lnum < from)
1570 from = wp->w_old_visual_lnum;
1571 if (wp->w_old_visual_lnum > to)
1572 to = wp->w_old_visual_lnum;
1573 }
1574 else
1575 {
1576 /*
1577 * Find the line numbers that need to be updated: The lines
1578 * between the old cursor position and the current cursor
1579 * position. Also check if the Visual position changed.
1580 */
1581 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1582 {
1583 from = curwin->w_cursor.lnum;
1584 to = wp->w_old_cursor_lnum;
1585 }
1586 else
1587 {
1588 from = wp->w_old_cursor_lnum;
1589 to = curwin->w_cursor.lnum;
1590 if (from == 0) /* Visual mode just started */
1591 from = to;
1592 }
1593
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001594 if (VIsual.lnum != wp->w_old_visual_lnum
1595 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597 if (wp->w_old_visual_lnum < from
1598 && wp->w_old_visual_lnum != 0)
1599 from = wp->w_old_visual_lnum;
1600 if (wp->w_old_visual_lnum > to)
1601 to = wp->w_old_visual_lnum;
1602 if (VIsual.lnum < from)
1603 from = VIsual.lnum;
1604 if (VIsual.lnum > to)
1605 to = VIsual.lnum;
1606 }
1607 }
1608
1609 /*
1610 * If in block mode and changed column or curwin->w_curswant:
1611 * update all lines.
1612 * First compute the actual start and end column.
1613 */
1614 if (VIsual_mode == Ctrl_V)
1615 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001616 colnr_T fromc, toc;
1617#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1618 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaar404406a2014-10-09 13:24:43 +02001620 if (curwin->w_p_lbr)
1621 ve_flags = VE_ALL;
1622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001624#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1625 ve_flags = save_ve_flags;
1626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 ++toc;
1628 if (curwin->w_curswant == MAXCOL)
1629 toc = MAXCOL;
1630
1631 if (fromc != wp->w_old_cursor_fcol
1632 || toc != wp->w_old_cursor_lcol)
1633 {
1634 if (from > VIsual.lnum)
1635 from = VIsual.lnum;
1636 if (to < VIsual.lnum)
1637 to = VIsual.lnum;
1638 }
1639 wp->w_old_cursor_fcol = fromc;
1640 wp->w_old_cursor_lcol = toc;
1641 }
1642 }
1643 else
1644 {
1645 /* Use the line numbers of the old Visual area. */
1646 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1647 {
1648 from = wp->w_old_cursor_lnum;
1649 to = wp->w_old_visual_lnum;
1650 }
1651 else
1652 {
1653 from = wp->w_old_visual_lnum;
1654 to = wp->w_old_cursor_lnum;
1655 }
1656 }
1657
1658 /*
1659 * There is no need to update lines above the top of the window.
1660 */
1661 if (from < wp->w_topline)
1662 from = wp->w_topline;
1663
1664 /*
1665 * If we know the value of w_botline, use it to restrict the update to
1666 * the lines that are visible in the window.
1667 */
1668 if (wp->w_valid & VALID_BOTLINE)
1669 {
1670 if (from >= wp->w_botline)
1671 from = wp->w_botline - 1;
1672 if (to >= wp->w_botline)
1673 to = wp->w_botline - 1;
1674 }
1675
1676 /*
1677 * Find the minimal part to be updated.
1678 * Watch out for scrolling that made entries in w_lines[] invalid.
1679 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1680 * top_end; need to redraw from top_end to the "to" line.
1681 * A middle mouse click with a Visual selection may change the text
1682 * above the Visual area and reset wl_valid, do count these for
1683 * mid_end (in srow).
1684 */
1685 if (mid_start > 0)
1686 {
1687 lnum = wp->w_topline;
1688 idx = 0;
1689 srow = 0;
1690 if (scrolled_down)
1691 mid_start = top_end;
1692 else
1693 mid_start = 0;
1694 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1695 {
1696 if (wp->w_lines[idx].wl_valid)
1697 mid_start += wp->w_lines[idx].wl_size;
1698 else if (!scrolled_down)
1699 srow += wp->w_lines[idx].wl_size;
1700 ++idx;
1701# ifdef FEAT_FOLDING
1702 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1703 lnum = wp->w_lines[idx].wl_lnum;
1704 else
1705# endif
1706 ++lnum;
1707 }
1708 srow += mid_start;
1709 mid_end = wp->w_height;
1710 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1711 {
1712 if (wp->w_lines[idx].wl_valid
1713 && wp->w_lines[idx].wl_lnum >= to + 1)
1714 {
1715 /* Only update until first row of this line */
1716 mid_end = srow;
1717 break;
1718 }
1719 srow += wp->w_lines[idx].wl_size;
1720 }
1721 }
1722 }
1723
1724 if (VIsual_active && buf == curwin->w_buffer)
1725 {
1726 wp->w_old_visual_mode = VIsual_mode;
1727 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1728 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001729 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 wp->w_old_curswant = curwin->w_curswant;
1731 }
1732 else
1733 {
1734 wp->w_old_visual_mode = 0;
1735 wp->w_old_cursor_lnum = 0;
1736 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001737 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1741 /* reset got_int, otherwise regexp won't work */
1742 save_got_int = got_int;
1743 got_int = 0;
1744#endif
1745#ifdef FEAT_FOLDING
1746 win_foldinfo.fi_level = 0;
1747#endif
1748
1749 /*
1750 * Update all the window rows.
1751 */
1752 idx = 0; /* first entry in w_lines[].wl_size */
1753 row = 0;
1754 srow = 0;
1755 lnum = wp->w_topline; /* first line shown in window */
1756 for (;;)
1757 {
1758 /* stop updating when reached the end of the window (check for _past_
1759 * the end of the window is at the end of the loop) */
1760 if (row == wp->w_height)
1761 {
1762 didline = TRUE;
1763 break;
1764 }
1765
1766 /* stop updating when hit the end of the file */
1767 if (lnum > buf->b_ml.ml_line_count)
1768 {
1769 eof = TRUE;
1770 break;
1771 }
1772
1773 /* Remember the starting row of the line that is going to be dealt
1774 * with. It is used further down when the line doesn't fit. */
1775 srow = row;
1776
1777 /*
1778 * Update a line when it is in an area that needs updating, when it
1779 * has changes or w_lines[idx] is invalid.
1780 * bot_start may be halfway a wrapped line after using
1781 * win_del_lines(), check if the current line includes it.
1782 * When syntax folding is being used, the saved syntax states will
1783 * already have been updated, we can't see where the syntax state is
1784 * the same again, just update until the end of the window.
1785 */
1786 if (row < top_end
1787 || (row >= mid_start && row < mid_end)
1788#ifdef FEAT_SEARCH_EXTRA
1789 || top_to_mod
1790#endif
1791 || idx >= wp->w_lines_valid
1792 || (row + wp->w_lines[idx].wl_size > bot_start)
1793 || (mod_top != 0
1794 && (lnum == mod_top
1795 || (lnum >= mod_top
1796 && (lnum < mod_bot
1797#ifdef FEAT_SYN_HL
1798 || did_update == DID_FOLD
1799 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001800 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 && (
1802# ifdef FEAT_FOLDING
1803 (foldmethodIsSyntax(wp)
1804 && hasAnyFolding(wp)) ||
1805# endif
1806 syntax_check_changed(lnum)))
1807#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001808#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001809 /* match in fixed position might need redraw
1810 * if lines were inserted or deleted */
1811 || (wp->w_match_head != NULL
1812 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 )))))
1815 {
1816#ifdef FEAT_SEARCH_EXTRA
1817 if (lnum == mod_top)
1818 top_to_mod = FALSE;
1819#endif
1820
1821 /*
1822 * When at start of changed lines: May scroll following lines
1823 * up or down to minimize redrawing.
1824 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001825 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 */
1827 if (lnum == mod_top
1828 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001829 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 {
1831 int old_rows = 0;
1832 int new_rows = 0;
1833 int xtra_rows;
1834 linenr_T l;
1835
1836 /* Count the old number of window rows, using w_lines[], which
1837 * should still contain the sizes for the lines as they are
1838 * currently displayed. */
1839 for (i = idx; i < wp->w_lines_valid; ++i)
1840 {
1841 /* Only valid lines have a meaningful wl_lnum. Invalid
1842 * lines are part of the changed area. */
1843 if (wp->w_lines[i].wl_valid
1844 && wp->w_lines[i].wl_lnum == mod_bot)
1845 break;
1846 old_rows += wp->w_lines[i].wl_size;
1847#ifdef FEAT_FOLDING
1848 if (wp->w_lines[i].wl_valid
1849 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1850 {
1851 /* Must have found the last valid entry above mod_bot.
1852 * Add following invalid entries. */
1853 ++i;
1854 while (i < wp->w_lines_valid
1855 && !wp->w_lines[i].wl_valid)
1856 old_rows += wp->w_lines[i++].wl_size;
1857 break;
1858 }
1859#endif
1860 }
1861
1862 if (i >= wp->w_lines_valid)
1863 {
1864 /* We can't find a valid line below the changed lines,
1865 * need to redraw until the end of the window.
1866 * Inserting/deleting lines has no use. */
1867 bot_start = 0;
1868 }
1869 else
1870 {
1871 /* Able to count old number of rows: Count new window
1872 * rows, and may insert/delete lines */
1873 j = idx;
1874 for (l = lnum; l < mod_bot; ++l)
1875 {
1876#ifdef FEAT_FOLDING
1877 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1878 ++new_rows;
1879 else
1880#endif
1881#ifdef FEAT_DIFF
1882 if (l == wp->w_topline)
1883 new_rows += plines_win_nofill(wp, l, TRUE)
1884 + wp->w_topfill;
1885 else
1886#endif
1887 new_rows += plines_win(wp, l, TRUE);
1888 ++j;
1889 if (new_rows > wp->w_height - row - 2)
1890 {
1891 /* it's getting too much, must redraw the rest */
1892 new_rows = 9999;
1893 break;
1894 }
1895 }
1896 xtra_rows = new_rows - old_rows;
1897 if (xtra_rows < 0)
1898 {
1899 /* May scroll text up. If there is not enough
1900 * remaining text or scrolling fails, must redraw the
1901 * rest. If scrolling works, must redraw the text
1902 * below the scrolled text. */
1903 if (row - xtra_rows >= wp->w_height - 2)
1904 mod_bot = MAXLNUM;
1905 else
1906 {
1907 check_for_delay(FALSE);
1908 if (win_del_lines(wp, row,
1909 -xtra_rows, FALSE, FALSE) == FAIL)
1910 mod_bot = MAXLNUM;
1911 else
1912 bot_start = wp->w_height + xtra_rows;
1913 }
1914 }
1915 else if (xtra_rows > 0)
1916 {
1917 /* May scroll text down. If there is not enough
1918 * remaining text of scrolling fails, must redraw the
1919 * rest. */
1920 if (row + xtra_rows >= wp->w_height - 2)
1921 mod_bot = MAXLNUM;
1922 else
1923 {
1924 check_for_delay(FALSE);
1925 if (win_ins_lines(wp, row + old_rows,
1926 xtra_rows, FALSE, FALSE) == FAIL)
1927 mod_bot = MAXLNUM;
1928 else if (top_end > row + old_rows)
1929 /* Scrolled the part at the top that requires
1930 * updating down. */
1931 top_end += xtra_rows;
1932 }
1933 }
1934
1935 /* When not updating the rest, may need to move w_lines[]
1936 * entries. */
1937 if (mod_bot != MAXLNUM && i != j)
1938 {
1939 if (j < i)
1940 {
1941 int x = row + new_rows;
1942
1943 /* move entries in w_lines[] upwards */
1944 for (;;)
1945 {
1946 /* stop at last valid entry in w_lines[] */
1947 if (i >= wp->w_lines_valid)
1948 {
1949 wp->w_lines_valid = j;
1950 break;
1951 }
1952 wp->w_lines[j] = wp->w_lines[i];
1953 /* stop at a line that won't fit */
1954 if (x + (int)wp->w_lines[j].wl_size
1955 > wp->w_height)
1956 {
1957 wp->w_lines_valid = j + 1;
1958 break;
1959 }
1960 x += wp->w_lines[j++].wl_size;
1961 ++i;
1962 }
1963 if (bot_start > x)
1964 bot_start = x;
1965 }
1966 else /* j > i */
1967 {
1968 /* move entries in w_lines[] downwards */
1969 j -= i;
1970 wp->w_lines_valid += j;
1971 if (wp->w_lines_valid > wp->w_height)
1972 wp->w_lines_valid = wp->w_height;
1973 for (i = wp->w_lines_valid; i - j >= idx; --i)
1974 wp->w_lines[i] = wp->w_lines[i - j];
1975
1976 /* The w_lines[] entries for inserted lines are
1977 * now invalid, but wl_size may be used above.
1978 * Reset to zero. */
1979 while (i >= idx)
1980 {
1981 wp->w_lines[i].wl_size = 0;
1982 wp->w_lines[i--].wl_valid = FALSE;
1983 }
1984 }
1985 }
1986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 }
1988
1989#ifdef FEAT_FOLDING
1990 /*
1991 * When lines are folded, display one line for all of them.
1992 * Otherwise, display normally (can be several display lines when
1993 * 'wrap' is on).
1994 */
1995 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1996 if (fold_count != 0)
1997 {
1998 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1999 ++row;
2000 --fold_count;
2001 wp->w_lines[idx].wl_folded = TRUE;
2002 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2003# ifdef FEAT_SYN_HL
2004 did_update = DID_FOLD;
2005# endif
2006 }
2007 else
2008#endif
2009 if (idx < wp->w_lines_valid
2010 && wp->w_lines[idx].wl_valid
2011 && wp->w_lines[idx].wl_lnum == lnum
2012 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002013 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 && srow + wp->w_lines[idx].wl_size > wp->w_height
2015#ifdef FEAT_DIFF
2016 && diff_check_fill(wp, lnum) == 0
2017#endif
2018 )
2019 {
2020 /* This line is not going to fit. Don't draw anything here,
2021 * will draw "@ " lines below. */
2022 row = wp->w_height + 1;
2023 }
2024 else
2025 {
2026#ifdef FEAT_SEARCH_EXTRA
2027 prepare_search_hl(wp, lnum);
2028#endif
2029#ifdef FEAT_SYN_HL
2030 /* Let the syntax stuff know we skipped a few lines. */
2031 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002032 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 syntax_end_parsing(syntax_last_parsed + 1);
2034#endif
2035
2036 /*
2037 * Display one line.
2038 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002039 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040
2041#ifdef FEAT_FOLDING
2042 wp->w_lines[idx].wl_folded = FALSE;
2043 wp->w_lines[idx].wl_lastlnum = lnum;
2044#endif
2045#ifdef FEAT_SYN_HL
2046 did_update = DID_LINE;
2047 syntax_last_parsed = lnum;
2048#endif
2049 }
2050
2051 wp->w_lines[idx].wl_lnum = lnum;
2052 wp->w_lines[idx].wl_valid = TRUE;
2053 if (row > wp->w_height) /* past end of screen */
2054 {
2055 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002056 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2058 ++idx;
2059 break;
2060 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002061 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 wp->w_lines[idx].wl_size = row - srow;
2063 ++idx;
2064#ifdef FEAT_FOLDING
2065 lnum += fold_count + 1;
2066#else
2067 ++lnum;
2068#endif
2069 }
2070 else
2071 {
2072 /* This line does not need updating, advance to the next one */
2073 row += wp->w_lines[idx++].wl_size;
2074 if (row > wp->w_height) /* past end of screen */
2075 break;
2076#ifdef FEAT_FOLDING
2077 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2078#else
2079 ++lnum;
2080#endif
2081#ifdef FEAT_SYN_HL
2082 did_update = DID_NONE;
2083#endif
2084 }
2085
2086 if (lnum > buf->b_ml.ml_line_count)
2087 {
2088 eof = TRUE;
2089 break;
2090 }
2091 }
2092 /*
2093 * End of loop over all window lines.
2094 */
2095
2096
2097 if (idx > wp->w_lines_valid)
2098 wp->w_lines_valid = idx;
2099
2100#ifdef FEAT_SYN_HL
2101 /*
2102 * Let the syntax stuff know we stop parsing here.
2103 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002104 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 syntax_end_parsing(syntax_last_parsed + 1);
2106#endif
2107
2108 /*
2109 * If we didn't hit the end of the file, and we didn't finish the last
2110 * line we were working on, then the line didn't fit.
2111 */
2112 wp->w_empty_rows = 0;
2113#ifdef FEAT_DIFF
2114 wp->w_filler_rows = 0;
2115#endif
2116 if (!eof && !didline)
2117 {
2118 if (lnum == wp->w_topline)
2119 {
2120 /*
2121 * Single line that does not fit!
2122 * Don't overwrite it, it can be edited.
2123 */
2124 wp->w_botline = lnum + 1;
2125 }
2126#ifdef FEAT_DIFF
2127 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2128 {
2129 /* Window ends in filler lines. */
2130 wp->w_botline = lnum;
2131 wp->w_filler_rows = wp->w_height - srow;
2132 }
2133#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002134 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2135 {
2136 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2137
2138 /*
2139 * Last line isn't finished: Display "@@@" in the last screen line.
2140 */
2141 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002142 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002143 screen_fill(scr_row, scr_row + 1,
2144 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002145 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002146 set_empty_rows(wp, srow);
2147 wp->w_botline = lnum;
2148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2150 {
2151 /*
2152 * Last line isn't finished: Display "@@@" at the end.
2153 */
2154 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2155 W_WINROW(wp) + wp->w_height,
2156 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002157 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 set_empty_rows(wp, srow);
2159 wp->w_botline = lnum;
2160 }
2161 else
2162 {
2163 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2164 wp->w_botline = lnum;
2165 }
2166 }
2167 else
2168 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002169#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 draw_vsep_win(wp, row);
2171#endif
2172 if (eof) /* we hit the end of the file */
2173 {
2174 wp->w_botline = buf->b_ml.ml_line_count + 1;
2175#ifdef FEAT_DIFF
2176 j = diff_check_fill(wp, wp->w_botline);
2177 if (j > 0 && !wp->w_botfill)
2178 {
2179 /*
2180 * Display filler lines at the end of the file
2181 */
2182 if (char2cells(fill_diff) > 1)
2183 i = '-';
2184 else
2185 i = fill_diff;
2186 if (row + j > wp->w_height)
2187 j = wp->w_height - row;
2188 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2189 row += j;
2190 }
2191#endif
2192 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002193 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 wp->w_botline = lnum;
2195
2196 /* make sure the rest of the screen is blank */
2197 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002198 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 }
2200
2201 /* Reset the type of redrawing required, the window has been updated. */
2202 wp->w_redr_type = 0;
2203#ifdef FEAT_DIFF
2204 wp->w_old_topfill = wp->w_topfill;
2205 wp->w_old_botfill = wp->w_botfill;
2206#endif
2207
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002208 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
2210 /*
2211 * There is a trick with w_botline. If we invalidate it on each
2212 * change that might modify it, this will cause a lot of expensive
2213 * calls to plines() in update_topline() each time. Therefore the
2214 * value of w_botline is often approximated, and this value is used to
2215 * compute the value of w_topline. If the value of w_botline was
2216 * wrong, check that the value of w_topline is correct (cursor is on
2217 * the visible part of the text). If it's not, we need to redraw
2218 * again. Mostly this just means scrolling up a few lines, so it
2219 * doesn't look too bad. Only do this for the current window (where
2220 * changes are relevant).
2221 */
2222 wp->w_valid |= VALID_BOTLINE;
2223 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2224 {
2225 recursive = TRUE;
2226 curwin->w_valid &= ~VALID_TOPLINE;
2227 update_topline(); /* may invalidate w_botline again */
2228 if (must_redraw != 0)
2229 {
2230 /* Don't update for changes in buffer again. */
2231 i = curbuf->b_mod_set;
2232 curbuf->b_mod_set = FALSE;
2233 win_update(curwin);
2234 must_redraw = 0;
2235 curbuf->b_mod_set = i;
2236 }
2237 recursive = FALSE;
2238 }
2239 }
2240
2241#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2242 /* restore got_int, unless CTRL-C was hit while redrawing */
2243 if (!got_int)
2244 got_int = save_got_int;
2245#endif
2246}
2247
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248/*
2249 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2250 * as the filler character.
2251 */
2252 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002253win_draw_end(
2254 win_T *wp,
2255 int c1,
2256 int c2,
2257 int row,
2258 int endrow,
2259 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260{
2261#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2262 int n = 0;
2263# define FDC_OFF n
2264#else
2265# define FDC_OFF 0
2266#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002267#ifdef FEAT_FOLDING
2268 int fdc = compute_foldcolumn(wp, 0);
2269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270
2271#ifdef FEAT_RIGHTLEFT
2272 if (wp->w_p_rl)
2273 {
2274 /* No check for cmdline window: should never be right-left. */
2275# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002276 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278 if (n > 0)
2279 {
2280 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002281 if (n > W_WIDTH(wp))
2282 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2284 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002285 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287# endif
2288# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002289 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 int nn = n + 2;
2292
2293 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002294 if (nn > W_WIDTH(wp))
2295 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2297 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002298 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 n = nn;
2300 }
2301# endif
2302 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2303 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002304 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2306 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002307 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
2309 else
2310#endif
2311 {
2312#ifdef FEAT_CMDWIN
2313 if (cmdwin_type != 0 && wp == curwin)
2314 {
2315 /* draw the cmdline character in the leftmost column */
2316 n = 1;
2317 if (n > wp->w_width)
2318 n = wp->w_width;
2319 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2320 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002321 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 }
2323#endif
2324#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002325 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002327 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
2329 /* draw the fold column at the left */
2330 if (nn > W_WIDTH(wp))
2331 nn = W_WIDTH(wp);
2332 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2333 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002334 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 n = nn;
2336 }
2337#endif
2338#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002339 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
2341 int nn = n + 2;
2342
2343 /* draw the sign column after the fold column */
2344 if (nn > W_WIDTH(wp))
2345 nn = W_WIDTH(wp);
2346 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2347 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002348 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 n = nn;
2350 }
2351#endif
2352 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2353 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002354 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356 set_empty_rows(wp, row);
2357}
2358
Bram Moolenaar1a384422010-07-14 19:53:30 +02002359#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002360static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002361
2362/*
2363 * Advance **color_cols and return TRUE when there are columns to draw.
2364 */
2365 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002366advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002367{
2368 while (**color_cols >= 0 && vcol > **color_cols)
2369 ++*color_cols;
2370 return (**color_cols >= 0);
2371}
2372#endif
2373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374#ifdef FEAT_FOLDING
2375/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002376 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2377 * space is available for window "wp", minus "col".
2378 */
2379 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002380compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002381{
2382 int fdc = wp->w_p_fdc;
2383 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2384 int wwidth = W_WIDTH(wp);
2385
2386 if (fdc > wwidth - (col + wmw))
2387 fdc = wwidth - (col + wmw);
2388 return fdc;
2389}
2390
2391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 * Display one folded line.
2393 */
2394 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002395fold_line(
2396 win_T *wp,
2397 long fold_count,
2398 foldinfo_T *foldinfo,
2399 linenr_T lnum,
2400 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002402 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 pos_T *top, *bot;
2404 linenr_T lnume = lnum + fold_count - 1;
2405 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002406 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 int col;
2409 int txtcol;
2410 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002411 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
2413 /* Build the fold line:
2414 * 1. Add the cmdwin_type for the command-line window
2415 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002416 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 * 4. Compose the text
2418 * 5. Add the text
2419 * 6. set highlighting for the Visual area an other text
2420 */
2421 col = 0;
2422
2423 /*
2424 * 1. Add the cmdwin_type for the command-line window
2425 * Ignores 'rightleft', this window is never right-left.
2426 */
2427#ifdef FEAT_CMDWIN
2428 if (cmdwin_type != 0 && wp == curwin)
2429 {
2430 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002431 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432#ifdef FEAT_MBYTE
2433 if (enc_utf8)
2434 ScreenLinesUC[off] = 0;
2435#endif
2436 ++col;
2437 }
2438#endif
2439
2440 /*
2441 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002442 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002444 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 if (fdc > 0)
2446 {
2447 fill_foldcolumn(buf, wp, TRUE, lnum);
2448#ifdef FEAT_RIGHTLEFT
2449 if (wp->w_p_rl)
2450 {
2451 int i;
2452
2453 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002454 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 /* reverse the fold column */
2456 for (i = 0; i < fdc; ++i)
2457 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2458 }
2459 else
2460#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002461 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 col += fdc;
2463 }
2464
2465#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002466# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2467 for (ri = 0; ri < l; ++ri) \
2468 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2469 else \
2470 for (ri = 0; ri < l; ++ri) \
2471 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002473# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2474 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475#endif
2476
Bram Moolenaar64486672010-05-16 15:46:46 +02002477 /* Set all attributes of the 'number' or 'relativenumber' column and the
2478 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002479 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481#ifdef FEAT_SIGNS
2482 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002483 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
2485 len = W_WIDTH(wp) - col;
2486 if (len > 0)
2487 {
2488 if (len > 2)
2489 len = 2;
2490# ifdef FEAT_RIGHTLEFT
2491 if (wp->w_p_rl)
2492 /* the line number isn't reversed */
2493 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002494 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 else
2496# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002497 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 col += len;
2499 }
2500 }
2501#endif
2502
2503 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002504 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002506 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508 len = W_WIDTH(wp) - col;
2509 if (len > 0)
2510 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002511 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002512 long num;
2513 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002514
2515 if (len > w + 1)
2516 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002517
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002518 if (wp->w_p_nu && !wp->w_p_rnu)
2519 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002520 num = (long)lnum;
2521 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002522 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002523 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002524 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002525 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002526 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002527 /* 'number' + 'relativenumber': cursor line shows absolute
2528 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002529 num = lnum;
2530 fmt = "%-*ld ";
2531 }
2532 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002533
Bram Moolenaar700e7342013-01-30 12:31:36 +01002534 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535#ifdef FEAT_RIGHTLEFT
2536 if (wp->w_p_rl)
2537 /* the line number isn't reversed */
2538 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002539 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 else
2541#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002542 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 col += len;
2544 }
2545 }
2546
2547 /*
2548 * 4. Compose the folded-line string with 'foldtext', if set.
2549 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002550 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552 txtcol = col; /* remember where text starts */
2553
2554 /*
2555 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2556 * Right-left text is put in columns 0 - number-col, normal text is put
2557 * in columns number-col - window-width.
2558 */
2559#ifdef FEAT_MBYTE
2560 if (has_mbyte)
2561 {
2562 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002563 int u8c, u8cc[MAX_MCO];
2564 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 int idx;
2566 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002567 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568# ifdef FEAT_ARABIC
2569 int prev_c = 0; /* previous Arabic character */
2570 int prev_c1 = 0; /* first composing char for prev_c */
2571# endif
2572
2573# ifdef FEAT_RIGHTLEFT
2574 if (wp->w_p_rl)
2575 idx = off;
2576 else
2577# endif
2578 idx = off + col;
2579
2580 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2581 for (p = text; *p != NUL; )
2582 {
2583 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002584 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (col + cells > W_WIDTH(wp)
2586# ifdef FEAT_RIGHTLEFT
2587 - (wp->w_p_rl ? col : 0)
2588# endif
2589 )
2590 break;
2591 ScreenLines[idx] = *p;
2592 if (enc_utf8)
2593 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002594 u8c = utfc_ptr2char(p, u8cc);
2595 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
2597 ScreenLinesUC[idx] = 0;
2598#ifdef FEAT_ARABIC
2599 prev_c = u8c;
2600#endif
2601 }
2602 else
2603 {
2604#ifdef FEAT_ARABIC
2605 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2606 {
2607 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002608 int pc, pc1, nc;
2609 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 int firstbyte = *p;
2611
2612 /* The idea of what is the previous and next
2613 * character depends on 'rightleft'. */
2614 if (wp->w_p_rl)
2615 {
2616 pc = prev_c;
2617 pc1 = prev_c1;
2618 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002619 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 }
2621 else
2622 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002623 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002625 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
2627 prev_c = u8c;
2628
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002629 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 pc, pc1, nc);
2631 ScreenLines[idx] = firstbyte;
2632 }
2633 else
2634 prev_c = u8c;
2635#endif
2636 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002637#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (u8c >= 0x10000)
2639 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2640 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002643 for (i = 0; i < Screen_mco; ++i)
2644 {
2645 ScreenLinesC[i][idx] = u8cc[i];
2646 if (u8cc[i] == 0)
2647 break;
2648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650 if (cells > 1)
2651 ScreenLines[idx + 1] = 0;
2652 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002653 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2654 /* double-byte single width character */
2655 ScreenLines2[idx] = p[1];
2656 else if (cells > 1)
2657 /* double-width character */
2658 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 col += cells;
2660 idx += cells;
2661 p += c_len;
2662 }
2663 }
2664 else
2665#endif
2666 {
2667 len = (int)STRLEN(text);
2668 if (len > W_WIDTH(wp) - col)
2669 len = W_WIDTH(wp) - col;
2670 if (len > 0)
2671 {
2672#ifdef FEAT_RIGHTLEFT
2673 if (wp->w_p_rl)
2674 STRNCPY(current_ScreenLine, text, len);
2675 else
2676#endif
2677 STRNCPY(current_ScreenLine + col, text, len);
2678 col += len;
2679 }
2680 }
2681
2682 /* Fill the rest of the line with the fold filler */
2683#ifdef FEAT_RIGHTLEFT
2684 if (wp->w_p_rl)
2685 col -= txtcol;
2686#endif
2687 while (col < W_WIDTH(wp)
2688#ifdef FEAT_RIGHTLEFT
2689 - (wp->w_p_rl ? txtcol : 0)
2690#endif
2691 )
2692 {
2693#ifdef FEAT_MBYTE
2694 if (enc_utf8)
2695 {
2696 if (fill_fold >= 0x80)
2697 {
2698 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002699 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002700 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702 else
2703 ScreenLinesUC[off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002704 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002706 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002708 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 }
2710
2711 if (text != buf)
2712 vim_free(text);
2713
2714 /*
2715 * 6. set highlighting for the Visual area an other text.
2716 * If all folded lines are in the Visual area, highlight the line.
2717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2719 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002720 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
2722 /* Visual is after curwin->w_cursor */
2723 top = &curwin->w_cursor;
2724 bot = &VIsual;
2725 }
2726 else
2727 {
2728 /* Visual is before curwin->w_cursor */
2729 top = &VIsual;
2730 bot = &curwin->w_cursor;
2731 }
2732 if (lnum >= top->lnum
2733 && lnume <= bot->lnum
2734 && (VIsual_mode != 'v'
2735 || ((lnum > top->lnum
2736 || (lnum == top->lnum
2737 && top->col == 0))
2738 && (lnume < bot->lnum
2739 || (lnume == bot->lnum
2740 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002741 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 {
2743 if (VIsual_mode == Ctrl_V)
2744 {
2745 /* Visual block mode: highlight the chars part of the block */
2746 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2747 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002748 if (wp->w_old_cursor_lcol != MAXCOL
2749 && wp->w_old_cursor_lcol + txtcol
2750 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 len = wp->w_old_cursor_lcol;
2752 else
2753 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002754 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002755 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757 }
2758 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002761 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002766#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002767 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2768 if (wp->w_p_cc_cols)
2769 {
2770 int i = 0;
2771 int j = wp->w_p_cc_cols[i];
2772 int old_txtcol = txtcol;
2773
2774 while (j > -1)
2775 {
2776 txtcol += j;
2777 if (wp->w_p_wrap)
2778 txtcol -= wp->w_skipcol;
2779 else
2780 txtcol -= wp->w_leftcol;
2781 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2782 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002783 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002784 txtcol = old_txtcol;
2785 j = wp->w_p_cc_cols[++i];
2786 }
2787 }
2788
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002789 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002790 if (wp->w_p_cuc)
2791 {
2792 txtcol += wp->w_virtcol;
2793 if (wp->w_p_wrap)
2794 txtcol -= wp->w_skipcol;
2795 else
2796 txtcol -= wp->w_leftcol;
2797 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2798 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002799 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002800 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
2803 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2804 (int)W_WIDTH(wp), FALSE);
2805
2806 /*
2807 * Update w_cline_height and w_cline_folded if the cursor line was
2808 * updated (saves a call to plines() later).
2809 */
2810 if (wp == curwin
2811 && lnum <= curwin->w_cursor.lnum
2812 && lnume >= curwin->w_cursor.lnum)
2813 {
2814 curwin->w_cline_row = row;
2815 curwin->w_cline_height = 1;
2816 curwin->w_cline_folded = TRUE;
2817 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2818 }
2819}
2820
2821/*
2822 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2823 */
2824 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002825copy_text_attr(
2826 int off,
2827 char_u *buf,
2828 int len,
2829 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002831 int i;
2832
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 mch_memmove(ScreenLines + off, buf, (size_t)len);
2834# ifdef FEAT_MBYTE
2835 if (enc_utf8)
2836 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2837# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002838 for (i = 0; i < len; ++i)
2839 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840}
2841
2842/*
2843 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002844 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 */
2846 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002847fill_foldcolumn(
2848 char_u *p,
2849 win_T *wp,
2850 int closed, /* TRUE of FALSE */
2851 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 int i = 0;
2854 int level;
2855 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002856 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002857 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002860 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
2862 level = win_foldinfo.fi_level;
2863 if (level > 0)
2864 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002865 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002866 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002867
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 /* If the column is too narrow, we start at the lowest level that
2869 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002870 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 if (first_level < 1)
2872 first_level = 1;
2873
Bram Moolenaar1c934292015-01-27 16:39:29 +01002874 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
2876 if (win_foldinfo.fi_lnum == lnum
2877 && first_level + i >= win_foldinfo.fi_low_level)
2878 p[i] = '-';
2879 else if (first_level == 1)
2880 p[i] = '|';
2881 else if (first_level + i <= 9)
2882 p[i] = '0' + first_level + i;
2883 else
2884 p[i] = '>';
2885 if (first_level + i == level)
2886 break;
2887 }
2888 }
2889 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002890 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891}
2892#endif /* FEAT_FOLDING */
2893
2894/*
2895 * Display line "lnum" of window 'wp' on the screen.
2896 * Start at row "startrow", stop when "endrow" is reached.
2897 * wp->w_virtcol needs to be valid.
2898 *
2899 * Return the number of last row the line occupies.
2900 */
2901 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002902win_line(
2903 win_T *wp,
2904 linenr_T lnum,
2905 int startrow,
2906 int endrow,
2907 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01002909 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2911 int c = 0; /* init for GCC */
2912 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002913#ifdef FEAT_LINEBREAK
2914 long vcol_sbr = -1; /* virtual column after showbreak */
2915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 long vcol_prev = -1; /* "vcol" of previous character */
2917 char_u *line; /* current line */
2918 char_u *ptr; /* current position in "line" */
2919 int row; /* row in the window, excl w_winrow */
2920 int screen_row; /* row on the screen, incl w_winrow */
2921
2922 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2923 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002924 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002925 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 int c_extra = NUL; /* extra chars, all the same */
2927 int extra_attr = 0; /* attributes when n_extra != 0 */
2928 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2929 displaying lcs_eol at end-of-line */
2930 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2931 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2932
2933 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2934 int saved_n_extra = 0;
2935 char_u *saved_p_extra = NULL;
2936 int saved_c_extra = 0;
2937 int saved_char_attr = 0;
2938
2939 int n_attr = 0; /* chars with special attr */
2940 int saved_attr2 = 0; /* char_attr saved for n_attr */
2941 int n_attr3 = 0; /* chars with overruling special attr */
2942 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2943
2944 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2945
2946 int fromcol, tocol; /* start/end of inverting */
2947 int fromcol_prev = -2; /* start of inverting after cursor */
2948 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002950 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 pos_T pos;
2952 long v;
2953
2954 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002955 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2957 in this line */
2958 int attr = 0; /* attributes for area highlighting */
2959 int area_attr = 0; /* attributes desired by highlighting */
2960 int search_attr = 0; /* attributes desired by 'hlsearch' */
2961#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002962 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 int syntax_attr = 0; /* attributes desired by syntax */
2964 int has_syntax = FALSE; /* this buffer has syntax highl. */
2965 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002966 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002967 int draw_color_col = FALSE; /* highlight colorcolumn */
2968 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002969#endif
2970#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002971 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002972# define SPWORDLEN 150
2973 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002974 int nextlinecol = 0; /* column where nextline[] starts */
2975 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002976 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002977 int spell_attr = 0; /* attributes desired by spelling */
2978 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002979 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2980 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002981 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002982 static int cap_col = -1; /* column to check for Cap word */
2983 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002984 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#endif
2986 int extra_check; /* has syntax or linebreak */
2987#ifdef FEAT_MBYTE
2988 int multi_attr = 0; /* attributes desired by multibyte */
2989 int mb_l = 1; /* multi-byte byte length */
2990 int mb_c = 0; /* decoded multi-byte character */
2991 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002992 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993#endif
2994#ifdef FEAT_DIFF
2995 int filler_lines; /* nr of filler lines to be drawn */
2996 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002997 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 int change_start = MAXCOL; /* first col of changed area */
2999 int change_end = -1; /* last col of changed area */
3000#endif
3001 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3002#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003003 int need_showbreak = FALSE; /* overlong line, skipping first x
3004 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003006#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3007 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003009 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010#endif
3011#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003012 matchitem_T *cur; /* points to the match list */
3013 match_T *shl; /* points to search_hl or a match */
3014 int shl_flag; /* flag to indicate whether search_hl
3015 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003016 int pos_inprogress; /* marks that position match search is
3017 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003018 int prevcol_hl_flag; /* flag to indicate whether prevcol
3019 equals startcol of search_hl or one
3020 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021#endif
3022#ifdef FEAT_ARABIC
3023 int prev_c = 0; /* previous Arabic character */
3024 int prev_c1 = 0; /* first composing char for prev_c */
3025#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003026#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003027 int did_line_attr = 0;
3028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030 /* draw_state: items that are drawn in sequence: */
3031#define WL_START 0 /* nothing done yet */
3032#ifdef FEAT_CMDWIN
3033# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3034#else
3035# define WL_CMDLINE WL_START
3036#endif
3037#ifdef FEAT_FOLDING
3038# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3039#else
3040# define WL_FOLD WL_CMDLINE
3041#endif
3042#ifdef FEAT_SIGNS
3043# define WL_SIGN WL_FOLD + 1 /* column for signs */
3044#else
3045# define WL_SIGN WL_FOLD /* column for signs */
3046#endif
3047#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003048#ifdef FEAT_LINEBREAK
3049# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003051# define WL_BRI WL_NR
3052#endif
3053#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3054# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3055#else
3056# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057#endif
3058#define WL_LINE WL_SBR + 1 /* text in the line */
3059 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003060#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 int feedback_col = 0;
3062 int feedback_old_attr = -1;
3063#endif
3064
Bram Moolenaar860cae12010-06-05 23:22:07 +02003065#ifdef FEAT_CONCEAL
3066 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003067 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003068 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003069 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003070 int is_concealing = FALSE;
3071 int boguscols = 0; /* nonexistent columns added to force
3072 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003073 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003074 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003075 int match_conc = 0; /* cchar for match functions */
3076 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003077 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003078# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003079# define FIX_FOR_BOGUSCOLS \
3080 { \
3081 n_extra += vcol_off; \
3082 vcol -= vcol_off; \
3083 vcol_off = 0; \
3084 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003085 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003086 boguscols = 0; \
3087 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003088#else
3089# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
3092 if (startrow > endrow) /* past the end already! */
3093 return startrow;
3094
3095 row = startrow;
3096 screen_row = row + W_WINROW(wp);
3097
3098 /*
3099 * To speed up the loop below, set extra_check when there is linebreak,
3100 * trailing white space and/or syntax processing to be done.
3101 */
3102#ifdef FEAT_LINEBREAK
3103 extra_check = wp->w_p_lbr;
3104#else
3105 extra_check = 0;
3106#endif
3107#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003108 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
3110 /* Prepare for syntax highlighting in this line. When there is an
3111 * error, stop syntax highlighting. */
3112 save_did_emsg = did_emsg;
3113 did_emsg = FALSE;
3114 syntax_start(wp, lnum);
3115 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003116 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 else
3118 {
3119 did_emsg = save_did_emsg;
3120 has_syntax = TRUE;
3121 extra_check = TRUE;
3122 }
3123 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003124
3125 /* Check for columns to display for 'colorcolumn'. */
3126 color_cols = wp->w_p_cc_cols;
3127 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003128 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003129#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003130
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003131#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003132 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003133 && *wp->w_s->b_p_spl != NUL
3134 && wp->w_s->b_langp.ga_len > 0
3135 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003136 {
3137 /* Prepare for spell checking. */
3138 has_spell = TRUE;
3139 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003140
3141 /* Get the start of the next line, so that words that wrap to the next
3142 * line are found too: "et<line-break>al.".
3143 * Trick: skip a few chars for C/shell/Vim comments */
3144 nextline[SPWORDLEN] = NUL;
3145 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3146 {
3147 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3148 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3149 }
3150
3151 /* When a word wrapped from the previous line the start of the current
3152 * line is valid. */
3153 if (lnum == checked_lnum)
3154 cur_checked_col = checked_col;
3155 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003156
3157 /* When there was a sentence end in the previous line may require a
3158 * word starting with capital in this line. In line 1 always check
3159 * the first word. */
3160 if (lnum != capcol_lnum)
3161 cap_col = -1;
3162 if (lnum == 1)
3163 cap_col = 0;
3164 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166#endif
3167
3168 /*
3169 * handle visual active in this window
3170 */
3171 fromcol = -10;
3172 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3174 {
3175 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003176 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 {
3178 top = &curwin->w_cursor;
3179 bot = &VIsual;
3180 }
3181 else /* Visual is before curwin->w_cursor */
3182 {
3183 top = &VIsual;
3184 bot = &curwin->w_cursor;
3185 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003186 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 if (VIsual_mode == Ctrl_V) /* block mode */
3188 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003189 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
3191 fromcol = wp->w_old_cursor_fcol;
3192 tocol = wp->w_old_cursor_lcol;
3193 }
3194 }
3195 else /* non-block mode */
3196 {
3197 if (lnum > top->lnum && lnum <= bot->lnum)
3198 fromcol = 0;
3199 else if (lnum == top->lnum)
3200 {
3201 if (VIsual_mode == 'V') /* linewise */
3202 fromcol = 0;
3203 else
3204 {
3205 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3206 if (gchar_pos(top) == NUL)
3207 tocol = fromcol + 1;
3208 }
3209 }
3210 if (VIsual_mode != 'V' && lnum == bot->lnum)
3211 {
3212 if (*p_sel == 'e' && bot->col == 0
3213#ifdef FEAT_VIRTUALEDIT
3214 && bot->coladd == 0
3215#endif
3216 )
3217 {
3218 fromcol = -10;
3219 tocol = MAXCOL;
3220 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003221 else if (bot->col == MAXCOL)
3222 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 else
3224 {
3225 pos = *bot;
3226 if (*p_sel == 'e')
3227 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3228 else
3229 {
3230 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3231 ++tocol;
3232 }
3233 }
3234 }
3235 }
3236
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 /* Check if the character under the cursor should not be inverted */
3238 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003239#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 )
3243 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 /* if inverting in this line set area_highlighting */
3246 if (fromcol >= 0)
3247 {
3248 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003249 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003251 if ((clip_star.available && !clip_star.owned
3252 && clip_isautosel_star())
3253 || (clip_plus.available && !clip_plus.owned
3254 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003255 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256#endif
3257 }
3258 }
3259
3260 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003261 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003263 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 && wp == curwin
3265 && lnum >= curwin->w_cursor.lnum
3266 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3267 {
3268 if (lnum == curwin->w_cursor.lnum)
3269 getvcol(curwin, &(curwin->w_cursor),
3270 (colnr_T *)&fromcol, NULL, NULL);
3271 else
3272 fromcol = 0;
3273 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3274 {
3275 pos.lnum = lnum;
3276 pos.col = search_match_endcol;
3277 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3278 }
3279 else
3280 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003281 /* do at least one character; happens when past end of line */
3282 if (fromcol == tocol)
3283 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003285 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 }
3287
3288#ifdef FEAT_DIFF
3289 filler_lines = diff_check(wp, lnum);
3290 if (filler_lines < 0)
3291 {
3292 if (filler_lines == -1)
3293 {
3294 if (diff_find_change(wp, lnum, &change_start, &change_end))
3295 diff_hlf = HLF_ADD; /* added line */
3296 else if (change_start == 0)
3297 diff_hlf = HLF_TXD; /* changed text */
3298 else
3299 diff_hlf = HLF_CHD; /* changed line */
3300 }
3301 else
3302 diff_hlf = HLF_ADD; /* added line */
3303 filler_lines = 0;
3304 area_highlighting = TRUE;
3305 }
3306 if (lnum == wp->w_topline)
3307 filler_lines = wp->w_topfill;
3308 filler_todo = filler_lines;
3309#endif
3310
3311#ifdef LINE_ATTR
3312# ifdef FEAT_SIGNS
3313 /* If this line has a sign with line highlighting set line_attr. */
3314 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3315 if (v != 0)
3316 line_attr = sign_get_attr((int)v, TRUE);
3317# endif
3318# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3319 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003320 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003321 line_attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322# endif
3323 if (line_attr != 0)
3324 area_highlighting = TRUE;
3325#endif
3326
3327 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3328 ptr = line;
3329
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003330#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003331 if (has_spell)
3332 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003333 /* For checking first word with a capital skip white space. */
3334 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003335 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003336
Bram Moolenaar30abd282005-06-22 22:35:10 +00003337 /* To be able to spell-check over line boundaries copy the end of the
3338 * current line into nextline[]. Above the start of the next line was
3339 * copied to nextline[SPWORDLEN]. */
3340 if (nextline[SPWORDLEN] == NUL)
3341 {
3342 /* No next line or it is empty. */
3343 nextlinecol = MAXCOL;
3344 nextline_idx = 0;
3345 }
3346 else
3347 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003348 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003349 if (v < SPWORDLEN)
3350 {
3351 /* Short line, use it completely and append the start of the
3352 * next line. */
3353 nextlinecol = 0;
3354 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003355 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003356 nextline_idx = v + 1;
3357 }
3358 else
3359 {
3360 /* Long line, use only the last SPWORDLEN bytes. */
3361 nextlinecol = v - SPWORDLEN;
3362 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3363 nextline_idx = SPWORDLEN + 1;
3364 }
3365 }
3366 }
3367#endif
3368
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003369 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003371 if (lcs_space || lcs_trail)
3372 extra_check = TRUE;
3373 /* find start of trailing whitespace */
3374 if (lcs_trail)
3375 {
3376 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003377 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003378 --trailcol;
3379 trailcol += (colnr_T) (ptr - line);
3380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
3382
3383 /*
3384 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3385 * first character to be displayed.
3386 */
3387 if (wp->w_p_wrap)
3388 v = wp->w_skipcol;
3389 else
3390 v = wp->w_leftcol;
3391 if (v > 0)
3392 {
3393#ifdef FEAT_MBYTE
3394 char_u *prev_ptr = ptr;
3395#endif
3396 while (vcol < v && *ptr != NUL)
3397 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003398 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 vcol += c;
3400#ifdef FEAT_MBYTE
3401 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003403 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 }
3405
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003406 /* When:
3407 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003408 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003409 * - 'virtualedit' is set, or
3410 * - the visual mode is active,
3411 * the end of the line may be before the start of the displayed part.
3412 */
3413 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003414#ifdef FEAT_SYN_HL
3415 wp->w_p_cuc || draw_color_col ||
3416#endif
3417#ifdef FEAT_VIRTUALEDIT
3418 virtual_active() ||
3419#endif
3420 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 /* Handle a character that's not completely on the screen: Put ptr at
3426 * that character but skip the first few screen characters. */
3427 if (vcol > v)
3428 {
3429 vcol -= c;
3430#ifdef FEAT_MBYTE
3431 ptr = prev_ptr;
3432#else
3433 --ptr;
3434#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003435 /* If the character fits on the screen, don't need to skip it.
3436 * Except for a TAB. */
3437 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003438#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003439 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003440#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003441 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003442 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
3444
3445 /*
3446 * Adjust for when the inverted text is before the screen,
3447 * and when the start of the inverted text is before the screen.
3448 */
3449 if (tocol <= vcol)
3450 fromcol = 0;
3451 else if (fromcol >= 0 && fromcol < vcol)
3452 fromcol = vcol;
3453
3454#ifdef FEAT_LINEBREAK
3455 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3456 if (wp->w_p_wrap)
3457 need_showbreak = TRUE;
3458#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003459#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003460 /* When spell checking a word we need to figure out the start of the
3461 * word and if it's badly spelled or not. */
3462 if (has_spell)
3463 {
3464 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003465 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003466 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003467
3468 pos = wp->w_cursor;
3469 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003470 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003471 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003472
3473 /* spell_move_to() may call ml_get() and make "line" invalid */
3474 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3475 ptr = line + linecol;
3476
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003477 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003478 {
3479 /* no bad word found at line start, don't check until end of a
3480 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003481 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003482 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003483 }
3484 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003485 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003486 /* bad word found, use attributes until end of word */
3487 word_end = wp->w_cursor.col + len + 1;
3488
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003489 /* Turn index into actual attributes. */
3490 if (spell_hlf != HLF_COUNT)
3491 spell_attr = highlight_attr[spell_hlf];
3492 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003493 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003494
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003495# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003496 /* Need to restart syntax highlighting for this line. */
3497 if (has_syntax)
3498 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003499# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003500 }
3501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
3503
3504 /*
3505 * Correct highlighting for cursor that can't be disabled.
3506 * Avoids having to check this for each character.
3507 */
3508 if (fromcol >= 0)
3509 {
3510 if (noinvcur)
3511 {
3512 if ((colnr_T)fromcol == wp->w_virtcol)
3513 {
3514 /* highlighting starts at cursor, let it start just after the
3515 * cursor */
3516 fromcol_prev = fromcol;
3517 fromcol = -1;
3518 }
3519 else if ((colnr_T)fromcol < wp->w_virtcol)
3520 /* restart highlighting after the cursor */
3521 fromcol_prev = wp->w_virtcol;
3522 }
3523 if (fromcol >= tocol)
3524 fromcol = -1;
3525 }
3526
3527#ifdef FEAT_SEARCH_EXTRA
3528 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003529 * Handle highlighting the last used search pattern and matches.
3530 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003532 cur = wp->w_match_head;
3533 shl_flag = FALSE;
3534 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003536 if (shl_flag == FALSE)
3537 {
3538 shl = &search_hl;
3539 shl_flag = TRUE;
3540 }
3541 else
3542 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003543 shl->startcol = MAXCOL;
3544 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003546 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003547 v = (long)(ptr - line);
3548 if (cur != NULL)
3549 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003550 next_search_hl(wp, shl, lnum, (colnr_T)v,
3551 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003552
3553 /* Need to get the line again, a multi-line regexp may have made it
3554 * invalid. */
3555 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3556 ptr = line + v;
3557
3558 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003560 if (shl->lnum == lnum)
3561 shl->startcol = shl->rm.startpos[0].col;
3562 else
3563 shl->startcol = 0;
3564 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3565 - shl->rm.startpos[0].lnum)
3566 shl->endcol = shl->rm.endpos[0].col;
3567 else
3568 shl->endcol = MAXCOL;
3569 /* Highlight one character for an empty match. */
3570 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003573 if (has_mbyte && line[shl->endcol] != NUL)
3574 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3575 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003577 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003579 if ((long)shl->startcol < v) /* match at leftcol */
3580 {
3581 shl->attr_cur = shl->attr;
3582 search_attr = shl->attr;
3583 }
3584 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003586 if (shl != &search_hl && cur != NULL)
3587 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
3589#endif
3590
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003591#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003592 /* Cursor line highlighting for 'cursorline' in the current window. Not
3593 * when Visual mode is active, because it's not clear what is selected
3594 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003595 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003596 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003597 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003598 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003599 area_highlighting = TRUE;
3600 }
3601#endif
3602
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003603 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 col = 0;
3605#ifdef FEAT_RIGHTLEFT
3606 if (wp->w_p_rl)
3607 {
3608 /* Rightleft window: process the text in the normal direction, but put
3609 * it in current_ScreenLine[] from right to left. Start at the
3610 * rightmost column of the window. */
3611 col = W_WIDTH(wp) - 1;
3612 off += col;
3613 }
3614#endif
3615
3616 /*
3617 * Repeat for the whole displayed line.
3618 */
3619 for (;;)
3620 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003621#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003622 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 /* Skip this quickly when working on the text. */
3625 if (draw_state != WL_LINE)
3626 {
3627#ifdef FEAT_CMDWIN
3628 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3629 {
3630 draw_state = WL_CMDLINE;
3631 if (cmdwin_type != 0 && wp == curwin)
3632 {
3633 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003635 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003636 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
3638 }
3639#endif
3640
3641#ifdef FEAT_FOLDING
3642 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3643 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003644 int fdc = compute_foldcolumn(wp, 0);
3645
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003647 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003649 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003650 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003651 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003652 p_extra_free = alloc(12 + 1);
3653
3654 if (p_extra_free != NULL)
3655 {
3656 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3657 n_extra = fdc;
3658 p_extra_free[n_extra] = NUL;
3659 p_extra = p_extra_free;
3660 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003661 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664 }
3665#endif
3666
3667#ifdef FEAT_SIGNS
3668 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3669 {
3670 draw_state = WL_SIGN;
3671 /* Show the sign column when there are any signs in this
3672 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003673 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003675 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003677 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678# endif
3679
3680 /* Draw two cells with the sign value or blank. */
3681 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003682 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 n_extra = 2;
3684
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003685 if (row == startrow
3686#ifdef FEAT_DIFF
3687 + filler_lines && filler_todo <= 0
3688#endif
3689 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
3691 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3692 SIGN_TEXT);
3693# ifdef FEAT_SIGN_ICONS
3694 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3695 SIGN_ICON);
3696 if (gui.in_use && icon_sign != 0)
3697 {
3698 /* Use the image in this position. */
3699 c_extra = SIGN_BYTE;
3700# ifdef FEAT_NETBEANS_INTG
3701 if (buf_signcount(wp->w_buffer, lnum) > 1)
3702 c_extra = MULTISIGN_BYTE;
3703# endif
3704 char_attr = icon_sign;
3705 }
3706 else
3707# endif
3708 if (text_sign != 0)
3709 {
3710 p_extra = sign_get_text(text_sign);
3711 if (p_extra != NULL)
3712 {
3713 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003714 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716 char_attr = sign_get_attr(text_sign, FALSE);
3717 }
3718 }
3719 }
3720 }
3721#endif
3722
3723 if (draw_state == WL_NR - 1 && n_extra == 0)
3724 {
3725 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003726 /* Display the absolute or relative line number. After the
3727 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3728 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 && (row == startrow
3730#ifdef FEAT_DIFF
3731 + filler_lines
3732#endif
3733 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3734 {
3735 /* Draw the line number (empty space after wrapping). */
3736 if (row == startrow
3737#ifdef FEAT_DIFF
3738 + filler_lines
3739#endif
3740 )
3741 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003742 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003743 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003744
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003745 if (wp->w_p_nu && !wp->w_p_rnu)
3746 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003747 num = (long)lnum;
3748 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003749 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003750 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003751 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003752 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003753 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003754 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003755 num = lnum;
3756 fmt = "%-*ld ";
3757 }
3758 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003759
Bram Moolenaar700e7342013-01-30 12:31:36 +01003760 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003761 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 if (wp->w_skipcol > 0)
3763 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3764 *p_extra = '-';
3765#ifdef FEAT_RIGHTLEFT
3766 if (wp->w_p_rl) /* reverse line numbers */
3767 rl_mirror(extra);
3768#endif
3769 p_extra = extra;
3770 c_extra = NUL;
3771 }
3772 else
3773 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003774 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003775 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003776#ifdef FEAT_SYN_HL
3777 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003778 * the current line differently.
3779 * TODO: Can we use CursorLine instead of CursorLineNr
3780 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003781 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003782 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003783 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786 }
3787
Bram Moolenaar597a4222014-06-25 14:39:50 +02003788#ifdef FEAT_LINEBREAK
3789 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3790 && n_extra == 0 && *p_sbr != NUL)
3791 /* draw indent after showbreak value */
3792 draw_state = WL_BRI;
3793 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3794 /* After the showbreak, draw the breakindent */
3795 draw_state = WL_BRI - 1;
3796
3797 /* draw 'breakindent': indent wrapped text accordingly */
3798 if (draw_state == WL_BRI - 1 && n_extra == 0)
3799 {
3800 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003801 /* if need_showbreak is set, breakindent also applies */
3802 if (wp->w_p_bri && n_extra == 0
3803 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003804# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003805 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003806# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003807 )
3808 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003809 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003810# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003811 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003812 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003813 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003814# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003815 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3816 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003817 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003818# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003819 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003820# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003821 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003822 c_extra = ' ';
3823 n_extra = get_breakindent_win(wp,
3824 ml_get_buf(wp->w_buffer, lnum, FALSE));
3825 /* Correct end of highlighted area for 'breakindent',
3826 * required when 'linebreak' is also set. */
3827 if (tocol == vcol)
3828 tocol += n_extra;
3829 }
3830 }
3831#endif
3832
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3834 if (draw_state == WL_SBR - 1 && n_extra == 0)
3835 {
3836 draw_state = WL_SBR;
3837# ifdef FEAT_DIFF
3838 if (filler_todo > 0)
3839 {
3840 /* Draw "deleted" diff line(s). */
3841 if (char2cells(fill_diff) > 1)
3842 c_extra = '-';
3843 else
3844 c_extra = fill_diff;
3845# ifdef FEAT_RIGHTLEFT
3846 if (wp->w_p_rl)
3847 n_extra = col + 1;
3848 else
3849# endif
3850 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003851 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 }
3853# endif
3854# ifdef FEAT_LINEBREAK
3855 if (*p_sbr != NUL && need_showbreak)
3856 {
3857 /* Draw 'showbreak' at the start of each broken line. */
3858 p_extra = p_sbr;
3859 c_extra = NUL;
3860 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003861 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003863 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 /* Correct end of highlighted area for 'showbreak',
3865 * required when 'linebreak' is also set. */
3866 if (tocol == vcol)
3867 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003868#ifdef FEAT_SYN_HL
3869 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003870 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003871 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003872 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 }
3875# endif
3876 }
3877#endif
3878
3879 if (draw_state == WL_LINE - 1 && n_extra == 0)
3880 {
3881 draw_state = WL_LINE;
3882 if (saved_n_extra)
3883 {
3884 /* Continue item from end of wrapped line. */
3885 n_extra = saved_n_extra;
3886 c_extra = saved_c_extra;
3887 p_extra = saved_p_extra;
3888 char_attr = saved_char_attr;
3889 }
3890 else
3891 char_attr = 0;
3892 }
3893 }
3894
3895 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003896 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003897 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898#ifdef FEAT_DIFF
3899 && filler_todo <= 0
3900#endif
3901 )
3902 {
3903 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3904 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003905 /* Pretend we have finished updating the window. Except when
3906 * 'cursorcolumn' is set. */
3907#ifdef FEAT_SYN_HL
3908 if (wp->w_p_cuc)
3909 row = wp->w_cline_row + wp->w_cline_height;
3910 else
3911#endif
3912 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 break;
3914 }
3915
3916 if (draw_state == WL_LINE && area_highlighting)
3917 {
3918 /* handle Visual or match highlighting in this line */
3919 if (vcol == fromcol
3920#ifdef FEAT_MBYTE
3921 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3922 && (*mb_ptr2cells)(ptr) > 1)
3923#endif
3924 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003925 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 && vcol < tocol))
3927 area_attr = attr; /* start highlighting */
3928 else if (area_attr != 0
3929 && (vcol == tocol
3930 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932
3933#ifdef FEAT_SEARCH_EXTRA
3934 if (!n_extra)
3935 {
3936 /*
3937 * Check for start/end of search pattern match.
3938 * After end, check for start/end of next match.
3939 * When another match, have to check for start again.
3940 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003941 * Do this for 'search_hl' and the match list (ordered by
3942 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003944 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003945 cur = wp->w_match_head;
3946 shl_flag = FALSE;
3947 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003949 if (shl_flag == FALSE
3950 && ((cur != NULL
3951 && cur->priority > SEARCH_HL_PRIORITY)
3952 || cur == NULL))
3953 {
3954 shl = &search_hl;
3955 shl_flag = TRUE;
3956 }
3957 else
3958 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003959 if (cur != NULL)
3960 cur->pos.cur = 0;
3961 pos_inprogress = TRUE;
3962 while (shl->rm.regprog != NULL
3963 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003965 if (shl->startcol != MAXCOL
3966 && v >= (long)shl->startcol
3967 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003969#ifdef FEAT_MBYTE
3970 int tmp_col = v + MB_PTR2LEN(ptr);
3971
3972 if (shl->endcol < tmp_col)
3973 shl->endcol = tmp_col;
3974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003976#ifdef FEAT_CONCEAL
3977 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3978 == cur->hlg_id)
3979 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02003980 has_match_conc =
3981 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003982 match_conc = cur->conceal_char;
3983 }
3984 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02003985 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003988 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
3990 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003991 next_search_hl(wp, shl, lnum, (colnr_T)v,
3992 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003993 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003994 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
3996 /* Need to get the line again, a multi-line regexp
3997 * may have made it invalid. */
3998 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3999 ptr = line + v;
4000
4001 if (shl->lnum == lnum)
4002 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004003 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004005 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004007 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004009 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
4011 /* highlight empty match, try again after
4012 * it */
4013#ifdef FEAT_MBYTE
4014 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004015 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004016 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 else
4018#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004019 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021
4022 /* Loop to check if the match starts at the
4023 * current position */
4024 continue;
4025 }
4026 }
4027 break;
4028 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004029 if (shl != &search_hl && cur != NULL)
4030 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004032
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004033 /* Use attributes from match with highest priority among
4034 * 'search_hl' and the match list. */
4035 search_attr = search_hl.attr_cur;
4036 cur = wp->w_match_head;
4037 shl_flag = FALSE;
4038 while (cur != NULL || shl_flag == FALSE)
4039 {
4040 if (shl_flag == FALSE
4041 && ((cur != NULL
4042 && cur->priority > SEARCH_HL_PRIORITY)
4043 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004044 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004045 shl = &search_hl;
4046 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004047 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004048 else
4049 shl = &cur->hl;
4050 if (shl->attr_cur != 0)
4051 search_attr = shl->attr_cur;
4052 if (shl != &search_hl && cur != NULL)
4053 cur = cur->next;
4054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 }
4056#endif
4057
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004059 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004061 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4062 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004064 if (diff_hlf == HLF_TXD && ptr - line > change_end
4065 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004067 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004068 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004069 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004072
4073 /* Decide which of the highlight attributes to use. */
4074 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004075#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004076 if (area_attr != 0)
4077 char_attr = hl_combine_attr(line_attr, area_attr);
4078 else if (search_attr != 0)
4079 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004080 /* Use line_attr when not in the Visual or 'incsearch' area
4081 * (area_attr may be 0 when "noinvcur" is set). */
4082 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004083 || vcol < fromcol || vcol_prev < fromcol_prev
4084 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004085 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004086#else
4087 if (area_attr != 0)
4088 char_attr = area_attr;
4089 else if (search_attr != 0)
4090 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004091#endif
4092 else
4093 {
4094 attr_pri = FALSE;
4095#ifdef FEAT_SYN_HL
4096 if (has_syntax)
4097 char_attr = syntax_attr;
4098 else
4099#endif
4100 char_attr = 0;
4101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103
4104 /*
4105 * Get the next character to put on the screen.
4106 */
4107 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004108 * The "p_extra" points to the extra stuff that is inserted to
4109 * represent special characters (non-printable stuff) and other
4110 * things. When all characters are the same, c_extra is used.
4111 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4112 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4114 */
4115 if (n_extra > 0)
4116 {
4117 if (c_extra != NUL)
4118 {
4119 c = c_extra;
4120#ifdef FEAT_MBYTE
4121 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4122 if (enc_utf8 && (*mb_char2len)(c) > 1)
4123 {
4124 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004125 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004126 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128 else
4129 mb_utf8 = FALSE;
4130#endif
4131 }
4132 else
4133 {
4134 c = *p_extra;
4135#ifdef FEAT_MBYTE
4136 if (has_mbyte)
4137 {
4138 mb_c = c;
4139 if (enc_utf8)
4140 {
4141 /* If the UTF-8 character is more than one byte:
4142 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004143 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 mb_utf8 = FALSE;
4145 if (mb_l > n_extra)
4146 mb_l = 1;
4147 else if (mb_l > 1)
4148 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004149 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004151 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 }
4153 }
4154 else
4155 {
4156 /* if this is a DBCS character, put it in "mb_c" */
4157 mb_l = MB_BYTE2LEN(c);
4158 if (mb_l >= n_extra)
4159 mb_l = 1;
4160 else if (mb_l > 1)
4161 mb_c = (c << 8) + p_extra[1];
4162 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004163 if (mb_l == 0) /* at the NUL at end-of-line */
4164 mb_l = 1;
4165
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 /* If a double-width char doesn't fit display a '>' in the
4167 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004168 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169# ifdef FEAT_RIGHTLEFT
4170 wp->w_p_rl ? (col <= 0) :
4171# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004172 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 && (*mb_char2cells)(mb_c) == 2)
4174 {
4175 c = '>';
4176 mb_c = c;
4177 mb_l = 1;
4178 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004179 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 /* put the pointer back to output the double-width
4181 * character at the start of the next line. */
4182 ++n_extra;
4183 --p_extra;
4184 }
4185 else
4186 {
4187 n_extra -= mb_l - 1;
4188 p_extra += mb_l - 1;
4189 }
4190 }
4191#endif
4192 ++p_extra;
4193 }
4194 --n_extra;
4195 }
4196 else
4197 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004198#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004199 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004200#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004201
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004202 if (p_extra_free != NULL)
4203 {
4204 vim_free(p_extra_free);
4205 p_extra_free = NULL;
4206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 /*
4208 * Get a character from the line itself.
4209 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004210 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004211#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004212 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214#ifdef FEAT_MBYTE
4215 if (has_mbyte)
4216 {
4217 mb_c = c;
4218 if (enc_utf8)
4219 {
4220 /* If the UTF-8 character is more than one byte: Decode it
4221 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004222 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 mb_utf8 = FALSE;
4224 if (mb_l > 1)
4225 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004226 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 /* Overlong encoded ASCII or ASCII with composing char
4228 * is displayed normally, except a NUL. */
4229 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004230 {
4231 c = mb_c;
4232# ifdef FEAT_LINEBREAK
4233 c0 = mb_c;
4234# endif
4235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004237
4238 /* At start of the line we can have a composing char.
4239 * Draw it as a space with a composing char. */
4240 if (utf_iscomposing(mb_c))
4241 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004242 int i;
4243
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004244 for (i = Screen_mco - 1; i > 0; --i)
4245 u8cc[i] = u8cc[i - 1];
4246 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004247 mb_c = ' ';
4248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250
4251 if ((mb_l == 1 && c >= 0x80)
4252 || (mb_l >= 1 && mb_c == 0)
4253 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004254# ifdef UNICODE16
4255 || mb_c >= 0x10000
4256# endif
4257 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
4259 /*
4260 * Illegal UTF-8 byte: display as <xx>.
4261 * Non-BMP character : display as ? or fullwidth ?.
4262 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004263# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 {
4267 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004268# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 if (wp->w_p_rl) /* reverse */
4270 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004273# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 else if (utf_char2cells(mb_c) != 2)
4275 STRCPY(extra, "?");
4276 else
4277 /* 0xff1f in UTF-8: full-width '?' */
4278 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
4281 p_extra = extra;
4282 c = *p_extra;
4283 mb_c = mb_ptr2char_adv(&p_extra);
4284 mb_utf8 = (c >= 0x80);
4285 n_extra = (int)STRLEN(p_extra);
4286 c_extra = NUL;
4287 if (area_attr == 0 && search_attr == 0)
4288 {
4289 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004290 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 saved_attr2 = char_attr; /* save current attr */
4292 }
4293 }
4294 else if (mb_l == 0) /* at the NUL at end-of-line */
4295 mb_l = 1;
4296#ifdef FEAT_ARABIC
4297 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4298 {
4299 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004300 int pc, pc1, nc;
4301 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
4303 /* The idea of what is the previous and next
4304 * character depends on 'rightleft'. */
4305 if (wp->w_p_rl)
4306 {
4307 pc = prev_c;
4308 pc1 = prev_c1;
4309 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004310 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
4312 else
4313 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004314 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004316 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
4318 prev_c = mb_c;
4319
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004320 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322 else
4323 prev_c = mb_c;
4324#endif
4325 }
4326 else /* enc_dbcs */
4327 {
4328 mb_l = MB_BYTE2LEN(c);
4329 if (mb_l == 0) /* at the NUL at end-of-line */
4330 mb_l = 1;
4331 else if (mb_l > 1)
4332 {
4333 /* We assume a second byte below 32 is illegal.
4334 * Hopefully this is OK for all double-byte encodings!
4335 */
4336 if (ptr[1] >= 32)
4337 mb_c = (c << 8) + ptr[1];
4338 else
4339 {
4340 if (ptr[1] == NUL)
4341 {
4342 /* head byte at end of line */
4343 mb_l = 1;
4344 transchar_nonprint(extra, c);
4345 }
4346 else
4347 {
4348 /* illegal tail byte */
4349 mb_l = 2;
4350 STRCPY(extra, "XX");
4351 }
4352 p_extra = extra;
4353 n_extra = (int)STRLEN(extra) - 1;
4354 c_extra = NUL;
4355 c = *p_extra++;
4356 if (area_attr == 0 && search_attr == 0)
4357 {
4358 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004359 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 saved_attr2 = char_attr; /* save current attr */
4361 }
4362 mb_c = c;
4363 }
4364 }
4365 }
4366 /* If a double-width char doesn't fit display a '>' in the
4367 * last column; the character is displayed at the start of the
4368 * next line. */
4369 if ((
4370# ifdef FEAT_RIGHTLEFT
4371 wp->w_p_rl ? (col <= 0) :
4372# endif
4373 (col >= W_WIDTH(wp) - 1))
4374 && (*mb_char2cells)(mb_c) == 2)
4375 {
4376 c = '>';
4377 mb_c = c;
4378 mb_utf8 = FALSE;
4379 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004380 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 /* Put pointer back so that the character will be
4382 * displayed at the start of the next line. */
4383 --ptr;
4384 }
4385 else if (*ptr != NUL)
4386 ptr += mb_l - 1;
4387
4388 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004389 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004390 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004391 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004394 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 c = ' ';
4396 if (area_attr == 0 && search_attr == 0)
4397 {
4398 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004399 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 saved_attr2 = char_attr; /* save current attr */
4401 }
4402 mb_c = c;
4403 mb_utf8 = FALSE;
4404 mb_l = 1;
4405 }
4406
4407 }
4408#endif
4409 ++ptr;
4410
4411 if (extra_check)
4412 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004413#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004414 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004415#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004416
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004417#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 /* Get syntax attribute, unless still at the start of the line
4419 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004420 v = (long)(ptr - line);
4421 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 {
4423 /* Get the syntax attribute for the character. If there
4424 * is an error, disable syntax highlighting. */
4425 save_did_emsg = did_emsg;
4426 did_emsg = FALSE;
4427
Bram Moolenaar217ad922005-03-20 22:37:15 +00004428 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004429# ifdef FEAT_SPELL
4430 has_spell ? &can_spell :
4431# endif
4432 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
4434 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004435 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004436 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004437 has_syntax = FALSE;
4438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 else
4440 did_emsg = save_did_emsg;
4441
4442 /* Need to get the line again, a multi-line regexp may
4443 * have made it invalid. */
4444 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4445 ptr = line + v;
4446
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004447 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004449 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004450 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004451# ifdef FEAT_CONCEAL
4452 /* no concealing past the end of the line, it interferes
4453 * with line highlighting */
4454 if (c == NUL)
4455 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004456 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004457 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004460#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004461
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004462#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004463 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004464 * Only do this when there is no syntax highlighting, the
4465 * @Spell cluster is not used or the current syntax item
4466 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004467 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004468 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004469 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004470# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004471 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004472 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004473# endif
4474 if (c != 0 && (
4475# ifdef FEAT_SYN_HL
4476 !has_syntax ||
4477# endif
4478 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004479 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004480 char_u *prev_ptr, *p;
4481 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004482 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004483# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004484 if (has_mbyte)
4485 {
4486 prev_ptr = ptr - mb_l;
4487 v -= mb_l - 1;
4488 }
4489 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004490# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004491 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004492
4493 /* Use nextline[] if possible, it has the start of the
4494 * next line concatenated. */
4495 if ((prev_ptr - line) - nextlinecol >= 0)
4496 p = nextline + (prev_ptr - line) - nextlinecol;
4497 else
4498 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004499 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004500 len = spell_check(wp, p, &spell_hlf, &cap_col,
4501 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004502 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004503
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004504 /* In Insert mode only highlight a word that
4505 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004506 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004507 && (State & INSERT) != 0
4508 && wp->w_cursor.lnum == lnum
4509 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004510 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004511 && wp->w_cursor.col < (colnr_T)word_end)
4512 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004513 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004514 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004515 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004516
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004517 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004518 && (p - nextline) + len > nextline_idx)
4519 {
4520 /* Remember that the good word continues at the
4521 * start of the next line. */
4522 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004523 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004524 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004525
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004526 /* Turn index into actual attributes. */
4527 if (spell_hlf != HLF_COUNT)
4528 spell_attr = highlight_attr[spell_hlf];
4529
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004530 if (cap_col > 0)
4531 {
4532 if (p != prev_ptr
4533 && (p - nextline) + cap_col >= nextline_idx)
4534 {
4535 /* Remember that the word in the next line
4536 * must start with a capital. */
4537 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004538 cap_col = (int)((p - nextline) + cap_col
4539 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004540 }
4541 else
4542 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004543 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004544 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004545 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004546 }
4547 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004548 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004549 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004550 char_attr = hl_combine_attr(char_attr, spell_attr);
4551 else
4552 char_attr = hl_combine_attr(spell_attr, char_attr);
4553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554#endif
4555#ifdef FEAT_LINEBREAK
4556 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004557 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004559 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004560 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004562# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004563 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004564# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004565 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004567 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004569 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004570
Bram Moolenaar597a4222014-06-25 14:39:50 +02004571 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004572 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004573 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004574 if (c == TAB && n_extra + col > W_WIDTH(wp))
4575 n_extra = (int)wp->w_buffer->b_p_ts
4576 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4577
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004578# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004579 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004580# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004582# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004583 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004584 {
4585#ifdef FEAT_CONCEAL
4586 if (c == TAB)
4587 /* See "Tab alignment" below. */
4588 FIX_FOR_BOGUSCOLS;
4589#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004590 if (!wp->w_p_list)
4591 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594#endif
4595
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004596 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4597 */
4598 if (wp->w_p_list
4599 && (((c == 160
4600#ifdef FEAT_MBYTE
4601 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4602#endif
4603 ) && lcs_nbsp)
4604 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4605 {
4606 c = (c == ' ') ? lcs_space : lcs_nbsp;
4607 if (area_attr == 0 && search_attr == 0)
4608 {
4609 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004610 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004611 saved_attr2 = char_attr; /* save current attr */
4612 }
4613#ifdef FEAT_MBYTE
4614 mb_c = c;
4615 if (enc_utf8 && (*mb_char2len)(c) > 1)
4616 {
4617 mb_utf8 = TRUE;
4618 u8cc[0] = 0;
4619 c = 0xc0;
4620 }
4621 else
4622 mb_utf8 = FALSE;
4623#endif
4624 }
4625
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4627 {
4628 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004629 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
4631 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004632 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 saved_attr2 = char_attr; /* save current attr */
4634 }
4635#ifdef FEAT_MBYTE
4636 mb_c = c;
4637 if (enc_utf8 && (*mb_char2len)(c) > 1)
4638 {
4639 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004640 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004641 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 }
4643 else
4644 mb_utf8 = FALSE;
4645#endif
4646 }
4647 }
4648
4649 /*
4650 * Handling of non-printable characters.
4651 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004652 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
4654 /*
4655 * when getting a character from the file, we may have to
4656 * turn it into something else on the way to putting it
4657 * into "ScreenLines".
4658 */
4659 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4660 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004661 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004662 long vcol_adjusted = vcol; /* removed showbreak length */
4663#ifdef FEAT_LINEBREAK
4664 /* only adjust the tab_len, when at the first column
4665 * after the showbreak value was drawn */
4666 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4667 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004670 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004671 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4672
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004673#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004674 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004675#endif
4676 /* tab amount depends on current column */
4677 n_extra = tab_len;
4678#ifdef FEAT_LINEBREAK
4679 else
4680 {
4681 char_u *p;
4682 int len = n_extra;
4683 int i;
4684 int saved_nextra = n_extra;
4685
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004686#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004687 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004688 /* there are characters to conceal */
4689 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004690 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4691 */
4692 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4693 && n_extra > tab_len)
4694 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004695#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004696
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004697 /* if n_extra > 0, it gives the number of chars, to
4698 * use for a tab, else we need to calculate the width
4699 * for a tab */
4700#ifdef FEAT_MBYTE
4701 len = (tab_len * mb_char2len(lcs_tab2));
4702 if (n_extra > 0)
4703 len += n_extra - tab_len;
4704#endif
4705 c = lcs_tab1;
4706 p = alloc((unsigned)(len + 1));
4707 vim_memset(p, ' ', len);
4708 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004709 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004710 p_extra_free = p;
4711 for (i = 0; i < tab_len; i++)
4712 {
4713#ifdef FEAT_MBYTE
4714 mb_char2bytes(lcs_tab2, p);
4715 p += mb_char2len(lcs_tab2);
4716 n_extra += mb_char2len(lcs_tab2)
4717 - (saved_nextra > 0 ? 1 : 0);
4718#else
4719 p[i] = lcs_tab2;
4720#endif
4721 }
4722 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004723#ifdef FEAT_CONCEAL
4724 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4725 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004726 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004727 n_extra -= vcol_off;
4728#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004729 }
4730#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004731#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004732 {
4733 int vc_saved = vcol_off;
4734
4735 /* Tab alignment should be identical regardless of
4736 * 'conceallevel' value. So tab compensates of all
4737 * previous concealed characters, and thus resets
4738 * vcol_off and boguscols accumulated so far in the
4739 * line. Note that the tab can be longer than
4740 * 'tabstop' when there are concealed characters. */
4741 FIX_FOR_BOGUSCOLS;
4742
4743 /* Make sure, the highlighting for the tab char will be
4744 * correctly set further below (effectively reverts the
4745 * FIX_FOR_BOGSUCOLS macro */
4746 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004747 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004748 tab_len += vc_saved;
4749 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751#ifdef FEAT_MBYTE
4752 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4753#endif
4754 if (wp->w_p_list)
4755 {
4756 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004757#ifdef FEAT_LINEBREAK
4758 if (wp->w_p_lbr)
4759 c_extra = NUL; /* using p_extra from above */
4760 else
4761#endif
4762 c_extra = lcs_tab2;
4763 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004764 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 saved_attr2 = char_attr; /* save current attr */
4766#ifdef FEAT_MBYTE
4767 mb_c = c;
4768 if (enc_utf8 && (*mb_char2len)(c) > 1)
4769 {
4770 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004771 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004772 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
4774#endif
4775 }
4776 else
4777 {
4778 c_extra = ' ';
4779 c = ' ';
4780 }
4781 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004782 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004783 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004784 || ((fromcol >= 0 || fromcol_prev >= 0)
4785 && tocol > vcol
4786 && VIsual_mode != Ctrl_V
4787 && (
4788# ifdef FEAT_RIGHTLEFT
4789 wp->w_p_rl ? (col >= 0) :
4790# endif
4791 (col < W_WIDTH(wp)))
4792 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004793 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004794 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004795 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004797 /* Display a '$' after the line or highlight an extra
4798 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4800 /* For a diff line the highlighting continues after the
4801 * "$". */
4802 if (
4803# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004804 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805# ifdef LINE_ATTR
4806 &&
4807# endif
4808# endif
4809# ifdef LINE_ATTR
4810 line_attr == 0
4811# endif
4812 )
4813#endif
4814 {
4815#ifdef FEAT_VIRTUALEDIT
4816 /* In virtualedit, visual selections may extend
4817 * beyond end of line. */
4818 if (area_highlighting && virtual_active()
4819 && tocol != MAXCOL && vcol < tocol)
4820 n_extra = 0;
4821 else
4822#endif
4823 {
4824 p_extra = at_end_str;
4825 n_extra = 1;
4826 c_extra = NUL;
4827 }
4828 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004829 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004830 c = lcs_eol;
4831 else
4832 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 lcs_eol_one = -1;
4834 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004835 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004837 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 n_attr = 1;
4839 }
4840#ifdef FEAT_MBYTE
4841 mb_c = c;
4842 if (enc_utf8 && (*mb_char2len)(c) > 1)
4843 {
4844 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004845 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004846 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848 else
4849 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4850#endif
4851 }
4852 else if (c != NUL)
4853 {
4854 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004855 if (n_extra == 0)
4856 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857#ifdef FEAT_RIGHTLEFT
4858 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4859 rl_mirror(p_extra); /* reverse "<12>" */
4860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004862#ifdef FEAT_LINEBREAK
4863 if (wp->w_p_lbr)
4864 {
4865 char_u *p;
4866
4867 c = *p_extra;
4868 p = alloc((unsigned)n_extra + 1);
4869 vim_memset(p, ' ', n_extra);
4870 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4871 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004872 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004873 p_extra_free = p_extra = p;
4874 }
4875 else
4876#endif
4877 {
4878 n_extra = byte2cells(c) - 1;
4879 c = *p_extra++;
4880 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004881 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
4883 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004884 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 saved_attr2 = char_attr; /* save current attr */
4886 }
4887#ifdef FEAT_MBYTE
4888 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4889#endif
4890 }
4891#ifdef FEAT_VIRTUALEDIT
4892 else if (VIsual_active
4893 && (VIsual_mode == Ctrl_V
4894 || VIsual_mode == 'v')
4895 && virtual_active()
4896 && tocol != MAXCOL
4897 && vcol < tocol
4898 && (
4899# ifdef FEAT_RIGHTLEFT
4900 wp->w_p_rl ? (col >= 0) :
4901# endif
4902 (col < W_WIDTH(wp))))
4903 {
4904 c = ' ';
4905 --ptr; /* put it back at the NUL */
4906 }
4907#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004908#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 else if ((
4910# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004911 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 ) && (
4915# ifdef FEAT_RIGHTLEFT
4916 wp->w_p_rl ? (col >= 0) :
4917# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004918 (col
4919# ifdef FEAT_CONCEAL
4920 - boguscols
4921# endif
4922 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
4924 /* Highlight until the right side of the window */
4925 c = ' ';
4926 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004927
4928 /* Remember we do the char for line highlighting. */
4929 ++did_line_attr;
4930
4931 /* don't do search HL for the rest of the line */
4932 if (line_attr != 0 && char_attr == search_attr && col > 0)
4933 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934# ifdef FEAT_DIFF
4935 if (diff_hlf == HLF_TXD)
4936 {
4937 diff_hlf = HLF_CHD;
4938 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004939 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004940 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004941 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4942 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004943 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02004944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946# endif
4947 }
4948#endif
4949 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004950
4951#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004952 if ( wp->w_p_cole > 0
4953 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004954 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02004955 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004956 && !(lnum_in_visual_area
4957 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004958 {
4959 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02004960 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004961 && (syn_get_sub_char() != NUL || match_conc
4962 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004963 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004964 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004965 /* First time at this concealed item: display one
4966 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004967 if (match_conc)
4968 c = match_conc;
4969 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004970 c = syn_get_sub_char();
4971 else if (lcs_conceal != NUL)
4972 c = lcs_conceal;
4973 else
4974 c = ' ';
4975
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004976 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004977
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004978 if (n_extra > 0)
4979 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004980 vcol += n_extra;
4981 if (wp->w_p_wrap && n_extra > 0)
4982 {
4983# ifdef FEAT_RIGHTLEFT
4984 if (wp->w_p_rl)
4985 {
4986 col -= n_extra;
4987 boguscols -= n_extra;
4988 }
4989 else
4990# endif
4991 {
4992 boguscols += n_extra;
4993 col += n_extra;
4994 }
4995 }
4996 n_extra = 0;
4997 n_attr = 0;
4998 }
4999 else if (n_skip == 0)
5000 {
5001 is_concealing = TRUE;
5002 n_skip = 1;
5003 }
5004# ifdef FEAT_MBYTE
5005 mb_c = c;
5006 if (enc_utf8 && (*mb_char2len)(c) > 1)
5007 {
5008 mb_utf8 = TRUE;
5009 u8cc[0] = 0;
5010 c = 0xc0;
5011 }
5012 else
5013 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5014# endif
5015 }
5016 else
5017 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005018 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005019 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005020 }
5021#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005024#ifdef FEAT_CONCEAL
5025 /* In the cursor line and we may be concealing characters: correct
5026 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005027 if (!did_wcol && draw_state == WL_LINE
5028 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005029 && conceal_cursor_line(wp)
5030 && (int)wp->w_virtcol <= vcol + n_skip)
5031 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005032# ifdef FEAT_RIGHTLEFT
5033 if (wp->w_p_rl)
5034 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5035 else
5036# endif
5037 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005038 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005039 did_wcol = TRUE;
5040 }
5041#endif
5042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 /* Don't override visual selection highlighting. */
5044 if (n_attr > 0
5045 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005046 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 char_attr = extra_attr;
5048
Bram Moolenaar81695252004-12-29 20:58:21 +00005049#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 /* XIM don't send preedit_start and preedit_end, but they send
5051 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5052 * im_is_preediting() here. */
5053 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005054 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 && (State & INSERT)
5056 && !p_imdisable
5057 && im_is_preediting()
5058 && draw_state == WL_LINE)
5059 {
5060 colnr_T tcol;
5061
5062 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005063 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 else
5065 tcol = preedit_end_col;
5066 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5067 {
5068 if (feedback_old_attr < 0)
5069 {
5070 feedback_col = 0;
5071 feedback_old_attr = char_attr;
5072 }
5073 char_attr = im_get_feedback_attr(feedback_col);
5074 if (char_attr < 0)
5075 char_attr = feedback_old_attr;
5076 feedback_col++;
5077 }
5078 else if (feedback_old_attr >= 0)
5079 {
5080 char_attr = feedback_old_attr;
5081 feedback_old_attr = -1;
5082 feedback_col = 0;
5083 }
5084 }
5085#endif
5086 /*
5087 * Handle the case where we are in column 0 but not on the first
5088 * character of the line and the user wants us to show us a
5089 * special character (via 'listchars' option "precedes:<char>".
5090 */
5091 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005092 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5094#ifdef FEAT_DIFF
5095 && filler_todo <= 0
5096#endif
5097 && draw_state > WL_NR
5098 && c != NUL)
5099 {
5100 c = lcs_prec;
5101 lcs_prec_todo = NUL;
5102#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005103 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5104 {
5105 /* Double-width character being overwritten by the "precedes"
5106 * character, need to fill up half the character. */
5107 c_extra = MB_FILLER_CHAR;
5108 n_extra = 1;
5109 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005110 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 mb_c = c;
5113 if (enc_utf8 && (*mb_char2len)(c) > 1)
5114 {
5115 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005116 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005117 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 else
5120 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5121#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005122 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
5124 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005125 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 n_attr3 = 1;
5127 }
5128 }
5129
5130 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005131 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005133 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005134#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005135 || did_line_attr == 1
5136#endif
5137 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005139#ifdef FEAT_SEARCH_EXTRA
5140 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005141
5142 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005143 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005144 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005145#endif
5146
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005147 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 * highlight match at end of line. If it's beyond the last
5149 * char on the screen, just overwrite that one (tricky!) Not
5150 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005151#ifdef FEAT_SEARCH_EXTRA
5152 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005153 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005154 prevcol_hl_flag = TRUE;
5155 else
5156 {
5157 cur = wp->w_match_head;
5158 while (cur != NULL)
5159 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005160 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005161 {
5162 prevcol_hl_flag = TRUE;
5163 break;
5164 }
5165 cur = cur->next;
5166 }
5167 }
5168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005170 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005171 && (VIsual_mode != Ctrl_V
5172 || lnum == VIsual.lnum
5173 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005174 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175#ifdef FEAT_SEARCH_EXTRA
5176 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005177 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005178# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005179 && did_line_attr <= 1
5180# endif
5181 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182#endif
5183 ))
5184 {
5185 int n = 0;
5186
5187#ifdef FEAT_RIGHTLEFT
5188 if (wp->w_p_rl)
5189 {
5190 if (col < 0)
5191 n = 1;
5192 }
5193 else
5194#endif
5195 {
5196 if (col >= W_WIDTH(wp))
5197 n = -1;
5198 }
5199 if (n != 0)
5200 {
5201 /* At the window boundary, highlight the last character
5202 * instead (better than nothing). */
5203 off += n;
5204 col += n;
5205 }
5206 else
5207 {
5208 /* Add a blank character to highlight. */
5209 ScreenLines[off] = ' ';
5210#ifdef FEAT_MBYTE
5211 if (enc_utf8)
5212 ScreenLinesUC[off] = 0;
5213#endif
5214 }
5215#ifdef FEAT_SEARCH_EXTRA
5216 if (area_attr == 0)
5217 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005218 /* Use attributes from match with highest priority among
5219 * 'search_hl' and the match list. */
5220 char_attr = search_hl.attr;
5221 cur = wp->w_match_head;
5222 shl_flag = FALSE;
5223 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005224 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005225 if (shl_flag == FALSE
5226 && ((cur != NULL
5227 && cur->priority > SEARCH_HL_PRIORITY)
5228 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005229 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005230 shl = &search_hl;
5231 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005232 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005233 else
5234 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005235 if ((ptr - line) - 1 == (long)shl->startcol
5236 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005237 char_attr = shl->attr;
5238 if (shl != &search_hl && cur != NULL)
5239 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
5242#endif
5243 ScreenAttrs[off] = char_attr;
5244#ifdef FEAT_RIGHTLEFT
5245 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005248 --off;
5249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 else
5251#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005254 ++off;
5255 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005256 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005257#ifdef FEAT_SYN_HL
5258 eol_hl_off = 1;
5259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
Bram Moolenaar91170f82006-05-05 21:15:17 +00005263 /*
5264 * At end of the text line.
5265 */
5266 if (c == NUL)
5267 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005268#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005269 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5270 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005271 {
5272 /* highlight last char after line */
5273 --col;
5274 --off;
5275 --vcol;
5276 }
5277
Bram Moolenaar1a384422010-07-14 19:53:30 +02005278 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005279 if (wp->w_p_wrap)
5280 v = wp->w_skipcol;
5281 else
5282 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005283
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005284 /* check if line ends before left margin */
5285 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005286 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005287#ifdef FEAT_CONCEAL
5288 /* Get rid of the boguscols now, we want to draw until the right
5289 * edge for 'cursorcolumn'. */
5290 col -= boguscols;
5291 boguscols = 0;
5292#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005293
5294 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005295 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005296
5297 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005298 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5299 && (int)wp->w_virtcol <
5300 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005301 && lnum != wp->w_cursor.lnum)
5302 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005303# ifdef FEAT_RIGHTLEFT
5304 && !wp->w_p_rl
5305# endif
5306 )
5307 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005308 int rightmost_vcol = 0;
5309 int i;
5310
5311 if (wp->w_p_cuc)
5312 rightmost_vcol = wp->w_virtcol;
5313 if (draw_color_col)
5314 /* determine rightmost colorcolumn to possibly draw */
5315 for (i = 0; color_cols[i] >= 0; ++i)
5316 if (rightmost_vcol < color_cols[i])
5317 rightmost_vcol = color_cols[i];
5318
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005319 while (col < W_WIDTH(wp))
5320 {
5321 ScreenLines[off] = ' ';
5322#ifdef FEAT_MBYTE
5323 if (enc_utf8)
5324 ScreenLinesUC[off] = 0;
5325#endif
5326 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005327 if (draw_color_col)
5328 draw_color_col = advance_color_col(VCOL_HLC,
5329 &color_cols);
5330
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005331 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005332 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005333 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005334 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005335 else
5336 ScreenAttrs[off++] = 0;
5337
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005338 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005339 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005340
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005341 ++vcol;
5342 }
5343 }
5344#endif
5345
Bram Moolenaar860cae12010-06-05 23:22:07 +02005346 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5347 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 row++;
5349
5350 /*
5351 * Update w_cline_height and w_cline_folded if the cursor line was
5352 * updated (saves a call to plines() later).
5353 */
5354 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5355 {
5356 curwin->w_cline_row = startrow;
5357 curwin->w_cline_height = row - startrow;
5358#ifdef FEAT_FOLDING
5359 curwin->w_cline_folded = FALSE;
5360#endif
5361 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5362 }
5363
5364 break;
5365 }
5366
5367 /* line continues beyond line end */
5368 if (lcs_ext
5369 && !wp->w_p_wrap
5370#ifdef FEAT_DIFF
5371 && filler_todo <= 0
5372#endif
5373 && (
5374#ifdef FEAT_RIGHTLEFT
5375 wp->w_p_rl ? col == 0 :
5376#endif
5377 col == W_WIDTH(wp) - 1)
5378 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005379 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5381 {
5382 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005383 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384#ifdef FEAT_MBYTE
5385 mb_c = c;
5386 if (enc_utf8 && (*mb_char2len)(c) > 1)
5387 {
5388 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005389 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005390 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 }
5392 else
5393 mb_utf8 = FALSE;
5394#endif
5395 }
5396
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005397#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005398 /* advance to the next 'colorcolumn' */
5399 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005400 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005401
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005402 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005403 * highlight the cursor position itself.
5404 * Also highlight the 'colorcolumn' if it is different than
5405 * 'cursorcolumn' */
5406 vcol_save_attr = -1;
5407 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005408 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005409 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005410 && lnum != wp->w_cursor.lnum)
5411 {
5412 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005413 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005414 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005415 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005416 {
5417 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005418 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005419 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005420 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005421#endif
5422
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 /*
5424 * Store character to be displayed.
5425 * Skip characters that are left of the screen for 'nowrap'.
5426 */
5427 vcol_prev = vcol;
5428 if (draw_state < WL_LINE || n_skip <= 0)
5429 {
5430 /*
5431 * Store the character.
5432 */
5433#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5434 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5435 {
5436 /* A double-wide character is: put first halve in left cell. */
5437 --off;
5438 --col;
5439 }
5440#endif
5441 ScreenLines[off] = c;
5442#ifdef FEAT_MBYTE
5443 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005444 {
5445 if ((mb_c & 0xff00) == 0x8e00)
5446 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 else if (enc_utf8)
5450 {
5451 if (mb_utf8)
5452 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005453 int i;
5454
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005456 if ((c & 0xff) == 0)
5457 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005458 for (i = 0; i < Screen_mco; ++i)
5459 {
5460 ScreenLinesC[i][off] = u8cc[i];
5461 if (u8cc[i] == 0)
5462 break;
5463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
5465 else
5466 ScreenLinesUC[off] = 0;
5467 }
5468 if (multi_attr)
5469 {
5470 ScreenAttrs[off] = multi_attr;
5471 multi_attr = 0;
5472 }
5473 else
5474#endif
5475 ScreenAttrs[off] = char_attr;
5476
5477#ifdef FEAT_MBYTE
5478 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5479 {
5480 /* Need to fill two screen columns. */
5481 ++off;
5482 ++col;
5483 if (enc_utf8)
5484 /* UTF-8: Put a 0 in the second screen char. */
5485 ScreenLines[off] = 0;
5486 else
5487 /* DBCS: Put second byte in the second screen char. */
5488 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005489 if (draw_state > WL_NR
5490#ifdef FEAT_DIFF
5491 && filler_todo <= 0
5492#endif
5493 )
5494 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 /* When "tocol" is halfway a character, set it to the end of
5496 * the character, otherwise highlighting won't stop. */
5497 if (tocol == vcol)
5498 ++tocol;
5499#ifdef FEAT_RIGHTLEFT
5500 if (wp->w_p_rl)
5501 {
5502 /* now it's time to backup one cell */
5503 --off;
5504 --col;
5505 }
5506#endif
5507 }
5508#endif
5509#ifdef FEAT_RIGHTLEFT
5510 if (wp->w_p_rl)
5511 {
5512 --off;
5513 --col;
5514 }
5515 else
5516#endif
5517 {
5518 ++off;
5519 ++col;
5520 }
5521 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005522#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005523 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005524 {
5525 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005526 ++vcol_off;
5527 if (n_extra > 0)
5528 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005529 if (wp->w_p_wrap)
5530 {
5531 /*
5532 * Special voodoo required if 'wrap' is on.
5533 *
5534 * Advance the column indicator to force the line
5535 * drawing to wrap early. This will make the line
5536 * take up the same screen space when parts are concealed,
5537 * so that cursor line computations aren't messed up.
5538 *
5539 * To avoid the fictitious advance of 'col' causing
5540 * trailing junk to be written out of the screen line
5541 * we are building, 'boguscols' keeps track of the number
5542 * of bad columns we have advanced.
5543 */
5544 if (n_extra > 0)
5545 {
5546 vcol += n_extra;
5547# ifdef FEAT_RIGHTLEFT
5548 if (wp->w_p_rl)
5549 {
5550 col -= n_extra;
5551 boguscols -= n_extra;
5552 }
5553 else
5554# endif
5555 {
5556 col += n_extra;
5557 boguscols += n_extra;
5558 }
5559 n_extra = 0;
5560 n_attr = 0;
5561 }
5562
5563
5564# ifdef FEAT_MBYTE
5565 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5566 {
5567 /* Need to fill two screen columns. */
5568# ifdef FEAT_RIGHTLEFT
5569 if (wp->w_p_rl)
5570 {
5571 --boguscols;
5572 --col;
5573 }
5574 else
5575# endif
5576 {
5577 ++boguscols;
5578 ++col;
5579 }
5580 }
5581# endif
5582
5583# ifdef FEAT_RIGHTLEFT
5584 if (wp->w_p_rl)
5585 {
5586 --boguscols;
5587 --col;
5588 }
5589 else
5590# endif
5591 {
5592 ++boguscols;
5593 ++col;
5594 }
5595 }
5596 else
5597 {
5598 if (n_extra > 0)
5599 {
5600 vcol += n_extra;
5601 n_extra = 0;
5602 n_attr = 0;
5603 }
5604 }
5605
5606 }
5607#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 else
5609 --n_skip;
5610
Bram Moolenaar64486672010-05-16 15:46:46 +02005611 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5612 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005613 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614#ifdef FEAT_DIFF
5615 && filler_todo <= 0
5616#endif
5617 )
5618 ++vcol;
5619
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005620#ifdef FEAT_SYN_HL
5621 if (vcol_save_attr >= 0)
5622 char_attr = vcol_save_attr;
5623#endif
5624
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 /* restore attributes after "predeces" in 'listchars' */
5626 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5627 char_attr = saved_attr3;
5628
5629 /* restore attributes after last 'listchars' or 'number' char */
5630 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5631 char_attr = saved_attr2;
5632
5633 /*
5634 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005635 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 */
5637 if ((
5638#ifdef FEAT_RIGHTLEFT
5639 wp->w_p_rl ? (col < 0) :
5640#endif
5641 (col >= W_WIDTH(wp)))
5642 && (*ptr != NUL
5643#ifdef FEAT_DIFF
5644 || filler_todo > 0
5645#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005646 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5648 )
5649 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005650#ifdef FEAT_CONCEAL
5651 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5652 (int)W_WIDTH(wp), wp->w_p_rl);
5653 boguscols = 0;
5654#else
5655 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5656 (int)W_WIDTH(wp), wp->w_p_rl);
5657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 ++row;
5659 ++screen_row;
5660
5661 /* When not wrapping and finished diff lines, or when displayed
5662 * '$' and highlighting until last column, break here. */
5663 if ((!wp->w_p_wrap
5664#ifdef FEAT_DIFF
5665 && filler_todo <= 0
5666#endif
5667 ) || lcs_eol_one == -1)
5668 break;
5669
5670 /* When the window is too narrow draw all "@" lines. */
5671 if (draw_state != WL_LINE
5672#ifdef FEAT_DIFF
5673 && filler_todo <= 0
5674#endif
5675 )
5676 {
5677 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005678#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 draw_vsep_win(wp, row);
5680#endif
5681 row = endrow;
5682 }
5683
5684 /* When line got too long for screen break here. */
5685 if (row == endrow)
5686 {
5687 ++row;
5688 break;
5689 }
5690
5691 if (screen_cur_row == screen_row - 1
5692#ifdef FEAT_DIFF
5693 && filler_todo <= 0
5694#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005695#ifdef FEAT_WINDOWS
5696 && W_WIDTH(wp) == Columns
5697#endif
5698 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {
5700 /* Remember that the line wraps, used for modeless copy. */
5701 LineWraps[screen_row - 1] = TRUE;
5702
5703 /*
5704 * Special trick to make copy/paste of wrapped lines work with
5705 * xterm/screen: write an extra character beyond the end of
5706 * the line. This will work with all terminal types
5707 * (regardless of the xn,am settings).
5708 * Only do this on a fast tty.
5709 * Only do this if the cursor is on the current line
5710 * (something has been written in it).
5711 * Don't do this for the GUI.
5712 * Don't do this for double-width characters.
5713 * Don't do this for a window not at the right screen border.
5714 */
5715 if (p_tf
5716#ifdef FEAT_GUI
5717 && !gui.in_use
5718#endif
5719#ifdef FEAT_MBYTE
5720 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005721 && ((*mb_off2cells)(LineOffset[screen_row],
5722 LineOffset[screen_row] + screen_Columns)
5723 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005725 + (int)Columns - 2,
5726 LineOffset[screen_row] + screen_Columns)
5727 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728#endif
5729 )
5730 {
5731 /* First make sure we are at the end of the screen line,
5732 * then output the same character again to let the
5733 * terminal know about the wrap. If the terminal doesn't
5734 * auto-wrap, we overwrite the character. */
5735 if (screen_cur_col != W_WIDTH(wp))
5736 screen_char(LineOffset[screen_row - 1]
5737 + (unsigned)Columns - 1,
5738 screen_row - 1, (int)(Columns - 1));
5739
5740#ifdef FEAT_MBYTE
5741 /* When there is a multi-byte character, just output a
5742 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005743 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5744 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 out_char(' ');
5746 else
5747#endif
5748 out_char(ScreenLines[LineOffset[screen_row - 1]
5749 + (Columns - 1)]);
5750 /* force a redraw of the first char on the next line */
5751 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5752 screen_start(); /* don't know where cursor is now */
5753 }
5754 }
5755
5756 col = 0;
5757 off = (unsigned)(current_ScreenLine - ScreenLines);
5758#ifdef FEAT_RIGHTLEFT
5759 if (wp->w_p_rl)
5760 {
5761 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5762 off += col;
5763 }
5764#endif
5765
5766 /* reset the drawing state for the start of a wrapped line */
5767 draw_state = WL_START;
5768 saved_n_extra = n_extra;
5769 saved_p_extra = p_extra;
5770 saved_c_extra = c_extra;
5771 saved_char_attr = char_attr;
5772 n_extra = 0;
5773 lcs_prec_todo = lcs_prec;
5774#ifdef FEAT_LINEBREAK
5775# ifdef FEAT_DIFF
5776 if (filler_todo <= 0)
5777# endif
5778 need_showbreak = TRUE;
5779#endif
5780#ifdef FEAT_DIFF
5781 --filler_todo;
5782 /* When the filler lines are actually below the last line of the
5783 * file, don't draw the line itself, break here. */
5784 if (filler_todo == 0 && wp->w_botfill)
5785 break;
5786#endif
5787 }
5788
5789 } /* for every character in the line */
5790
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005791#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005792 /* After an empty line check first word for capital. */
5793 if (*skipwhite(line) == NUL)
5794 {
5795 capcol_lnum = lnum + 1;
5796 cap_col = 0;
5797 }
5798#endif
5799
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005800 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 return row;
5802}
5803
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005804#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005805static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005806
5807/*
5808 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005809 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005810 */
5811 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005812comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005813{
5814 int i;
5815
5816 for (i = 0; i < Screen_mco; ++i)
5817 {
5818 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5819 return TRUE;
5820 if (ScreenLinesC[i][off_from] == 0)
5821 break;
5822 }
5823 return FALSE;
5824}
5825#endif
5826
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827/*
5828 * Check whether the given character needs redrawing:
5829 * - the (first byte of the) character is different
5830 * - the attributes are different
5831 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005832 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 */
5834 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005835char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836{
5837 if (cols > 0
5838 && ((ScreenLines[off_from] != ScreenLines[off_to]
5839 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5840
5841#ifdef FEAT_MBYTE
5842 || (enc_dbcs != 0
5843 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5844 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5845 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5846 : (cols > 1 && ScreenLines[off_from + 1]
5847 != ScreenLines[off_to + 1])))
5848 || (enc_utf8
5849 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5850 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005851 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005852 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5853 && ScreenLines[off_from + 1]
5854 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855#endif
5856 ))
5857 return TRUE;
5858 return FALSE;
5859}
5860
5861/*
5862 * Move one "cooked" screen line to the screen, but only the characters that
5863 * have actually changed. Handle insert/delete character.
5864 * "coloff" gives the first column on the screen for this line.
5865 * "endcol" gives the columns where valid characters are.
5866 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5867 * needs to be cleared, negative otherwise.
5868 * "rlflag" is TRUE in a rightleft window:
5869 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5870 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5871 */
5872 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005873screen_line(
5874 int row,
5875 int coloff,
5876 int endcol,
5877 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005879 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005881 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882{
5883 unsigned off_from;
5884 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005885#ifdef FEAT_MBYTE
5886 unsigned max_off_from;
5887 unsigned max_off_to;
5888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005890#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 int hl;
5892#endif
5893 int force = FALSE; /* force update rest of the line */
5894 int redraw_this /* bool: does character need redraw? */
5895#ifdef FEAT_GUI
5896 = TRUE /* For GUI when while-loop empty */
5897#endif
5898 ;
5899 int redraw_next; /* redraw_this for next character */
5900#ifdef FEAT_MBYTE
5901 int clear_next = FALSE;
5902 int char_cells; /* 1: normal char */
5903 /* 2: occupies two display cells */
5904# define CHAR_CELLS char_cells
5905#else
5906# define CHAR_CELLS 1
5907#endif
5908
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005909 /* Check for illegal row and col, just in case. */
5910 if (row >= Rows)
5911 row = Rows - 1;
5912 if (endcol > Columns)
5913 endcol = Columns;
5914
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915# ifdef FEAT_CLIPBOARD
5916 clip_may_clear_selection(row, row);
5917# endif
5918
5919 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5920 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005921#ifdef FEAT_MBYTE
5922 max_off_from = off_from + screen_Columns;
5923 max_off_to = LineOffset[row] + screen_Columns;
5924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
5926#ifdef FEAT_RIGHTLEFT
5927 if (rlflag)
5928 {
5929 /* Clear rest first, because it's left of the text. */
5930 if (clear_width > 0)
5931 {
5932 while (col <= endcol && ScreenLines[off_to] == ' '
5933 && ScreenAttrs[off_to] == 0
5934# ifdef FEAT_MBYTE
5935 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5936# endif
5937 )
5938 {
5939 ++off_to;
5940 ++col;
5941 }
5942 if (col <= endcol)
5943 screen_fill(row, row + 1, col + coloff,
5944 endcol + coloff + 1, ' ', ' ', 0);
5945 }
5946 col = endcol + 1;
5947 off_to = LineOffset[row] + col + coloff;
5948 off_from += col;
5949 endcol = (clear_width > 0 ? clear_width : -clear_width);
5950 }
5951#endif /* FEAT_RIGHTLEFT */
5952
5953 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5954
5955 while (col < endcol)
5956 {
5957#ifdef FEAT_MBYTE
5958 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005959 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 else
5961 char_cells = 1;
5962#endif
5963
5964 redraw_this = redraw_next;
5965 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5966 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5967
5968#ifdef FEAT_GUI
5969 /* If the next character was bold, then redraw the current character to
5970 * remove any pixels that might have spilt over into us. This only
5971 * happens in the GUI.
5972 */
5973 if (redraw_next && gui.in_use)
5974 {
5975 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005976 if (hl > HL_ALL)
5977 hl = syn_attr2attr(hl);
5978 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 redraw_this = TRUE;
5980 }
5981#endif
5982
5983 if (redraw_this)
5984 {
5985 /*
5986 * Special handling when 'xs' termcap flag set (hpterm):
5987 * Attributes for characters are stored at the position where the
5988 * cursor is when writing the highlighting code. The
5989 * start-highlighting code must be written with the cursor on the
5990 * first highlighted character. The stop-highlighting code must
5991 * be written with the cursor just after the last highlighted
5992 * character.
5993 * Overwriting a character doesn't remove it's highlighting. Need
5994 * to clear the rest of the line, and force redrawing it
5995 * completely.
5996 */
5997 if ( p_wiv
5998 && !force
5999#ifdef FEAT_GUI
6000 && !gui.in_use
6001#endif
6002 && ScreenAttrs[off_to] != 0
6003 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6004 {
6005 /*
6006 * Need to remove highlighting attributes here.
6007 */
6008 windgoto(row, col + coloff);
6009 out_str(T_CE); /* clear rest of this screen line */
6010 screen_start(); /* don't know where cursor is now */
6011 force = TRUE; /* force redraw of rest of the line */
6012 redraw_next = TRUE; /* or else next char would miss out */
6013
6014 /*
6015 * If the previous character was highlighted, need to stop
6016 * highlighting at this character.
6017 */
6018 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6019 {
6020 screen_attr = ScreenAttrs[off_to - 1];
6021 term_windgoto(row, col + coloff);
6022 screen_stop_highlight();
6023 }
6024 else
6025 screen_attr = 0; /* highlighting has stopped */
6026 }
6027#ifdef FEAT_MBYTE
6028 if (enc_dbcs != 0)
6029 {
6030 /* Check if overwriting a double-byte with a single-byte or
6031 * the other way around requires another character to be
6032 * redrawn. For UTF-8 this isn't needed, because comparing
6033 * ScreenLinesUC[] is sufficient. */
6034 if (char_cells == 1
6035 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006036 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 {
6038 /* Writing a single-cell character over a double-cell
6039 * character: need to redraw the next cell. */
6040 ScreenLines[off_to + 1] = 0;
6041 redraw_next = TRUE;
6042 }
6043 else if (char_cells == 2
6044 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006045 && (*mb_off2cells)(off_to, max_off_to) == 1
6046 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 {
6048 /* Writing the second half of a double-cell character over
6049 * a double-cell character: need to redraw the second
6050 * cell. */
6051 ScreenLines[off_to + 2] = 0;
6052 redraw_next = TRUE;
6053 }
6054
6055 if (enc_dbcs == DBCS_JPNU)
6056 ScreenLines2[off_to] = ScreenLines2[off_from];
6057 }
6058 /* When writing a single-width character over a double-width
6059 * character and at the end of the redrawn text, need to clear out
6060 * the right halve of the old character.
6061 * Also required when writing the right halve of a double-width
6062 * char over the left halve of an existing one. */
6063 if (has_mbyte && col + char_cells == endcol
6064 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006065 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006067 && (*mb_off2cells)(off_to, max_off_to) == 1
6068 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 clear_next = TRUE;
6070#endif
6071
6072 ScreenLines[off_to] = ScreenLines[off_from];
6073#ifdef FEAT_MBYTE
6074 if (enc_utf8)
6075 {
6076 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6077 if (ScreenLinesUC[off_from] != 0)
6078 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006079 int i;
6080
6081 for (i = 0; i < Screen_mco; ++i)
6082 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 }
6084 }
6085 if (char_cells == 2)
6086 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6087#endif
6088
6089#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006090 /* The bold trick makes a single column of pixels appear in the
6091 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 * character should be redrawn too. This happens for our own GUI
6093 * and for some xterms. */
6094 if (
6095# ifdef FEAT_GUI
6096 gui.in_use
6097# endif
6098# if defined(FEAT_GUI) && defined(UNIX)
6099 ||
6100# endif
6101# ifdef UNIX
6102 term_is_xterm
6103# endif
6104 )
6105 {
6106 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006107 if (hl > HL_ALL)
6108 hl = syn_attr2attr(hl);
6109 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 redraw_next = TRUE;
6111 }
6112#endif
6113 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6114#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006115 /* For simplicity set the attributes of second half of a
6116 * double-wide character equal to the first half. */
6117 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006119
6120 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 else
6123#endif
6124 screen_char(off_to, row, col + coloff);
6125 }
6126 else if ( p_wiv
6127#ifdef FEAT_GUI
6128 && !gui.in_use
6129#endif
6130 && col + coloff > 0)
6131 {
6132 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6133 {
6134 /*
6135 * Don't output stop-highlight when moving the cursor, it will
6136 * stop the highlighting when it should continue.
6137 */
6138 screen_attr = 0;
6139 }
6140 else if (screen_attr != 0)
6141 screen_stop_highlight();
6142 }
6143
6144 off_to += CHAR_CELLS;
6145 off_from += CHAR_CELLS;
6146 col += CHAR_CELLS;
6147 }
6148
6149#ifdef FEAT_MBYTE
6150 if (clear_next)
6151 {
6152 /* Clear the second half of a double-wide character of which the left
6153 * half was overwritten with a single-wide character. */
6154 ScreenLines[off_to] = ' ';
6155 if (enc_utf8)
6156 ScreenLinesUC[off_to] = 0;
6157 screen_char(off_to, row, col + coloff);
6158 }
6159#endif
6160
6161 if (clear_width > 0
6162#ifdef FEAT_RIGHTLEFT
6163 && !rlflag
6164#endif
6165 )
6166 {
6167#ifdef FEAT_GUI
6168 int startCol = col;
6169#endif
6170
6171 /* blank out the rest of the line */
6172 while (col < clear_width && ScreenLines[off_to] == ' '
6173 && ScreenAttrs[off_to] == 0
6174#ifdef FEAT_MBYTE
6175 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6176#endif
6177 )
6178 {
6179 ++off_to;
6180 ++col;
6181 }
6182 if (col < clear_width)
6183 {
6184#ifdef FEAT_GUI
6185 /*
6186 * In the GUI, clearing the rest of the line may leave pixels
6187 * behind if the first character cleared was bold. Some bold
6188 * fonts spill over the left. In this case we redraw the previous
6189 * character too. If we didn't skip any blanks above, then we
6190 * only redraw if the character wasn't already redrawn anyway.
6191 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006192 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
6194 hl = ScreenAttrs[off_to];
6195 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006196 {
6197 int prev_cells = 1;
6198# ifdef FEAT_MBYTE
6199 if (enc_utf8)
6200 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6201 * that its width is 2. */
6202 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6203 else if (enc_dbcs != 0)
6204 {
6205 /* find previous character by counting from first
6206 * column and get its width. */
6207 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006208 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006209
6210 while (off < off_to)
6211 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006212 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006213 off += prev_cells;
6214 }
6215 }
6216
6217 if (enc_dbcs != 0 && prev_cells > 1)
6218 screen_char_2(off_to - prev_cells, row,
6219 col + coloff - prev_cells);
6220 else
6221# endif
6222 screen_char(off_to - prev_cells, row,
6223 col + coloff - prev_cells);
6224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 }
6226#endif
6227 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6228 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006229#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 off_to += clear_width - col;
6231 col = clear_width;
6232#endif
6233 }
6234 }
6235
6236 if (clear_width > 0)
6237 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006238#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 /* For a window that's left of another, draw the separator char. */
6240 if (col + coloff < Columns)
6241 {
6242 int c;
6243
6244 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006245 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006247 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6248 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249# endif
6250 || ScreenAttrs[off_to] != hl)
6251 {
6252 ScreenLines[off_to] = c;
6253 ScreenAttrs[off_to] = hl;
6254# ifdef FEAT_MBYTE
6255 if (enc_utf8)
6256 {
6257 if (c >= 0x80)
6258 {
6259 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006260 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
6262 else
6263 ScreenLinesUC[off_to] = 0;
6264 }
6265# endif
6266 screen_char(off_to, row, col + coloff);
6267 }
6268 }
6269 else
6270#endif
6271 LineWraps[row] = FALSE;
6272 }
6273}
6274
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006275#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006277 * Mirror text "str" for right-left displaying.
6278 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006280 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006281rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
6283 char_u *p1, *p2;
6284 int t;
6285
6286 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6287 {
6288 t = *p1;
6289 *p1 = *p2;
6290 *p2 = t;
6291 }
6292}
6293#endif
6294
6295#if defined(FEAT_WINDOWS) || defined(PROTO)
6296/*
6297 * mark all status lines for redraw; used after first :cd
6298 */
6299 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006300status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 win_T *wp;
6303
Bram Moolenaar29323592016-07-24 22:04:11 +02006304 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 if (wp->w_status_height)
6306 {
6307 wp->w_redr_status = TRUE;
6308 redraw_later(VALID);
6309 }
6310}
6311
6312/*
6313 * mark all status lines of the current buffer for redraw
6314 */
6315 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006316status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 win_T *wp;
6319
Bram Moolenaar29323592016-07-24 22:04:11 +02006320 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6322 {
6323 wp->w_redr_status = TRUE;
6324 redraw_later(VALID);
6325 }
6326}
6327
6328/*
6329 * Redraw all status lines that need to be redrawn.
6330 */
6331 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006332redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
6334 win_T *wp;
6335
Bram Moolenaar29323592016-07-24 22:04:11 +02006336 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 if (wp->w_redr_status)
6338 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006339 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006340 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341}
6342#endif
6343
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006344#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345/*
6346 * Redraw all status lines at the bottom of frame "frp".
6347 */
6348 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006349win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
6351 if (frp->fr_layout == FR_LEAF)
6352 frp->fr_win->w_redr_status = TRUE;
6353 else if (frp->fr_layout == FR_ROW)
6354 {
6355 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6356 win_redraw_last_status(frp);
6357 }
6358 else /* frp->fr_layout == FR_COL */
6359 {
6360 frp = frp->fr_child;
6361 while (frp->fr_next != NULL)
6362 frp = frp->fr_next;
6363 win_redraw_last_status(frp);
6364 }
6365}
6366#endif
6367
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006368#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369/*
6370 * Draw the verticap separator right of window "wp" starting with line "row".
6371 */
6372 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006373draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
6375 int hl;
6376 int c;
6377
6378 if (wp->w_vsep_width)
6379 {
6380 /* draw the vertical separator right of this window */
6381 c = fillchar_vsep(&hl);
6382 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6383 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6384 c, ' ', hl);
6385 }
6386}
6387#endif
6388
6389#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006390static int status_match_len(expand_T *xp, char_u *s);
6391static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392
6393/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006394 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 */
6396 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006397status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398{
6399 int len = 0;
6400
6401#ifdef FEAT_MENU
6402 int emenu = (xp->xp_context == EXPAND_MENUS
6403 || xp->xp_context == EXPAND_MENUNAMES);
6404
6405 /* Check for menu separators - replace with '|'. */
6406 if (emenu && menu_is_separator(s))
6407 return 1;
6408#endif
6409
6410 while (*s != NUL)
6411 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006412 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006413 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006414 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
6416
6417 return len;
6418}
6419
6420/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006421 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006422 * These are backslashes used for escaping. Do show backslashes in help tags.
6423 */
6424 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006425skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006426{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006427 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006428#ifdef FEAT_MENU
6429 || ((xp->xp_context == EXPAND_MENUS
6430 || xp->xp_context == EXPAND_MENUNAMES)
6431 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6432#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006433 )
6434 {
6435#ifndef BACKSLASH_IN_FILENAME
6436 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6437 return 2;
6438#endif
6439 return 1;
6440 }
6441 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006442}
6443
6444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 * Show wildchar matches in the status line.
6446 * Show at least the "match" item.
6447 * We start at item 'first_match' in the list and show all matches that fit.
6448 *
6449 * If inversion is possible we use it. Else '=' characters are used.
6450 */
6451 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006452win_redr_status_matches(
6453 expand_T *xp,
6454 int num_matches,
6455 char_u **matches, /* list of matches */
6456 int match,
6457 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
6459#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6460 int row;
6461 char_u *buf;
6462 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006463 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 int fillchar;
6465 int attr;
6466 int i;
6467 int highlight = TRUE;
6468 char_u *selstart = NULL;
6469 int selstart_col = 0;
6470 char_u *selend = NULL;
6471 static int first_match = 0;
6472 int add_left = FALSE;
6473 char_u *s;
6474#ifdef FEAT_MENU
6475 int emenu;
6476#endif
6477#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6478 int l;
6479#endif
6480
6481 if (matches == NULL) /* interrupted completion? */
6482 return;
6483
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006484#ifdef FEAT_MBYTE
6485 if (has_mbyte)
6486 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6487 else
6488#endif
6489 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (buf == NULL)
6491 return;
6492
6493 if (match == -1) /* don't show match but original text */
6494 {
6495 match = 0;
6496 highlight = FALSE;
6497 }
6498 /* count 1 for the ending ">" */
6499 clen = status_match_len(xp, L_MATCH(match)) + 3;
6500 if (match == 0)
6501 first_match = 0;
6502 else if (match < first_match)
6503 {
6504 /* jumping left, as far as we can go */
6505 first_match = match;
6506 add_left = TRUE;
6507 }
6508 else
6509 {
6510 /* check if match fits on the screen */
6511 for (i = first_match; i < match; ++i)
6512 clen += status_match_len(xp, L_MATCH(i)) + 2;
6513 if (first_match > 0)
6514 clen += 2;
6515 /* jumping right, put match at the left */
6516 if ((long)clen > Columns)
6517 {
6518 first_match = match;
6519 /* if showing the last match, we can add some on the left */
6520 clen = 2;
6521 for (i = match; i < num_matches; ++i)
6522 {
6523 clen += status_match_len(xp, L_MATCH(i)) + 2;
6524 if ((long)clen >= Columns)
6525 break;
6526 }
6527 if (i == num_matches)
6528 add_left = TRUE;
6529 }
6530 }
6531 if (add_left)
6532 while (first_match > 0)
6533 {
6534 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6535 if ((long)clen >= Columns)
6536 break;
6537 --first_match;
6538 }
6539
6540 fillchar = fillchar_status(&attr, TRUE);
6541
6542 if (first_match == 0)
6543 {
6544 *buf = NUL;
6545 len = 0;
6546 }
6547 else
6548 {
6549 STRCPY(buf, "< ");
6550 len = 2;
6551 }
6552 clen = len;
6553
6554 i = first_match;
6555 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6556 {
6557 if (i == match)
6558 {
6559 selstart = buf + len;
6560 selstart_col = clen;
6561 }
6562
6563 s = L_MATCH(i);
6564 /* Check for menu separators - replace with '|' */
6565#ifdef FEAT_MENU
6566 emenu = (xp->xp_context == EXPAND_MENUS
6567 || xp->xp_context == EXPAND_MENUNAMES);
6568 if (emenu && menu_is_separator(s))
6569 {
6570 STRCPY(buf + len, transchar('|'));
6571 l = (int)STRLEN(buf + len);
6572 len += l;
6573 clen += l;
6574 }
6575 else
6576#endif
6577 for ( ; *s != NUL; ++s)
6578 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006579 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 clen += ptr2cells(s);
6581#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006582 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 {
6584 STRNCPY(buf + len, s, l);
6585 s += l - 1;
6586 len += l;
6587 }
6588 else
6589#endif
6590 {
6591 STRCPY(buf + len, transchar_byte(*s));
6592 len += (int)STRLEN(buf + len);
6593 }
6594 }
6595 if (i == match)
6596 selend = buf + len;
6597
6598 *(buf + len++) = ' ';
6599 *(buf + len++) = ' ';
6600 clen += 2;
6601 if (++i == num_matches)
6602 break;
6603 }
6604
6605 if (i != num_matches)
6606 {
6607 *(buf + len++) = '>';
6608 ++clen;
6609 }
6610
6611 buf[len] = NUL;
6612
6613 row = cmdline_row - 1;
6614 if (row >= 0)
6615 {
6616 if (wild_menu_showing == 0)
6617 {
6618 if (msg_scrolled > 0)
6619 {
6620 /* Put the wildmenu just above the command line. If there is
6621 * no room, scroll the screen one line up. */
6622 if (cmdline_row == Rows - 1)
6623 {
6624 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6625 ++msg_scrolled;
6626 }
6627 else
6628 {
6629 ++cmdline_row;
6630 ++row;
6631 }
6632 wild_menu_showing = WM_SCROLLED;
6633 }
6634 else
6635 {
6636 /* Create status line if needed by setting 'laststatus' to 2.
6637 * Set 'winminheight' to zero to avoid that the window is
6638 * resized. */
6639 if (lastwin->w_status_height == 0)
6640 {
6641 save_p_ls = p_ls;
6642 save_p_wmh = p_wmh;
6643 p_ls = 2;
6644 p_wmh = 0;
6645 last_status(FALSE);
6646 }
6647 wild_menu_showing = WM_SHOWN;
6648 }
6649 }
6650
6651 screen_puts(buf, row, 0, attr);
6652 if (selstart != NULL && highlight)
6653 {
6654 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006655 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 }
6657
6658 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6659 }
6660
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006661#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 win_redraw_last_status(topframe);
6663#else
6664 lastwin->w_redr_status = TRUE;
6665#endif
6666 vim_free(buf);
6667}
6668#endif
6669
6670#if defined(FEAT_WINDOWS) || defined(PROTO)
6671/*
6672 * Redraw the status line of window wp.
6673 *
6674 * If inversion is possible we use it. Else '=' characters are used.
6675 */
6676 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006677win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678{
6679 int row;
6680 char_u *p;
6681 int len;
6682 int fillchar;
6683 int attr;
6684 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006685 static int busy = FALSE;
6686
6687 /* It's possible to get here recursively when 'statusline' (indirectly)
6688 * invokes ":redrawstatus". Simply ignore the call then. */
6689 if (busy)
6690 return;
6691 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
6693 wp->w_redr_status = FALSE;
6694 if (wp->w_status_height == 0)
6695 {
6696 /* no status line, can only be last window */
6697 redraw_cmdline = TRUE;
6698 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006699 else if (!redrawing()
6700#ifdef FEAT_INS_EXPAND
6701 /* don't update status line when popup menu is visible and may be
6702 * drawn over it */
6703 || pum_visible()
6704#endif
6705 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 {
6707 /* Don't redraw right now, do it later. */
6708 wp->w_redr_status = TRUE;
6709 }
6710#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006711 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 {
6713 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006714 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 }
6716#endif
6717 else
6718 {
6719 fillchar = fillchar_status(&attr, wp == curwin);
6720
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006721 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 p = NameBuff;
6723 len = (int)STRLEN(p);
6724
6725 if (wp->w_buffer->b_help
6726#ifdef FEAT_QUICKFIX
6727 || wp->w_p_pvw
6728#endif
6729 || bufIsChanged(wp->w_buffer)
6730 || wp->w_buffer->b_p_ro)
6731 *(p + len++) = ' ';
6732 if (wp->w_buffer->b_help)
6733 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006734 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 len += (int)STRLEN(p + len);
6736 }
6737#ifdef FEAT_QUICKFIX
6738 if (wp->w_p_pvw)
6739 {
6740 STRCPY(p + len, _("[Preview]"));
6741 len += (int)STRLEN(p + len);
6742 }
6743#endif
6744 if (bufIsChanged(wp->w_buffer))
6745 {
6746 STRCPY(p + len, "[+]");
6747 len += 3;
6748 }
6749 if (wp->w_buffer->b_p_ro)
6750 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006751 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006752 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 }
6754
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6756 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6757 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6758 if (this_ru_col <= 1)
6759 {
6760 p = (char_u *)"<"; /* No room for file name! */
6761 len = 1;
6762 }
6763 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764#ifdef FEAT_MBYTE
6765 if (has_mbyte)
6766 {
6767 int clen = 0, i;
6768
6769 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006770 clen = mb_string2cells(p, -1);
6771
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 /* Find first character that will fit.
6773 * Going from start to end is much faster for DBCS. */
6774 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006775 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 clen -= (*mb_ptr2cells)(p + i);
6777 len = clen;
6778 if (i > 0)
6779 {
6780 p = p + i - 1;
6781 *p = '<';
6782 ++len;
6783 }
6784
6785 }
6786 else
6787#endif
6788 if (len > this_ru_col - 1)
6789 {
6790 p += len - (this_ru_col - 1);
6791 *p = '<';
6792 len = this_ru_col - 1;
6793 }
6794
6795 row = W_WINROW(wp) + wp->w_height;
6796 screen_puts(p, row, W_WINCOL(wp), attr);
6797 screen_fill(row, row + 1, len + W_WINCOL(wp),
6798 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6799
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006800 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6802 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6803 - 1 + W_WINCOL(wp)), attr);
6804
6805#ifdef FEAT_CMDL_INFO
6806 win_redr_ruler(wp, TRUE);
6807#endif
6808 }
6809
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 /*
6811 * May need to draw the character below the vertical separator.
6812 */
6813 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6814 {
6815 if (stl_connected(wp))
6816 fillchar = fillchar_status(&attr, wp == curwin);
6817 else
6818 fillchar = fillchar_vsep(&attr);
6819 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6820 attr);
6821 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006822 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823}
6824
Bram Moolenaar238a5642006-02-21 22:12:05 +00006825#ifdef FEAT_STL_OPT
6826/*
6827 * Redraw the status line according to 'statusline' and take care of any
6828 * errors encountered.
6829 */
6830 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006831redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006832{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006833 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006834 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006835
6836 /* When called recursively return. This can happen when the statusline
6837 * contains an expression that triggers a redraw. */
6838 if (entered)
6839 return;
6840 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006841
Bram Moolenaara742e082016-04-05 21:10:38 +02006842 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006843 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006844 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006845 {
6846 /* When there is an error disable the statusline, otherwise the
6847 * display is messed up with errors and a redraw triggers the problem
6848 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006849 set_string_option_direct((char_u *)"statusline", -1,
6850 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006851 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006852 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006853 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006854 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006855}
6856#endif
6857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858/*
6859 * Return TRUE if the status line of window "wp" is connected to the status
6860 * line of the window right of it. If not, then it's a vertical separator.
6861 * Only call if (wp->w_vsep_width != 0).
6862 */
6863 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006864stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 frame_T *fr;
6867
6868 fr = wp->w_frame;
6869 while (fr->fr_parent != NULL)
6870 {
6871 if (fr->fr_parent->fr_layout == FR_COL)
6872 {
6873 if (fr->fr_next != NULL)
6874 break;
6875 }
6876 else
6877 {
6878 if (fr->fr_next != NULL)
6879 return TRUE;
6880 }
6881 fr = fr->fr_parent;
6882 }
6883 return FALSE;
6884}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885
6886#endif /* FEAT_WINDOWS */
6887
6888#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6889/*
6890 * Get the value to show for the language mappings, active 'keymap'.
6891 */
6892 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006893get_keymap_str(
6894 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006895 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01006896 char_u *buf, /* buffer for the result */
6897 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898{
6899 char_u *p;
6900
6901 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6902 return FALSE;
6903
6904 {
6905#ifdef FEAT_EVAL
6906 buf_T *old_curbuf = curbuf;
6907 win_T *old_curwin = curwin;
6908 char_u *s;
6909
6910 curbuf = wp->w_buffer;
6911 curwin = wp;
6912 STRCPY(buf, "b:keymap_name"); /* must be writable */
6913 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006914 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 --emsg_skip;
6916 curbuf = old_curbuf;
6917 curwin = old_curwin;
6918 if (p == NULL || *p == NUL)
6919#endif
6920 {
6921#ifdef FEAT_KEYMAP
6922 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6923 p = wp->w_buffer->b_p_keymap;
6924 else
6925#endif
6926 p = (char_u *)"lang";
6927 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006928 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 buf[0] = NUL;
6930#ifdef FEAT_EVAL
6931 vim_free(s);
6932#endif
6933 }
6934 return buf[0] != NUL;
6935}
6936#endif
6937
6938#if defined(FEAT_STL_OPT) || defined(PROTO)
6939/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006940 * Redraw the status line or ruler of window "wp".
6941 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 */
6943 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006944win_redr_custom(
6945 win_T *wp,
6946 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006948 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 int attr;
6950 int curattr;
6951 int row;
6952 int col = 0;
6953 int maxwidth;
6954 int width;
6955 int n;
6956 int len;
6957 int fillchar;
6958 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006959 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006961 struct stl_hlrec hltab[STL_MAX_ITEM];
6962 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006963 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006964 win_T *ewp;
6965 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
Bram Moolenaar1d633412013-12-11 15:52:01 +01006967 /* There is a tiny chance that this gets called recursively: When
6968 * redrawing a status line triggers redrawing the ruler or tabline.
6969 * Avoid trouble by not allowing recursion. */
6970 if (entered)
6971 return;
6972 entered = TRUE;
6973
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006975 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006977 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006978 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006979 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006980 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01006981 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006982 maxwidth = Columns;
6983# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006984 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006987 else
6988 {
6989 row = W_WINROW(wp) + wp->w_height;
6990 fillchar = fillchar_status(&attr, wp == curwin);
6991 maxwidth = W_WIDTH(wp);
6992
6993 if (draw_ruler)
6994 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006995 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006996 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006997 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006998 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006999 if (*++stl == '-')
7000 stl++;
7001 if (atoi((char *)stl))
7002 while (VIM_ISDIGIT(*stl))
7003 stl++;
7004 if (*stl++ != '(')
7005 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007006 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007007#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007008 col = ru_col - (Columns - W_WIDTH(wp));
7009 if (col < (W_WIDTH(wp) + 1) / 2)
7010 col = (W_WIDTH(wp) + 1) / 2;
7011#else
7012 col = ru_col;
7013 if (col > (Columns + 1) / 2)
7014 col = (Columns + 1) / 2;
7015#endif
7016 maxwidth = W_WIDTH(wp) - col;
7017#ifdef FEAT_WINDOWS
7018 if (!wp->w_status_height)
7019#endif
7020 {
7021 row = Rows - 1;
7022 --maxwidth; /* writing in last column may cause scrolling */
7023 fillchar = ' ';
7024 attr = 0;
7025 }
7026
7027# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007028 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007029# endif
7030 }
7031 else
7032 {
7033 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007034 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007035 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007036 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007037# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007038 use_sandbox = was_set_insecurely((char_u *)"statusline",
7039 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007040# endif
7041 }
7042
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007043#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007044 col += W_WINCOL(wp);
7045#endif
7046 }
7047
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007049 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050
Bram Moolenaar61452852011-02-01 18:01:11 +01007051 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7052 * the cursor away and back. */
7053 ewp = wp == NULL ? curwin : wp;
7054 p_crb_save = ewp->w_p_crb;
7055 ewp->w_p_crb = FALSE;
7056
Bram Moolenaar362f3562009-11-03 16:20:34 +00007057 /* Make a copy, because the statusline may include a function call that
7058 * might change the option value and free the memory. */
7059 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007060 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007061 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007062 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007063 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007064 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007066 /* Make all characters printable. */
7067 p = transstr(buf);
7068 if (p != NULL)
7069 {
7070 vim_strncpy(buf, p, sizeof(buf) - 1);
7071 vim_free(p);
7072 }
7073
7074 /* fill up with "fillchar" */
7075 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007076 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {
7078#ifdef FEAT_MBYTE
7079 len += (*mb_char2bytes)(fillchar, buf + len);
7080#else
7081 buf[len++] = fillchar;
7082#endif
7083 ++width;
7084 }
7085 buf[len] = NUL;
7086
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007087 /*
7088 * Draw each snippet with the specified highlighting.
7089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 curattr = attr;
7091 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007092 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007094 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 screen_puts_len(p, len, row, col, curattr);
7096 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007097 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007099 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007101 else if (hltab[n].userhl < 0)
7102 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007104 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007105 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106#endif
7107 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007108 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 }
7110 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007111
7112 if (wp == NULL)
7113 {
7114 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7115 col = 0;
7116 len = 0;
7117 p = buf;
7118 fillchar = 0;
7119 for (n = 0; tabtab[n].start != NULL; n++)
7120 {
7121 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7122 while (col < len)
7123 TabPageIdxs[col++] = fillchar;
7124 p = tabtab[n].start;
7125 fillchar = tabtab[n].userhl;
7126 }
7127 while (col < Columns)
7128 TabPageIdxs[col++] = fillchar;
7129 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007130
7131theend:
7132 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133}
7134
7135#endif /* FEAT_STL_OPT */
7136
7137/*
7138 * Output a single character directly to the screen and update ScreenLines.
7139 */
7140 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007141screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 char_u buf[MB_MAXBYTES + 1];
7144
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007145#ifdef FEAT_MBYTE
7146 if (has_mbyte)
7147 buf[(*mb_char2bytes)(c, buf)] = NUL;
7148 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007150 {
7151 buf[0] = c;
7152 buf[1] = NUL;
7153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 screen_puts(buf, row, col, attr);
7155}
7156
7157/*
7158 * Get a single character directly from ScreenLines into "bytes[]".
7159 * Also return its attribute in *attrp;
7160 */
7161 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007162screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163{
7164 unsigned off;
7165
7166 /* safety check */
7167 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7168 {
7169 off = LineOffset[row] + col;
7170 *attrp = ScreenAttrs[off];
7171 bytes[0] = ScreenLines[off];
7172 bytes[1] = NUL;
7173
7174#ifdef FEAT_MBYTE
7175 if (enc_utf8 && ScreenLinesUC[off] != 0)
7176 bytes[utfc_char2bytes(off, bytes)] = NUL;
7177 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7178 {
7179 bytes[0] = ScreenLines[off];
7180 bytes[1] = ScreenLines2[off];
7181 bytes[2] = NUL;
7182 }
7183 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7184 {
7185 bytes[1] = ScreenLines[off + 1];
7186 bytes[2] = NUL;
7187 }
7188#endif
7189 }
7190}
7191
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007192#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007193static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007194
7195/*
7196 * Return TRUE if composing characters for screen posn "off" differs from
7197 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007198 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007199 */
7200 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007201screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007202{
7203 int i;
7204
7205 for (i = 0; i < Screen_mco; ++i)
7206 {
7207 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7208 return TRUE;
7209 if (u8cc[i] == 0)
7210 break;
7211 }
7212 return FALSE;
7213}
7214#endif
7215
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216/*
7217 * Put string '*text' on the screen at position 'row' and 'col', with
7218 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7219 * Note: only outputs within one row, message is truncated at screen boundary!
7220 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7221 */
7222 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007223screen_puts(
7224 char_u *text,
7225 int row,
7226 int col,
7227 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228{
7229 screen_puts_len(text, -1, row, col, attr);
7230}
7231
7232/*
7233 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7234 * a NUL.
7235 */
7236 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007237screen_puts_len(
7238 char_u *text,
7239 int textlen,
7240 int row,
7241 int col,
7242 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243{
7244 unsigned off;
7245 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007246 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 int c;
7248#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007249 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 int mbyte_blen = 1;
7251 int mbyte_cells = 1;
7252 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007253 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 int clear_next_cell = FALSE;
7255# ifdef FEAT_ARABIC
7256 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007257 int pc, nc, nc1;
7258 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259# endif
7260#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007261#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7262 int force_redraw_this;
7263 int force_redraw_next = FALSE;
7264#endif
7265 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266
7267 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7268 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007269 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270
Bram Moolenaarc236c162008-07-13 17:41:49 +00007271#ifdef FEAT_MBYTE
7272 /* When drawing over the right halve of a double-wide char clear out the
7273 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007274 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007275# ifdef FEAT_GUI
7276 && !gui.in_use
7277# endif
7278 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007279 {
7280 ScreenLines[off - 1] = ' ';
7281 ScreenAttrs[off - 1] = 0;
7282 if (enc_utf8)
7283 {
7284 ScreenLinesUC[off - 1] = 0;
7285 ScreenLinesC[0][off - 1] = 0;
7286 }
7287 /* redraw the previous cell, make it empty */
7288 screen_char(off - 1, row, col - 1);
7289 /* force the cell at "col" to be redrawn */
7290 force_redraw_next = TRUE;
7291 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007292#endif
7293
Bram Moolenaar367329b2007-08-30 11:53:22 +00007294#ifdef FEAT_MBYTE
7295 max_off = LineOffset[row] + screen_Columns;
7296#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007297 while (col < screen_Columns
7298 && (len < 0 || (int)(ptr - text) < len)
7299 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 {
7301 c = *ptr;
7302#ifdef FEAT_MBYTE
7303 /* check if this is the first byte of a multibyte */
7304 if (has_mbyte)
7305 {
7306 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007307 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007309 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7311 mbyte_cells = 1;
7312 else if (enc_dbcs != 0)
7313 mbyte_cells = mbyte_blen;
7314 else /* enc_utf8 */
7315 {
7316 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007317 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 (int)((text + len) - ptr));
7319 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007320 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007322# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 /* Non-BMP character: display as ? or fullwidth ?. */
7324 if (u8c >= 0x10000)
7325 {
7326 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7327 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007328 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331# ifdef FEAT_ARABIC
7332 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7333 {
7334 /* Do Arabic shaping. */
7335 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7336 {
7337 /* Past end of string to be displayed. */
7338 nc = NUL;
7339 nc1 = NUL;
7340 }
7341 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007342 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007343 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7344 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007345 nc1 = pcc[0];
7346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 pc = prev_c;
7348 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007349 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 }
7351 else
7352 prev_c = u8c;
7353# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007354 if (col + mbyte_cells > screen_Columns)
7355 {
7356 /* Only 1 cell left, but character requires 2 cells:
7357 * display a '>' in the last column to avoid wrapping. */
7358 c = '>';
7359 mbyte_cells = 1;
7360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 }
7362 }
7363#endif
7364
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007365#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7366 force_redraw_this = force_redraw_next;
7367 force_redraw_next = FALSE;
7368#endif
7369
7370 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371#ifdef FEAT_MBYTE
7372 || (mbyte_cells == 2
7373 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7374 || (enc_dbcs == DBCS_JPNU
7375 && c == 0x8e
7376 && ScreenLines2[off] != ptr[1])
7377 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007378 && (ScreenLinesUC[off] !=
7379 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7380 || (ScreenLinesUC[off] != 0
7381 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382#endif
7383 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007384 || exmode_active;
7385
7386 if (need_redraw
7387#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7388 || force_redraw_this
7389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 )
7391 {
7392#if defined(FEAT_GUI) || defined(UNIX)
7393 /* The bold trick makes a single row of pixels appear in the next
7394 * character. When a bold character is removed, the next
7395 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007396 * and for some xterms. */
7397 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398# ifdef FEAT_GUI
7399 gui.in_use
7400# endif
7401# if defined(FEAT_GUI) && defined(UNIX)
7402 ||
7403# endif
7404# ifdef UNIX
7405 term_is_xterm
7406# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007407 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007409 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007411 if (n > HL_ALL)
7412 n = syn_attr2attr(n);
7413 if (n & HL_BOLD)
7414 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 }
7416#endif
7417#ifdef FEAT_MBYTE
7418 /* When at the end of the text and overwriting a two-cell
7419 * character with a one-cell character, need to clear the next
7420 * cell. Also when overwriting the left halve of a two-cell char
7421 * with the right halve of a two-cell char. Do this only once
7422 * (mb_off2cells() may return 2 on the right halve). */
7423 if (clear_next_cell)
7424 clear_next_cell = FALSE;
7425 else if (has_mbyte
7426 && (len < 0 ? ptr[mbyte_blen] == NUL
7427 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007428 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007430 && (*mb_off2cells)(off, max_off) == 1
7431 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 clear_next_cell = TRUE;
7433
7434 /* Make sure we never leave a second byte of a double-byte behind,
7435 * it confuses mb_off2cells(). */
7436 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007437 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007439 && (*mb_off2cells)(off, max_off) == 1
7440 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 ScreenLines[off + mbyte_blen] = 0;
7442#endif
7443 ScreenLines[off] = c;
7444 ScreenAttrs[off] = attr;
7445#ifdef FEAT_MBYTE
7446 if (enc_utf8)
7447 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007448 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 ScreenLinesUC[off] = 0;
7450 else
7451 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007452 int i;
7453
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007455 for (i = 0; i < Screen_mco; ++i)
7456 {
7457 ScreenLinesC[i][off] = u8cc[i];
7458 if (u8cc[i] == 0)
7459 break;
7460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 }
7462 if (mbyte_cells == 2)
7463 {
7464 ScreenLines[off + 1] = 0;
7465 ScreenAttrs[off + 1] = attr;
7466 }
7467 screen_char(off, row, col);
7468 }
7469 else if (mbyte_cells == 2)
7470 {
7471 ScreenLines[off + 1] = ptr[1];
7472 ScreenAttrs[off + 1] = attr;
7473 screen_char_2(off, row, col);
7474 }
7475 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7476 {
7477 ScreenLines2[off] = ptr[1];
7478 screen_char(off, row, col);
7479 }
7480 else
7481#endif
7482 screen_char(off, row, col);
7483 }
7484#ifdef FEAT_MBYTE
7485 if (has_mbyte)
7486 {
7487 off += mbyte_cells;
7488 col += mbyte_cells;
7489 ptr += mbyte_blen;
7490 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007491 {
7492 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007494 len = -1;
7495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 }
7497 else
7498#endif
7499 {
7500 ++off;
7501 ++col;
7502 ++ptr;
7503 }
7504 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007505
7506#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7507 /* If we detected the next character needs to be redrawn, but the text
7508 * doesn't extend up to there, update the character here. */
7509 if (force_redraw_next && col < screen_Columns)
7510 {
7511# ifdef FEAT_MBYTE
7512 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7513 screen_char_2(off, row, col);
7514 else
7515# endif
7516 screen_char(off, row, col);
7517 }
7518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519}
7520
7521#ifdef FEAT_SEARCH_EXTRA
7522/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007523 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 */
7525 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007526start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527{
7528 if (p_hls && !no_hlsearch)
7529 {
7530 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007531 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007532# ifdef FEAT_RELTIME
7533 /* Set the time limit to 'redrawtime'. */
7534 profile_setlimit(p_rdt, &search_hl.tm);
7535# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 }
7537}
7538
7539/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007540 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 */
7542 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007543end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544{
7545 if (search_hl.rm.regprog != NULL)
7546 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007547 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 search_hl.rm.regprog = NULL;
7549 }
7550}
7551
7552/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007553 * Init for calling prepare_search_hl().
7554 */
7555 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007556init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007557{
7558 matchitem_T *cur;
7559
7560 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7561 * match */
7562 cur = wp->w_match_head;
7563 while (cur != NULL)
7564 {
7565 cur->hl.rm = cur->match;
7566 if (cur->hlg_id == 0)
7567 cur->hl.attr = 0;
7568 else
7569 cur->hl.attr = syn_id2attr(cur->hlg_id);
7570 cur->hl.buf = wp->w_buffer;
7571 cur->hl.lnum = 0;
7572 cur->hl.first_lnum = 0;
7573# ifdef FEAT_RELTIME
7574 /* Set the time limit to 'redrawtime'. */
7575 profile_setlimit(p_rdt, &(cur->hl.tm));
7576# endif
7577 cur = cur->next;
7578 }
7579 search_hl.buf = wp->w_buffer;
7580 search_hl.lnum = 0;
7581 search_hl.first_lnum = 0;
7582 /* time limit is set at the toplevel, for all windows */
7583}
7584
7585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 * Advance to the match in window "wp" line "lnum" or past it.
7587 */
7588 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007589prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007591 matchitem_T *cur; /* points to the match list */
7592 match_T *shl; /* points to search_hl or a match */
7593 int shl_flag; /* flag to indicate whether search_hl
7594 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007595 int pos_inprogress; /* marks that position match search is
7596 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 int n;
7598
7599 /*
7600 * When using a multi-line pattern, start searching at the top
7601 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007602 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007604 cur = wp->w_match_head;
7605 shl_flag = FALSE;
7606 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007608 if (shl_flag == FALSE)
7609 {
7610 shl = &search_hl;
7611 shl_flag = TRUE;
7612 }
7613 else
7614 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 if (shl->rm.regprog != NULL
7616 && shl->lnum == 0
7617 && re_multiline(shl->rm.regprog))
7618 {
7619 if (shl->first_lnum == 0)
7620 {
7621# ifdef FEAT_FOLDING
7622 for (shl->first_lnum = lnum;
7623 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7624 if (hasFoldingWin(wp, shl->first_lnum - 1,
7625 NULL, NULL, TRUE, NULL))
7626 break;
7627# else
7628 shl->first_lnum = wp->w_topline;
7629# endif
7630 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007631 if (cur != NULL)
7632 cur->pos.cur = 0;
7633 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007635 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7636 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007638 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7639 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007640 pos_inprogress = cur == NULL || cur->pos.cur == 0
7641 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 if (shl->lnum != 0)
7643 {
7644 shl->first_lnum = shl->lnum
7645 + shl->rm.endpos[0].lnum
7646 - shl->rm.startpos[0].lnum;
7647 n = shl->rm.endpos[0].col;
7648 }
7649 else
7650 {
7651 ++shl->first_lnum;
7652 n = 0;
7653 }
7654 }
7655 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007656 if (shl != &search_hl && cur != NULL)
7657 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 }
7659}
7660
7661/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007662 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 * Uses shl->buf.
7664 * Sets shl->lnum and shl->rm contents.
7665 * Note: Assumes a previous match is always before "lnum", unless
7666 * shl->lnum is zero.
7667 * Careful: Any pointers for buffer lines will become invalid.
7668 */
7669 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007670next_search_hl(
7671 win_T *win,
7672 match_T *shl, /* points to search_hl or a match */
7673 linenr_T lnum,
7674 colnr_T mincol, /* minimal column for a match */
7675 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676{
7677 linenr_T l;
7678 colnr_T matchcol;
7679 long nmatched;
7680
7681 if (shl->lnum != 0)
7682 {
7683 /* Check for three situations:
7684 * 1. If the "lnum" is below a previous match, start a new search.
7685 * 2. If the previous match includes "mincol", use it.
7686 * 3. Continue after the previous match.
7687 */
7688 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7689 if (lnum > l)
7690 shl->lnum = 0;
7691 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7692 return;
7693 }
7694
7695 /*
7696 * Repeat searching for a match until one is found that includes "mincol"
7697 * or none is found in this line.
7698 */
7699 called_emsg = FALSE;
7700 for (;;)
7701 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007702#ifdef FEAT_RELTIME
7703 /* Stop searching after passing the time limit. */
7704 if (profile_passed_limit(&(shl->tm)))
7705 {
7706 shl->lnum = 0; /* no match found in time */
7707 break;
7708 }
7709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 /* Three situations:
7711 * 1. No useful previous match: search from start of line.
7712 * 2. Not Vi compatible or empty match: continue at next character.
7713 * Break the loop if this is beyond the end of the line.
7714 * 3. Vi compatible searching: continue at end of previous match.
7715 */
7716 if (shl->lnum == 0)
7717 matchcol = 0;
7718 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7719 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007720 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007722 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007723
7724 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007725 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007726 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007728 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 shl->lnum = 0;
7730 break;
7731 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007732#ifdef FEAT_MBYTE
7733 if (has_mbyte)
7734 matchcol += mb_ptr2len(ml);
7735 else
7736#endif
7737 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
7739 else
7740 matchcol = shl->rm.endpos[0].col;
7741
7742 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007743 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007745 /* Remember whether shl->rm is using a copy of the regprog in
7746 * cur->match. */
7747 int regprog_is_copy = (shl != &search_hl && cur != NULL
7748 && shl == &cur->hl
7749 && cur->match.regprog == cur->hl.rm.regprog);
7750
Bram Moolenaarb3414592014-06-17 17:48:32 +02007751 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7752 matchcol,
7753#ifdef FEAT_RELTIME
7754 &(shl->tm)
7755#else
7756 NULL
7757#endif
7758 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007759 /* Copy the regprog, in case it got freed and recompiled. */
7760 if (regprog_is_copy)
7761 cur->match.regprog = cur->hl.rm.regprog;
7762
Bram Moolenaarb3414592014-06-17 17:48:32 +02007763 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007764 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007765 /* Error while handling regexp: stop using this regexp. */
7766 if (shl == &search_hl)
7767 {
7768 /* don't free regprog in the match list, it's a copy */
7769 vim_regfree(shl->rm.regprog);
7770 SET_NO_HLSEARCH(TRUE);
7771 }
7772 shl->rm.regprog = NULL;
7773 shl->lnum = 0;
7774 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7775 message */
7776 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007777 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007778 }
7779 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007780 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007781 else
7782 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 if (nmatched == 0)
7784 {
7785 shl->lnum = 0; /* no match found */
7786 break;
7787 }
7788 if (shl->rm.startpos[0].lnum > 0
7789 || shl->rm.startpos[0].col >= mincol
7790 || nmatched > 1
7791 || shl->rm.endpos[0].col > mincol)
7792 {
7793 shl->lnum += shl->rm.startpos[0].lnum;
7794 break; /* useful match found */
7795 }
7796 }
7797}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798
Bram Moolenaar85077472016-10-16 14:35:48 +02007799/*
7800 * If there is a match fill "shl" and return one.
7801 * Return zero otherwise.
7802 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007803 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007804next_search_hl_pos(
7805 match_T *shl, /* points to a match */
7806 linenr_T lnum,
7807 posmatch_T *posmatch, /* match positions */
7808 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007809{
7810 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007811 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007812
Bram Moolenaarb3414592014-06-17 17:48:32 +02007813 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7814 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007815 llpos_T *pos = &posmatch->pos[i];
7816
7817 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007818 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007819 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007820 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007821 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007822 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007823 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007824 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007825 /* if this match comes before the one at "found" then swap
7826 * them */
7827 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007828 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007829 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007830
Bram Moolenaar85077472016-10-16 14:35:48 +02007831 *pos = posmatch->pos[found];
7832 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007833 }
7834 }
7835 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007836 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007837 }
7838 }
7839 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007840 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007841 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007842 colnr_T start = posmatch->pos[found].col == 0
7843 ? 0 : posmatch->pos[found].col - 1;
7844 colnr_T end = posmatch->pos[found].col == 0
7845 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007846
Bram Moolenaar85077472016-10-16 14:35:48 +02007847 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007848 shl->rm.startpos[0].lnum = 0;
7849 shl->rm.startpos[0].col = start;
7850 shl->rm.endpos[0].lnum = 0;
7851 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007852 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007853 posmatch->cur = found + 1;
7854 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007855 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007856 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007857}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007858#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007859
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007861screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862{
7863 attrentry_T *aep = NULL;
7864
7865 screen_attr = attr;
7866 if (full_screen
7867#ifdef WIN3264
7868 && termcap_active
7869#endif
7870 )
7871 {
7872#ifdef FEAT_GUI
7873 if (gui.in_use)
7874 {
7875 char buf[20];
7876
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007877 /* The GUI handles this internally. */
7878 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 OUT_STR(buf);
7880 }
7881 else
7882#endif
7883 {
7884 if (attr > HL_ALL) /* special HL attr. */
7885 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007886 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 aep = syn_cterm_attr2entry(attr);
7888 else
7889 aep = syn_term_attr2entry(attr);
7890 if (aep == NULL) /* did ":syntax clear" */
7891 attr = 0;
7892 else
7893 attr = aep->ae_attr;
7894 }
7895 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7896 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007897 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007898#ifdef FEAT_TERMGUICOLORS
7899 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007900 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007901#endif
7902 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007903#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007904 )
7905#endif
7906 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007907 /* If the Normal FG color has BOLD attribute and the new HL
7908 * has a FG color defined, clear BOLD. */
7909 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7911 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007912 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7913 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 out_str(T_US);
7915 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7916 out_str(T_CZH);
7917 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7918 out_str(T_MR);
7919
7920 /*
7921 * Output the color or start string after bold etc., in case the
7922 * bold etc. override the color setting.
7923 */
7924 if (aep != NULL)
7925 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007926#ifdef FEAT_TERMGUICOLORS
7927 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007929 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007930 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007931 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007932 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 }
7934 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007937 if (t_colors > 1)
7938 {
7939 if (aep->ae_u.cterm.fg_color)
7940 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7941 if (aep->ae_u.cterm.bg_color)
7942 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7943 }
7944 else
7945 {
7946 if (aep->ae_u.term.start != NULL)
7947 out_str(aep->ae_u.term.start);
7948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 }
7950 }
7951 }
7952 }
7953}
7954
7955 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007956screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957{
7958 int do_ME = FALSE; /* output T_ME code */
7959
7960 if (screen_attr != 0
7961#ifdef WIN3264
7962 && termcap_active
7963#endif
7964 )
7965 {
7966#ifdef FEAT_GUI
7967 if (gui.in_use)
7968 {
7969 char buf[20];
7970
7971 /* use internal GUI code */
7972 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7973 OUT_STR(buf);
7974 }
7975 else
7976#endif
7977 {
7978 if (screen_attr > HL_ALL) /* special HL attr. */
7979 {
7980 attrentry_T *aep;
7981
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007982 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {
7984 /*
7985 * Assume that t_me restores the original colors!
7986 */
7987 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007988 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007989#ifdef FEAT_TERMGUICOLORS
7990 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007991 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
7992 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007993#endif
7994 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007995#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007996 )
7997#endif
7998 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 do_ME = TRUE;
8000 }
8001 else
8002 {
8003 aep = syn_term_attr2entry(screen_attr);
8004 if (aep != NULL && aep->ae_u.term.stop != NULL)
8005 {
8006 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8007 do_ME = TRUE;
8008 else
8009 out_str(aep->ae_u.term.stop);
8010 }
8011 }
8012 if (aep == NULL) /* did ":syntax clear" */
8013 screen_attr = 0;
8014 else
8015 screen_attr = aep->ae_attr;
8016 }
8017
8018 /*
8019 * Often all ending-codes are equal to T_ME. Avoid outputting the
8020 * same sequence several times.
8021 */
8022 if (screen_attr & HL_STANDOUT)
8023 {
8024 if (STRCMP(T_SE, T_ME) == 0)
8025 do_ME = TRUE;
8026 else
8027 out_str(T_SE);
8028 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008029 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {
8031 if (STRCMP(T_UE, T_ME) == 0)
8032 do_ME = TRUE;
8033 else
8034 out_str(T_UE);
8035 }
8036 if (screen_attr & HL_ITALIC)
8037 {
8038 if (STRCMP(T_CZR, T_ME) == 0)
8039 do_ME = TRUE;
8040 else
8041 out_str(T_CZR);
8042 }
8043 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8044 out_str(T_ME);
8045
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008046#ifdef FEAT_TERMGUICOLORS
8047 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008049 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008050 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008051 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008052 term_bg_rgb_color(cterm_normal_bg_gui_color);
8053 }
8054 else
8055#endif
8056 {
8057 if (t_colors > 1)
8058 {
8059 /* set Normal cterm colors */
8060 if (cterm_normal_fg_color != 0)
8061 term_fg_color(cterm_normal_fg_color - 1);
8062 if (cterm_normal_bg_color != 0)
8063 term_bg_color(cterm_normal_bg_color - 1);
8064 if (cterm_normal_fg_bold)
8065 out_str(T_MD);
8066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 }
8068 }
8069 }
8070 screen_attr = 0;
8071}
8072
8073/*
8074 * Reset the colors for a cterm. Used when leaving Vim.
8075 * The machine specific code may override this again.
8076 */
8077 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008078reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008080 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {
8082 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008083#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008084 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8085 || cterm_normal_bg_gui_color != INVALCOLOR)
8086 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008087#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {
8091 out_str(T_OP);
8092 screen_attr = -1;
8093 }
8094 if (cterm_normal_fg_bold)
8095 {
8096 out_str(T_ME);
8097 screen_attr = -1;
8098 }
8099 }
8100}
8101
8102/*
8103 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8104 * using the attributes from ScreenAttrs["off"].
8105 */
8106 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008107screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
8109 int attr;
8110
8111 /* Check for illegal values, just in case (could happen just after
8112 * resizing). */
8113 if (row >= screen_Rows || col >= screen_Columns)
8114 return;
8115
Bram Moolenaar494838a2015-02-10 19:20:37 +01008116 /* Outputting a character in the last cell on the screen may scroll the
8117 * screen up. Only do it when the "xn" termcap property is set, otherwise
8118 * mark the character invalid (update it when scrolled up). */
8119 if (*T_XN == NUL
8120 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121#ifdef FEAT_RIGHTLEFT
8122 /* account for first command-line character in rightleft mode */
8123 && !cmdmsg_rl
8124#endif
8125 )
8126 {
8127 ScreenAttrs[off] = (sattr_T)-1;
8128 return;
8129 }
8130
8131 /*
8132 * Stop highlighting first, so it's easier to move the cursor.
8133 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008134#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 if (screen_char_attr != 0)
8136 attr = screen_char_attr;
8137 else
8138#endif
8139 attr = ScreenAttrs[off];
8140 if (screen_attr != attr)
8141 screen_stop_highlight();
8142
8143 windgoto(row, col);
8144
8145 if (screen_attr != attr)
8146 screen_start_highlight(attr);
8147
8148#ifdef FEAT_MBYTE
8149 if (enc_utf8 && ScreenLinesUC[off] != 0)
8150 {
8151 char_u buf[MB_MAXBYTES + 1];
8152
8153 /* Convert UTF-8 character to bytes and write it. */
8154
8155 buf[utfc_char2bytes(off, buf)] = NUL;
8156
8157 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008158 if (utf_ambiguous_width(ScreenLinesUC[off]))
8159 screen_cur_col = 9999;
8160 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 ++screen_cur_col;
8162 }
8163 else
8164#endif
8165 {
8166#ifdef FEAT_MBYTE
8167 out_flush_check();
8168#endif
8169 out_char(ScreenLines[off]);
8170#ifdef FEAT_MBYTE
8171 /* double-byte character in single-width cell */
8172 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8173 out_char(ScreenLines2[off]);
8174#endif
8175 }
8176
8177 screen_cur_col++;
8178}
8179
8180#ifdef FEAT_MBYTE
8181
8182/*
8183 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8184 * on the screen at position 'row' and 'col'.
8185 * The attributes of the first byte is used for all. This is required to
8186 * output the two bytes of a double-byte character with nothing in between.
8187 */
8188 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008189screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190{
8191 /* Check for illegal values (could be wrong when screen was resized). */
8192 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8193 return;
8194
8195 /* Outputting the last character on the screen may scrollup the screen.
8196 * Don't to it! Mark the character invalid (update it when scrolled up) */
8197 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8198 {
8199 ScreenAttrs[off] = (sattr_T)-1;
8200 return;
8201 }
8202
8203 /* Output the first byte normally (positions the cursor), then write the
8204 * second byte directly. */
8205 screen_char(off, row, col);
8206 out_char(ScreenLines[off + 1]);
8207 ++screen_cur_col;
8208}
8209#endif
8210
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008211#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212/*
8213 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8214 * This uses the contents of ScreenLines[] and doesn't change it.
8215 */
8216 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008217screen_draw_rectangle(
8218 int row,
8219 int col,
8220 int height,
8221 int width,
8222 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223{
8224 int r, c;
8225 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008226#ifdef FEAT_MBYTE
8227 int max_off;
8228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008230 /* Can't use ScreenLines unless initialized */
8231 if (ScreenLines == NULL)
8232 return;
8233
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 if (invert)
8235 screen_char_attr = HL_INVERSE;
8236 for (r = row; r < row + height; ++r)
8237 {
8238 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008239#ifdef FEAT_MBYTE
8240 max_off = off + screen_Columns;
8241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 for (c = col; c < col + width; ++c)
8243 {
8244#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008245 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {
8247 screen_char_2(off + c, r, c);
8248 ++c;
8249 }
8250 else
8251#endif
8252 {
8253 screen_char(off + c, r, c);
8254#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008255 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 ++c;
8257#endif
8258 }
8259 }
8260 }
8261 screen_char_attr = 0;
8262}
8263#endif
8264
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008265#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266/*
8267 * Redraw the characters for a vertically split window.
8268 */
8269 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008270redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271{
8272 int col;
8273 int width;
8274
8275# ifdef FEAT_CLIPBOARD
8276 clip_may_clear_selection(row, end - 1);
8277# endif
8278
8279 if (wp == NULL)
8280 {
8281 col = 0;
8282 width = Columns;
8283 }
8284 else
8285 {
8286 col = wp->w_wincol;
8287 width = wp->w_width;
8288 }
8289 screen_draw_rectangle(row, col, end - row, width, FALSE);
8290}
8291#endif
8292
8293/*
8294 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8295 * with character 'c1' in first column followed by 'c2' in the other columns.
8296 * Use attributes 'attr'.
8297 */
8298 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008299screen_fill(
8300 int start_row,
8301 int end_row,
8302 int start_col,
8303 int end_col,
8304 int c1,
8305 int c2,
8306 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307{
8308 int row;
8309 int col;
8310 int off;
8311 int end_off;
8312 int did_delete;
8313 int c;
8314 int norm_term;
8315#if defined(FEAT_GUI) || defined(UNIX)
8316 int force_next = FALSE;
8317#endif
8318
8319 if (end_row > screen_Rows) /* safety check */
8320 end_row = screen_Rows;
8321 if (end_col > screen_Columns) /* safety check */
8322 end_col = screen_Columns;
8323 if (ScreenLines == NULL
8324 || start_row >= end_row
8325 || start_col >= end_col) /* nothing to do */
8326 return;
8327
8328 /* it's a "normal" terminal when not in a GUI or cterm */
8329 norm_term = (
8330#ifdef FEAT_GUI
8331 !gui.in_use &&
8332#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008333 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 for (row = start_row; row < end_row; ++row)
8335 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008336#ifdef FEAT_MBYTE
8337 if (has_mbyte
8338# ifdef FEAT_GUI
8339 && !gui.in_use
8340# endif
8341 )
8342 {
8343 /* When drawing over the right halve of a double-wide char clear
8344 * out the left halve. When drawing over the left halve of a
8345 * double wide-char clear out the right halve. Only needed in a
8346 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008347 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008348 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008349 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008350 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008351 }
8352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 /*
8354 * Try to use delete-line termcap code, when no attributes or in a
8355 * "normal" terminal, where a bold/italic space is just a
8356 * space.
8357 */
8358 did_delete = FALSE;
8359 if (c2 == ' '
8360 && end_col == Columns
8361 && can_clear(T_CE)
8362 && (attr == 0
8363 || (norm_term
8364 && attr <= HL_ALL
8365 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8366 {
8367 /*
8368 * check if we really need to clear something
8369 */
8370 col = start_col;
8371 if (c1 != ' ') /* don't clear first char */
8372 ++col;
8373
8374 off = LineOffset[row] + col;
8375 end_off = LineOffset[row] + end_col;
8376
8377 /* skip blanks (used often, keep it fast!) */
8378#ifdef FEAT_MBYTE
8379 if (enc_utf8)
8380 while (off < end_off && ScreenLines[off] == ' '
8381 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8382 ++off;
8383 else
8384#endif
8385 while (off < end_off && ScreenLines[off] == ' '
8386 && ScreenAttrs[off] == 0)
8387 ++off;
8388 if (off < end_off) /* something to be cleared */
8389 {
8390 col = off - LineOffset[row];
8391 screen_stop_highlight();
8392 term_windgoto(row, col);/* clear rest of this screen line */
8393 out_str(T_CE);
8394 screen_start(); /* don't know where cursor is now */
8395 col = end_col - col;
8396 while (col--) /* clear chars in ScreenLines */
8397 {
8398 ScreenLines[off] = ' ';
8399#ifdef FEAT_MBYTE
8400 if (enc_utf8)
8401 ScreenLinesUC[off] = 0;
8402#endif
8403 ScreenAttrs[off] = 0;
8404 ++off;
8405 }
8406 }
8407 did_delete = TRUE; /* the chars are cleared now */
8408 }
8409
8410 off = LineOffset[row] + start_col;
8411 c = c1;
8412 for (col = start_col; col < end_col; ++col)
8413 {
8414 if (ScreenLines[off] != c
8415#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008416 || (enc_utf8 && (int)ScreenLinesUC[off]
8417 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418#endif
8419 || ScreenAttrs[off] != attr
8420#if defined(FEAT_GUI) || defined(UNIX)
8421 || force_next
8422#endif
8423 )
8424 {
8425#if defined(FEAT_GUI) || defined(UNIX)
8426 /* The bold trick may make a single row of pixels appear in
8427 * the next character. When a bold character is removed, the
8428 * next character should be redrawn too. This happens for our
8429 * own GUI and for some xterms. */
8430 if (
8431# ifdef FEAT_GUI
8432 gui.in_use
8433# endif
8434# if defined(FEAT_GUI) && defined(UNIX)
8435 ||
8436# endif
8437# ifdef UNIX
8438 term_is_xterm
8439# endif
8440 )
8441 {
8442 if (ScreenLines[off] != ' '
8443 && (ScreenAttrs[off] > HL_ALL
8444 || ScreenAttrs[off] & HL_BOLD))
8445 force_next = TRUE;
8446 else
8447 force_next = FALSE;
8448 }
8449#endif
8450 ScreenLines[off] = c;
8451#ifdef FEAT_MBYTE
8452 if (enc_utf8)
8453 {
8454 if (c >= 0x80)
8455 {
8456 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008457 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 }
8459 else
8460 ScreenLinesUC[off] = 0;
8461 }
8462#endif
8463 ScreenAttrs[off] = attr;
8464 if (!did_delete || c != ' ')
8465 screen_char(off, row, col);
8466 }
8467 ++off;
8468 if (col == start_col)
8469 {
8470 if (did_delete)
8471 break;
8472 c = c2;
8473 }
8474 }
8475 if (end_col == Columns)
8476 LineWraps[row] = FALSE;
8477 if (row == Rows - 1) /* overwritten the command line */
8478 {
8479 redraw_cmdline = TRUE;
8480 if (c1 == ' ' && c2 == ' ')
8481 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008482 if (start_col == 0)
8483 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 }
8485 }
8486}
8487
8488/*
8489 * Check if there should be a delay. Used before clearing or redrawing the
8490 * screen or the command line.
8491 */
8492 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008493check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
8495 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8496 && !did_wait_return
8497 && emsg_silent == 0)
8498 {
8499 out_flush();
8500 ui_delay(1000L, TRUE);
8501 emsg_on_display = FALSE;
8502 if (check_msg_scroll)
8503 msg_scroll = FALSE;
8504 }
8505}
8506
8507/*
8508 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008509 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 * Returns TRUE if there is a valid screen to write to.
8511 * Returns FALSE when starting up and screen not initialized yet.
8512 */
8513 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008514screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008516 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 return (ScreenLines != NULL);
8518}
8519
8520/*
8521 * Resize the shell to Rows and Columns.
8522 * Allocate ScreenLines[] and associated items.
8523 *
8524 * There may be some time between setting Rows and Columns and (re)allocating
8525 * ScreenLines[]. This happens when starting up and when (manually) changing
8526 * the shell size. Always use screen_Rows and screen_Columns to access items
8527 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8528 * final size of the shell is needed.
8529 */
8530 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008531screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
8533 int new_row, old_row;
8534#ifdef FEAT_GUI
8535 int old_Rows;
8536#endif
8537 win_T *wp;
8538 int outofmem = FALSE;
8539 int len;
8540 schar_T *new_ScreenLines;
8541#ifdef FEAT_MBYTE
8542 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008543 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008545 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546#endif
8547 sattr_T *new_ScreenAttrs;
8548 unsigned *new_LineOffset;
8549 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008550#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008551 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008552 tabpage_T *tp;
8553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008555 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008556#ifdef FEAT_AUTOCMD
8557 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008559retry:
8560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 /*
8562 * Allocation of the screen buffers is done only when the size changes and
8563 * when Rows and Columns have been set and we have started doing full
8564 * screen stuff.
8565 */
8566 if ((ScreenLines != NULL
8567 && Rows == screen_Rows
8568 && Columns == screen_Columns
8569#ifdef FEAT_MBYTE
8570 && enc_utf8 == (ScreenLinesUC != NULL)
8571 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008572 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573#endif
8574 )
8575 || Rows == 0
8576 || Columns == 0
8577 || (!full_screen && ScreenLines == NULL))
8578 return;
8579
8580 /*
8581 * It's possible that we produce an out-of-memory message below, which
8582 * will cause this function to be called again. To break the loop, just
8583 * return here.
8584 */
8585 if (entered)
8586 return;
8587 entered = TRUE;
8588
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008589 /*
8590 * Note that the window sizes are updated before reallocating the arrays,
8591 * thus we must not redraw here!
8592 */
8593 ++RedrawingDisabled;
8594
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 win_new_shellsize(); /* fit the windows in the new sized shell */
8596
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 comp_col(); /* recompute columns for shown command and ruler */
8598
8599 /*
8600 * We're changing the size of the screen.
8601 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8602 * - Move lines from the old arrays into the new arrays, clear extra
8603 * lines (unless the screen is going to be cleared).
8604 * - Free the old arrays.
8605 *
8606 * If anything fails, make ScreenLines NULL, so we don't do anything!
8607 * Continuing with the old ScreenLines may result in a crash, because the
8608 * size is wrong.
8609 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008610 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008612#ifdef FEAT_AUTOCMD
8613 if (aucmd_win != NULL)
8614 win_free_lsize(aucmd_win);
8615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616
8617 new_ScreenLines = (schar_T *)lalloc((long_u)(
8618 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8619#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008620 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 if (enc_utf8)
8622 {
8623 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8624 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008625 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008626 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8628 }
8629 if (enc_dbcs == DBCS_JPNU)
8630 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8631 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8632#endif
8633 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8634 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8635 new_LineOffset = (unsigned *)lalloc((long_u)(
8636 Rows * sizeof(unsigned)), FALSE);
8637 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008638#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008639 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008642 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 {
8644 if (win_alloc_lines(wp) == FAIL)
8645 {
8646 outofmem = TRUE;
8647#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008648 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649#endif
8650 }
8651 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008652#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008653 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8654 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008655 outofmem = TRUE;
8656#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008657#ifdef FEAT_WINDOWS
8658give_up:
8659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008661#ifdef FEAT_MBYTE
8662 for (i = 0; i < p_mco; ++i)
8663 if (new_ScreenLinesC[i] == NULL)
8664 break;
8665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 if (new_ScreenLines == NULL
8667#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008668 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8670#endif
8671 || new_ScreenAttrs == NULL
8672 || new_LineOffset == NULL
8673 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008674#ifdef FEAT_WINDOWS
8675 || new_TabPageIdxs == NULL
8676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 || outofmem)
8678 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008679 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008680 {
8681 /* guess the size */
8682 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8683
8684 /* Remember we did this to avoid getting outofmem messages over
8685 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008686 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 vim_free(new_ScreenLines);
8689 new_ScreenLines = NULL;
8690#ifdef FEAT_MBYTE
8691 vim_free(new_ScreenLinesUC);
8692 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008693 for (i = 0; i < p_mco; ++i)
8694 {
8695 vim_free(new_ScreenLinesC[i]);
8696 new_ScreenLinesC[i] = NULL;
8697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 vim_free(new_ScreenLines2);
8699 new_ScreenLines2 = NULL;
8700#endif
8701 vim_free(new_ScreenAttrs);
8702 new_ScreenAttrs = NULL;
8703 vim_free(new_LineOffset);
8704 new_LineOffset = NULL;
8705 vim_free(new_LineWraps);
8706 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008707#ifdef FEAT_WINDOWS
8708 vim_free(new_TabPageIdxs);
8709 new_TabPageIdxs = NULL;
8710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 }
8712 else
8713 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008714 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008715
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 for (new_row = 0; new_row < Rows; ++new_row)
8717 {
8718 new_LineOffset[new_row] = new_row * Columns;
8719 new_LineWraps[new_row] = FALSE;
8720
8721 /*
8722 * If the screen is not going to be cleared, copy as much as
8723 * possible from the old screen to the new one and clear the rest
8724 * (used when resizing the window at the "--more--" prompt or when
8725 * executing an external command, for the GUI).
8726 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008727 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 {
8729 (void)vim_memset(new_ScreenLines + new_row * Columns,
8730 ' ', (size_t)Columns * sizeof(schar_T));
8731#ifdef FEAT_MBYTE
8732 if (enc_utf8)
8733 {
8734 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8735 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008736 for (i = 0; i < p_mco; ++i)
8737 (void)vim_memset(new_ScreenLinesC[i]
8738 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 0, (size_t)Columns * sizeof(u8char_T));
8740 }
8741 if (enc_dbcs == DBCS_JPNU)
8742 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8743 0, (size_t)Columns * sizeof(schar_T));
8744#endif
8745 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8746 0, (size_t)Columns * sizeof(sattr_T));
8747 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008748 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 {
8750 if (screen_Columns < Columns)
8751 len = screen_Columns;
8752 else
8753 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008754#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008755 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008756 * may be invalid now. Also when p_mco changes. */
8757 if (!(enc_utf8 && ScreenLinesUC == NULL)
8758 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008759#endif
8760 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8761 ScreenLines + LineOffset[old_row],
8762 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008764 if (enc_utf8 && ScreenLinesUC != NULL
8765 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 {
8767 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8768 ScreenLinesUC + LineOffset[old_row],
8769 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008770 for (i = 0; i < p_mco; ++i)
8771 mch_memmove(new_ScreenLinesC[i]
8772 + new_LineOffset[new_row],
8773 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 (size_t)len * sizeof(u8char_T));
8775 }
8776 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8777 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8778 ScreenLines2 + LineOffset[old_row],
8779 (size_t)len * sizeof(schar_T));
8780#endif
8781 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8782 ScreenAttrs + LineOffset[old_row],
8783 (size_t)len * sizeof(sattr_T));
8784 }
8785 }
8786 }
8787 /* Use the last line of the screen for the current line. */
8788 current_ScreenLine = new_ScreenLines + Rows * Columns;
8789 }
8790
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008791 free_screenlines();
8792
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 ScreenLines = new_ScreenLines;
8794#ifdef FEAT_MBYTE
8795 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008796 for (i = 0; i < p_mco; ++i)
8797 ScreenLinesC[i] = new_ScreenLinesC[i];
8798 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 ScreenLines2 = new_ScreenLines2;
8800#endif
8801 ScreenAttrs = new_ScreenAttrs;
8802 LineOffset = new_LineOffset;
8803 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008804#ifdef FEAT_WINDOWS
8805 TabPageIdxs = new_TabPageIdxs;
8806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807
8808 /* It's important that screen_Rows and screen_Columns reflect the actual
8809 * size of ScreenLines[]. Set them before calling anything. */
8810#ifdef FEAT_GUI
8811 old_Rows = screen_Rows;
8812#endif
8813 screen_Rows = Rows;
8814 screen_Columns = Columns;
8815
8816 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008817 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 screenclear2();
8819
8820#ifdef FEAT_GUI
8821 else if (gui.in_use
8822 && !gui.starting
8823 && ScreenLines != NULL
8824 && old_Rows != Rows)
8825 {
8826 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8827 /*
8828 * Adjust the position of the cursor, for when executing an external
8829 * command.
8830 */
8831 if (msg_row >= Rows) /* Rows got smaller */
8832 msg_row = Rows - 1; /* put cursor at last row */
8833 else if (Rows > old_Rows) /* Rows got bigger */
8834 msg_row += Rows - old_Rows; /* put cursor in same place */
8835 if (msg_col >= Columns) /* Columns got smaller */
8836 msg_col = Columns - 1; /* put cursor at last column */
8837 }
8838#endif
8839
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008841 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008842
8843#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008844 /*
8845 * Do not apply autocommands more than 3 times to avoid an endless loop
8846 * in case applying autocommands always changes Rows or Columns.
8847 */
8848 if (starting == 0 && ++retry_count <= 3)
8849 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008850 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008851 /* In rare cases, autocommands may have altered Rows or Columns,
8852 * jump back to check if we need to allocate the screen again. */
8853 goto retry;
8854 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856}
8857
8858 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008859free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008860{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008861#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008862 int i;
8863
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008864 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008865 for (i = 0; i < Screen_mco; ++i)
8866 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008867 vim_free(ScreenLines2);
8868#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008869 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008870 vim_free(ScreenAttrs);
8871 vim_free(LineOffset);
8872 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008873#ifdef FEAT_WINDOWS
8874 vim_free(TabPageIdxs);
8875#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008876}
8877
8878 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008879screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
8881 check_for_delay(FALSE);
8882 screenalloc(FALSE); /* allocate screen buffers if size changed */
8883 screenclear2(); /* clear the screen */
8884}
8885
8886 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008887screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 int i;
8890
8891 if (starting == NO_SCREEN || ScreenLines == NULL
8892#ifdef FEAT_GUI
8893 || (gui.in_use && gui.starting)
8894#endif
8895 )
8896 return;
8897
8898#ifdef FEAT_GUI
8899 if (!gui.in_use)
8900#endif
8901 screen_attr = -1; /* force setting the Normal colors */
8902 screen_stop_highlight(); /* don't want highlighting here */
8903
8904#ifdef FEAT_CLIPBOARD
8905 /* disable selection without redrawing it */
8906 clip_scroll_selection(9999);
8907#endif
8908
8909 /* blank out ScreenLines */
8910 for (i = 0; i < Rows; ++i)
8911 {
8912 lineclear(LineOffset[i], (int)Columns);
8913 LineWraps[i] = FALSE;
8914 }
8915
8916 if (can_clear(T_CL))
8917 {
8918 out_str(T_CL); /* clear the display */
8919 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008920 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 }
8922 else
8923 {
8924 /* can't clear the screen, mark all chars with invalid attributes */
8925 for (i = 0; i < Rows; ++i)
8926 lineinvalid(LineOffset[i], (int)Columns);
8927 clear_cmdline = TRUE;
8928 }
8929
8930 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8931
8932 win_rest_invalid(firstwin);
8933 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008934#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008935 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 if (must_redraw == CLEAR) /* no need to clear again */
8938 must_redraw = NOT_VALID;
8939 compute_cmdrow();
8940 msg_row = cmdline_row; /* put cursor on last line for messages */
8941 msg_col = 0;
8942 screen_start(); /* don't know where cursor is now */
8943 msg_scrolled = 0; /* can't scroll back */
8944 msg_didany = FALSE;
8945 msg_didout = FALSE;
8946}
8947
8948/*
8949 * Clear one line in ScreenLines.
8950 */
8951 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008952lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953{
8954 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8955#ifdef FEAT_MBYTE
8956 if (enc_utf8)
8957 (void)vim_memset(ScreenLinesUC + off, 0,
8958 (size_t)width * sizeof(u8char_T));
8959#endif
8960 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8961}
8962
8963/*
8964 * Mark one line in ScreenLines invalid by setting the attributes to an
8965 * invalid value.
8966 */
8967 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008968lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
8970 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8971}
8972
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008973#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974/*
8975 * Copy part of a Screenline for vertically split window "wp".
8976 */
8977 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008978linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
8980 unsigned off_to = LineOffset[to] + wp->w_wincol;
8981 unsigned off_from = LineOffset[from] + wp->w_wincol;
8982
8983 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8984 wp->w_width * sizeof(schar_T));
8985# ifdef FEAT_MBYTE
8986 if (enc_utf8)
8987 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008988 int i;
8989
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8991 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008992 for (i = 0; i < p_mco; ++i)
8993 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8994 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 }
8996 if (enc_dbcs == DBCS_JPNU)
8997 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8998 wp->w_width * sizeof(schar_T));
8999# endif
9000 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9001 wp->w_width * sizeof(sattr_T));
9002}
9003#endif
9004
9005/*
9006 * Return TRUE if clearing with term string "p" would work.
9007 * It can't work when the string is empty or it won't set the right background.
9008 */
9009 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009010can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011{
9012 return (*p != NUL && (t_colors <= 1
9013#ifdef FEAT_GUI
9014 || gui.in_use
9015#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009016#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009017 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009018 || (!p_tgc && cterm_normal_bg_color == 0)
9019#else
9020 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009021#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009022 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023}
9024
9025/*
9026 * Reset cursor position. Use whenever cursor was moved because of outputting
9027 * something directly to the screen (shell commands) or a terminal control
9028 * code.
9029 */
9030 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009031screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032{
9033 screen_cur_row = screen_cur_col = 9999;
9034}
9035
9036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 * Move the cursor to position "row","col" in the screen.
9038 * This tries to find the most efficient way to move, minimizing the number of
9039 * characters sent to the terminal.
9040 */
9041 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009042windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009044 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 int i;
9046 int plan;
9047 int cost;
9048 int wouldbe_col;
9049 int noinvcurs;
9050 char_u *bs;
9051 int goto_cost;
9052 int attr;
9053
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009054#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9056
9057#define PLAN_LE 1
9058#define PLAN_CR 2
9059#define PLAN_NL 3
9060#define PLAN_WRITE 4
9061 /* Can't use ScreenLines unless initialized */
9062 if (ScreenLines == NULL)
9063 return;
9064
9065 if (col != screen_cur_col || row != screen_cur_row)
9066 {
9067 /* Check for valid position. */
9068 if (row < 0) /* window without text lines? */
9069 row = 0;
9070 if (row >= screen_Rows)
9071 row = screen_Rows - 1;
9072 if (col >= screen_Columns)
9073 col = screen_Columns - 1;
9074
9075 /* check if no cursor movement is allowed in highlight mode */
9076 if (screen_attr && *T_MS == NUL)
9077 noinvcurs = HIGHL_COST;
9078 else
9079 noinvcurs = 0;
9080 goto_cost = GOTO_COST + noinvcurs;
9081
9082 /*
9083 * Plan how to do the positioning:
9084 * 1. Use CR to move it to column 0, same row.
9085 * 2. Use T_LE to move it a few columns to the left.
9086 * 3. Use NL to move a few lines down, column 0.
9087 * 4. Move a few columns to the right with T_ND or by writing chars.
9088 *
9089 * Don't do this if the cursor went beyond the last column, the cursor
9090 * position is unknown then (some terminals wrap, some don't )
9091 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009092 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 * characters to move the cursor to the right.
9094 */
9095 if (row >= screen_cur_row && screen_cur_col < Columns)
9096 {
9097 /*
9098 * If the cursor is in the same row, bigger col, we can use CR
9099 * or T_LE.
9100 */
9101 bs = NULL; /* init for GCC */
9102 attr = screen_attr;
9103 if (row == screen_cur_row && col < screen_cur_col)
9104 {
9105 /* "le" is preferred over "bc", because "bc" is obsolete */
9106 if (*T_LE)
9107 bs = T_LE; /* "cursor left" */
9108 else
9109 bs = T_BC; /* "backspace character (old) */
9110 if (*bs)
9111 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9112 else
9113 cost = 999;
9114 if (col + 1 < cost) /* using CR is less characters */
9115 {
9116 plan = PLAN_CR;
9117 wouldbe_col = 0;
9118 cost = 1; /* CR is just one character */
9119 }
9120 else
9121 {
9122 plan = PLAN_LE;
9123 wouldbe_col = col;
9124 }
9125 if (noinvcurs) /* will stop highlighting */
9126 {
9127 cost += noinvcurs;
9128 attr = 0;
9129 }
9130 }
9131
9132 /*
9133 * If the cursor is above where we want to be, we can use CR LF.
9134 */
9135 else if (row > screen_cur_row)
9136 {
9137 plan = PLAN_NL;
9138 wouldbe_col = 0;
9139 cost = (row - screen_cur_row) * 2; /* CR LF */
9140 if (noinvcurs) /* will stop highlighting */
9141 {
9142 cost += noinvcurs;
9143 attr = 0;
9144 }
9145 }
9146
9147 /*
9148 * If the cursor is in the same row, smaller col, just use write.
9149 */
9150 else
9151 {
9152 plan = PLAN_WRITE;
9153 wouldbe_col = screen_cur_col;
9154 cost = 0;
9155 }
9156
9157 /*
9158 * Check if any characters that need to be written have the
9159 * correct attributes. Also avoid UTF-8 characters.
9160 */
9161 i = col - wouldbe_col;
9162 if (i > 0)
9163 cost += i;
9164 if (cost < goto_cost && i > 0)
9165 {
9166 /*
9167 * Check if the attributes are correct without additionally
9168 * stopping highlighting.
9169 */
9170 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9171 while (i && *p++ == attr)
9172 --i;
9173 if (i != 0)
9174 {
9175 /*
9176 * Try if it works when highlighting is stopped here.
9177 */
9178 if (*--p == 0)
9179 {
9180 cost += noinvcurs;
9181 while (i && *p++ == 0)
9182 --i;
9183 }
9184 if (i != 0)
9185 cost = 999; /* different attributes, don't do it */
9186 }
9187#ifdef FEAT_MBYTE
9188 if (enc_utf8)
9189 {
9190 /* Don't use an UTF-8 char for positioning, it's slow. */
9191 for (i = wouldbe_col; i < col; ++i)
9192 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9193 {
9194 cost = 999;
9195 break;
9196 }
9197 }
9198#endif
9199 }
9200
9201 /*
9202 * We can do it without term_windgoto()!
9203 */
9204 if (cost < goto_cost)
9205 {
9206 if (plan == PLAN_LE)
9207 {
9208 if (noinvcurs)
9209 screen_stop_highlight();
9210 while (screen_cur_col > col)
9211 {
9212 out_str(bs);
9213 --screen_cur_col;
9214 }
9215 }
9216 else if (plan == PLAN_CR)
9217 {
9218 if (noinvcurs)
9219 screen_stop_highlight();
9220 out_char('\r');
9221 screen_cur_col = 0;
9222 }
9223 else if (plan == PLAN_NL)
9224 {
9225 if (noinvcurs)
9226 screen_stop_highlight();
9227 while (screen_cur_row < row)
9228 {
9229 out_char('\n');
9230 ++screen_cur_row;
9231 }
9232 screen_cur_col = 0;
9233 }
9234
9235 i = col - screen_cur_col;
9236 if (i > 0)
9237 {
9238 /*
9239 * Use cursor-right if it's one character only. Avoids
9240 * removing a line of pixels from the last bold char, when
9241 * using the bold trick in the GUI.
9242 */
9243 if (T_ND[0] != NUL && T_ND[1] == NUL)
9244 {
9245 while (i-- > 0)
9246 out_char(*T_ND);
9247 }
9248 else
9249 {
9250 int off;
9251
9252 off = LineOffset[row] + screen_cur_col;
9253 while (i-- > 0)
9254 {
9255 if (ScreenAttrs[off] != screen_attr)
9256 screen_stop_highlight();
9257#ifdef FEAT_MBYTE
9258 out_flush_check();
9259#endif
9260 out_char(ScreenLines[off]);
9261#ifdef FEAT_MBYTE
9262 if (enc_dbcs == DBCS_JPNU
9263 && ScreenLines[off] == 0x8e)
9264 out_char(ScreenLines2[off]);
9265#endif
9266 ++off;
9267 }
9268 }
9269 }
9270 }
9271 }
9272 else
9273 cost = 999;
9274
9275 if (cost >= goto_cost)
9276 {
9277 if (noinvcurs)
9278 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009279 if (row == screen_cur_row && (col > screen_cur_col)
9280 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 term_cursor_right(col - screen_cur_col);
9282 else
9283 term_windgoto(row, col);
9284 }
9285 screen_cur_row = row;
9286 screen_cur_col = col;
9287 }
9288}
9289
9290/*
9291 * Set cursor to its position in the current window.
9292 */
9293 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009294setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295{
9296 if (redrawing())
9297 {
9298 validate_cursor();
9299 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9300 W_WINCOL(curwin) + (
9301#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009302 /* With 'rightleft' set and the cursor on a double-wide
9303 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9305# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009306 (has_mbyte
9307 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9308 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309# endif
9310 1)) :
9311#endif
9312 curwin->w_wcol));
9313 }
9314}
9315
9316
9317/*
9318 * insert 'line_count' lines at 'row' in window 'wp'
9319 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9320 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9321 * scrolling.
9322 * Returns FAIL if the lines are not inserted, OK for success.
9323 */
9324 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009325win_ins_lines(
9326 win_T *wp,
9327 int row,
9328 int line_count,
9329 int invalid,
9330 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331{
9332 int did_delete;
9333 int nextrow;
9334 int lastrow;
9335 int retval;
9336
9337 if (invalid)
9338 wp->w_lines_valid = 0;
9339
9340 if (wp->w_height < 5)
9341 return FAIL;
9342
9343 if (line_count > wp->w_height - row)
9344 line_count = wp->w_height - row;
9345
9346 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9347 if (retval != MAYBE)
9348 return retval;
9349
9350 /*
9351 * If there is a next window or a status line, we first try to delete the
9352 * lines at the bottom to avoid messing what is after the window.
9353 * If this fails and there are following windows, don't do anything to avoid
9354 * messing up those windows, better just redraw.
9355 */
9356 did_delete = FALSE;
9357#ifdef FEAT_WINDOWS
9358 if (wp->w_next != NULL || wp->w_status_height)
9359 {
9360 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9361 line_count, (int)Rows, FALSE, NULL) == OK)
9362 did_delete = TRUE;
9363 else if (wp->w_next)
9364 return FAIL;
9365 }
9366#endif
9367 /*
9368 * if no lines deleted, blank the lines that will end up below the window
9369 */
9370 if (!did_delete)
9371 {
9372#ifdef FEAT_WINDOWS
9373 wp->w_redr_status = TRUE;
9374#endif
9375 redraw_cmdline = TRUE;
9376 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9377 lastrow = nextrow + line_count;
9378 if (lastrow > Rows)
9379 lastrow = Rows;
9380 screen_fill(nextrow - line_count, lastrow - line_count,
9381 W_WINCOL(wp), (int)W_ENDCOL(wp),
9382 ' ', ' ', 0);
9383 }
9384
9385 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9386 == FAIL)
9387 {
9388 /* deletion will have messed up other windows */
9389 if (did_delete)
9390 {
9391#ifdef FEAT_WINDOWS
9392 wp->w_redr_status = TRUE;
9393#endif
9394 win_rest_invalid(W_NEXT(wp));
9395 }
9396 return FAIL;
9397 }
9398
9399 return OK;
9400}
9401
9402/*
9403 * delete "line_count" window lines at "row" in window "wp"
9404 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9405 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9406 * scrolling
9407 * Return OK for success, FAIL if the lines are not deleted.
9408 */
9409 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009410win_del_lines(
9411 win_T *wp,
9412 int row,
9413 int line_count,
9414 int invalid,
9415 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416{
9417 int retval;
9418
9419 if (invalid)
9420 wp->w_lines_valid = 0;
9421
9422 if (line_count > wp->w_height - row)
9423 line_count = wp->w_height - row;
9424
9425 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9426 if (retval != MAYBE)
9427 return retval;
9428
9429 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9430 (int)Rows, FALSE, NULL) == FAIL)
9431 return FAIL;
9432
9433#ifdef FEAT_WINDOWS
9434 /*
9435 * If there are windows or status lines below, try to put them at the
9436 * correct place. If we can't do that, they have to be redrawn.
9437 */
9438 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9439 {
9440 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9441 line_count, (int)Rows, NULL) == FAIL)
9442 {
9443 wp->w_redr_status = TRUE;
9444 win_rest_invalid(wp->w_next);
9445 }
9446 }
9447 /*
9448 * If this is the last window and there is no status line, redraw the
9449 * command line later.
9450 */
9451 else
9452#endif
9453 redraw_cmdline = TRUE;
9454 return OK;
9455}
9456
9457/*
9458 * Common code for win_ins_lines() and win_del_lines().
9459 * Returns OK or FAIL when the work has been done.
9460 * Returns MAYBE when not finished yet.
9461 */
9462 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009463win_do_lines(
9464 win_T *wp,
9465 int row,
9466 int line_count,
9467 int mayclear,
9468 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 int retval;
9471
9472 if (!redrawing() || line_count <= 0)
9473 return FAIL;
9474
9475 /* only a few lines left: redraw is faster */
9476 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009477#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 && wp->w_width == Columns
9479#endif
9480 )
9481 {
9482 screenclear(); /* will set wp->w_lines_valid to 0 */
9483 return FAIL;
9484 }
9485
9486 /*
9487 * Delete all remaining lines
9488 */
9489 if (row + line_count >= wp->w_height)
9490 {
9491 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9492 W_WINCOL(wp), (int)W_ENDCOL(wp),
9493 ' ', ' ', 0);
9494 return OK;
9495 }
9496
9497 /*
9498 * when scrolling, the message on the command line should be cleared,
9499 * otherwise it will stay there forever.
9500 */
9501 clear_cmdline = TRUE;
9502
9503 /*
9504 * If the terminal can set a scroll region, use that.
9505 * Always do this in a vertically split window. This will redraw from
9506 * ScreenLines[] when t_CV isn't defined. That's faster than using
9507 * win_line().
9508 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009509 * a character in the lower right corner of the scroll region may cause a
9510 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 */
9512 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009513#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 || W_WIDTH(wp) != Columns
9515#endif
9516 )
9517 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009518#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9520#endif
9521 scroll_region_set(wp, row);
9522 if (del)
9523 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9524 wp->w_height - row, FALSE, wp);
9525 else
9526 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9527 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009528#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9530#endif
9531 scroll_region_reset();
9532 return retval;
9533 }
9534
9535#ifdef FEAT_WINDOWS
9536 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9537 return FAIL;
9538#endif
9539
9540 return MAYBE;
9541}
9542
9543/*
9544 * window 'wp' and everything after it is messed up, mark it for redraw
9545 */
9546 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009547win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548{
9549#ifdef FEAT_WINDOWS
9550 while (wp != NULL)
9551#else
9552 if (wp != NULL)
9553#endif
9554 {
9555 redraw_win_later(wp, NOT_VALID);
9556#ifdef FEAT_WINDOWS
9557 wp->w_redr_status = TRUE;
9558 wp = wp->w_next;
9559#endif
9560 }
9561 redraw_cmdline = TRUE;
9562}
9563
9564/*
9565 * The rest of the routines in this file perform screen manipulations. The
9566 * given operation is performed physically on the screen. The corresponding
9567 * change is also made to the internal screen image. In this way, the editor
9568 * anticipates the effect of editing changes on the appearance of the screen.
9569 * That way, when we call screenupdate a complete redraw isn't usually
9570 * necessary. Another advantage is that we can keep adding code to anticipate
9571 * screen changes, and in the meantime, everything still works.
9572 */
9573
9574/*
9575 * types for inserting or deleting lines
9576 */
9577#define USE_T_CAL 1
9578#define USE_T_CDL 2
9579#define USE_T_AL 3
9580#define USE_T_CE 4
9581#define USE_T_DL 5
9582#define USE_T_SR 6
9583#define USE_NL 7
9584#define USE_T_CD 8
9585#define USE_REDRAW 9
9586
9587/*
9588 * insert lines on the screen and update ScreenLines[]
9589 * 'end' is the line after the scrolled part. Normally it is Rows.
9590 * When scrolling region used 'off' is the offset from the top for the region.
9591 * 'row' and 'end' are relative to the start of the region.
9592 *
9593 * return FAIL for failure, OK for success.
9594 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009595 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009596screen_ins_lines(
9597 int off,
9598 int row,
9599 int line_count,
9600 int end,
9601 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602{
9603 int i;
9604 int j;
9605 unsigned temp;
9606 int cursor_row;
9607 int type;
9608 int result_empty;
9609 int can_ce = can_clear(T_CE);
9610
9611 /*
9612 * FAIL if
9613 * - there is no valid screen
9614 * - the screen has to be redrawn completely
9615 * - the line count is less than one
9616 * - the line count is more than 'ttyscroll'
9617 */
9618 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9619 return FAIL;
9620
9621 /*
9622 * There are seven ways to insert lines:
9623 * 0. When in a vertically split window and t_CV isn't set, redraw the
9624 * characters from ScreenLines[].
9625 * 1. Use T_CD (clear to end of display) if it exists and the result of
9626 * the insert is just empty lines
9627 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9628 * present or line_count > 1. It looks better if we do all the inserts
9629 * at once.
9630 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9631 * insert is just empty lines and T_CE is not present or line_count >
9632 * 1.
9633 * 4. Use T_AL (insert line) if it exists.
9634 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9635 * just empty lines.
9636 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9637 * just empty lines.
9638 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9639 * the 'da' flag is not set or we have clear line capability.
9640 * 8. redraw the characters from ScreenLines[].
9641 *
9642 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9643 * the scrollbar for the window. It does have insert line, use that if it
9644 * exists.
9645 */
9646 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009647#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9649 type = USE_REDRAW;
9650 else
9651#endif
9652 if (can_clear(T_CD) && result_empty)
9653 type = USE_T_CD;
9654 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9655 type = USE_T_CAL;
9656 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9657 type = USE_T_CDL;
9658 else if (*T_AL != NUL)
9659 type = USE_T_AL;
9660 else if (can_ce && result_empty)
9661 type = USE_T_CE;
9662 else if (*T_DL != NUL && result_empty)
9663 type = USE_T_DL;
9664 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9665 type = USE_T_SR;
9666 else
9667 return FAIL;
9668
9669 /*
9670 * For clearing the lines screen_del_lines() is used. This will also take
9671 * care of t_db if necessary.
9672 */
9673 if (type == USE_T_CD || type == USE_T_CDL ||
9674 type == USE_T_CE || type == USE_T_DL)
9675 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9676
9677 /*
9678 * If text is retained below the screen, first clear or delete as many
9679 * lines at the bottom of the window as are about to be inserted so that
9680 * the deleted lines won't later surface during a screen_del_lines.
9681 */
9682 if (*T_DB)
9683 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9684
9685#ifdef FEAT_CLIPBOARD
9686 /* Remove a modeless selection when inserting lines halfway the screen
9687 * or not the full width of the screen. */
9688 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009689# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 || (wp != NULL && wp->w_width != Columns)
9691# endif
9692 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009693 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 else
9695 clip_scroll_selection(-line_count);
9696#endif
9697
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698#ifdef FEAT_GUI
9699 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9700 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009701 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702#endif
9703
9704 if (*T_CCS != NUL) /* cursor relative to region */
9705 cursor_row = row;
9706 else
9707 cursor_row = row + off;
9708
9709 /*
9710 * Shift LineOffset[] line_count down to reflect the inserted lines.
9711 * Clear the inserted lines in ScreenLines[].
9712 */
9713 row += off;
9714 end += off;
9715 for (i = 0; i < line_count; ++i)
9716 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009717#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 if (wp != NULL && wp->w_width != Columns)
9719 {
9720 /* need to copy part of a line */
9721 j = end - 1 - i;
9722 while ((j -= line_count) >= row)
9723 linecopy(j + line_count, j, wp);
9724 j += line_count;
9725 if (can_clear((char_u *)" "))
9726 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9727 else
9728 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9729 LineWraps[j] = FALSE;
9730 }
9731 else
9732#endif
9733 {
9734 j = end - 1 - i;
9735 temp = LineOffset[j];
9736 while ((j -= line_count) >= row)
9737 {
9738 LineOffset[j + line_count] = LineOffset[j];
9739 LineWraps[j + line_count] = LineWraps[j];
9740 }
9741 LineOffset[j + line_count] = temp;
9742 LineWraps[j + line_count] = FALSE;
9743 if (can_clear((char_u *)" "))
9744 lineclear(temp, (int)Columns);
9745 else
9746 lineinvalid(temp, (int)Columns);
9747 }
9748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749
9750 screen_stop_highlight();
9751 windgoto(cursor_row, 0);
9752
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009753#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 /* redraw the characters */
9755 if (type == USE_REDRAW)
9756 redraw_block(row, end, wp);
9757 else
9758#endif
9759 if (type == USE_T_CAL)
9760 {
9761 term_append_lines(line_count);
9762 screen_start(); /* don't know where cursor is now */
9763 }
9764 else
9765 {
9766 for (i = 0; i < line_count; i++)
9767 {
9768 if (type == USE_T_AL)
9769 {
9770 if (i && cursor_row != 0)
9771 windgoto(cursor_row, 0);
9772 out_str(T_AL);
9773 }
9774 else /* type == USE_T_SR */
9775 out_str(T_SR);
9776 screen_start(); /* don't know where cursor is now */
9777 }
9778 }
9779
9780 /*
9781 * With scroll-reverse and 'da' flag set we need to clear the lines that
9782 * have been scrolled down into the region.
9783 */
9784 if (type == USE_T_SR && *T_DA)
9785 {
9786 for (i = 0; i < line_count; ++i)
9787 {
9788 windgoto(off + i, 0);
9789 out_str(T_CE);
9790 screen_start(); /* don't know where cursor is now */
9791 }
9792 }
9793
9794#ifdef FEAT_GUI
9795 gui_can_update_cursor();
9796 if (gui.in_use)
9797 out_flush(); /* always flush after a scroll */
9798#endif
9799 return OK;
9800}
9801
9802/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009803 * Delete lines on the screen and update ScreenLines[].
9804 * "end" is the line after the scrolled part. Normally it is Rows.
9805 * When scrolling region used "off" is the offset from the top for the region.
9806 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 *
9808 * Return OK for success, FAIL if the lines are not deleted.
9809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009811screen_del_lines(
9812 int off,
9813 int row,
9814 int line_count,
9815 int end,
9816 int force, /* even when line_count > p_ttyscroll */
9817 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818{
9819 int j;
9820 int i;
9821 unsigned temp;
9822 int cursor_row;
9823 int cursor_end;
9824 int result_empty; /* result is empty until end of region */
9825 int can_delete; /* deleting line codes can be used */
9826 int type;
9827
9828 /*
9829 * FAIL if
9830 * - there is no valid screen
9831 * - the screen has to be redrawn completely
9832 * - the line count is less than one
9833 * - the line count is more than 'ttyscroll'
9834 */
9835 if (!screen_valid(TRUE) || line_count <= 0 ||
9836 (!force && line_count > p_ttyscroll))
9837 return FAIL;
9838
9839 /*
9840 * Check if the rest of the current region will become empty.
9841 */
9842 result_empty = row + line_count >= end;
9843
9844 /*
9845 * We can delete lines only when 'db' flag not set or when 'ce' option
9846 * available.
9847 */
9848 can_delete = (*T_DB == NUL || can_clear(T_CE));
9849
9850 /*
9851 * There are six ways to delete lines:
9852 * 0. When in a vertically split window and t_CV isn't set, redraw the
9853 * characters from ScreenLines[].
9854 * 1. Use T_CD if it exists and the result is empty.
9855 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9856 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9857 * none of the other ways work.
9858 * 4. Use T_CE (erase line) if the result is empty.
9859 * 5. Use T_DL (delete line) if it exists.
9860 * 6. redraw the characters from ScreenLines[].
9861 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009862#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9864 type = USE_REDRAW;
9865 else
9866#endif
9867 if (can_clear(T_CD) && result_empty)
9868 type = USE_T_CD;
9869#if defined(__BEOS__) && defined(BEOS_DR8)
9870 /*
9871 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9872 * its internal termcap... this works okay for tests which test *T_DB !=
9873 * NUL. It has the disadvantage that the user cannot use any :set t_*
9874 * command to get T_DB (back) to empty_option, only :set term=... will do
9875 * the trick...
9876 * Anyway, this hack will hopefully go away with the next OS release.
9877 * (Olaf Seibert)
9878 */
9879 else if (row == 0 && T_DB == empty_option
9880 && (line_count == 1 || *T_CDL == NUL))
9881#else
9882 else if (row == 0 && (
9883#ifndef AMIGA
9884 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9885 * up, so use delete-line command */
9886 line_count == 1 ||
9887#endif
9888 *T_CDL == NUL))
9889#endif
9890 type = USE_NL;
9891 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9892 type = USE_T_CDL;
9893 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009894#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 && (wp == NULL || wp->w_width == Columns)
9896#endif
9897 )
9898 type = USE_T_CE;
9899 else if (*T_DL != NUL && can_delete)
9900 type = USE_T_DL;
9901 else if (*T_CDL != NUL && can_delete)
9902 type = USE_T_CDL;
9903 else
9904 return FAIL;
9905
9906#ifdef FEAT_CLIPBOARD
9907 /* Remove a modeless selection when deleting lines halfway the screen or
9908 * not the full width of the screen. */
9909 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009910# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 || (wp != NULL && wp->w_width != Columns)
9912# endif
9913 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009914 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 else
9916 clip_scroll_selection(line_count);
9917#endif
9918
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919#ifdef FEAT_GUI
9920 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9921 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009922 gui_dont_update_cursor(gui.cursor_row >= row + off
9923 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924#endif
9925
9926 if (*T_CCS != NUL) /* cursor relative to region */
9927 {
9928 cursor_row = row;
9929 cursor_end = end;
9930 }
9931 else
9932 {
9933 cursor_row = row + off;
9934 cursor_end = end + off;
9935 }
9936
9937 /*
9938 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9939 * Clear the inserted lines in ScreenLines[].
9940 */
9941 row += off;
9942 end += off;
9943 for (i = 0; i < line_count; ++i)
9944 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009945#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 if (wp != NULL && wp->w_width != Columns)
9947 {
9948 /* need to copy part of a line */
9949 j = row + i;
9950 while ((j += line_count) <= end - 1)
9951 linecopy(j - line_count, j, wp);
9952 j -= line_count;
9953 if (can_clear((char_u *)" "))
9954 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9955 else
9956 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9957 LineWraps[j] = FALSE;
9958 }
9959 else
9960#endif
9961 {
9962 /* whole width, moving the line pointers is faster */
9963 j = row + i;
9964 temp = LineOffset[j];
9965 while ((j += line_count) <= end - 1)
9966 {
9967 LineOffset[j - line_count] = LineOffset[j];
9968 LineWraps[j - line_count] = LineWraps[j];
9969 }
9970 LineOffset[j - line_count] = temp;
9971 LineWraps[j - line_count] = FALSE;
9972 if (can_clear((char_u *)" "))
9973 lineclear(temp, (int)Columns);
9974 else
9975 lineinvalid(temp, (int)Columns);
9976 }
9977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978
9979 screen_stop_highlight();
9980
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009981#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 /* redraw the characters */
9983 if (type == USE_REDRAW)
9984 redraw_block(row, end, wp);
9985 else
9986#endif
9987 if (type == USE_T_CD) /* delete the lines */
9988 {
9989 windgoto(cursor_row, 0);
9990 out_str(T_CD);
9991 screen_start(); /* don't know where cursor is now */
9992 }
9993 else if (type == USE_T_CDL)
9994 {
9995 windgoto(cursor_row, 0);
9996 term_delete_lines(line_count);
9997 screen_start(); /* don't know where cursor is now */
9998 }
9999 /*
10000 * Deleting lines at top of the screen or scroll region: Just scroll
10001 * the whole screen (scroll region) up by outputting newlines on the
10002 * last line.
10003 */
10004 else if (type == USE_NL)
10005 {
10006 windgoto(cursor_end - 1, 0);
10007 for (i = line_count; --i >= 0; )
10008 out_char('\n'); /* cursor will remain on same line */
10009 }
10010 else
10011 {
10012 for (i = line_count; --i >= 0; )
10013 {
10014 if (type == USE_T_DL)
10015 {
10016 windgoto(cursor_row, 0);
10017 out_str(T_DL); /* delete a line */
10018 }
10019 else /* type == USE_T_CE */
10020 {
10021 windgoto(cursor_row + i, 0);
10022 out_str(T_CE); /* erase a line */
10023 }
10024 screen_start(); /* don't know where cursor is now */
10025 }
10026 }
10027
10028 /*
10029 * If the 'db' flag is set, we need to clear the lines that have been
10030 * scrolled up at the bottom of the region.
10031 */
10032 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10033 {
10034 for (i = line_count; i > 0; --i)
10035 {
10036 windgoto(cursor_end - i, 0);
10037 out_str(T_CE); /* erase a line */
10038 screen_start(); /* don't know where cursor is now */
10039 }
10040 }
10041
10042#ifdef FEAT_GUI
10043 gui_can_update_cursor();
10044 if (gui.in_use)
10045 out_flush(); /* always flush after a scroll */
10046#endif
10047
10048 return OK;
10049}
10050
10051/*
10052 * show the current mode and ruler
10053 *
10054 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10055 * If clear_cmdline is FALSE there may be a message there that needs to be
10056 * cleared only if a mode is shown.
10057 * Return the length of the message (0 if no message).
10058 */
10059 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010060showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061{
10062 int need_clear;
10063 int length = 0;
10064 int do_mode;
10065 int attr;
10066 int nwr_save;
10067#ifdef FEAT_INS_EXPAND
10068 int sub_attr;
10069#endif
10070
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010071 do_mode = ((p_smd && msg_silent == 0)
10072 && ((State & INSERT)
10073 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010074 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 if (do_mode || Recording)
10076 {
10077 /*
10078 * Don't show mode right now, when not redrawing or inside a mapping.
10079 * Call char_avail() only when we are going to show something, because
10080 * it takes a bit of time.
10081 */
10082 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10083 {
10084 redraw_cmdline = TRUE; /* show mode later */
10085 return 0;
10086 }
10087
10088 nwr_save = need_wait_return;
10089
10090 /* wait a bit before overwriting an important message */
10091 check_for_delay(FALSE);
10092
10093 /* if the cmdline is more than one line high, erase top lines */
10094 need_clear = clear_cmdline;
10095 if (clear_cmdline && cmdline_row < Rows - 1)
10096 msg_clr_cmdline(); /* will reset clear_cmdline */
10097
10098 /* Position on the last line in the window, column 0 */
10099 msg_pos_mode();
10100 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010101 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 if (do_mode)
10103 {
10104 MSG_PUTS_ATTR("--", attr);
10105#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010106 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010107# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010108 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010109# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010110 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010111# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010112 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010113# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 MSG_PUTS_ATTR(" IM", attr);
10115# else
10116 MSG_PUTS_ATTR(" XIM", attr);
10117# endif
10118#endif
10119#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10120 if (gui.in_use)
10121 {
10122 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010123 {
10124 /* HANGUL */
10125 if (enc_utf8)
10126 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10127 else
10128 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 }
10131#endif
10132#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010133 /* CTRL-X in Insert mode */
10134 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 {
10136 /* These messages can get long, avoid a wrap in a narrow
10137 * window. Prefer showing edit_submode_extra. */
10138 length = (Rows - msg_row) * Columns - 3;
10139 if (edit_submode_extra != NULL)
10140 length -= vim_strsize(edit_submode_extra);
10141 if (length > 0)
10142 {
10143 if (edit_submode_pre != NULL)
10144 length -= vim_strsize(edit_submode_pre);
10145 if (length - vim_strsize(edit_submode) > 0)
10146 {
10147 if (edit_submode_pre != NULL)
10148 msg_puts_attr(edit_submode_pre, attr);
10149 msg_puts_attr(edit_submode, attr);
10150 }
10151 if (edit_submode_extra != NULL)
10152 {
10153 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10154 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010155 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 else
10157 sub_attr = attr;
10158 msg_puts_attr(edit_submode_extra, sub_attr);
10159 }
10160 }
10161 length = 0;
10162 }
10163 else
10164#endif
10165 {
10166#ifdef FEAT_VREPLACE
10167 if (State & VREPLACE_FLAG)
10168 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10169 else
10170#endif
10171 if (State & REPLACE_FLAG)
10172 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10173 else if (State & INSERT)
10174 {
10175#ifdef FEAT_RIGHTLEFT
10176 if (p_ri)
10177 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10178#endif
10179 MSG_PUTS_ATTR(_(" INSERT"), attr);
10180 }
10181 else if (restart_edit == 'I')
10182 MSG_PUTS_ATTR(_(" (insert)"), attr);
10183 else if (restart_edit == 'R')
10184 MSG_PUTS_ATTR(_(" (replace)"), attr);
10185 else if (restart_edit == 'V')
10186 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10187#ifdef FEAT_RIGHTLEFT
10188 if (p_hkmap)
10189 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10190# ifdef FEAT_FKMAP
10191 if (p_fkmap)
10192 MSG_PUTS_ATTR(farsi_text_5, attr);
10193# endif
10194#endif
10195#ifdef FEAT_KEYMAP
10196 if (State & LANGMAP)
10197 {
10198# ifdef FEAT_ARABIC
10199 if (curwin->w_p_arab)
10200 MSG_PUTS_ATTR(_(" Arabic"), attr);
10201 else
10202# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010203 if (get_keymap_str(curwin, (char_u *)" (%s)",
10204 NameBuff, MAXPATHL))
10205 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 }
10207#endif
10208 if ((State & INSERT) && p_paste)
10209 MSG_PUTS_ATTR(_(" (paste)"), attr);
10210
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 if (VIsual_active)
10212 {
10213 char *p;
10214
10215 /* Don't concatenate separate words to avoid translation
10216 * problems. */
10217 switch ((VIsual_select ? 4 : 0)
10218 + (VIsual_mode == Ctrl_V) * 2
10219 + (VIsual_mode == 'V'))
10220 {
10221 case 0: p = N_(" VISUAL"); break;
10222 case 1: p = N_(" VISUAL LINE"); break;
10223 case 2: p = N_(" VISUAL BLOCK"); break;
10224 case 4: p = N_(" SELECT"); break;
10225 case 5: p = N_(" SELECT LINE"); break;
10226 default: p = N_(" SELECT BLOCK"); break;
10227 }
10228 MSG_PUTS_ATTR(_(p), attr);
10229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 MSG_PUTS_ATTR(" --", attr);
10231 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010232
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 need_clear = TRUE;
10234 }
10235 if (Recording
10236#ifdef FEAT_INS_EXPAND
10237 && edit_submode == NULL /* otherwise it gets too long */
10238#endif
10239 )
10240 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010241 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 need_clear = TRUE;
10243 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010244
10245 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 if (need_clear || clear_cmdline)
10247 msg_clr_eos();
10248 msg_didout = FALSE; /* overwrite this message */
10249 length = msg_col;
10250 msg_col = 0;
10251 need_wait_return = nwr_save; /* never ask for hit-return for this */
10252 }
10253 else if (clear_cmdline && msg_silent == 0)
10254 /* Clear the whole command line. Will reset "clear_cmdline". */
10255 msg_clr_cmdline();
10256
10257#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 /* In Visual mode the size of the selected area must be redrawn. */
10259 if (VIsual_active)
10260 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261
10262 /* If the last window has no status line, the ruler is after the mode
10263 * message and must be redrawn */
10264 if (redrawing()
10265# ifdef FEAT_WINDOWS
10266 && lastwin->w_status_height == 0
10267# endif
10268 )
10269 win_redr_ruler(lastwin, TRUE);
10270#endif
10271 redraw_cmdline = FALSE;
10272 clear_cmdline = FALSE;
10273
10274 return length;
10275}
10276
10277/*
10278 * Position for a mode message.
10279 */
10280 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010281msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282{
10283 msg_col = 0;
10284 msg_row = Rows - 1;
10285}
10286
10287/*
10288 * Delete mode message. Used when ESC is typed which is expected to end
10289 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010290 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 */
10292 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010293unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294{
10295 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010296 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 */
10298 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10299 redraw_cmdline = TRUE; /* delete mode later */
10300 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010301 clearmode();
10302}
10303
10304/*
10305 * Clear the mode message.
10306 */
10307 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010308clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010309{
10310 msg_pos_mode();
10311 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010312 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010313 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314}
10315
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010316 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010317recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010318{
10319 MSG_PUTS_ATTR(_("recording"), attr);
10320 if (!shortmess(SHM_RECORDING))
10321 {
10322 char_u s[4];
10323 sprintf((char *)s, " @%c", Recording);
10324 MSG_PUTS_ATTR(s, attr);
10325 }
10326}
10327
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010328#if defined(FEAT_WINDOWS)
10329/*
10330 * Draw the tab pages line at the top of the Vim window.
10331 */
10332 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010333draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010334{
10335 int tabcount = 0;
10336 tabpage_T *tp;
10337 int tabwidth;
10338 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010339 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010340 int attr;
10341 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010342 win_T *cwp;
10343 int wincount;
10344 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010345 int c;
10346 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010347 int attr_sel = HL_ATTR(HLF_TPS);
10348 int attr_nosel = HL_ATTR(HLF_TP);
10349 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010350 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010351 int room;
10352 int use_sep_chars = (t_colors < 8
10353#ifdef FEAT_GUI
10354 && !gui.in_use
10355#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010356#ifdef FEAT_TERMGUICOLORS
10357 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010358#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010359 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010360
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010361 if (ScreenLines == NULL)
10362 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010363 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010364
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010365#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010366 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010367 if (gui_use_tabline())
10368 {
10369 gui_update_tabline();
10370 return;
10371 }
10372#endif
10373
10374 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010375 return;
10376
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010377#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010378
10379 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10380 for (scol = 0; scol < Columns; ++scol)
10381 TabPageIdxs[scol] = 0;
10382
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010383 /* Use the 'tabline' option if it's set. */
10384 if (*p_tal != NUL)
10385 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010386 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010387
10388 /* Check for an error. If there is one we would loop in redrawing the
10389 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010390 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010391 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010392 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010393 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010394 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010395 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010396 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010397 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010398#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010399 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010400 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010401 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010402
Bram Moolenaar238a5642006-02-21 22:12:05 +000010403 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10404 if (tabwidth < 6)
10405 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010406
Bram Moolenaar238a5642006-02-21 22:12:05 +000010407 attr = attr_nosel;
10408 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010409 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010410 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10411 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010412 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010413 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010414
Bram Moolenaar238a5642006-02-21 22:12:05 +000010415 if (tp->tp_topframe == topframe)
10416 attr = attr_sel;
10417 if (use_sep_chars && col > 0)
10418 screen_putchar('|', 0, col++, attr);
10419
10420 if (tp->tp_topframe != topframe)
10421 attr = attr_nosel;
10422
10423 screen_putchar(' ', 0, col++, attr);
10424
10425 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010426 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010427 cwp = curwin;
10428 wp = firstwin;
10429 }
10430 else
10431 {
10432 cwp = tp->tp_curwin;
10433 wp = tp->tp_firstwin;
10434 }
10435
10436 modified = FALSE;
10437 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10438 if (bufIsChanged(wp->w_buffer))
10439 modified = TRUE;
10440 if (modified || wincount > 1)
10441 {
10442 if (wincount > 1)
10443 {
10444 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010445 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010446 if (col + len >= Columns - 3)
10447 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010448 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010449#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010450 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010451#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010452 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010453#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010454 );
10455 col += len;
10456 }
10457 if (modified)
10458 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10459 screen_putchar(' ', 0, col++, attr);
10460 }
10461
10462 room = scol - col + tabwidth - 1;
10463 if (room > 0)
10464 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010465 /* Get buffer name in NameBuff[] */
10466 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010467 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010468 len = vim_strsize(NameBuff);
10469 p = NameBuff;
10470#ifdef FEAT_MBYTE
10471 if (has_mbyte)
10472 while (len > room)
10473 {
10474 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010475 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010476 }
10477 else
10478#endif
10479 if (len > room)
10480 {
10481 p += len - room;
10482 len = room;
10483 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010484 if (len > Columns - col - 1)
10485 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010486
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010487 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010488 col += len;
10489 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010490 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010491
10492 /* Store the tab page number in TabPageIdxs[], so that
10493 * jump_to_mouse() knows where each one is. */
10494 ++tabcount;
10495 while (scol < col)
10496 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010497 }
10498
Bram Moolenaar238a5642006-02-21 22:12:05 +000010499 if (use_sep_chars)
10500 c = '_';
10501 else
10502 c = ' ';
10503 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010504
10505 /* Put an "X" for closing the current tab if there are several. */
10506 if (first_tabpage->tp_next != NULL)
10507 {
10508 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10509 TabPageIdxs[Columns - 1] = -999;
10510 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010511 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010512
10513 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10514 * set. */
10515 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010516}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010517
10518/*
10519 * Get buffer name for "buf" into NameBuff[].
10520 * Takes care of special buffer names and translates special characters.
10521 */
10522 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010523get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010524{
10525 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010526 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010527 else
10528 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10529 trans_characters(NameBuff, MAXPATHL);
10530}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010531#endif
10532
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10534/*
10535 * Get the character to use in a status line. Get its attributes in "*attr".
10536 */
10537 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010538fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539{
10540 int fill;
10541 if (is_curwin)
10542 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010543 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 fill = fill_stl;
10545 }
10546 else
10547 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010548 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 fill = fill_stlnc;
10550 }
10551 /* Use fill when there is highlighting, and highlighting of current
10552 * window differs, or the fillchars differ, or this is not the
10553 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010554 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010555 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 || (fill_stl != fill_stlnc)))
10557 return fill;
10558 if (is_curwin)
10559 return '^';
10560 return '=';
10561}
10562#endif
10563
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010564#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565/*
10566 * Get the character to use in a separator between vertically split windows.
10567 * Get its attributes in "*attr".
10568 */
10569 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010570fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010572 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 if (*attr == 0 && fill_vert == ' ')
10574 return '|';
10575 else
10576 return fill_vert;
10577}
10578#endif
10579
10580/*
10581 * Return TRUE if redrawing should currently be done.
10582 */
10583 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010584redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010586#ifdef FEAT_EVAL
10587 if (disable_redraw_for_testing)
10588 return 0;
10589 else
10590#endif
10591 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10593}
10594
10595/*
10596 * Return TRUE if printing messages should currently be done.
10597 */
10598 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010599messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600{
10601 return (!(p_lz && char_avail() && !KeyTyped));
10602}
10603
10604/*
10605 * Show current status info in ruler and various other places
10606 * If always is FALSE, only show ruler if position has changed.
10607 */
10608 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010609showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610{
10611 if (!always && !redrawing())
10612 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010613#ifdef FEAT_INS_EXPAND
10614 if (pum_visible())
10615 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010616# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010617 /* Don't redraw right now, do it later. */
10618 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010619# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010620 return;
10621 }
10622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010624 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010625 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010626 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 else
10629#endif
10630#ifdef FEAT_CMDL_INFO
10631 win_redr_ruler(curwin, always);
10632#endif
10633
10634#ifdef FEAT_TITLE
10635 if (need_maketitle
10636# ifdef FEAT_STL_OPT
10637 || (p_icon && (stl_syntax & STL_IN_ICON))
10638 || (p_title && (stl_syntax & STL_IN_TITLE))
10639# endif
10640 )
10641 maketitle();
10642#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010643#ifdef FEAT_WINDOWS
10644 /* Redraw the tab pages line if needed. */
10645 if (redraw_tabline)
10646 draw_tabline();
10647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648}
10649
10650#ifdef FEAT_CMDL_INFO
10651 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010652win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010654#define RULER_BUF_LEN 70
10655 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 int row;
10657 int fillchar;
10658 int attr;
10659 int empty_line = FALSE;
10660 colnr_T virtcol;
10661 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010662 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010664#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 int this_ru_col;
10666 int off = 0;
10667 int width = Columns;
10668# define WITH_OFF(x) x
10669# define WITH_WIDTH(x) x
10670#else
10671# define WITH_OFF(x) 0
10672# define WITH_WIDTH(x) Columns
10673# define this_ru_col ru_col
10674#endif
10675
10676 /* If 'ruler' off or redrawing disabled, don't do anything */
10677 if (!p_ru)
10678 return;
10679
10680 /*
10681 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10682 * after deleting lines, before cursor.lnum is corrected.
10683 */
10684 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10685 return;
10686
10687#ifdef FEAT_INS_EXPAND
10688 /* Don't draw the ruler while doing insert-completion, it might overwrite
10689 * the (long) mode message. */
10690# ifdef FEAT_WINDOWS
10691 if (wp == lastwin && lastwin->w_status_height == 0)
10692# endif
10693 if (edit_submode != NULL)
10694 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010695 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10696 if (pum_visible())
10697 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698#endif
10699
10700#ifdef FEAT_STL_OPT
10701 if (*p_ruf)
10702 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010703 int save_called_emsg = called_emsg;
10704
10705 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010707 if (called_emsg)
10708 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010709 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010710 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 return;
10712 }
10713#endif
10714
10715 /*
10716 * Check if not in Insert mode and the line is empty (will show "0-1").
10717 */
10718 if (!(State & INSERT)
10719 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10720 empty_line = TRUE;
10721
10722 /*
10723 * Only draw the ruler when something changed.
10724 */
10725 validate_virtcol_win(wp);
10726 if ( redraw_cmdline
10727 || always
10728 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10729 || wp->w_cursor.col != wp->w_ru_cursor.col
10730 || wp->w_virtcol != wp->w_ru_virtcol
10731#ifdef FEAT_VIRTUALEDIT
10732 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10733#endif
10734 || wp->w_topline != wp->w_ru_topline
10735 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10736#ifdef FEAT_DIFF
10737 || wp->w_topfill != wp->w_ru_topfill
10738#endif
10739 || empty_line != wp->w_ru_empty)
10740 {
10741 cursor_off();
10742#ifdef FEAT_WINDOWS
10743 if (wp->w_status_height)
10744 {
10745 row = W_WINROW(wp) + wp->w_height;
10746 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 off = W_WINCOL(wp);
10748 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 }
10750 else
10751#endif
10752 {
10753 row = Rows - 1;
10754 fillchar = ' ';
10755 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010756#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 width = Columns;
10758 off = 0;
10759#endif
10760 }
10761
10762 /* In list mode virtcol needs to be recomputed */
10763 virtcol = wp->w_virtcol;
10764 if (wp->w_p_list && lcs_tab1 == NUL)
10765 {
10766 wp->w_p_list = FALSE;
10767 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10768 wp->w_p_list = TRUE;
10769 }
10770
10771 /*
10772 * Some sprintfs return the length, some return a pointer.
10773 * To avoid portability problems we use strlen() here.
10774 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010775 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10777 ? 0L
10778 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010779 len = STRLEN(buffer);
10780 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10782 (int)virtcol + 1);
10783
10784 /*
10785 * Add a "50%" if there is room for it.
10786 * On the last line, don't print in the last column (scrolls the
10787 * screen up on some terminals).
10788 */
10789 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010790 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 o = i + vim_strsize(buffer + i + 1);
10792#ifdef FEAT_WINDOWS
10793 if (wp->w_status_height == 0) /* can't use last char of screen */
10794#endif
10795 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010796#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 this_ru_col = ru_col - (Columns - width);
10798 if (this_ru_col < 0)
10799 this_ru_col = 0;
10800#endif
10801 /* Never use more than half the window/screen width, leave the other
10802 * half for the filename. */
10803 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10804 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10805 if (this_ru_col + o < WITH_WIDTH(width))
10806 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010807 /* need at least 3 chars left for get_rel_pos() + NUL */
10808 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 {
10810#ifdef FEAT_MBYTE
10811 if (has_mbyte)
10812 i += (*mb_char2bytes)(fillchar, buffer + i);
10813 else
10814#endif
10815 buffer[i++] = fillchar;
10816 ++o;
10817 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010818 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 }
10820 /* Truncate at window boundary. */
10821#ifdef FEAT_MBYTE
10822 if (has_mbyte)
10823 {
10824 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010825 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 {
10827 o += (*mb_ptr2cells)(buffer + i);
10828 if (this_ru_col + o > WITH_WIDTH(width))
10829 {
10830 buffer[i] = NUL;
10831 break;
10832 }
10833 }
10834 }
10835 else
10836#endif
10837 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10838 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10839
10840 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10841 i = redraw_cmdline;
10842 screen_fill(row, row + 1,
10843 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10844 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10845 fillchar, fillchar, attr);
10846 /* don't redraw the cmdline because of showing the ruler */
10847 redraw_cmdline = i;
10848 wp->w_ru_cursor = wp->w_cursor;
10849 wp->w_ru_virtcol = wp->w_virtcol;
10850 wp->w_ru_empty = empty_line;
10851 wp->w_ru_topline = wp->w_topline;
10852 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10853#ifdef FEAT_DIFF
10854 wp->w_ru_topfill = wp->w_topfill;
10855#endif
10856 }
10857}
10858#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010859
10860#if defined(FEAT_LINEBREAK) || defined(PROTO)
10861/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010862 * Return the width of the 'number' and 'relativenumber' column.
10863 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010864 * Otherwise it depends on 'numberwidth' and the line count.
10865 */
10866 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010867number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010868{
10869 int n;
10870 linenr_T lnum;
10871
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010872 if (wp->w_p_rnu && !wp->w_p_nu)
10873 /* cursor line shows "0" */
10874 lnum = wp->w_height;
10875 else
10876 /* cursor line shows absolute line number */
10877 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010878
Bram Moolenaar6b314672015-03-20 15:42:10 +010010879 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010880 return wp->w_nrwidth_width;
10881 wp->w_nrwidth_line_count = lnum;
10882
10883 n = 0;
10884 do
10885 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010886 lnum /= 10;
10887 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010888 } while (lnum > 0);
10889
10890 /* 'numberwidth' gives the minimal width plus one */
10891 if (n < wp->w_p_nuw - 1)
10892 n = wp->w_p_nuw - 1;
10893
10894 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010895 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010896 return n;
10897}
10898#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010899
10900/*
10901 * Return the current cursor column. This is the actual position on the
10902 * screen. First column is 0.
10903 */
10904 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010905screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010906{
10907 return screen_cur_col;
10908}
10909
10910/*
10911 * Return the current cursor row. This is the actual position on the screen.
10912 * First row is 0.
10913 */
10914 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010915screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010916{
10917 return screen_cur_row;
10918}