blob: 2c9bbef9e53c339a5002b3a681c5d8b2c84a5706 [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),
2142 hl_attr(HLF_AT));
2143 screen_fill(scr_row, scr_row + 1,
2144 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
2145 '@', ' ', hl_attr(HLF_AT));
2146 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),
2157 '@', '@', hl_attr(HLF_AT));
2158 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),
2285 ' ', ' ', hl_attr(HLF_FC));
2286 }
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,
2298 ' ', ' ', hl_attr(HLF_SC));
2299 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,
2304 c2, c2, hl_attr(hl));
2305 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2306 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2307 c1, c2, hl_attr(hl));
2308 }
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,
2321 cmdwin_type, ' ', hl_attr(HLF_AT));
2322 }
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,
2334 ' ', ' ', hl_attr(HLF_FC));
2335 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,
2348 ' ', ' ', hl_attr(HLF_SC));
2349 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),
2354 c1, c2, hl_attr(hl));
2355 }
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;
2431 ScreenAttrs[off] = hl_attr(HLF_AT);
2432#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,
2454 hl_attr(HLF_FC));
2455 /* 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
2461 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2462 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 Moolenaar4317d9b2005-03-18 20:25:31 +00002479 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,
2494 (char_u *)" ", len, hl_attr(HLF_FL));
2495 else
2496# endif
2497 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2498 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,
2539 hl_attr(HLF_FL));
2540 else
2541#endif
2542 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2543 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 Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701 else
2702 ScreenLinesUC[off + col] = 0;
2703 }
2704#endif
2705 ScreenLines[off + col++] = fill_fold;
2706 }
2707
2708 if (text != buf)
2709 vim_free(text);
2710
2711 /*
2712 * 6. set highlighting for the Visual area an other text.
2713 * If all folded lines are in the Visual area, highlight the line.
2714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2716 {
2717 if (ltoreq(curwin->w_cursor, VIsual))
2718 {
2719 /* Visual is after curwin->w_cursor */
2720 top = &curwin->w_cursor;
2721 bot = &VIsual;
2722 }
2723 else
2724 {
2725 /* Visual is before curwin->w_cursor */
2726 top = &VIsual;
2727 bot = &curwin->w_cursor;
2728 }
2729 if (lnum >= top->lnum
2730 && lnume <= bot->lnum
2731 && (VIsual_mode != 'v'
2732 || ((lnum > top->lnum
2733 || (lnum == top->lnum
2734 && top->col == 0))
2735 && (lnume < bot->lnum
2736 || (lnume == bot->lnum
2737 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002738 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
2740 if (VIsual_mode == Ctrl_V)
2741 {
2742 /* Visual block mode: highlight the chars part of the block */
2743 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2744 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002745 if (wp->w_old_cursor_lcol != MAXCOL
2746 && wp->w_old_cursor_lcol + txtcol
2747 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 len = wp->w_old_cursor_lcol;
2749 else
2750 len = W_WIDTH(wp) - txtcol;
2751 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002752 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754 }
2755 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002758 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002763#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002764 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2765 if (wp->w_p_cc_cols)
2766 {
2767 int i = 0;
2768 int j = wp->w_p_cc_cols[i];
2769 int old_txtcol = txtcol;
2770
2771 while (j > -1)
2772 {
2773 txtcol += j;
2774 if (wp->w_p_wrap)
2775 txtcol -= wp->w_skipcol;
2776 else
2777 txtcol -= wp->w_leftcol;
2778 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2779 ScreenAttrs[off + txtcol] = hl_combine_attr(
2780 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2781 txtcol = old_txtcol;
2782 j = wp->w_p_cc_cols[++i];
2783 }
2784 }
2785
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002786 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002787 if (wp->w_p_cuc)
2788 {
2789 txtcol += wp->w_virtcol;
2790 if (wp->w_p_wrap)
2791 txtcol -= wp->w_skipcol;
2792 else
2793 txtcol -= wp->w_leftcol;
2794 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2795 ScreenAttrs[off + txtcol] = hl_combine_attr(
2796 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2797 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2801 (int)W_WIDTH(wp), FALSE);
2802
2803 /*
2804 * Update w_cline_height and w_cline_folded if the cursor line was
2805 * updated (saves a call to plines() later).
2806 */
2807 if (wp == curwin
2808 && lnum <= curwin->w_cursor.lnum
2809 && lnume >= curwin->w_cursor.lnum)
2810 {
2811 curwin->w_cline_row = row;
2812 curwin->w_cline_height = 1;
2813 curwin->w_cline_folded = TRUE;
2814 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2815 }
2816}
2817
2818/*
2819 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2820 */
2821 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002822copy_text_attr(
2823 int off,
2824 char_u *buf,
2825 int len,
2826 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002828 int i;
2829
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 mch_memmove(ScreenLines + off, buf, (size_t)len);
2831# ifdef FEAT_MBYTE
2832 if (enc_utf8)
2833 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2834# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002835 for (i = 0; i < len; ++i)
2836 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837}
2838
2839/*
2840 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002841 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 */
2843 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002844fill_foldcolumn(
2845 char_u *p,
2846 win_T *wp,
2847 int closed, /* TRUE of FALSE */
2848 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
2850 int i = 0;
2851 int level;
2852 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002853 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002854 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
2856 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002857 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 level = win_foldinfo.fi_level;
2860 if (level > 0)
2861 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002862 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002863 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002864
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 /* If the column is too narrow, we start at the lowest level that
2866 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002867 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 if (first_level < 1)
2869 first_level = 1;
2870
Bram Moolenaar1c934292015-01-27 16:39:29 +01002871 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 {
2873 if (win_foldinfo.fi_lnum == lnum
2874 && first_level + i >= win_foldinfo.fi_low_level)
2875 p[i] = '-';
2876 else if (first_level == 1)
2877 p[i] = '|';
2878 else if (first_level + i <= 9)
2879 p[i] = '0' + first_level + i;
2880 else
2881 p[i] = '>';
2882 if (first_level + i == level)
2883 break;
2884 }
2885 }
2886 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002887 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888}
2889#endif /* FEAT_FOLDING */
2890
2891/*
2892 * Display line "lnum" of window 'wp' on the screen.
2893 * Start at row "startrow", stop when "endrow" is reached.
2894 * wp->w_virtcol needs to be valid.
2895 *
2896 * Return the number of last row the line occupies.
2897 */
2898 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002899win_line(
2900 win_T *wp,
2901 linenr_T lnum,
2902 int startrow,
2903 int endrow,
2904 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01002906 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2908 int c = 0; /* init for GCC */
2909 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002910#ifdef FEAT_LINEBREAK
2911 long vcol_sbr = -1; /* virtual column after showbreak */
2912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 long vcol_prev = -1; /* "vcol" of previous character */
2914 char_u *line; /* current line */
2915 char_u *ptr; /* current position in "line" */
2916 int row; /* row in the window, excl w_winrow */
2917 int screen_row; /* row on the screen, incl w_winrow */
2918
2919 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2920 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002921 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002922 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 int c_extra = NUL; /* extra chars, all the same */
2924 int extra_attr = 0; /* attributes when n_extra != 0 */
2925 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2926 displaying lcs_eol at end-of-line */
2927 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2928 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2929
2930 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2931 int saved_n_extra = 0;
2932 char_u *saved_p_extra = NULL;
2933 int saved_c_extra = 0;
2934 int saved_char_attr = 0;
2935
2936 int n_attr = 0; /* chars with special attr */
2937 int saved_attr2 = 0; /* char_attr saved for n_attr */
2938 int n_attr3 = 0; /* chars with overruling special attr */
2939 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2940
2941 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2942
2943 int fromcol, tocol; /* start/end of inverting */
2944 int fromcol_prev = -2; /* start of inverting after cursor */
2945 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002947 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 pos_T pos;
2949 long v;
2950
2951 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002952 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2954 in this line */
2955 int attr = 0; /* attributes for area highlighting */
2956 int area_attr = 0; /* attributes desired by highlighting */
2957 int search_attr = 0; /* attributes desired by 'hlsearch' */
2958#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002959 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 int syntax_attr = 0; /* attributes desired by syntax */
2961 int has_syntax = FALSE; /* this buffer has syntax highl. */
2962 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002963 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002964 int draw_color_col = FALSE; /* highlight colorcolumn */
2965 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002966#endif
2967#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002968 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002969# define SPWORDLEN 150
2970 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002971 int nextlinecol = 0; /* column where nextline[] starts */
2972 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002973 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002974 int spell_attr = 0; /* attributes desired by spelling */
2975 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002976 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2977 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002978 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002979 static int cap_col = -1; /* column to check for Cap word */
2980 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002981 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982#endif
2983 int extra_check; /* has syntax or linebreak */
2984#ifdef FEAT_MBYTE
2985 int multi_attr = 0; /* attributes desired by multibyte */
2986 int mb_l = 1; /* multi-byte byte length */
2987 int mb_c = 0; /* decoded multi-byte character */
2988 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002989 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990#endif
2991#ifdef FEAT_DIFF
2992 int filler_lines; /* nr of filler lines to be drawn */
2993 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002994 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 int change_start = MAXCOL; /* first col of changed area */
2996 int change_end = -1; /* last col of changed area */
2997#endif
2998 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2999#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003000 int need_showbreak = FALSE; /* overlong line, skipping first x
3001 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003003#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3004 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003006 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007#endif
3008#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003009 matchitem_T *cur; /* points to the match list */
3010 match_T *shl; /* points to search_hl or a match */
3011 int shl_flag; /* flag to indicate whether search_hl
3012 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003013 int pos_inprogress; /* marks that position match search is
3014 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003015 int prevcol_hl_flag; /* flag to indicate whether prevcol
3016 equals startcol of search_hl or one
3017 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018#endif
3019#ifdef FEAT_ARABIC
3020 int prev_c = 0; /* previous Arabic character */
3021 int prev_c1 = 0; /* first composing char for prev_c */
3022#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003023#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003024 int did_line_attr = 0;
3025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026
3027 /* draw_state: items that are drawn in sequence: */
3028#define WL_START 0 /* nothing done yet */
3029#ifdef FEAT_CMDWIN
3030# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3031#else
3032# define WL_CMDLINE WL_START
3033#endif
3034#ifdef FEAT_FOLDING
3035# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3036#else
3037# define WL_FOLD WL_CMDLINE
3038#endif
3039#ifdef FEAT_SIGNS
3040# define WL_SIGN WL_FOLD + 1 /* column for signs */
3041#else
3042# define WL_SIGN WL_FOLD /* column for signs */
3043#endif
3044#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003045#ifdef FEAT_LINEBREAK
3046# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003048# define WL_BRI WL_NR
3049#endif
3050#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3051# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3052#else
3053# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054#endif
3055#define WL_LINE WL_SBR + 1 /* text in the line */
3056 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003057#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 int feedback_col = 0;
3059 int feedback_old_attr = -1;
3060#endif
3061
Bram Moolenaar860cae12010-06-05 23:22:07 +02003062#ifdef FEAT_CONCEAL
3063 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003064 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003065 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003066 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003067 int is_concealing = FALSE;
3068 int boguscols = 0; /* nonexistent columns added to force
3069 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003070 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003071 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003072 int match_conc = 0; /* cchar for match functions */
3073 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003074 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003075# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003076# define FIX_FOR_BOGUSCOLS \
3077 { \
3078 n_extra += vcol_off; \
3079 vcol -= vcol_off; \
3080 vcol_off = 0; \
3081 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003082 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003083 boguscols = 0; \
3084 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003085#else
3086# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
3089 if (startrow > endrow) /* past the end already! */
3090 return startrow;
3091
3092 row = startrow;
3093 screen_row = row + W_WINROW(wp);
3094
3095 /*
3096 * To speed up the loop below, set extra_check when there is linebreak,
3097 * trailing white space and/or syntax processing to be done.
3098 */
3099#ifdef FEAT_LINEBREAK
3100 extra_check = wp->w_p_lbr;
3101#else
3102 extra_check = 0;
3103#endif
3104#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003105 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 {
3107 /* Prepare for syntax highlighting in this line. When there is an
3108 * error, stop syntax highlighting. */
3109 save_did_emsg = did_emsg;
3110 did_emsg = FALSE;
3111 syntax_start(wp, lnum);
3112 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003113 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 else
3115 {
3116 did_emsg = save_did_emsg;
3117 has_syntax = TRUE;
3118 extra_check = TRUE;
3119 }
3120 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003121
3122 /* Check for columns to display for 'colorcolumn'. */
3123 color_cols = wp->w_p_cc_cols;
3124 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003125 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003126#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003127
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003128#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003129 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003130 && *wp->w_s->b_p_spl != NUL
3131 && wp->w_s->b_langp.ga_len > 0
3132 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003133 {
3134 /* Prepare for spell checking. */
3135 has_spell = TRUE;
3136 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003137
3138 /* Get the start of the next line, so that words that wrap to the next
3139 * line are found too: "et<line-break>al.".
3140 * Trick: skip a few chars for C/shell/Vim comments */
3141 nextline[SPWORDLEN] = NUL;
3142 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3143 {
3144 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3145 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3146 }
3147
3148 /* When a word wrapped from the previous line the start of the current
3149 * line is valid. */
3150 if (lnum == checked_lnum)
3151 cur_checked_col = checked_col;
3152 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003153
3154 /* When there was a sentence end in the previous line may require a
3155 * word starting with capital in this line. In line 1 always check
3156 * the first word. */
3157 if (lnum != capcol_lnum)
3158 cap_col = -1;
3159 if (lnum == 1)
3160 cap_col = 0;
3161 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#endif
3164
3165 /*
3166 * handle visual active in this window
3167 */
3168 fromcol = -10;
3169 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3171 {
3172 /* Visual is after curwin->w_cursor */
3173 if (ltoreq(curwin->w_cursor, VIsual))
3174 {
3175 top = &curwin->w_cursor;
3176 bot = &VIsual;
3177 }
3178 else /* Visual is before curwin->w_cursor */
3179 {
3180 top = &VIsual;
3181 bot = &curwin->w_cursor;
3182 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003183 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (VIsual_mode == Ctrl_V) /* block mode */
3185 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003186 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 fromcol = wp->w_old_cursor_fcol;
3189 tocol = wp->w_old_cursor_lcol;
3190 }
3191 }
3192 else /* non-block mode */
3193 {
3194 if (lnum > top->lnum && lnum <= bot->lnum)
3195 fromcol = 0;
3196 else if (lnum == top->lnum)
3197 {
3198 if (VIsual_mode == 'V') /* linewise */
3199 fromcol = 0;
3200 else
3201 {
3202 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3203 if (gchar_pos(top) == NUL)
3204 tocol = fromcol + 1;
3205 }
3206 }
3207 if (VIsual_mode != 'V' && lnum == bot->lnum)
3208 {
3209 if (*p_sel == 'e' && bot->col == 0
3210#ifdef FEAT_VIRTUALEDIT
3211 && bot->coladd == 0
3212#endif
3213 )
3214 {
3215 fromcol = -10;
3216 tocol = MAXCOL;
3217 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003218 else if (bot->col == MAXCOL)
3219 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 else
3221 {
3222 pos = *bot;
3223 if (*p_sel == 'e')
3224 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3225 else
3226 {
3227 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3228 ++tocol;
3229 }
3230 }
3231 }
3232 }
3233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 /* Check if the character under the cursor should not be inverted */
3235 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003236#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 )
3240 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
3242 /* if inverting in this line set area_highlighting */
3243 if (fromcol >= 0)
3244 {
3245 area_highlighting = TRUE;
3246 attr = hl_attr(HLF_V);
3247#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003248 if ((clip_star.available && !clip_star.owned
3249 && clip_isautosel_star())
3250 || (clip_plus.available && !clip_plus.owned
3251 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 attr = hl_attr(HLF_VNC);
3253#endif
3254 }
3255 }
3256
3257 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003258 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003260 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 && wp == curwin
3262 && lnum >= curwin->w_cursor.lnum
3263 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3264 {
3265 if (lnum == curwin->w_cursor.lnum)
3266 getvcol(curwin, &(curwin->w_cursor),
3267 (colnr_T *)&fromcol, NULL, NULL);
3268 else
3269 fromcol = 0;
3270 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3271 {
3272 pos.lnum = lnum;
3273 pos.col = search_match_endcol;
3274 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3275 }
3276 else
3277 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003278 /* do at least one character; happens when past end of line */
3279 if (fromcol == tocol)
3280 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 area_highlighting = TRUE;
3282 attr = hl_attr(HLF_I);
3283 }
3284
3285#ifdef FEAT_DIFF
3286 filler_lines = diff_check(wp, lnum);
3287 if (filler_lines < 0)
3288 {
3289 if (filler_lines == -1)
3290 {
3291 if (diff_find_change(wp, lnum, &change_start, &change_end))
3292 diff_hlf = HLF_ADD; /* added line */
3293 else if (change_start == 0)
3294 diff_hlf = HLF_TXD; /* changed text */
3295 else
3296 diff_hlf = HLF_CHD; /* changed line */
3297 }
3298 else
3299 diff_hlf = HLF_ADD; /* added line */
3300 filler_lines = 0;
3301 area_highlighting = TRUE;
3302 }
3303 if (lnum == wp->w_topline)
3304 filler_lines = wp->w_topfill;
3305 filler_todo = filler_lines;
3306#endif
3307
3308#ifdef LINE_ATTR
3309# ifdef FEAT_SIGNS
3310 /* If this line has a sign with line highlighting set line_attr. */
3311 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3312 if (v != 0)
3313 line_attr = sign_get_attr((int)v, TRUE);
3314# endif
3315# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3316 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003317 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 line_attr = hl_attr(HLF_L);
3319# endif
3320 if (line_attr != 0)
3321 area_highlighting = TRUE;
3322#endif
3323
3324 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3325 ptr = line;
3326
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003327#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003328 if (has_spell)
3329 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003330 /* For checking first word with a capital skip white space. */
3331 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003332 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003333
Bram Moolenaar30abd282005-06-22 22:35:10 +00003334 /* To be able to spell-check over line boundaries copy the end of the
3335 * current line into nextline[]. Above the start of the next line was
3336 * copied to nextline[SPWORDLEN]. */
3337 if (nextline[SPWORDLEN] == NUL)
3338 {
3339 /* No next line or it is empty. */
3340 nextlinecol = MAXCOL;
3341 nextline_idx = 0;
3342 }
3343 else
3344 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003345 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003346 if (v < SPWORDLEN)
3347 {
3348 /* Short line, use it completely and append the start of the
3349 * next line. */
3350 nextlinecol = 0;
3351 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003352 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003353 nextline_idx = v + 1;
3354 }
3355 else
3356 {
3357 /* Long line, use only the last SPWORDLEN bytes. */
3358 nextlinecol = v - SPWORDLEN;
3359 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3360 nextline_idx = SPWORDLEN + 1;
3361 }
3362 }
3363 }
3364#endif
3365
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003366 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003368 if (lcs_space || lcs_trail)
3369 extra_check = TRUE;
3370 /* find start of trailing whitespace */
3371 if (lcs_trail)
3372 {
3373 trailcol = (colnr_T)STRLEN(ptr);
3374 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3375 --trailcol;
3376 trailcol += (colnr_T) (ptr - line);
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379
3380 /*
3381 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3382 * first character to be displayed.
3383 */
3384 if (wp->w_p_wrap)
3385 v = wp->w_skipcol;
3386 else
3387 v = wp->w_leftcol;
3388 if (v > 0)
3389 {
3390#ifdef FEAT_MBYTE
3391 char_u *prev_ptr = ptr;
3392#endif
3393 while (vcol < v && *ptr != NUL)
3394 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003395 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 vcol += c;
3397#ifdef FEAT_MBYTE
3398 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003400 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003403 /* When:
3404 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003405 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003406 * - 'virtualedit' is set, or
3407 * - the visual mode is active,
3408 * the end of the line may be before the start of the displayed part.
3409 */
3410 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003411#ifdef FEAT_SYN_HL
3412 wp->w_p_cuc || draw_color_col ||
3413#endif
3414#ifdef FEAT_VIRTUALEDIT
3415 virtual_active() ||
3416#endif
3417 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003418 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 /* Handle a character that's not completely on the screen: Put ptr at
3423 * that character but skip the first few screen characters. */
3424 if (vcol > v)
3425 {
3426 vcol -= c;
3427#ifdef FEAT_MBYTE
3428 ptr = prev_ptr;
3429#else
3430 --ptr;
3431#endif
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003432#ifdef FEAT_MBYTE
3433 /* character fits on the screen, don't need to skip it */
3434 if ((*mb_ptr2cells)(ptr) >= c && col == 0)
3435#endif
3436 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438
3439 /*
3440 * Adjust for when the inverted text is before the screen,
3441 * and when the start of the inverted text is before the screen.
3442 */
3443 if (tocol <= vcol)
3444 fromcol = 0;
3445 else if (fromcol >= 0 && fromcol < vcol)
3446 fromcol = vcol;
3447
3448#ifdef FEAT_LINEBREAK
3449 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3450 if (wp->w_p_wrap)
3451 need_showbreak = TRUE;
3452#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003453#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003454 /* When spell checking a word we need to figure out the start of the
3455 * word and if it's badly spelled or not. */
3456 if (has_spell)
3457 {
3458 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003459 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003460 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003461
3462 pos = wp->w_cursor;
3463 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003464 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003465 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003466
3467 /* spell_move_to() may call ml_get() and make "line" invalid */
3468 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3469 ptr = line + linecol;
3470
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003471 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003472 {
3473 /* no bad word found at line start, don't check until end of a
3474 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003475 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003476 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003477 }
3478 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003479 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003480 /* bad word found, use attributes until end of word */
3481 word_end = wp->w_cursor.col + len + 1;
3482
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003483 /* Turn index into actual attributes. */
3484 if (spell_hlf != HLF_COUNT)
3485 spell_attr = highlight_attr[spell_hlf];
3486 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003487 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003488
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003489# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003490 /* Need to restart syntax highlighting for this line. */
3491 if (has_syntax)
3492 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003493# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003494 }
3495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497
3498 /*
3499 * Correct highlighting for cursor that can't be disabled.
3500 * Avoids having to check this for each character.
3501 */
3502 if (fromcol >= 0)
3503 {
3504 if (noinvcur)
3505 {
3506 if ((colnr_T)fromcol == wp->w_virtcol)
3507 {
3508 /* highlighting starts at cursor, let it start just after the
3509 * cursor */
3510 fromcol_prev = fromcol;
3511 fromcol = -1;
3512 }
3513 else if ((colnr_T)fromcol < wp->w_virtcol)
3514 /* restart highlighting after the cursor */
3515 fromcol_prev = wp->w_virtcol;
3516 }
3517 if (fromcol >= tocol)
3518 fromcol = -1;
3519 }
3520
3521#ifdef FEAT_SEARCH_EXTRA
3522 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003523 * Handle highlighting the last used search pattern and matches.
3524 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003526 cur = wp->w_match_head;
3527 shl_flag = FALSE;
3528 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003530 if (shl_flag == FALSE)
3531 {
3532 shl = &search_hl;
3533 shl_flag = TRUE;
3534 }
3535 else
3536 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003537 shl->startcol = MAXCOL;
3538 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003540 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003541 v = (long)(ptr - line);
3542 if (cur != NULL)
3543 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003544 next_search_hl(wp, shl, lnum, (colnr_T)v,
3545 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003546
3547 /* Need to get the line again, a multi-line regexp may have made it
3548 * invalid. */
3549 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3550 ptr = line + v;
3551
3552 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003554 if (shl->lnum == lnum)
3555 shl->startcol = shl->rm.startpos[0].col;
3556 else
3557 shl->startcol = 0;
3558 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3559 - shl->rm.startpos[0].lnum)
3560 shl->endcol = shl->rm.endpos[0].col;
3561 else
3562 shl->endcol = MAXCOL;
3563 /* Highlight one character for an empty match. */
3564 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003567 if (has_mbyte && line[shl->endcol] != NUL)
3568 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3569 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003571 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003573 if ((long)shl->startcol < v) /* match at leftcol */
3574 {
3575 shl->attr_cur = shl->attr;
3576 search_attr = shl->attr;
3577 }
3578 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003580 if (shl != &search_hl && cur != NULL)
3581 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583#endif
3584
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003585#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003586 /* Cursor line highlighting for 'cursorline' in the current window. Not
3587 * when Visual mode is active, because it's not clear what is selected
3588 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003589 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003590 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003591 {
3592 line_attr = hl_attr(HLF_CUL);
3593 area_highlighting = TRUE;
3594 }
3595#endif
3596
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003597 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 col = 0;
3599#ifdef FEAT_RIGHTLEFT
3600 if (wp->w_p_rl)
3601 {
3602 /* Rightleft window: process the text in the normal direction, but put
3603 * it in current_ScreenLine[] from right to left. Start at the
3604 * rightmost column of the window. */
3605 col = W_WIDTH(wp) - 1;
3606 off += col;
3607 }
3608#endif
3609
3610 /*
3611 * Repeat for the whole displayed line.
3612 */
3613 for (;;)
3614 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003615#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003616 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 /* Skip this quickly when working on the text. */
3619 if (draw_state != WL_LINE)
3620 {
3621#ifdef FEAT_CMDWIN
3622 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3623 {
3624 draw_state = WL_CMDLINE;
3625 if (cmdwin_type != 0 && wp == curwin)
3626 {
3627 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003629 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 char_attr = hl_attr(HLF_AT);
3631 }
3632 }
3633#endif
3634
3635#ifdef FEAT_FOLDING
3636 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3637 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003638 int fdc = compute_foldcolumn(wp, 0);
3639
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003641 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003643 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003644 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003645 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003646 p_extra_free = alloc(12 + 1);
3647
3648 if (p_extra_free != NULL)
3649 {
3650 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3651 n_extra = fdc;
3652 p_extra_free[n_extra] = NUL;
3653 p_extra = p_extra_free;
3654 c_extra = NUL;
3655 char_attr = hl_attr(HLF_FC);
3656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
3658 }
3659#endif
3660
3661#ifdef FEAT_SIGNS
3662 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3663 {
3664 draw_state = WL_SIGN;
3665 /* Show the sign column when there are any signs in this
3666 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003667 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003669 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003671 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672# endif
3673
3674 /* Draw two cells with the sign value or blank. */
3675 c_extra = ' ';
3676 char_attr = hl_attr(HLF_SC);
3677 n_extra = 2;
3678
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003679 if (row == startrow
3680#ifdef FEAT_DIFF
3681 + filler_lines && filler_todo <= 0
3682#endif
3683 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
3685 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3686 SIGN_TEXT);
3687# ifdef FEAT_SIGN_ICONS
3688 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3689 SIGN_ICON);
3690 if (gui.in_use && icon_sign != 0)
3691 {
3692 /* Use the image in this position. */
3693 c_extra = SIGN_BYTE;
3694# ifdef FEAT_NETBEANS_INTG
3695 if (buf_signcount(wp->w_buffer, lnum) > 1)
3696 c_extra = MULTISIGN_BYTE;
3697# endif
3698 char_attr = icon_sign;
3699 }
3700 else
3701# endif
3702 if (text_sign != 0)
3703 {
3704 p_extra = sign_get_text(text_sign);
3705 if (p_extra != NULL)
3706 {
3707 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003708 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
3710 char_attr = sign_get_attr(text_sign, FALSE);
3711 }
3712 }
3713 }
3714 }
3715#endif
3716
3717 if (draw_state == WL_NR - 1 && n_extra == 0)
3718 {
3719 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003720 /* Display the absolute or relative line number. After the
3721 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3722 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 && (row == startrow
3724#ifdef FEAT_DIFF
3725 + filler_lines
3726#endif
3727 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3728 {
3729 /* Draw the line number (empty space after wrapping). */
3730 if (row == startrow
3731#ifdef FEAT_DIFF
3732 + filler_lines
3733#endif
3734 )
3735 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003736 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003737 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003738
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003739 if (wp->w_p_nu && !wp->w_p_rnu)
3740 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003741 num = (long)lnum;
3742 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003743 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003744 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003745 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003746 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003747 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003748 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003749 num = lnum;
3750 fmt = "%-*ld ";
3751 }
3752 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003753
Bram Moolenaar700e7342013-01-30 12:31:36 +01003754 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003755 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 if (wp->w_skipcol > 0)
3757 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3758 *p_extra = '-';
3759#ifdef FEAT_RIGHTLEFT
3760 if (wp->w_p_rl) /* reverse line numbers */
3761 rl_mirror(extra);
3762#endif
3763 p_extra = extra;
3764 c_extra = NUL;
3765 }
3766 else
3767 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003768 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003770#ifdef FEAT_SYN_HL
3771 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003772 * the current line differently.
3773 * TODO: Can we use CursorLine instead of CursorLineNr
3774 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003775 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003776 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003777 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 }
3781
Bram Moolenaar597a4222014-06-25 14:39:50 +02003782#ifdef FEAT_LINEBREAK
3783 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3784 && n_extra == 0 && *p_sbr != NUL)
3785 /* draw indent after showbreak value */
3786 draw_state = WL_BRI;
3787 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3788 /* After the showbreak, draw the breakindent */
3789 draw_state = WL_BRI - 1;
3790
3791 /* draw 'breakindent': indent wrapped text accordingly */
3792 if (draw_state == WL_BRI - 1 && n_extra == 0)
3793 {
3794 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003795 /* if need_showbreak is set, breakindent also applies */
3796 if (wp->w_p_bri && n_extra == 0
3797 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003798# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003799 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003800# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003801 )
3802 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003803 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003804# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003805 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003806 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003807 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003808# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003809 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3810 char_attr = hl_combine_attr(char_attr,
3811 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003812# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003813 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003814# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003815 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003816 c_extra = ' ';
3817 n_extra = get_breakindent_win(wp,
3818 ml_get_buf(wp->w_buffer, lnum, FALSE));
3819 /* Correct end of highlighted area for 'breakindent',
3820 * required when 'linebreak' is also set. */
3821 if (tocol == vcol)
3822 tocol += n_extra;
3823 }
3824 }
3825#endif
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3828 if (draw_state == WL_SBR - 1 && n_extra == 0)
3829 {
3830 draw_state = WL_SBR;
3831# ifdef FEAT_DIFF
3832 if (filler_todo > 0)
3833 {
3834 /* Draw "deleted" diff line(s). */
3835 if (char2cells(fill_diff) > 1)
3836 c_extra = '-';
3837 else
3838 c_extra = fill_diff;
3839# ifdef FEAT_RIGHTLEFT
3840 if (wp->w_p_rl)
3841 n_extra = col + 1;
3842 else
3843# endif
3844 n_extra = W_WIDTH(wp) - col;
3845 char_attr = hl_attr(HLF_DED);
3846 }
3847# endif
3848# ifdef FEAT_LINEBREAK
3849 if (*p_sbr != NUL && need_showbreak)
3850 {
3851 /* Draw 'showbreak' at the start of each broken line. */
3852 p_extra = p_sbr;
3853 c_extra = NUL;
3854 n_extra = (int)STRLEN(p_sbr);
3855 char_attr = hl_attr(HLF_AT);
3856 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003857 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 /* Correct end of highlighted area for 'showbreak',
3859 * required when 'linebreak' is also set. */
3860 if (tocol == vcol)
3861 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003862#ifdef FEAT_SYN_HL
3863 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003864 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003865 char_attr = hl_combine_attr(char_attr,
3866 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
3869# endif
3870 }
3871#endif
3872
3873 if (draw_state == WL_LINE - 1 && n_extra == 0)
3874 {
3875 draw_state = WL_LINE;
3876 if (saved_n_extra)
3877 {
3878 /* Continue item from end of wrapped line. */
3879 n_extra = saved_n_extra;
3880 c_extra = saved_c_extra;
3881 p_extra = saved_p_extra;
3882 char_attr = saved_char_attr;
3883 }
3884 else
3885 char_attr = 0;
3886 }
3887 }
3888
3889 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003890 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003891 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892#ifdef FEAT_DIFF
3893 && filler_todo <= 0
3894#endif
3895 )
3896 {
3897 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3898 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003899 /* Pretend we have finished updating the window. Except when
3900 * 'cursorcolumn' is set. */
3901#ifdef FEAT_SYN_HL
3902 if (wp->w_p_cuc)
3903 row = wp->w_cline_row + wp->w_cline_height;
3904 else
3905#endif
3906 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 break;
3908 }
3909
3910 if (draw_state == WL_LINE && area_highlighting)
3911 {
3912 /* handle Visual or match highlighting in this line */
3913 if (vcol == fromcol
3914#ifdef FEAT_MBYTE
3915 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3916 && (*mb_ptr2cells)(ptr) > 1)
3917#endif
3918 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003919 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 && vcol < tocol))
3921 area_attr = attr; /* start highlighting */
3922 else if (area_attr != 0
3923 && (vcol == tocol
3924 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927#ifdef FEAT_SEARCH_EXTRA
3928 if (!n_extra)
3929 {
3930 /*
3931 * Check for start/end of search pattern match.
3932 * After end, check for start/end of next match.
3933 * When another match, have to check for start again.
3934 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003935 * Do this for 'search_hl' and the match list (ordered by
3936 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003938 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003939 cur = wp->w_match_head;
3940 shl_flag = FALSE;
3941 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003943 if (shl_flag == FALSE
3944 && ((cur != NULL
3945 && cur->priority > SEARCH_HL_PRIORITY)
3946 || cur == NULL))
3947 {
3948 shl = &search_hl;
3949 shl_flag = TRUE;
3950 }
3951 else
3952 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003953 if (cur != NULL)
3954 cur->pos.cur = 0;
3955 pos_inprogress = TRUE;
3956 while (shl->rm.regprog != NULL
3957 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003959 if (shl->startcol != MAXCOL
3960 && v >= (long)shl->startcol
3961 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003963#ifdef FEAT_MBYTE
3964 int tmp_col = v + MB_PTR2LEN(ptr);
3965
3966 if (shl->endcol < tmp_col)
3967 shl->endcol = tmp_col;
3968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003970#ifdef FEAT_CONCEAL
3971 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3972 == cur->hlg_id)
3973 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02003974 has_match_conc =
3975 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003976 match_conc = cur->conceal_char;
3977 }
3978 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02003979 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003982 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 {
3984 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003985 next_search_hl(wp, shl, lnum, (colnr_T)v,
3986 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003987 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003988 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
3990 /* Need to get the line again, a multi-line regexp
3991 * may have made it invalid. */
3992 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3993 ptr = line + v;
3994
3995 if (shl->lnum == lnum)
3996 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003997 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003999 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004001 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004003 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
4005 /* highlight empty match, try again after
4006 * it */
4007#ifdef FEAT_MBYTE
4008 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004009 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004010 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 else
4012#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004013 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015
4016 /* Loop to check if the match starts at the
4017 * current position */
4018 continue;
4019 }
4020 }
4021 break;
4022 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004023 if (shl != &search_hl && cur != NULL)
4024 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004026
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004027 /* Use attributes from match with highest priority among
4028 * 'search_hl' and the match list. */
4029 search_attr = search_hl.attr_cur;
4030 cur = wp->w_match_head;
4031 shl_flag = FALSE;
4032 while (cur != NULL || shl_flag == FALSE)
4033 {
4034 if (shl_flag == FALSE
4035 && ((cur != NULL
4036 && cur->priority > SEARCH_HL_PRIORITY)
4037 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004038 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004039 shl = &search_hl;
4040 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004041 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004042 else
4043 shl = &cur->hl;
4044 if (shl->attr_cur != 0)
4045 search_attr = shl->attr_cur;
4046 if (shl != &search_hl && cur != NULL)
4047 cur = cur->next;
4048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050#endif
4051
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004053 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004055 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4056 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004058 if (diff_hlf == HLF_TXD && ptr - line > change_end
4059 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004061 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004062 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4063 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004066
4067 /* Decide which of the highlight attributes to use. */
4068 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004069#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004070 if (area_attr != 0)
4071 char_attr = hl_combine_attr(line_attr, area_attr);
4072 else if (search_attr != 0)
4073 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004074 /* Use line_attr when not in the Visual or 'incsearch' area
4075 * (area_attr may be 0 when "noinvcur" is set). */
4076 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004077 || vcol < fromcol || vcol_prev < fromcol_prev
4078 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004079 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004080#else
4081 if (area_attr != 0)
4082 char_attr = area_attr;
4083 else if (search_attr != 0)
4084 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004085#endif
4086 else
4087 {
4088 attr_pri = FALSE;
4089#ifdef FEAT_SYN_HL
4090 if (has_syntax)
4091 char_attr = syntax_attr;
4092 else
4093#endif
4094 char_attr = 0;
4095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097
4098 /*
4099 * Get the next character to put on the screen.
4100 */
4101 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004102 * The "p_extra" points to the extra stuff that is inserted to
4103 * represent special characters (non-printable stuff) and other
4104 * things. When all characters are the same, c_extra is used.
4105 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4106 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4108 */
4109 if (n_extra > 0)
4110 {
4111 if (c_extra != NUL)
4112 {
4113 c = c_extra;
4114#ifdef FEAT_MBYTE
4115 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4116 if (enc_utf8 && (*mb_char2len)(c) > 1)
4117 {
4118 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004119 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004120 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122 else
4123 mb_utf8 = FALSE;
4124#endif
4125 }
4126 else
4127 {
4128 c = *p_extra;
4129#ifdef FEAT_MBYTE
4130 if (has_mbyte)
4131 {
4132 mb_c = c;
4133 if (enc_utf8)
4134 {
4135 /* If the UTF-8 character is more than one byte:
4136 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004137 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 mb_utf8 = FALSE;
4139 if (mb_l > n_extra)
4140 mb_l = 1;
4141 else if (mb_l > 1)
4142 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004143 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004145 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 }
4147 }
4148 else
4149 {
4150 /* if this is a DBCS character, put it in "mb_c" */
4151 mb_l = MB_BYTE2LEN(c);
4152 if (mb_l >= n_extra)
4153 mb_l = 1;
4154 else if (mb_l > 1)
4155 mb_c = (c << 8) + p_extra[1];
4156 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004157 if (mb_l == 0) /* at the NUL at end-of-line */
4158 mb_l = 1;
4159
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 /* If a double-width char doesn't fit display a '>' in the
4161 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004162 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163# ifdef FEAT_RIGHTLEFT
4164 wp->w_p_rl ? (col <= 0) :
4165# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004166 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 && (*mb_char2cells)(mb_c) == 2)
4168 {
4169 c = '>';
4170 mb_c = c;
4171 mb_l = 1;
4172 mb_utf8 = FALSE;
4173 multi_attr = hl_attr(HLF_AT);
4174 /* put the pointer back to output the double-width
4175 * character at the start of the next line. */
4176 ++n_extra;
4177 --p_extra;
4178 }
4179 else
4180 {
4181 n_extra -= mb_l - 1;
4182 p_extra += mb_l - 1;
4183 }
4184 }
4185#endif
4186 ++p_extra;
4187 }
4188 --n_extra;
4189 }
4190 else
4191 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004192#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004193 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004194#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004195
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004196 if (p_extra_free != NULL)
4197 {
4198 vim_free(p_extra_free);
4199 p_extra_free = NULL;
4200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 /*
4202 * Get a character from the line itself.
4203 */
Bram Moolenaar88e76882017-02-27 20:33:46 +01004204#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004205 c0 = c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207#ifdef FEAT_MBYTE
4208 if (has_mbyte)
4209 {
4210 mb_c = c;
4211 if (enc_utf8)
4212 {
4213 /* If the UTF-8 character is more than one byte: Decode it
4214 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004215 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 mb_utf8 = FALSE;
4217 if (mb_l > 1)
4218 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004219 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 /* Overlong encoded ASCII or ASCII with composing char
4221 * is displayed normally, except a NUL. */
4222 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004223 {
4224 c = mb_c;
4225# ifdef FEAT_LINEBREAK
4226 c0 = mb_c;
4227# endif
4228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004230
4231 /* At start of the line we can have a composing char.
4232 * Draw it as a space with a composing char. */
4233 if (utf_iscomposing(mb_c))
4234 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004235 int i;
4236
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004237 for (i = Screen_mco - 1; i > 0; --i)
4238 u8cc[i] = u8cc[i - 1];
4239 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004240 mb_c = ' ';
4241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243
4244 if ((mb_l == 1 && c >= 0x80)
4245 || (mb_l >= 1 && mb_c == 0)
4246 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004247# ifdef UNICODE16
4248 || mb_c >= 0x10000
4249# endif
4250 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
4252 /*
4253 * Illegal UTF-8 byte: display as <xx>.
4254 * Non-BMP character : display as ? or fullwidth ?.
4255 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004256# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
4260 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004261# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (wp->w_p_rl) /* reverse */
4263 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004266# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 else if (utf_char2cells(mb_c) != 2)
4268 STRCPY(extra, "?");
4269 else
4270 /* 0xff1f in UTF-8: full-width '?' */
4271 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
4274 p_extra = extra;
4275 c = *p_extra;
4276 mb_c = mb_ptr2char_adv(&p_extra);
4277 mb_utf8 = (c >= 0x80);
4278 n_extra = (int)STRLEN(p_extra);
4279 c_extra = NUL;
4280 if (area_attr == 0 && search_attr == 0)
4281 {
4282 n_attr = n_extra + 1;
4283 extra_attr = hl_attr(HLF_8);
4284 saved_attr2 = char_attr; /* save current attr */
4285 }
4286 }
4287 else if (mb_l == 0) /* at the NUL at end-of-line */
4288 mb_l = 1;
4289#ifdef FEAT_ARABIC
4290 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4291 {
4292 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004293 int pc, pc1, nc;
4294 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
4296 /* The idea of what is the previous and next
4297 * character depends on 'rightleft'. */
4298 if (wp->w_p_rl)
4299 {
4300 pc = prev_c;
4301 pc1 = prev_c1;
4302 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004303 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 else
4306 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004307 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004309 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311 prev_c = mb_c;
4312
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004313 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315 else
4316 prev_c = mb_c;
4317#endif
4318 }
4319 else /* enc_dbcs */
4320 {
4321 mb_l = MB_BYTE2LEN(c);
4322 if (mb_l == 0) /* at the NUL at end-of-line */
4323 mb_l = 1;
4324 else if (mb_l > 1)
4325 {
4326 /* We assume a second byte below 32 is illegal.
4327 * Hopefully this is OK for all double-byte encodings!
4328 */
4329 if (ptr[1] >= 32)
4330 mb_c = (c << 8) + ptr[1];
4331 else
4332 {
4333 if (ptr[1] == NUL)
4334 {
4335 /* head byte at end of line */
4336 mb_l = 1;
4337 transchar_nonprint(extra, c);
4338 }
4339 else
4340 {
4341 /* illegal tail byte */
4342 mb_l = 2;
4343 STRCPY(extra, "XX");
4344 }
4345 p_extra = extra;
4346 n_extra = (int)STRLEN(extra) - 1;
4347 c_extra = NUL;
4348 c = *p_extra++;
4349 if (area_attr == 0 && search_attr == 0)
4350 {
4351 n_attr = n_extra + 1;
4352 extra_attr = hl_attr(HLF_8);
4353 saved_attr2 = char_attr; /* save current attr */
4354 }
4355 mb_c = c;
4356 }
4357 }
4358 }
4359 /* If a double-width char doesn't fit display a '>' in the
4360 * last column; the character is displayed at the start of the
4361 * next line. */
4362 if ((
4363# ifdef FEAT_RIGHTLEFT
4364 wp->w_p_rl ? (col <= 0) :
4365# endif
4366 (col >= W_WIDTH(wp) - 1))
4367 && (*mb_char2cells)(mb_c) == 2)
4368 {
4369 c = '>';
4370 mb_c = c;
4371 mb_utf8 = FALSE;
4372 mb_l = 1;
4373 multi_attr = hl_attr(HLF_AT);
4374 /* Put pointer back so that the character will be
4375 * displayed at the start of the next line. */
4376 --ptr;
4377 }
4378 else if (*ptr != NUL)
4379 ptr += mb_l - 1;
4380
4381 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004382 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004383 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004384 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004387 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 c = ' ';
4389 if (area_attr == 0 && search_attr == 0)
4390 {
4391 n_attr = n_extra + 1;
4392 extra_attr = hl_attr(HLF_AT);
4393 saved_attr2 = char_attr; /* save current attr */
4394 }
4395 mb_c = c;
4396 mb_utf8 = FALSE;
4397 mb_l = 1;
4398 }
4399
4400 }
4401#endif
4402 ++ptr;
4403
4404 if (extra_check)
4405 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004406#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004407 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004408#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004409
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004410#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 /* Get syntax attribute, unless still at the start of the line
4412 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004413 v = (long)(ptr - line);
4414 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
4416 /* Get the syntax attribute for the character. If there
4417 * is an error, disable syntax highlighting. */
4418 save_did_emsg = did_emsg;
4419 did_emsg = FALSE;
4420
Bram Moolenaar217ad922005-03-20 22:37:15 +00004421 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004422# ifdef FEAT_SPELL
4423 has_spell ? &can_spell :
4424# endif
4425 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004428 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004429 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004430 has_syntax = FALSE;
4431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 else
4433 did_emsg = save_did_emsg;
4434
4435 /* Need to get the line again, a multi-line regexp may
4436 * have made it invalid. */
4437 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4438 ptr = line + v;
4439
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004440 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004442 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004443 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004444# ifdef FEAT_CONCEAL
4445 /* no concealing past the end of the line, it interferes
4446 * with line highlighting */
4447 if (c == NUL)
4448 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004449 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004450 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004453#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004454
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004455#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004456 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004457 * Only do this when there is no syntax highlighting, the
4458 * @Spell cluster is not used or the current syntax item
4459 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004460 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004461 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004462 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004463# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004464 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004465 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004466# endif
4467 if (c != 0 && (
4468# ifdef FEAT_SYN_HL
4469 !has_syntax ||
4470# endif
4471 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004472 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004473 char_u *prev_ptr, *p;
4474 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004475 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004476# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004477 if (has_mbyte)
4478 {
4479 prev_ptr = ptr - mb_l;
4480 v -= mb_l - 1;
4481 }
4482 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004483# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004484 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004485
4486 /* Use nextline[] if possible, it has the start of the
4487 * next line concatenated. */
4488 if ((prev_ptr - line) - nextlinecol >= 0)
4489 p = nextline + (prev_ptr - line) - nextlinecol;
4490 else
4491 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004492 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004493 len = spell_check(wp, p, &spell_hlf, &cap_col,
4494 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004495 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004496
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004497 /* In Insert mode only highlight a word that
4498 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004499 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004500 && (State & INSERT) != 0
4501 && wp->w_cursor.lnum == lnum
4502 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004503 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004504 && wp->w_cursor.col < (colnr_T)word_end)
4505 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004506 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004507 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004508 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004509
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004510 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004511 && (p - nextline) + len > nextline_idx)
4512 {
4513 /* Remember that the good word continues at the
4514 * start of the next line. */
4515 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004516 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004517 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004518
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004519 /* Turn index into actual attributes. */
4520 if (spell_hlf != HLF_COUNT)
4521 spell_attr = highlight_attr[spell_hlf];
4522
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004523 if (cap_col > 0)
4524 {
4525 if (p != prev_ptr
4526 && (p - nextline) + cap_col >= nextline_idx)
4527 {
4528 /* Remember that the word in the next line
4529 * must start with a capital. */
4530 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004531 cap_col = (int)((p - nextline) + cap_col
4532 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004533 }
4534 else
4535 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004536 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004537 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004538 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004539 }
4540 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004541 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004542 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004543 char_attr = hl_combine_attr(char_attr, spell_attr);
4544 else
4545 char_attr = hl_combine_attr(spell_attr, char_attr);
4546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547#endif
4548#ifdef FEAT_LINEBREAK
4549 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004550 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004552 if (wp->w_p_lbr && c0 == c
4553 && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004555# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004556 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004557# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004558 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004560 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004562 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004563
Bram Moolenaar597a4222014-06-25 14:39:50 +02004564 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004565 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004566 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004567 if (c == TAB && n_extra + col > W_WIDTH(wp))
4568 n_extra = (int)wp->w_buffer->b_p_ts
4569 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4570
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004571# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004572 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004573# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004575# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004577 {
4578#ifdef FEAT_CONCEAL
4579 if (c == TAB)
4580 /* See "Tab alignment" below. */
4581 FIX_FOR_BOGUSCOLS;
4582#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004583 if (!wp->w_p_list)
4584 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 }
4587#endif
4588
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004589 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4590 */
4591 if (wp->w_p_list
4592 && (((c == 160
4593#ifdef FEAT_MBYTE
4594 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4595#endif
4596 ) && lcs_nbsp)
4597 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4598 {
4599 c = (c == ' ') ? lcs_space : lcs_nbsp;
4600 if (area_attr == 0 && search_attr == 0)
4601 {
4602 n_attr = 1;
4603 extra_attr = hl_attr(HLF_8);
4604 saved_attr2 = char_attr; /* save current attr */
4605 }
4606#ifdef FEAT_MBYTE
4607 mb_c = c;
4608 if (enc_utf8 && (*mb_char2len)(c) > 1)
4609 {
4610 mb_utf8 = TRUE;
4611 u8cc[0] = 0;
4612 c = 0xc0;
4613 }
4614 else
4615 mb_utf8 = FALSE;
4616#endif
4617 }
4618
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4620 {
4621 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004622 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
4624 n_attr = 1;
4625 extra_attr = hl_attr(HLF_8);
4626 saved_attr2 = char_attr; /* save current attr */
4627 }
4628#ifdef FEAT_MBYTE
4629 mb_c = c;
4630 if (enc_utf8 && (*mb_char2len)(c) > 1)
4631 {
4632 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004633 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004634 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636 else
4637 mb_utf8 = FALSE;
4638#endif
4639 }
4640 }
4641
4642 /*
4643 * Handling of non-printable characters.
4644 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004645 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
4647 /*
4648 * when getting a character from the file, we may have to
4649 * turn it into something else on the way to putting it
4650 * into "ScreenLines".
4651 */
4652 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4653 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004654 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004655 long vcol_adjusted = vcol; /* removed showbreak length */
4656#ifdef FEAT_LINEBREAK
4657 /* only adjust the tab_len, when at the first column
4658 * after the showbreak value was drawn */
4659 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4660 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004663 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004664 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4665
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004666#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004667 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004668#endif
4669 /* tab amount depends on current column */
4670 n_extra = tab_len;
4671#ifdef FEAT_LINEBREAK
4672 else
4673 {
4674 char_u *p;
4675 int len = n_extra;
4676 int i;
4677 int saved_nextra = n_extra;
4678
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004679#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004680 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004681 /* there are characters to conceal */
4682 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004683 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4684 */
4685 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4686 && n_extra > tab_len)
4687 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004688#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004689
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004690 /* if n_extra > 0, it gives the number of chars, to
4691 * use for a tab, else we need to calculate the width
4692 * for a tab */
4693#ifdef FEAT_MBYTE
4694 len = (tab_len * mb_char2len(lcs_tab2));
4695 if (n_extra > 0)
4696 len += n_extra - tab_len;
4697#endif
4698 c = lcs_tab1;
4699 p = alloc((unsigned)(len + 1));
4700 vim_memset(p, ' ', len);
4701 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004702 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004703 p_extra_free = p;
4704 for (i = 0; i < tab_len; i++)
4705 {
4706#ifdef FEAT_MBYTE
4707 mb_char2bytes(lcs_tab2, p);
4708 p += mb_char2len(lcs_tab2);
4709 n_extra += mb_char2len(lcs_tab2)
4710 - (saved_nextra > 0 ? 1 : 0);
4711#else
4712 p[i] = lcs_tab2;
4713#endif
4714 }
4715 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004716#ifdef FEAT_CONCEAL
4717 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4718 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004719 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004720 n_extra -= vcol_off;
4721#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004722 }
4723#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004724#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004725 {
4726 int vc_saved = vcol_off;
4727
4728 /* Tab alignment should be identical regardless of
4729 * 'conceallevel' value. So tab compensates of all
4730 * previous concealed characters, and thus resets
4731 * vcol_off and boguscols accumulated so far in the
4732 * line. Note that the tab can be longer than
4733 * 'tabstop' when there are concealed characters. */
4734 FIX_FOR_BOGUSCOLS;
4735
4736 /* Make sure, the highlighting for the tab char will be
4737 * correctly set further below (effectively reverts the
4738 * FIX_FOR_BOGSUCOLS macro */
4739 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004740 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004741 tab_len += vc_saved;
4742 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744#ifdef FEAT_MBYTE
4745 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4746#endif
4747 if (wp->w_p_list)
4748 {
4749 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004750#ifdef FEAT_LINEBREAK
4751 if (wp->w_p_lbr)
4752 c_extra = NUL; /* using p_extra from above */
4753 else
4754#endif
4755 c_extra = lcs_tab2;
4756 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 extra_attr = hl_attr(HLF_8);
4758 saved_attr2 = char_attr; /* save current attr */
4759#ifdef FEAT_MBYTE
4760 mb_c = c;
4761 if (enc_utf8 && (*mb_char2len)(c) > 1)
4762 {
4763 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004764 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004765 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
4767#endif
4768 }
4769 else
4770 {
4771 c_extra = ' ';
4772 c = ' ';
4773 }
4774 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004775 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004776 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004777 || ((fromcol >= 0 || fromcol_prev >= 0)
4778 && tocol > vcol
4779 && VIsual_mode != Ctrl_V
4780 && (
4781# ifdef FEAT_RIGHTLEFT
4782 wp->w_p_rl ? (col >= 0) :
4783# endif
4784 (col < W_WIDTH(wp)))
4785 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004786 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004787 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004788 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004790 /* Display a '$' after the line or highlight an extra
4791 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4793 /* For a diff line the highlighting continues after the
4794 * "$". */
4795 if (
4796# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004797 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798# ifdef LINE_ATTR
4799 &&
4800# endif
4801# endif
4802# ifdef LINE_ATTR
4803 line_attr == 0
4804# endif
4805 )
4806#endif
4807 {
4808#ifdef FEAT_VIRTUALEDIT
4809 /* In virtualedit, visual selections may extend
4810 * beyond end of line. */
4811 if (area_highlighting && virtual_active()
4812 && tocol != MAXCOL && vcol < tocol)
4813 n_extra = 0;
4814 else
4815#endif
4816 {
4817 p_extra = at_end_str;
4818 n_extra = 1;
4819 c_extra = NUL;
4820 }
4821 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004822 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004823 c = lcs_eol;
4824 else
4825 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 lcs_eol_one = -1;
4827 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004828 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
4830 extra_attr = hl_attr(HLF_AT);
4831 n_attr = 1;
4832 }
4833#ifdef FEAT_MBYTE
4834 mb_c = c;
4835 if (enc_utf8 && (*mb_char2len)(c) > 1)
4836 {
4837 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004838 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004839 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841 else
4842 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4843#endif
4844 }
4845 else if (c != NUL)
4846 {
4847 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004848 if (n_extra == 0)
4849 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850#ifdef FEAT_RIGHTLEFT
4851 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4852 rl_mirror(p_extra); /* reverse "<12>" */
4853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004855#ifdef FEAT_LINEBREAK
4856 if (wp->w_p_lbr)
4857 {
4858 char_u *p;
4859
4860 c = *p_extra;
4861 p = alloc((unsigned)n_extra + 1);
4862 vim_memset(p, ' ', n_extra);
4863 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4864 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004865 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004866 p_extra_free = p_extra = p;
4867 }
4868 else
4869#endif
4870 {
4871 n_extra = byte2cells(c) - 1;
4872 c = *p_extra++;
4873 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004874 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
4876 n_attr = n_extra + 1;
4877 extra_attr = hl_attr(HLF_8);
4878 saved_attr2 = char_attr; /* save current attr */
4879 }
4880#ifdef FEAT_MBYTE
4881 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4882#endif
4883 }
4884#ifdef FEAT_VIRTUALEDIT
4885 else if (VIsual_active
4886 && (VIsual_mode == Ctrl_V
4887 || VIsual_mode == 'v')
4888 && virtual_active()
4889 && tocol != MAXCOL
4890 && vcol < tocol
4891 && (
4892# ifdef FEAT_RIGHTLEFT
4893 wp->w_p_rl ? (col >= 0) :
4894# endif
4895 (col < W_WIDTH(wp))))
4896 {
4897 c = ' ';
4898 --ptr; /* put it back at the NUL */
4899 }
4900#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004901#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 else if ((
4903# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004904 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 ) && (
4908# ifdef FEAT_RIGHTLEFT
4909 wp->w_p_rl ? (col >= 0) :
4910# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004911 (col
4912# ifdef FEAT_CONCEAL
4913 - boguscols
4914# endif
4915 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
4917 /* Highlight until the right side of the window */
4918 c = ' ';
4919 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004920
4921 /* Remember we do the char for line highlighting. */
4922 ++did_line_attr;
4923
4924 /* don't do search HL for the rest of the line */
4925 if (line_attr != 0 && char_attr == search_attr && col > 0)
4926 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927# ifdef FEAT_DIFF
4928 if (diff_hlf == HLF_TXD)
4929 {
4930 diff_hlf = HLF_CHD;
4931 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004934 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4935 char_attr = hl_combine_attr(char_attr,
4936 hl_attr(HLF_CUL));
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
4939# endif
4940 }
4941#endif
4942 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004943
4944#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004945 if ( wp->w_p_cole > 0
4946 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004947 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02004948 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004949 && !(lnum_in_visual_area
4950 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004951 {
4952 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02004953 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004954 && (syn_get_sub_char() != NUL || match_conc
4955 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004956 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004957 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004958 /* First time at this concealed item: display one
4959 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004960 if (match_conc)
4961 c = match_conc;
4962 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004963 c = syn_get_sub_char();
4964 else if (lcs_conceal != NUL)
4965 c = lcs_conceal;
4966 else
4967 c = ' ';
4968
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004969 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004970
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004971 if (n_extra > 0)
4972 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004973 vcol += n_extra;
4974 if (wp->w_p_wrap && n_extra > 0)
4975 {
4976# ifdef FEAT_RIGHTLEFT
4977 if (wp->w_p_rl)
4978 {
4979 col -= n_extra;
4980 boguscols -= n_extra;
4981 }
4982 else
4983# endif
4984 {
4985 boguscols += n_extra;
4986 col += n_extra;
4987 }
4988 }
4989 n_extra = 0;
4990 n_attr = 0;
4991 }
4992 else if (n_skip == 0)
4993 {
4994 is_concealing = TRUE;
4995 n_skip = 1;
4996 }
4997# ifdef FEAT_MBYTE
4998 mb_c = c;
4999 if (enc_utf8 && (*mb_char2len)(c) > 1)
5000 {
5001 mb_utf8 = TRUE;
5002 u8cc[0] = 0;
5003 c = 0xc0;
5004 }
5005 else
5006 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5007# endif
5008 }
5009 else
5010 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005011 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005012 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005013 }
5014#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
5016
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005017#ifdef FEAT_CONCEAL
5018 /* In the cursor line and we may be concealing characters: correct
5019 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005020 if (!did_wcol && draw_state == WL_LINE
5021 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005022 && conceal_cursor_line(wp)
5023 && (int)wp->w_virtcol <= vcol + n_skip)
5024 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005025# ifdef FEAT_RIGHTLEFT
5026 if (wp->w_p_rl)
5027 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5028 else
5029# endif
5030 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005031 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005032 did_wcol = TRUE;
5033 }
5034#endif
5035
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 /* Don't override visual selection highlighting. */
5037 if (n_attr > 0
5038 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005039 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 char_attr = extra_attr;
5041
Bram Moolenaar81695252004-12-29 20:58:21 +00005042#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 /* XIM don't send preedit_start and preedit_end, but they send
5044 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5045 * im_is_preediting() here. */
5046 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005047 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 && (State & INSERT)
5049 && !p_imdisable
5050 && im_is_preediting()
5051 && draw_state == WL_LINE)
5052 {
5053 colnr_T tcol;
5054
5055 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005056 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 else
5058 tcol = preedit_end_col;
5059 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5060 {
5061 if (feedback_old_attr < 0)
5062 {
5063 feedback_col = 0;
5064 feedback_old_attr = char_attr;
5065 }
5066 char_attr = im_get_feedback_attr(feedback_col);
5067 if (char_attr < 0)
5068 char_attr = feedback_old_attr;
5069 feedback_col++;
5070 }
5071 else if (feedback_old_attr >= 0)
5072 {
5073 char_attr = feedback_old_attr;
5074 feedback_old_attr = -1;
5075 feedback_col = 0;
5076 }
5077 }
5078#endif
5079 /*
5080 * Handle the case where we are in column 0 but not on the first
5081 * character of the line and the user wants us to show us a
5082 * special character (via 'listchars' option "precedes:<char>".
5083 */
5084 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005085 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5087#ifdef FEAT_DIFF
5088 && filler_todo <= 0
5089#endif
5090 && draw_state > WL_NR
5091 && c != NUL)
5092 {
5093 c = lcs_prec;
5094 lcs_prec_todo = NUL;
5095#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005096 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5097 {
5098 /* Double-width character being overwritten by the "precedes"
5099 * character, need to fill up half the character. */
5100 c_extra = MB_FILLER_CHAR;
5101 n_extra = 1;
5102 n_attr = 2;
5103 extra_attr = hl_attr(HLF_AT);
5104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 mb_c = c;
5106 if (enc_utf8 && (*mb_char2len)(c) > 1)
5107 {
5108 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005109 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005110 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
5112 else
5113 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5114#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005115 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
5117 saved_attr3 = char_attr; /* save current attr */
5118 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5119 n_attr3 = 1;
5120 }
5121 }
5122
5123 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005124 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005126 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005127#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005128 || did_line_attr == 1
5129#endif
5130 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005132#ifdef FEAT_SEARCH_EXTRA
5133 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005134
5135 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005136 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005137 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005138#endif
5139
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005140 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 * highlight match at end of line. If it's beyond the last
5142 * char on the screen, just overwrite that one (tricky!) Not
5143 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005144#ifdef FEAT_SEARCH_EXTRA
5145 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005146 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005147 prevcol_hl_flag = TRUE;
5148 else
5149 {
5150 cur = wp->w_match_head;
5151 while (cur != NULL)
5152 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005153 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005154 {
5155 prevcol_hl_flag = TRUE;
5156 break;
5157 }
5158 cur = cur->next;
5159 }
5160 }
5161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005163 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005164 && (VIsual_mode != Ctrl_V
5165 || lnum == VIsual.lnum
5166 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005167 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168#ifdef FEAT_SEARCH_EXTRA
5169 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005170 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005171# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005172 && did_line_attr <= 1
5173# endif
5174 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175#endif
5176 ))
5177 {
5178 int n = 0;
5179
5180#ifdef FEAT_RIGHTLEFT
5181 if (wp->w_p_rl)
5182 {
5183 if (col < 0)
5184 n = 1;
5185 }
5186 else
5187#endif
5188 {
5189 if (col >= W_WIDTH(wp))
5190 n = -1;
5191 }
5192 if (n != 0)
5193 {
5194 /* At the window boundary, highlight the last character
5195 * instead (better than nothing). */
5196 off += n;
5197 col += n;
5198 }
5199 else
5200 {
5201 /* Add a blank character to highlight. */
5202 ScreenLines[off] = ' ';
5203#ifdef FEAT_MBYTE
5204 if (enc_utf8)
5205 ScreenLinesUC[off] = 0;
5206#endif
5207 }
5208#ifdef FEAT_SEARCH_EXTRA
5209 if (area_attr == 0)
5210 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005211 /* Use attributes from match with highest priority among
5212 * 'search_hl' and the match list. */
5213 char_attr = search_hl.attr;
5214 cur = wp->w_match_head;
5215 shl_flag = FALSE;
5216 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005217 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005218 if (shl_flag == FALSE
5219 && ((cur != NULL
5220 && cur->priority > SEARCH_HL_PRIORITY)
5221 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005222 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005223 shl = &search_hl;
5224 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005225 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005226 else
5227 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005228 if ((ptr - line) - 1 == (long)shl->startcol
5229 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005230 char_attr = shl->attr;
5231 if (shl != &search_hl && cur != NULL)
5232 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
5235#endif
5236 ScreenAttrs[off] = char_attr;
5237#ifdef FEAT_RIGHTLEFT
5238 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005241 --off;
5242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 else
5244#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005247 ++off;
5248 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005249 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005250#ifdef FEAT_SYN_HL
5251 eol_hl_off = 1;
5252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
Bram Moolenaar91170f82006-05-05 21:15:17 +00005256 /*
5257 * At end of the text line.
5258 */
5259 if (c == NUL)
5260 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005261#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005262 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5263 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005264 {
5265 /* highlight last char after line */
5266 --col;
5267 --off;
5268 --vcol;
5269 }
5270
Bram Moolenaar1a384422010-07-14 19:53:30 +02005271 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005272 if (wp->w_p_wrap)
5273 v = wp->w_skipcol;
5274 else
5275 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005276
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005277 /* check if line ends before left margin */
5278 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005279 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005280#ifdef FEAT_CONCEAL
5281 /* Get rid of the boguscols now, we want to draw until the right
5282 * edge for 'cursorcolumn'. */
5283 col -= boguscols;
5284 boguscols = 0;
5285#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005286
5287 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005288 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005289
5290 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005291 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5292 && (int)wp->w_virtcol <
5293 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005294 && lnum != wp->w_cursor.lnum)
5295 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005296# ifdef FEAT_RIGHTLEFT
5297 && !wp->w_p_rl
5298# endif
5299 )
5300 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005301 int rightmost_vcol = 0;
5302 int i;
5303
5304 if (wp->w_p_cuc)
5305 rightmost_vcol = wp->w_virtcol;
5306 if (draw_color_col)
5307 /* determine rightmost colorcolumn to possibly draw */
5308 for (i = 0; color_cols[i] >= 0; ++i)
5309 if (rightmost_vcol < color_cols[i])
5310 rightmost_vcol = color_cols[i];
5311
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005312 while (col < W_WIDTH(wp))
5313 {
5314 ScreenLines[off] = ' ';
5315#ifdef FEAT_MBYTE
5316 if (enc_utf8)
5317 ScreenLinesUC[off] = 0;
5318#endif
5319 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005320 if (draw_color_col)
5321 draw_color_col = advance_color_col(VCOL_HLC,
5322 &color_cols);
5323
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005324 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005325 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005326 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005327 ScreenAttrs[off++] = hl_attr(HLF_MC);
5328 else
5329 ScreenAttrs[off++] = 0;
5330
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005331 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005332 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005333
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005334 ++vcol;
5335 }
5336 }
5337#endif
5338
Bram Moolenaar860cae12010-06-05 23:22:07 +02005339 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5340 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 row++;
5342
5343 /*
5344 * Update w_cline_height and w_cline_folded if the cursor line was
5345 * updated (saves a call to plines() later).
5346 */
5347 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5348 {
5349 curwin->w_cline_row = startrow;
5350 curwin->w_cline_height = row - startrow;
5351#ifdef FEAT_FOLDING
5352 curwin->w_cline_folded = FALSE;
5353#endif
5354 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5355 }
5356
5357 break;
5358 }
5359
5360 /* line continues beyond line end */
5361 if (lcs_ext
5362 && !wp->w_p_wrap
5363#ifdef FEAT_DIFF
5364 && filler_todo <= 0
5365#endif
5366 && (
5367#ifdef FEAT_RIGHTLEFT
5368 wp->w_p_rl ? col == 0 :
5369#endif
5370 col == W_WIDTH(wp) - 1)
5371 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005372 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5374 {
5375 c = lcs_ext;
5376 char_attr = hl_attr(HLF_AT);
5377#ifdef FEAT_MBYTE
5378 mb_c = c;
5379 if (enc_utf8 && (*mb_char2len)(c) > 1)
5380 {
5381 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005382 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005383 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 }
5385 else
5386 mb_utf8 = FALSE;
5387#endif
5388 }
5389
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005390#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005391 /* advance to the next 'colorcolumn' */
5392 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005393 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005394
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005395 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005396 * highlight the cursor position itself.
5397 * Also highlight the 'colorcolumn' if it is different than
5398 * 'cursorcolumn' */
5399 vcol_save_attr = -1;
5400 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005401 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005402 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005403 && lnum != wp->w_cursor.lnum)
5404 {
5405 vcol_save_attr = char_attr;
5406 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5407 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005408 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005409 {
5410 vcol_save_attr = char_attr;
5411 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5412 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005413 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005414#endif
5415
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 /*
5417 * Store character to be displayed.
5418 * Skip characters that are left of the screen for 'nowrap'.
5419 */
5420 vcol_prev = vcol;
5421 if (draw_state < WL_LINE || n_skip <= 0)
5422 {
5423 /*
5424 * Store the character.
5425 */
5426#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5427 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5428 {
5429 /* A double-wide character is: put first halve in left cell. */
5430 --off;
5431 --col;
5432 }
5433#endif
5434 ScreenLines[off] = c;
5435#ifdef FEAT_MBYTE
5436 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005437 {
5438 if ((mb_c & 0xff00) == 0x8e00)
5439 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 else if (enc_utf8)
5443 {
5444 if (mb_utf8)
5445 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005446 int i;
5447
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005449 if ((c & 0xff) == 0)
5450 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005451 for (i = 0; i < Screen_mco; ++i)
5452 {
5453 ScreenLinesC[i][off] = u8cc[i];
5454 if (u8cc[i] == 0)
5455 break;
5456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
5458 else
5459 ScreenLinesUC[off] = 0;
5460 }
5461 if (multi_attr)
5462 {
5463 ScreenAttrs[off] = multi_attr;
5464 multi_attr = 0;
5465 }
5466 else
5467#endif
5468 ScreenAttrs[off] = char_attr;
5469
5470#ifdef FEAT_MBYTE
5471 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5472 {
5473 /* Need to fill two screen columns. */
5474 ++off;
5475 ++col;
5476 if (enc_utf8)
5477 /* UTF-8: Put a 0 in the second screen char. */
5478 ScreenLines[off] = 0;
5479 else
5480 /* DBCS: Put second byte in the second screen char. */
5481 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005482 if (draw_state > WL_NR
5483#ifdef FEAT_DIFF
5484 && filler_todo <= 0
5485#endif
5486 )
5487 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 /* When "tocol" is halfway a character, set it to the end of
5489 * the character, otherwise highlighting won't stop. */
5490 if (tocol == vcol)
5491 ++tocol;
5492#ifdef FEAT_RIGHTLEFT
5493 if (wp->w_p_rl)
5494 {
5495 /* now it's time to backup one cell */
5496 --off;
5497 --col;
5498 }
5499#endif
5500 }
5501#endif
5502#ifdef FEAT_RIGHTLEFT
5503 if (wp->w_p_rl)
5504 {
5505 --off;
5506 --col;
5507 }
5508 else
5509#endif
5510 {
5511 ++off;
5512 ++col;
5513 }
5514 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005515#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005516 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005517 {
5518 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005519 ++vcol_off;
5520 if (n_extra > 0)
5521 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005522 if (wp->w_p_wrap)
5523 {
5524 /*
5525 * Special voodoo required if 'wrap' is on.
5526 *
5527 * Advance the column indicator to force the line
5528 * drawing to wrap early. This will make the line
5529 * take up the same screen space when parts are concealed,
5530 * so that cursor line computations aren't messed up.
5531 *
5532 * To avoid the fictitious advance of 'col' causing
5533 * trailing junk to be written out of the screen line
5534 * we are building, 'boguscols' keeps track of the number
5535 * of bad columns we have advanced.
5536 */
5537 if (n_extra > 0)
5538 {
5539 vcol += n_extra;
5540# ifdef FEAT_RIGHTLEFT
5541 if (wp->w_p_rl)
5542 {
5543 col -= n_extra;
5544 boguscols -= n_extra;
5545 }
5546 else
5547# endif
5548 {
5549 col += n_extra;
5550 boguscols += n_extra;
5551 }
5552 n_extra = 0;
5553 n_attr = 0;
5554 }
5555
5556
5557# ifdef FEAT_MBYTE
5558 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5559 {
5560 /* Need to fill two screen columns. */
5561# ifdef FEAT_RIGHTLEFT
5562 if (wp->w_p_rl)
5563 {
5564 --boguscols;
5565 --col;
5566 }
5567 else
5568# endif
5569 {
5570 ++boguscols;
5571 ++col;
5572 }
5573 }
5574# endif
5575
5576# ifdef FEAT_RIGHTLEFT
5577 if (wp->w_p_rl)
5578 {
5579 --boguscols;
5580 --col;
5581 }
5582 else
5583# endif
5584 {
5585 ++boguscols;
5586 ++col;
5587 }
5588 }
5589 else
5590 {
5591 if (n_extra > 0)
5592 {
5593 vcol += n_extra;
5594 n_extra = 0;
5595 n_attr = 0;
5596 }
5597 }
5598
5599 }
5600#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 else
5602 --n_skip;
5603
Bram Moolenaar64486672010-05-16 15:46:46 +02005604 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5605 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005606 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607#ifdef FEAT_DIFF
5608 && filler_todo <= 0
5609#endif
5610 )
5611 ++vcol;
5612
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005613#ifdef FEAT_SYN_HL
5614 if (vcol_save_attr >= 0)
5615 char_attr = vcol_save_attr;
5616#endif
5617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 /* restore attributes after "predeces" in 'listchars' */
5619 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5620 char_attr = saved_attr3;
5621
5622 /* restore attributes after last 'listchars' or 'number' char */
5623 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5624 char_attr = saved_attr2;
5625
5626 /*
5627 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005628 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 */
5630 if ((
5631#ifdef FEAT_RIGHTLEFT
5632 wp->w_p_rl ? (col < 0) :
5633#endif
5634 (col >= W_WIDTH(wp)))
5635 && (*ptr != NUL
5636#ifdef FEAT_DIFF
5637 || filler_todo > 0
5638#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005639 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5641 )
5642 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005643#ifdef FEAT_CONCEAL
5644 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5645 (int)W_WIDTH(wp), wp->w_p_rl);
5646 boguscols = 0;
5647#else
5648 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5649 (int)W_WIDTH(wp), wp->w_p_rl);
5650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 ++row;
5652 ++screen_row;
5653
5654 /* When not wrapping and finished diff lines, or when displayed
5655 * '$' and highlighting until last column, break here. */
5656 if ((!wp->w_p_wrap
5657#ifdef FEAT_DIFF
5658 && filler_todo <= 0
5659#endif
5660 ) || lcs_eol_one == -1)
5661 break;
5662
5663 /* When the window is too narrow draw all "@" lines. */
5664 if (draw_state != WL_LINE
5665#ifdef FEAT_DIFF
5666 && filler_todo <= 0
5667#endif
5668 )
5669 {
5670 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005671#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 draw_vsep_win(wp, row);
5673#endif
5674 row = endrow;
5675 }
5676
5677 /* When line got too long for screen break here. */
5678 if (row == endrow)
5679 {
5680 ++row;
5681 break;
5682 }
5683
5684 if (screen_cur_row == screen_row - 1
5685#ifdef FEAT_DIFF
5686 && filler_todo <= 0
5687#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005688#ifdef FEAT_WINDOWS
5689 && W_WIDTH(wp) == Columns
5690#endif
5691 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
5693 /* Remember that the line wraps, used for modeless copy. */
5694 LineWraps[screen_row - 1] = TRUE;
5695
5696 /*
5697 * Special trick to make copy/paste of wrapped lines work with
5698 * xterm/screen: write an extra character beyond the end of
5699 * the line. This will work with all terminal types
5700 * (regardless of the xn,am settings).
5701 * Only do this on a fast tty.
5702 * Only do this if the cursor is on the current line
5703 * (something has been written in it).
5704 * Don't do this for the GUI.
5705 * Don't do this for double-width characters.
5706 * Don't do this for a window not at the right screen border.
5707 */
5708 if (p_tf
5709#ifdef FEAT_GUI
5710 && !gui.in_use
5711#endif
5712#ifdef FEAT_MBYTE
5713 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005714 && ((*mb_off2cells)(LineOffset[screen_row],
5715 LineOffset[screen_row] + screen_Columns)
5716 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005718 + (int)Columns - 2,
5719 LineOffset[screen_row] + screen_Columns)
5720 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721#endif
5722 )
5723 {
5724 /* First make sure we are at the end of the screen line,
5725 * then output the same character again to let the
5726 * terminal know about the wrap. If the terminal doesn't
5727 * auto-wrap, we overwrite the character. */
5728 if (screen_cur_col != W_WIDTH(wp))
5729 screen_char(LineOffset[screen_row - 1]
5730 + (unsigned)Columns - 1,
5731 screen_row - 1, (int)(Columns - 1));
5732
5733#ifdef FEAT_MBYTE
5734 /* When there is a multi-byte character, just output a
5735 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005736 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5737 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 out_char(' ');
5739 else
5740#endif
5741 out_char(ScreenLines[LineOffset[screen_row - 1]
5742 + (Columns - 1)]);
5743 /* force a redraw of the first char on the next line */
5744 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5745 screen_start(); /* don't know where cursor is now */
5746 }
5747 }
5748
5749 col = 0;
5750 off = (unsigned)(current_ScreenLine - ScreenLines);
5751#ifdef FEAT_RIGHTLEFT
5752 if (wp->w_p_rl)
5753 {
5754 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5755 off += col;
5756 }
5757#endif
5758
5759 /* reset the drawing state for the start of a wrapped line */
5760 draw_state = WL_START;
5761 saved_n_extra = n_extra;
5762 saved_p_extra = p_extra;
5763 saved_c_extra = c_extra;
5764 saved_char_attr = char_attr;
5765 n_extra = 0;
5766 lcs_prec_todo = lcs_prec;
5767#ifdef FEAT_LINEBREAK
5768# ifdef FEAT_DIFF
5769 if (filler_todo <= 0)
5770# endif
5771 need_showbreak = TRUE;
5772#endif
5773#ifdef FEAT_DIFF
5774 --filler_todo;
5775 /* When the filler lines are actually below the last line of the
5776 * file, don't draw the line itself, break here. */
5777 if (filler_todo == 0 && wp->w_botfill)
5778 break;
5779#endif
5780 }
5781
5782 } /* for every character in the line */
5783
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005784#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005785 /* After an empty line check first word for capital. */
5786 if (*skipwhite(line) == NUL)
5787 {
5788 capcol_lnum = lnum + 1;
5789 cap_col = 0;
5790 }
5791#endif
5792
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005793 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 return row;
5795}
5796
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005797#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005798static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005799
5800/*
5801 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005802 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005803 */
5804 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005805comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005806{
5807 int i;
5808
5809 for (i = 0; i < Screen_mco; ++i)
5810 {
5811 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5812 return TRUE;
5813 if (ScreenLinesC[i][off_from] == 0)
5814 break;
5815 }
5816 return FALSE;
5817}
5818#endif
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820/*
5821 * Check whether the given character needs redrawing:
5822 * - the (first byte of the) character is different
5823 * - the attributes are different
5824 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005825 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 */
5827 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005828char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829{
5830 if (cols > 0
5831 && ((ScreenLines[off_from] != ScreenLines[off_to]
5832 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5833
5834#ifdef FEAT_MBYTE
5835 || (enc_dbcs != 0
5836 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5837 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5838 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5839 : (cols > 1 && ScreenLines[off_from + 1]
5840 != ScreenLines[off_to + 1])))
5841 || (enc_utf8
5842 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5843 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005844 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005845 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5846 && ScreenLines[off_from + 1]
5847 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848#endif
5849 ))
5850 return TRUE;
5851 return FALSE;
5852}
5853
5854/*
5855 * Move one "cooked" screen line to the screen, but only the characters that
5856 * have actually changed. Handle insert/delete character.
5857 * "coloff" gives the first column on the screen for this line.
5858 * "endcol" gives the columns where valid characters are.
5859 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5860 * needs to be cleared, negative otherwise.
5861 * "rlflag" is TRUE in a rightleft window:
5862 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5863 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5864 */
5865 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005866screen_line(
5867 int row,
5868 int coloff,
5869 int endcol,
5870 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005872 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005874 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
5876 unsigned off_from;
5877 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005878#ifdef FEAT_MBYTE
5879 unsigned max_off_from;
5880 unsigned max_off_to;
5881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005883#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 int hl;
5885#endif
5886 int force = FALSE; /* force update rest of the line */
5887 int redraw_this /* bool: does character need redraw? */
5888#ifdef FEAT_GUI
5889 = TRUE /* For GUI when while-loop empty */
5890#endif
5891 ;
5892 int redraw_next; /* redraw_this for next character */
5893#ifdef FEAT_MBYTE
5894 int clear_next = FALSE;
5895 int char_cells; /* 1: normal char */
5896 /* 2: occupies two display cells */
5897# define CHAR_CELLS char_cells
5898#else
5899# define CHAR_CELLS 1
5900#endif
5901
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005902 /* Check for illegal row and col, just in case. */
5903 if (row >= Rows)
5904 row = Rows - 1;
5905 if (endcol > Columns)
5906 endcol = Columns;
5907
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908# ifdef FEAT_CLIPBOARD
5909 clip_may_clear_selection(row, row);
5910# endif
5911
5912 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5913 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005914#ifdef FEAT_MBYTE
5915 max_off_from = off_from + screen_Columns;
5916 max_off_to = LineOffset[row] + screen_Columns;
5917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918
5919#ifdef FEAT_RIGHTLEFT
5920 if (rlflag)
5921 {
5922 /* Clear rest first, because it's left of the text. */
5923 if (clear_width > 0)
5924 {
5925 while (col <= endcol && ScreenLines[off_to] == ' '
5926 && ScreenAttrs[off_to] == 0
5927# ifdef FEAT_MBYTE
5928 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5929# endif
5930 )
5931 {
5932 ++off_to;
5933 ++col;
5934 }
5935 if (col <= endcol)
5936 screen_fill(row, row + 1, col + coloff,
5937 endcol + coloff + 1, ' ', ' ', 0);
5938 }
5939 col = endcol + 1;
5940 off_to = LineOffset[row] + col + coloff;
5941 off_from += col;
5942 endcol = (clear_width > 0 ? clear_width : -clear_width);
5943 }
5944#endif /* FEAT_RIGHTLEFT */
5945
5946 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5947
5948 while (col < endcol)
5949 {
5950#ifdef FEAT_MBYTE
5951 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005952 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 else
5954 char_cells = 1;
5955#endif
5956
5957 redraw_this = redraw_next;
5958 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5959 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5960
5961#ifdef FEAT_GUI
5962 /* If the next character was bold, then redraw the current character to
5963 * remove any pixels that might have spilt over into us. This only
5964 * happens in the GUI.
5965 */
5966 if (redraw_next && gui.in_use)
5967 {
5968 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005969 if (hl > HL_ALL)
5970 hl = syn_attr2attr(hl);
5971 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 redraw_this = TRUE;
5973 }
5974#endif
5975
5976 if (redraw_this)
5977 {
5978 /*
5979 * Special handling when 'xs' termcap flag set (hpterm):
5980 * Attributes for characters are stored at the position where the
5981 * cursor is when writing the highlighting code. The
5982 * start-highlighting code must be written with the cursor on the
5983 * first highlighted character. The stop-highlighting code must
5984 * be written with the cursor just after the last highlighted
5985 * character.
5986 * Overwriting a character doesn't remove it's highlighting. Need
5987 * to clear the rest of the line, and force redrawing it
5988 * completely.
5989 */
5990 if ( p_wiv
5991 && !force
5992#ifdef FEAT_GUI
5993 && !gui.in_use
5994#endif
5995 && ScreenAttrs[off_to] != 0
5996 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5997 {
5998 /*
5999 * Need to remove highlighting attributes here.
6000 */
6001 windgoto(row, col + coloff);
6002 out_str(T_CE); /* clear rest of this screen line */
6003 screen_start(); /* don't know where cursor is now */
6004 force = TRUE; /* force redraw of rest of the line */
6005 redraw_next = TRUE; /* or else next char would miss out */
6006
6007 /*
6008 * If the previous character was highlighted, need to stop
6009 * highlighting at this character.
6010 */
6011 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6012 {
6013 screen_attr = ScreenAttrs[off_to - 1];
6014 term_windgoto(row, col + coloff);
6015 screen_stop_highlight();
6016 }
6017 else
6018 screen_attr = 0; /* highlighting has stopped */
6019 }
6020#ifdef FEAT_MBYTE
6021 if (enc_dbcs != 0)
6022 {
6023 /* Check if overwriting a double-byte with a single-byte or
6024 * the other way around requires another character to be
6025 * redrawn. For UTF-8 this isn't needed, because comparing
6026 * ScreenLinesUC[] is sufficient. */
6027 if (char_cells == 1
6028 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006029 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 {
6031 /* Writing a single-cell character over a double-cell
6032 * character: need to redraw the next cell. */
6033 ScreenLines[off_to + 1] = 0;
6034 redraw_next = TRUE;
6035 }
6036 else if (char_cells == 2
6037 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006038 && (*mb_off2cells)(off_to, max_off_to) == 1
6039 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 {
6041 /* Writing the second half of a double-cell character over
6042 * a double-cell character: need to redraw the second
6043 * cell. */
6044 ScreenLines[off_to + 2] = 0;
6045 redraw_next = TRUE;
6046 }
6047
6048 if (enc_dbcs == DBCS_JPNU)
6049 ScreenLines2[off_to] = ScreenLines2[off_from];
6050 }
6051 /* When writing a single-width character over a double-width
6052 * character and at the end of the redrawn text, need to clear out
6053 * the right halve of the old character.
6054 * Also required when writing the right halve of a double-width
6055 * char over the left halve of an existing one. */
6056 if (has_mbyte && col + char_cells == endcol
6057 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006058 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006060 && (*mb_off2cells)(off_to, max_off_to) == 1
6061 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 clear_next = TRUE;
6063#endif
6064
6065 ScreenLines[off_to] = ScreenLines[off_from];
6066#ifdef FEAT_MBYTE
6067 if (enc_utf8)
6068 {
6069 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6070 if (ScreenLinesUC[off_from] != 0)
6071 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006072 int i;
6073
6074 for (i = 0; i < Screen_mco; ++i)
6075 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
6077 }
6078 if (char_cells == 2)
6079 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6080#endif
6081
6082#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006083 /* The bold trick makes a single column of pixels appear in the
6084 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 * character should be redrawn too. This happens for our own GUI
6086 * and for some xterms. */
6087 if (
6088# ifdef FEAT_GUI
6089 gui.in_use
6090# endif
6091# if defined(FEAT_GUI) && defined(UNIX)
6092 ||
6093# endif
6094# ifdef UNIX
6095 term_is_xterm
6096# endif
6097 )
6098 {
6099 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006100 if (hl > HL_ALL)
6101 hl = syn_attr2attr(hl);
6102 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 redraw_next = TRUE;
6104 }
6105#endif
6106 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6107#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006108 /* For simplicity set the attributes of second half of a
6109 * double-wide character equal to the first half. */
6110 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006112
6113 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 else
6116#endif
6117 screen_char(off_to, row, col + coloff);
6118 }
6119 else if ( p_wiv
6120#ifdef FEAT_GUI
6121 && !gui.in_use
6122#endif
6123 && col + coloff > 0)
6124 {
6125 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6126 {
6127 /*
6128 * Don't output stop-highlight when moving the cursor, it will
6129 * stop the highlighting when it should continue.
6130 */
6131 screen_attr = 0;
6132 }
6133 else if (screen_attr != 0)
6134 screen_stop_highlight();
6135 }
6136
6137 off_to += CHAR_CELLS;
6138 off_from += CHAR_CELLS;
6139 col += CHAR_CELLS;
6140 }
6141
6142#ifdef FEAT_MBYTE
6143 if (clear_next)
6144 {
6145 /* Clear the second half of a double-wide character of which the left
6146 * half was overwritten with a single-wide character. */
6147 ScreenLines[off_to] = ' ';
6148 if (enc_utf8)
6149 ScreenLinesUC[off_to] = 0;
6150 screen_char(off_to, row, col + coloff);
6151 }
6152#endif
6153
6154 if (clear_width > 0
6155#ifdef FEAT_RIGHTLEFT
6156 && !rlflag
6157#endif
6158 )
6159 {
6160#ifdef FEAT_GUI
6161 int startCol = col;
6162#endif
6163
6164 /* blank out the rest of the line */
6165 while (col < clear_width && ScreenLines[off_to] == ' '
6166 && ScreenAttrs[off_to] == 0
6167#ifdef FEAT_MBYTE
6168 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6169#endif
6170 )
6171 {
6172 ++off_to;
6173 ++col;
6174 }
6175 if (col < clear_width)
6176 {
6177#ifdef FEAT_GUI
6178 /*
6179 * In the GUI, clearing the rest of the line may leave pixels
6180 * behind if the first character cleared was bold. Some bold
6181 * fonts spill over the left. In this case we redraw the previous
6182 * character too. If we didn't skip any blanks above, then we
6183 * only redraw if the character wasn't already redrawn anyway.
6184 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006185 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {
6187 hl = ScreenAttrs[off_to];
6188 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006189 {
6190 int prev_cells = 1;
6191# ifdef FEAT_MBYTE
6192 if (enc_utf8)
6193 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6194 * that its width is 2. */
6195 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6196 else if (enc_dbcs != 0)
6197 {
6198 /* find previous character by counting from first
6199 * column and get its width. */
6200 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006201 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006202
6203 while (off < off_to)
6204 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006205 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006206 off += prev_cells;
6207 }
6208 }
6209
6210 if (enc_dbcs != 0 && prev_cells > 1)
6211 screen_char_2(off_to - prev_cells, row,
6212 col + coloff - prev_cells);
6213 else
6214# endif
6215 screen_char(off_to - prev_cells, row,
6216 col + coloff - prev_cells);
6217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 }
6219#endif
6220 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6221 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006222#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 off_to += clear_width - col;
6224 col = clear_width;
6225#endif
6226 }
6227 }
6228
6229 if (clear_width > 0)
6230 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006231#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 /* For a window that's left of another, draw the separator char. */
6233 if (col + coloff < Columns)
6234 {
6235 int c;
6236
6237 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006238 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006240 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6241 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242# endif
6243 || ScreenAttrs[off_to] != hl)
6244 {
6245 ScreenLines[off_to] = c;
6246 ScreenAttrs[off_to] = hl;
6247# ifdef FEAT_MBYTE
6248 if (enc_utf8)
6249 {
6250 if (c >= 0x80)
6251 {
6252 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006253 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255 else
6256 ScreenLinesUC[off_to] = 0;
6257 }
6258# endif
6259 screen_char(off_to, row, col + coloff);
6260 }
6261 }
6262 else
6263#endif
6264 LineWraps[row] = FALSE;
6265 }
6266}
6267
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006268#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006270 * Mirror text "str" for right-left displaying.
6271 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006273 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006274rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275{
6276 char_u *p1, *p2;
6277 int t;
6278
6279 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6280 {
6281 t = *p1;
6282 *p1 = *p2;
6283 *p2 = t;
6284 }
6285}
6286#endif
6287
6288#if defined(FEAT_WINDOWS) || defined(PROTO)
6289/*
6290 * mark all status lines for redraw; used after first :cd
6291 */
6292 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006293status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
6295 win_T *wp;
6296
Bram Moolenaar29323592016-07-24 22:04:11 +02006297 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if (wp->w_status_height)
6299 {
6300 wp->w_redr_status = TRUE;
6301 redraw_later(VALID);
6302 }
6303}
6304
6305/*
6306 * mark all status lines of the current buffer for redraw
6307 */
6308 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006309status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 win_T *wp;
6312
Bram Moolenaar29323592016-07-24 22:04:11 +02006313 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6315 {
6316 wp->w_redr_status = TRUE;
6317 redraw_later(VALID);
6318 }
6319}
6320
6321/*
6322 * Redraw all status lines that need to be redrawn.
6323 */
6324 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006325redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 win_T *wp;
6328
Bram Moolenaar29323592016-07-24 22:04:11 +02006329 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 if (wp->w_redr_status)
6331 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006332 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006333 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334}
6335#endif
6336
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006337#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338/*
6339 * Redraw all status lines at the bottom of frame "frp".
6340 */
6341 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006342win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 if (frp->fr_layout == FR_LEAF)
6345 frp->fr_win->w_redr_status = TRUE;
6346 else if (frp->fr_layout == FR_ROW)
6347 {
6348 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6349 win_redraw_last_status(frp);
6350 }
6351 else /* frp->fr_layout == FR_COL */
6352 {
6353 frp = frp->fr_child;
6354 while (frp->fr_next != NULL)
6355 frp = frp->fr_next;
6356 win_redraw_last_status(frp);
6357 }
6358}
6359#endif
6360
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006361#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362/*
6363 * Draw the verticap separator right of window "wp" starting with line "row".
6364 */
6365 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006366draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 int hl;
6369 int c;
6370
6371 if (wp->w_vsep_width)
6372 {
6373 /* draw the vertical separator right of this window */
6374 c = fillchar_vsep(&hl);
6375 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6376 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6377 c, ' ', hl);
6378 }
6379}
6380#endif
6381
6382#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006383static int status_match_len(expand_T *xp, char_u *s);
6384static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
6386/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006387 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 */
6389 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006390status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391{
6392 int len = 0;
6393
6394#ifdef FEAT_MENU
6395 int emenu = (xp->xp_context == EXPAND_MENUS
6396 || xp->xp_context == EXPAND_MENUNAMES);
6397
6398 /* Check for menu separators - replace with '|'. */
6399 if (emenu && menu_is_separator(s))
6400 return 1;
6401#endif
6402
6403 while (*s != NUL)
6404 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006405 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006406 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006407 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 }
6409
6410 return len;
6411}
6412
6413/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006414 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006415 * These are backslashes used for escaping. Do show backslashes in help tags.
6416 */
6417 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006418skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006419{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006420 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006421#ifdef FEAT_MENU
6422 || ((xp->xp_context == EXPAND_MENUS
6423 || xp->xp_context == EXPAND_MENUNAMES)
6424 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6425#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006426 )
6427 {
6428#ifndef BACKSLASH_IN_FILENAME
6429 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6430 return 2;
6431#endif
6432 return 1;
6433 }
6434 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006435}
6436
6437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 * Show wildchar matches in the status line.
6439 * Show at least the "match" item.
6440 * We start at item 'first_match' in the list and show all matches that fit.
6441 *
6442 * If inversion is possible we use it. Else '=' characters are used.
6443 */
6444 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006445win_redr_status_matches(
6446 expand_T *xp,
6447 int num_matches,
6448 char_u **matches, /* list of matches */
6449 int match,
6450 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
6452#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6453 int row;
6454 char_u *buf;
6455 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006456 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 int fillchar;
6458 int attr;
6459 int i;
6460 int highlight = TRUE;
6461 char_u *selstart = NULL;
6462 int selstart_col = 0;
6463 char_u *selend = NULL;
6464 static int first_match = 0;
6465 int add_left = FALSE;
6466 char_u *s;
6467#ifdef FEAT_MENU
6468 int emenu;
6469#endif
6470#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6471 int l;
6472#endif
6473
6474 if (matches == NULL) /* interrupted completion? */
6475 return;
6476
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006477#ifdef FEAT_MBYTE
6478 if (has_mbyte)
6479 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6480 else
6481#endif
6482 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 if (buf == NULL)
6484 return;
6485
6486 if (match == -1) /* don't show match but original text */
6487 {
6488 match = 0;
6489 highlight = FALSE;
6490 }
6491 /* count 1 for the ending ">" */
6492 clen = status_match_len(xp, L_MATCH(match)) + 3;
6493 if (match == 0)
6494 first_match = 0;
6495 else if (match < first_match)
6496 {
6497 /* jumping left, as far as we can go */
6498 first_match = match;
6499 add_left = TRUE;
6500 }
6501 else
6502 {
6503 /* check if match fits on the screen */
6504 for (i = first_match; i < match; ++i)
6505 clen += status_match_len(xp, L_MATCH(i)) + 2;
6506 if (first_match > 0)
6507 clen += 2;
6508 /* jumping right, put match at the left */
6509 if ((long)clen > Columns)
6510 {
6511 first_match = match;
6512 /* if showing the last match, we can add some on the left */
6513 clen = 2;
6514 for (i = match; i < num_matches; ++i)
6515 {
6516 clen += status_match_len(xp, L_MATCH(i)) + 2;
6517 if ((long)clen >= Columns)
6518 break;
6519 }
6520 if (i == num_matches)
6521 add_left = TRUE;
6522 }
6523 }
6524 if (add_left)
6525 while (first_match > 0)
6526 {
6527 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6528 if ((long)clen >= Columns)
6529 break;
6530 --first_match;
6531 }
6532
6533 fillchar = fillchar_status(&attr, TRUE);
6534
6535 if (first_match == 0)
6536 {
6537 *buf = NUL;
6538 len = 0;
6539 }
6540 else
6541 {
6542 STRCPY(buf, "< ");
6543 len = 2;
6544 }
6545 clen = len;
6546
6547 i = first_match;
6548 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6549 {
6550 if (i == match)
6551 {
6552 selstart = buf + len;
6553 selstart_col = clen;
6554 }
6555
6556 s = L_MATCH(i);
6557 /* Check for menu separators - replace with '|' */
6558#ifdef FEAT_MENU
6559 emenu = (xp->xp_context == EXPAND_MENUS
6560 || xp->xp_context == EXPAND_MENUNAMES);
6561 if (emenu && menu_is_separator(s))
6562 {
6563 STRCPY(buf + len, transchar('|'));
6564 l = (int)STRLEN(buf + len);
6565 len += l;
6566 clen += l;
6567 }
6568 else
6569#endif
6570 for ( ; *s != NUL; ++s)
6571 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006572 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 clen += ptr2cells(s);
6574#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006575 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 {
6577 STRNCPY(buf + len, s, l);
6578 s += l - 1;
6579 len += l;
6580 }
6581 else
6582#endif
6583 {
6584 STRCPY(buf + len, transchar_byte(*s));
6585 len += (int)STRLEN(buf + len);
6586 }
6587 }
6588 if (i == match)
6589 selend = buf + len;
6590
6591 *(buf + len++) = ' ';
6592 *(buf + len++) = ' ';
6593 clen += 2;
6594 if (++i == num_matches)
6595 break;
6596 }
6597
6598 if (i != num_matches)
6599 {
6600 *(buf + len++) = '>';
6601 ++clen;
6602 }
6603
6604 buf[len] = NUL;
6605
6606 row = cmdline_row - 1;
6607 if (row >= 0)
6608 {
6609 if (wild_menu_showing == 0)
6610 {
6611 if (msg_scrolled > 0)
6612 {
6613 /* Put the wildmenu just above the command line. If there is
6614 * no room, scroll the screen one line up. */
6615 if (cmdline_row == Rows - 1)
6616 {
6617 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6618 ++msg_scrolled;
6619 }
6620 else
6621 {
6622 ++cmdline_row;
6623 ++row;
6624 }
6625 wild_menu_showing = WM_SCROLLED;
6626 }
6627 else
6628 {
6629 /* Create status line if needed by setting 'laststatus' to 2.
6630 * Set 'winminheight' to zero to avoid that the window is
6631 * resized. */
6632 if (lastwin->w_status_height == 0)
6633 {
6634 save_p_ls = p_ls;
6635 save_p_wmh = p_wmh;
6636 p_ls = 2;
6637 p_wmh = 0;
6638 last_status(FALSE);
6639 }
6640 wild_menu_showing = WM_SHOWN;
6641 }
6642 }
6643
6644 screen_puts(buf, row, 0, attr);
6645 if (selstart != NULL && highlight)
6646 {
6647 *selend = NUL;
6648 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6649 }
6650
6651 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6652 }
6653
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006654#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 win_redraw_last_status(topframe);
6656#else
6657 lastwin->w_redr_status = TRUE;
6658#endif
6659 vim_free(buf);
6660}
6661#endif
6662
6663#if defined(FEAT_WINDOWS) || defined(PROTO)
6664/*
6665 * Redraw the status line of window wp.
6666 *
6667 * If inversion is possible we use it. Else '=' characters are used.
6668 */
6669 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006670win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 int row;
6673 char_u *p;
6674 int len;
6675 int fillchar;
6676 int attr;
6677 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006678 static int busy = FALSE;
6679
6680 /* It's possible to get here recursively when 'statusline' (indirectly)
6681 * invokes ":redrawstatus". Simply ignore the call then. */
6682 if (busy)
6683 return;
6684 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685
6686 wp->w_redr_status = FALSE;
6687 if (wp->w_status_height == 0)
6688 {
6689 /* no status line, can only be last window */
6690 redraw_cmdline = TRUE;
6691 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006692 else if (!redrawing()
6693#ifdef FEAT_INS_EXPAND
6694 /* don't update status line when popup menu is visible and may be
6695 * drawn over it */
6696 || pum_visible()
6697#endif
6698 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 {
6700 /* Don't redraw right now, do it later. */
6701 wp->w_redr_status = TRUE;
6702 }
6703#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006704 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 {
6706 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006707 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 }
6709#endif
6710 else
6711 {
6712 fillchar = fillchar_status(&attr, wp == curwin);
6713
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006714 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 p = NameBuff;
6716 len = (int)STRLEN(p);
6717
6718 if (wp->w_buffer->b_help
6719#ifdef FEAT_QUICKFIX
6720 || wp->w_p_pvw
6721#endif
6722 || bufIsChanged(wp->w_buffer)
6723 || wp->w_buffer->b_p_ro)
6724 *(p + len++) = ' ';
6725 if (wp->w_buffer->b_help)
6726 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006727 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 len += (int)STRLEN(p + len);
6729 }
6730#ifdef FEAT_QUICKFIX
6731 if (wp->w_p_pvw)
6732 {
6733 STRCPY(p + len, _("[Preview]"));
6734 len += (int)STRLEN(p + len);
6735 }
6736#endif
6737 if (bufIsChanged(wp->w_buffer))
6738 {
6739 STRCPY(p + len, "[+]");
6740 len += 3;
6741 }
6742 if (wp->w_buffer->b_p_ro)
6743 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006744 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006745 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 }
6747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6749 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6750 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6751 if (this_ru_col <= 1)
6752 {
6753 p = (char_u *)"<"; /* No room for file name! */
6754 len = 1;
6755 }
6756 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757#ifdef FEAT_MBYTE
6758 if (has_mbyte)
6759 {
6760 int clen = 0, i;
6761
6762 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006763 clen = mb_string2cells(p, -1);
6764
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 /* Find first character that will fit.
6766 * Going from start to end is much faster for DBCS. */
6767 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006768 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 clen -= (*mb_ptr2cells)(p + i);
6770 len = clen;
6771 if (i > 0)
6772 {
6773 p = p + i - 1;
6774 *p = '<';
6775 ++len;
6776 }
6777
6778 }
6779 else
6780#endif
6781 if (len > this_ru_col - 1)
6782 {
6783 p += len - (this_ru_col - 1);
6784 *p = '<';
6785 len = this_ru_col - 1;
6786 }
6787
6788 row = W_WINROW(wp) + wp->w_height;
6789 screen_puts(p, row, W_WINCOL(wp), attr);
6790 screen_fill(row, row + 1, len + W_WINCOL(wp),
6791 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6792
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006793 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6795 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6796 - 1 + W_WINCOL(wp)), attr);
6797
6798#ifdef FEAT_CMDL_INFO
6799 win_redr_ruler(wp, TRUE);
6800#endif
6801 }
6802
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 /*
6804 * May need to draw the character below the vertical separator.
6805 */
6806 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6807 {
6808 if (stl_connected(wp))
6809 fillchar = fillchar_status(&attr, wp == curwin);
6810 else
6811 fillchar = fillchar_vsep(&attr);
6812 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6813 attr);
6814 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006815 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816}
6817
Bram Moolenaar238a5642006-02-21 22:12:05 +00006818#ifdef FEAT_STL_OPT
6819/*
6820 * Redraw the status line according to 'statusline' and take care of any
6821 * errors encountered.
6822 */
6823 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006824redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006825{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006826 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006827 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006828
6829 /* When called recursively return. This can happen when the statusline
6830 * contains an expression that triggers a redraw. */
6831 if (entered)
6832 return;
6833 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006834
Bram Moolenaara742e082016-04-05 21:10:38 +02006835 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006836 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006837 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006838 {
6839 /* When there is an error disable the statusline, otherwise the
6840 * display is messed up with errors and a redraw triggers the problem
6841 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006842 set_string_option_direct((char_u *)"statusline", -1,
6843 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006844 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006845 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006846 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006847 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006848}
6849#endif
6850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851/*
6852 * Return TRUE if the status line of window "wp" is connected to the status
6853 * line of the window right of it. If not, then it's a vertical separator.
6854 * Only call if (wp->w_vsep_width != 0).
6855 */
6856 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006857stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858{
6859 frame_T *fr;
6860
6861 fr = wp->w_frame;
6862 while (fr->fr_parent != NULL)
6863 {
6864 if (fr->fr_parent->fr_layout == FR_COL)
6865 {
6866 if (fr->fr_next != NULL)
6867 break;
6868 }
6869 else
6870 {
6871 if (fr->fr_next != NULL)
6872 return TRUE;
6873 }
6874 fr = fr->fr_parent;
6875 }
6876 return FALSE;
6877}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
6879#endif /* FEAT_WINDOWS */
6880
6881#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6882/*
6883 * Get the value to show for the language mappings, active 'keymap'.
6884 */
6885 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006886get_keymap_str(
6887 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006888 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01006889 char_u *buf, /* buffer for the result */
6890 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 char_u *p;
6893
6894 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6895 return FALSE;
6896
6897 {
6898#ifdef FEAT_EVAL
6899 buf_T *old_curbuf = curbuf;
6900 win_T *old_curwin = curwin;
6901 char_u *s;
6902
6903 curbuf = wp->w_buffer;
6904 curwin = wp;
6905 STRCPY(buf, "b:keymap_name"); /* must be writable */
6906 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006907 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 --emsg_skip;
6909 curbuf = old_curbuf;
6910 curwin = old_curwin;
6911 if (p == NULL || *p == NUL)
6912#endif
6913 {
6914#ifdef FEAT_KEYMAP
6915 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6916 p = wp->w_buffer->b_p_keymap;
6917 else
6918#endif
6919 p = (char_u *)"lang";
6920 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006921 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 buf[0] = NUL;
6923#ifdef FEAT_EVAL
6924 vim_free(s);
6925#endif
6926 }
6927 return buf[0] != NUL;
6928}
6929#endif
6930
6931#if defined(FEAT_STL_OPT) || defined(PROTO)
6932/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006933 * Redraw the status line or ruler of window "wp".
6934 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 */
6936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006937win_redr_custom(
6938 win_T *wp,
6939 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006941 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 int attr;
6943 int curattr;
6944 int row;
6945 int col = 0;
6946 int maxwidth;
6947 int width;
6948 int n;
6949 int len;
6950 int fillchar;
6951 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006952 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006954 struct stl_hlrec hltab[STL_MAX_ITEM];
6955 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006956 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006957 win_T *ewp;
6958 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
Bram Moolenaar1d633412013-12-11 15:52:01 +01006960 /* There is a tiny chance that this gets called recursively: When
6961 * redrawing a status line triggers redrawing the ruler or tabline.
6962 * Avoid trouble by not allowing recursion. */
6963 if (entered)
6964 return;
6965 entered = TRUE;
6966
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006968 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006970 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006971 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006972 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006973 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006974 attr = hl_attr(HLF_TPF);
6975 maxwidth = Columns;
6976# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006977 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006980 else
6981 {
6982 row = W_WINROW(wp) + wp->w_height;
6983 fillchar = fillchar_status(&attr, wp == curwin);
6984 maxwidth = W_WIDTH(wp);
6985
6986 if (draw_ruler)
6987 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006988 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006989 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006990 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006991 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006992 if (*++stl == '-')
6993 stl++;
6994 if (atoi((char *)stl))
6995 while (VIM_ISDIGIT(*stl))
6996 stl++;
6997 if (*stl++ != '(')
6998 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006999 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007000#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007001 col = ru_col - (Columns - W_WIDTH(wp));
7002 if (col < (W_WIDTH(wp) + 1) / 2)
7003 col = (W_WIDTH(wp) + 1) / 2;
7004#else
7005 col = ru_col;
7006 if (col > (Columns + 1) / 2)
7007 col = (Columns + 1) / 2;
7008#endif
7009 maxwidth = W_WIDTH(wp) - col;
7010#ifdef FEAT_WINDOWS
7011 if (!wp->w_status_height)
7012#endif
7013 {
7014 row = Rows - 1;
7015 --maxwidth; /* writing in last column may cause scrolling */
7016 fillchar = ' ';
7017 attr = 0;
7018 }
7019
7020# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007021 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007022# endif
7023 }
7024 else
7025 {
7026 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007027 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007028 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007029 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007030# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007031 use_sandbox = was_set_insecurely((char_u *)"statusline",
7032 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007033# endif
7034 }
7035
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007036#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007037 col += W_WINCOL(wp);
7038#endif
7039 }
7040
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007042 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
Bram Moolenaar61452852011-02-01 18:01:11 +01007044 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7045 * the cursor away and back. */
7046 ewp = wp == NULL ? curwin : wp;
7047 p_crb_save = ewp->w_p_crb;
7048 ewp->w_p_crb = FALSE;
7049
Bram Moolenaar362f3562009-11-03 16:20:34 +00007050 /* Make a copy, because the statusline may include a function call that
7051 * might change the option value and free the memory. */
7052 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007053 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007054 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007055 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007056 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007057 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007059 /* Make all characters printable. */
7060 p = transstr(buf);
7061 if (p != NULL)
7062 {
7063 vim_strncpy(buf, p, sizeof(buf) - 1);
7064 vim_free(p);
7065 }
7066
7067 /* fill up with "fillchar" */
7068 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007069 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {
7071#ifdef FEAT_MBYTE
7072 len += (*mb_char2bytes)(fillchar, buf + len);
7073#else
7074 buf[len++] = fillchar;
7075#endif
7076 ++width;
7077 }
7078 buf[len] = NUL;
7079
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007080 /*
7081 * Draw each snippet with the specified highlighting.
7082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 curattr = attr;
7084 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007085 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007087 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 screen_puts_len(p, len, row, col, curattr);
7089 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007090 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007092 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007094 else if (hltab[n].userhl < 0)
7095 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007097 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007098 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099#endif
7100 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007101 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 }
7103 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007104
7105 if (wp == NULL)
7106 {
7107 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7108 col = 0;
7109 len = 0;
7110 p = buf;
7111 fillchar = 0;
7112 for (n = 0; tabtab[n].start != NULL; n++)
7113 {
7114 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7115 while (col < len)
7116 TabPageIdxs[col++] = fillchar;
7117 p = tabtab[n].start;
7118 fillchar = tabtab[n].userhl;
7119 }
7120 while (col < Columns)
7121 TabPageIdxs[col++] = fillchar;
7122 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007123
7124theend:
7125 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126}
7127
7128#endif /* FEAT_STL_OPT */
7129
7130/*
7131 * Output a single character directly to the screen and update ScreenLines.
7132 */
7133 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007134screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 char_u buf[MB_MAXBYTES + 1];
7137
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007138#ifdef FEAT_MBYTE
7139 if (has_mbyte)
7140 buf[(*mb_char2bytes)(c, buf)] = NUL;
7141 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007143 {
7144 buf[0] = c;
7145 buf[1] = NUL;
7146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 screen_puts(buf, row, col, attr);
7148}
7149
7150/*
7151 * Get a single character directly from ScreenLines into "bytes[]".
7152 * Also return its attribute in *attrp;
7153 */
7154 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007155screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
7157 unsigned off;
7158
7159 /* safety check */
7160 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7161 {
7162 off = LineOffset[row] + col;
7163 *attrp = ScreenAttrs[off];
7164 bytes[0] = ScreenLines[off];
7165 bytes[1] = NUL;
7166
7167#ifdef FEAT_MBYTE
7168 if (enc_utf8 && ScreenLinesUC[off] != 0)
7169 bytes[utfc_char2bytes(off, bytes)] = NUL;
7170 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7171 {
7172 bytes[0] = ScreenLines[off];
7173 bytes[1] = ScreenLines2[off];
7174 bytes[2] = NUL;
7175 }
7176 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7177 {
7178 bytes[1] = ScreenLines[off + 1];
7179 bytes[2] = NUL;
7180 }
7181#endif
7182 }
7183}
7184
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007185#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007186static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007187
7188/*
7189 * Return TRUE if composing characters for screen posn "off" differs from
7190 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007191 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007192 */
7193 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007194screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007195{
7196 int i;
7197
7198 for (i = 0; i < Screen_mco; ++i)
7199 {
7200 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7201 return TRUE;
7202 if (u8cc[i] == 0)
7203 break;
7204 }
7205 return FALSE;
7206}
7207#endif
7208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209/*
7210 * Put string '*text' on the screen at position 'row' and 'col', with
7211 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7212 * Note: only outputs within one row, message is truncated at screen boundary!
7213 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7214 */
7215 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007216screen_puts(
7217 char_u *text,
7218 int row,
7219 int col,
7220 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222 screen_puts_len(text, -1, row, col, attr);
7223}
7224
7225/*
7226 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7227 * a NUL.
7228 */
7229 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007230screen_puts_len(
7231 char_u *text,
7232 int textlen,
7233 int row,
7234 int col,
7235 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236{
7237 unsigned off;
7238 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007239 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 int c;
7241#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007242 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 int mbyte_blen = 1;
7244 int mbyte_cells = 1;
7245 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007246 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 int clear_next_cell = FALSE;
7248# ifdef FEAT_ARABIC
7249 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007250 int pc, nc, nc1;
7251 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252# endif
7253#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007254#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7255 int force_redraw_this;
7256 int force_redraw_next = FALSE;
7257#endif
7258 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259
7260 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7261 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007262 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
Bram Moolenaarc236c162008-07-13 17:41:49 +00007264#ifdef FEAT_MBYTE
7265 /* When drawing over the right halve of a double-wide char clear out the
7266 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007267 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007268# ifdef FEAT_GUI
7269 && !gui.in_use
7270# endif
7271 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007272 {
7273 ScreenLines[off - 1] = ' ';
7274 ScreenAttrs[off - 1] = 0;
7275 if (enc_utf8)
7276 {
7277 ScreenLinesUC[off - 1] = 0;
7278 ScreenLinesC[0][off - 1] = 0;
7279 }
7280 /* redraw the previous cell, make it empty */
7281 screen_char(off - 1, row, col - 1);
7282 /* force the cell at "col" to be redrawn */
7283 force_redraw_next = TRUE;
7284 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007285#endif
7286
Bram Moolenaar367329b2007-08-30 11:53:22 +00007287#ifdef FEAT_MBYTE
7288 max_off = LineOffset[row] + screen_Columns;
7289#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007290 while (col < screen_Columns
7291 && (len < 0 || (int)(ptr - text) < len)
7292 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
7294 c = *ptr;
7295#ifdef FEAT_MBYTE
7296 /* check if this is the first byte of a multibyte */
7297 if (has_mbyte)
7298 {
7299 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007300 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007302 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7304 mbyte_cells = 1;
7305 else if (enc_dbcs != 0)
7306 mbyte_cells = mbyte_blen;
7307 else /* enc_utf8 */
7308 {
7309 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007310 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 (int)((text + len) - ptr));
7312 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007313 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007315# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 /* Non-BMP character: display as ? or fullwidth ?. */
7317 if (u8c >= 0x10000)
7318 {
7319 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7320 if (attr == 0)
7321 attr = hl_attr(HLF_8);
7322 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007323# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324# ifdef FEAT_ARABIC
7325 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7326 {
7327 /* Do Arabic shaping. */
7328 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7329 {
7330 /* Past end of string to be displayed. */
7331 nc = NUL;
7332 nc1 = NUL;
7333 }
7334 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007335 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007336 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7337 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007338 nc1 = pcc[0];
7339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 pc = prev_c;
7341 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007342 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 }
7344 else
7345 prev_c = u8c;
7346# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007347 if (col + mbyte_cells > screen_Columns)
7348 {
7349 /* Only 1 cell left, but character requires 2 cells:
7350 * display a '>' in the last column to avoid wrapping. */
7351 c = '>';
7352 mbyte_cells = 1;
7353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 }
7355 }
7356#endif
7357
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007358#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7359 force_redraw_this = force_redraw_next;
7360 force_redraw_next = FALSE;
7361#endif
7362
7363 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364#ifdef FEAT_MBYTE
7365 || (mbyte_cells == 2
7366 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7367 || (enc_dbcs == DBCS_JPNU
7368 && c == 0x8e
7369 && ScreenLines2[off] != ptr[1])
7370 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007371 && (ScreenLinesUC[off] !=
7372 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7373 || (ScreenLinesUC[off] != 0
7374 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375#endif
7376 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007377 || exmode_active;
7378
7379 if (need_redraw
7380#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7381 || force_redraw_this
7382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 )
7384 {
7385#if defined(FEAT_GUI) || defined(UNIX)
7386 /* The bold trick makes a single row of pixels appear in the next
7387 * character. When a bold character is removed, the next
7388 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007389 * and for some xterms. */
7390 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391# ifdef FEAT_GUI
7392 gui.in_use
7393# endif
7394# if defined(FEAT_GUI) && defined(UNIX)
7395 ||
7396# endif
7397# ifdef UNIX
7398 term_is_xterm
7399# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007400 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007402 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007404 if (n > HL_ALL)
7405 n = syn_attr2attr(n);
7406 if (n & HL_BOLD)
7407 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 }
7409#endif
7410#ifdef FEAT_MBYTE
7411 /* When at the end of the text and overwriting a two-cell
7412 * character with a one-cell character, need to clear the next
7413 * cell. Also when overwriting the left halve of a two-cell char
7414 * with the right halve of a two-cell char. Do this only once
7415 * (mb_off2cells() may return 2 on the right halve). */
7416 if (clear_next_cell)
7417 clear_next_cell = FALSE;
7418 else if (has_mbyte
7419 && (len < 0 ? ptr[mbyte_blen] == NUL
7420 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007421 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007423 && (*mb_off2cells)(off, max_off) == 1
7424 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 clear_next_cell = TRUE;
7426
7427 /* Make sure we never leave a second byte of a double-byte behind,
7428 * it confuses mb_off2cells(). */
7429 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007430 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007432 && (*mb_off2cells)(off, max_off) == 1
7433 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 ScreenLines[off + mbyte_blen] = 0;
7435#endif
7436 ScreenLines[off] = c;
7437 ScreenAttrs[off] = attr;
7438#ifdef FEAT_MBYTE
7439 if (enc_utf8)
7440 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007441 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 ScreenLinesUC[off] = 0;
7443 else
7444 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007445 int i;
7446
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007448 for (i = 0; i < Screen_mco; ++i)
7449 {
7450 ScreenLinesC[i][off] = u8cc[i];
7451 if (u8cc[i] == 0)
7452 break;
7453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 }
7455 if (mbyte_cells == 2)
7456 {
7457 ScreenLines[off + 1] = 0;
7458 ScreenAttrs[off + 1] = attr;
7459 }
7460 screen_char(off, row, col);
7461 }
7462 else if (mbyte_cells == 2)
7463 {
7464 ScreenLines[off + 1] = ptr[1];
7465 ScreenAttrs[off + 1] = attr;
7466 screen_char_2(off, row, col);
7467 }
7468 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7469 {
7470 ScreenLines2[off] = ptr[1];
7471 screen_char(off, row, col);
7472 }
7473 else
7474#endif
7475 screen_char(off, row, col);
7476 }
7477#ifdef FEAT_MBYTE
7478 if (has_mbyte)
7479 {
7480 off += mbyte_cells;
7481 col += mbyte_cells;
7482 ptr += mbyte_blen;
7483 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007484 {
7485 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007487 len = -1;
7488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 }
7490 else
7491#endif
7492 {
7493 ++off;
7494 ++col;
7495 ++ptr;
7496 }
7497 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007498
7499#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7500 /* If we detected the next character needs to be redrawn, but the text
7501 * doesn't extend up to there, update the character here. */
7502 if (force_redraw_next && col < screen_Columns)
7503 {
7504# ifdef FEAT_MBYTE
7505 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7506 screen_char_2(off, row, col);
7507 else
7508# endif
7509 screen_char(off, row, col);
7510 }
7511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512}
7513
7514#ifdef FEAT_SEARCH_EXTRA
7515/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007516 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 */
7518 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007519start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
7521 if (p_hls && !no_hlsearch)
7522 {
7523 last_pat_prog(&search_hl.rm);
7524 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007525# ifdef FEAT_RELTIME
7526 /* Set the time limit to 'redrawtime'. */
7527 profile_setlimit(p_rdt, &search_hl.tm);
7528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 }
7530}
7531
7532/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007533 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 */
7535 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007536end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 if (search_hl.rm.regprog != NULL)
7539 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007540 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 search_hl.rm.regprog = NULL;
7542 }
7543}
7544
7545/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007546 * Init for calling prepare_search_hl().
7547 */
7548 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007549init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007550{
7551 matchitem_T *cur;
7552
7553 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7554 * match */
7555 cur = wp->w_match_head;
7556 while (cur != NULL)
7557 {
7558 cur->hl.rm = cur->match;
7559 if (cur->hlg_id == 0)
7560 cur->hl.attr = 0;
7561 else
7562 cur->hl.attr = syn_id2attr(cur->hlg_id);
7563 cur->hl.buf = wp->w_buffer;
7564 cur->hl.lnum = 0;
7565 cur->hl.first_lnum = 0;
7566# ifdef FEAT_RELTIME
7567 /* Set the time limit to 'redrawtime'. */
7568 profile_setlimit(p_rdt, &(cur->hl.tm));
7569# endif
7570 cur = cur->next;
7571 }
7572 search_hl.buf = wp->w_buffer;
7573 search_hl.lnum = 0;
7574 search_hl.first_lnum = 0;
7575 /* time limit is set at the toplevel, for all windows */
7576}
7577
7578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 * Advance to the match in window "wp" line "lnum" or past it.
7580 */
7581 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007582prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007584 matchitem_T *cur; /* points to the match list */
7585 match_T *shl; /* points to search_hl or a match */
7586 int shl_flag; /* flag to indicate whether search_hl
7587 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007588 int pos_inprogress; /* marks that position match search is
7589 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 int n;
7591
7592 /*
7593 * When using a multi-line pattern, start searching at the top
7594 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007595 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007597 cur = wp->w_match_head;
7598 shl_flag = FALSE;
7599 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007601 if (shl_flag == FALSE)
7602 {
7603 shl = &search_hl;
7604 shl_flag = TRUE;
7605 }
7606 else
7607 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 if (shl->rm.regprog != NULL
7609 && shl->lnum == 0
7610 && re_multiline(shl->rm.regprog))
7611 {
7612 if (shl->first_lnum == 0)
7613 {
7614# ifdef FEAT_FOLDING
7615 for (shl->first_lnum = lnum;
7616 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7617 if (hasFoldingWin(wp, shl->first_lnum - 1,
7618 NULL, NULL, TRUE, NULL))
7619 break;
7620# else
7621 shl->first_lnum = wp->w_topline;
7622# endif
7623 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007624 if (cur != NULL)
7625 cur->pos.cur = 0;
7626 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007628 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7629 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007631 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7632 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007633 pos_inprogress = cur == NULL || cur->pos.cur == 0
7634 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 if (shl->lnum != 0)
7636 {
7637 shl->first_lnum = shl->lnum
7638 + shl->rm.endpos[0].lnum
7639 - shl->rm.startpos[0].lnum;
7640 n = shl->rm.endpos[0].col;
7641 }
7642 else
7643 {
7644 ++shl->first_lnum;
7645 n = 0;
7646 }
7647 }
7648 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007649 if (shl != &search_hl && cur != NULL)
7650 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 }
7652}
7653
7654/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007655 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 * Uses shl->buf.
7657 * Sets shl->lnum and shl->rm contents.
7658 * Note: Assumes a previous match is always before "lnum", unless
7659 * shl->lnum is zero.
7660 * Careful: Any pointers for buffer lines will become invalid.
7661 */
7662 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007663next_search_hl(
7664 win_T *win,
7665 match_T *shl, /* points to search_hl or a match */
7666 linenr_T lnum,
7667 colnr_T mincol, /* minimal column for a match */
7668 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
7670 linenr_T l;
7671 colnr_T matchcol;
7672 long nmatched;
7673
7674 if (shl->lnum != 0)
7675 {
7676 /* Check for three situations:
7677 * 1. If the "lnum" is below a previous match, start a new search.
7678 * 2. If the previous match includes "mincol", use it.
7679 * 3. Continue after the previous match.
7680 */
7681 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7682 if (lnum > l)
7683 shl->lnum = 0;
7684 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7685 return;
7686 }
7687
7688 /*
7689 * Repeat searching for a match until one is found that includes "mincol"
7690 * or none is found in this line.
7691 */
7692 called_emsg = FALSE;
7693 for (;;)
7694 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007695#ifdef FEAT_RELTIME
7696 /* Stop searching after passing the time limit. */
7697 if (profile_passed_limit(&(shl->tm)))
7698 {
7699 shl->lnum = 0; /* no match found in time */
7700 break;
7701 }
7702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 /* Three situations:
7704 * 1. No useful previous match: search from start of line.
7705 * 2. Not Vi compatible or empty match: continue at next character.
7706 * Break the loop if this is beyond the end of the line.
7707 * 3. Vi compatible searching: continue at end of previous match.
7708 */
7709 if (shl->lnum == 0)
7710 matchcol = 0;
7711 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7712 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007713 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007715 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007716
7717 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007718 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007719 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007721 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 shl->lnum = 0;
7723 break;
7724 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007725#ifdef FEAT_MBYTE
7726 if (has_mbyte)
7727 matchcol += mb_ptr2len(ml);
7728 else
7729#endif
7730 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 }
7732 else
7733 matchcol = shl->rm.endpos[0].col;
7734
7735 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007736 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007738 /* Remember whether shl->rm is using a copy of the regprog in
7739 * cur->match. */
7740 int regprog_is_copy = (shl != &search_hl && cur != NULL
7741 && shl == &cur->hl
7742 && cur->match.regprog == cur->hl.rm.regprog);
7743
Bram Moolenaarb3414592014-06-17 17:48:32 +02007744 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7745 matchcol,
7746#ifdef FEAT_RELTIME
7747 &(shl->tm)
7748#else
7749 NULL
7750#endif
7751 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007752 /* Copy the regprog, in case it got freed and recompiled. */
7753 if (regprog_is_copy)
7754 cur->match.regprog = cur->hl.rm.regprog;
7755
Bram Moolenaarb3414592014-06-17 17:48:32 +02007756 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007757 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007758 /* Error while handling regexp: stop using this regexp. */
7759 if (shl == &search_hl)
7760 {
7761 /* don't free regprog in the match list, it's a copy */
7762 vim_regfree(shl->rm.regprog);
7763 SET_NO_HLSEARCH(TRUE);
7764 }
7765 shl->rm.regprog = NULL;
7766 shl->lnum = 0;
7767 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7768 message */
7769 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007770 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007771 }
7772 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007773 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007774 else
7775 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 if (nmatched == 0)
7777 {
7778 shl->lnum = 0; /* no match found */
7779 break;
7780 }
7781 if (shl->rm.startpos[0].lnum > 0
7782 || shl->rm.startpos[0].col >= mincol
7783 || nmatched > 1
7784 || shl->rm.endpos[0].col > mincol)
7785 {
7786 shl->lnum += shl->rm.startpos[0].lnum;
7787 break; /* useful match found */
7788 }
7789 }
7790}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791
Bram Moolenaar85077472016-10-16 14:35:48 +02007792/*
7793 * If there is a match fill "shl" and return one.
7794 * Return zero otherwise.
7795 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007796 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007797next_search_hl_pos(
7798 match_T *shl, /* points to a match */
7799 linenr_T lnum,
7800 posmatch_T *posmatch, /* match positions */
7801 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007802{
7803 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007804 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007805
Bram Moolenaarb3414592014-06-17 17:48:32 +02007806 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7807 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007808 llpos_T *pos = &posmatch->pos[i];
7809
7810 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007811 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007812 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007813 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007814 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007815 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007816 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007817 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007818 /* if this match comes before the one at "found" then swap
7819 * them */
7820 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007821 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007822 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007823
Bram Moolenaar85077472016-10-16 14:35:48 +02007824 *pos = posmatch->pos[found];
7825 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007826 }
7827 }
7828 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007829 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007830 }
7831 }
7832 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007833 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007834 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007835 colnr_T start = posmatch->pos[found].col == 0
7836 ? 0 : posmatch->pos[found].col - 1;
7837 colnr_T end = posmatch->pos[found].col == 0
7838 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007839
Bram Moolenaar85077472016-10-16 14:35:48 +02007840 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007841 shl->rm.startpos[0].lnum = 0;
7842 shl->rm.startpos[0].col = start;
7843 shl->rm.endpos[0].lnum = 0;
7844 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007845 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007846 posmatch->cur = found + 1;
7847 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007848 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007849 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007850}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007851#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007852
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007854screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856 attrentry_T *aep = NULL;
7857
7858 screen_attr = attr;
7859 if (full_screen
7860#ifdef WIN3264
7861 && termcap_active
7862#endif
7863 )
7864 {
7865#ifdef FEAT_GUI
7866 if (gui.in_use)
7867 {
7868 char buf[20];
7869
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007870 /* The GUI handles this internally. */
7871 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 OUT_STR(buf);
7873 }
7874 else
7875#endif
7876 {
7877 if (attr > HL_ALL) /* special HL attr. */
7878 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007879 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 aep = syn_cterm_attr2entry(attr);
7881 else
7882 aep = syn_term_attr2entry(attr);
7883 if (aep == NULL) /* did ":syntax clear" */
7884 attr = 0;
7885 else
7886 attr = aep->ae_attr;
7887 }
7888 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7889 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007890 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007891#ifdef FEAT_TERMGUICOLORS
7892 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007893 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007894#endif
7895 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007896#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007897 )
7898#endif
7899 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007900 /* If the Normal FG color has BOLD attribute and the new HL
7901 * has a FG color defined, clear BOLD. */
7902 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7904 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007905 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7906 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 out_str(T_US);
7908 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7909 out_str(T_CZH);
7910 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7911 out_str(T_MR);
7912
7913 /*
7914 * Output the color or start string after bold etc., in case the
7915 * bold etc. override the color setting.
7916 */
7917 if (aep != NULL)
7918 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007919#ifdef FEAT_TERMGUICOLORS
7920 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007922 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007923 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007924 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007925 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 }
7927 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007930 if (t_colors > 1)
7931 {
7932 if (aep->ae_u.cterm.fg_color)
7933 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7934 if (aep->ae_u.cterm.bg_color)
7935 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7936 }
7937 else
7938 {
7939 if (aep->ae_u.term.start != NULL)
7940 out_str(aep->ae_u.term.start);
7941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 }
7943 }
7944 }
7945 }
7946}
7947
7948 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007949screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950{
7951 int do_ME = FALSE; /* output T_ME code */
7952
7953 if (screen_attr != 0
7954#ifdef WIN3264
7955 && termcap_active
7956#endif
7957 )
7958 {
7959#ifdef FEAT_GUI
7960 if (gui.in_use)
7961 {
7962 char buf[20];
7963
7964 /* use internal GUI code */
7965 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7966 OUT_STR(buf);
7967 }
7968 else
7969#endif
7970 {
7971 if (screen_attr > HL_ALL) /* special HL attr. */
7972 {
7973 attrentry_T *aep;
7974
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007975 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 {
7977 /*
7978 * Assume that t_me restores the original colors!
7979 */
7980 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007981 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007982#ifdef FEAT_TERMGUICOLORS
7983 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007984 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
7985 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007986#endif
7987 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007988#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007989 )
7990#endif
7991 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 do_ME = TRUE;
7993 }
7994 else
7995 {
7996 aep = syn_term_attr2entry(screen_attr);
7997 if (aep != NULL && aep->ae_u.term.stop != NULL)
7998 {
7999 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8000 do_ME = TRUE;
8001 else
8002 out_str(aep->ae_u.term.stop);
8003 }
8004 }
8005 if (aep == NULL) /* did ":syntax clear" */
8006 screen_attr = 0;
8007 else
8008 screen_attr = aep->ae_attr;
8009 }
8010
8011 /*
8012 * Often all ending-codes are equal to T_ME. Avoid outputting the
8013 * same sequence several times.
8014 */
8015 if (screen_attr & HL_STANDOUT)
8016 {
8017 if (STRCMP(T_SE, T_ME) == 0)
8018 do_ME = TRUE;
8019 else
8020 out_str(T_SE);
8021 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008022 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {
8024 if (STRCMP(T_UE, T_ME) == 0)
8025 do_ME = TRUE;
8026 else
8027 out_str(T_UE);
8028 }
8029 if (screen_attr & HL_ITALIC)
8030 {
8031 if (STRCMP(T_CZR, T_ME) == 0)
8032 do_ME = TRUE;
8033 else
8034 out_str(T_CZR);
8035 }
8036 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8037 out_str(T_ME);
8038
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008039#ifdef FEAT_TERMGUICOLORS
8040 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008042 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008043 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008044 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008045 term_bg_rgb_color(cterm_normal_bg_gui_color);
8046 }
8047 else
8048#endif
8049 {
8050 if (t_colors > 1)
8051 {
8052 /* set Normal cterm colors */
8053 if (cterm_normal_fg_color != 0)
8054 term_fg_color(cterm_normal_fg_color - 1);
8055 if (cterm_normal_bg_color != 0)
8056 term_bg_color(cterm_normal_bg_color - 1);
8057 if (cterm_normal_fg_bold)
8058 out_str(T_MD);
8059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 }
8061 }
8062 }
8063 screen_attr = 0;
8064}
8065
8066/*
8067 * Reset the colors for a cterm. Used when leaving Vim.
8068 * The machine specific code may override this again.
8069 */
8070 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008071reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008073 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 {
8075 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008076#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008077 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8078 || cterm_normal_bg_gui_color != INVALCOLOR)
8079 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008080#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {
8084 out_str(T_OP);
8085 screen_attr = -1;
8086 }
8087 if (cterm_normal_fg_bold)
8088 {
8089 out_str(T_ME);
8090 screen_attr = -1;
8091 }
8092 }
8093}
8094
8095/*
8096 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8097 * using the attributes from ScreenAttrs["off"].
8098 */
8099 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008100screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101{
8102 int attr;
8103
8104 /* Check for illegal values, just in case (could happen just after
8105 * resizing). */
8106 if (row >= screen_Rows || col >= screen_Columns)
8107 return;
8108
Bram Moolenaar494838a2015-02-10 19:20:37 +01008109 /* Outputting a character in the last cell on the screen may scroll the
8110 * screen up. Only do it when the "xn" termcap property is set, otherwise
8111 * mark the character invalid (update it when scrolled up). */
8112 if (*T_XN == NUL
8113 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114#ifdef FEAT_RIGHTLEFT
8115 /* account for first command-line character in rightleft mode */
8116 && !cmdmsg_rl
8117#endif
8118 )
8119 {
8120 ScreenAttrs[off] = (sattr_T)-1;
8121 return;
8122 }
8123
8124 /*
8125 * Stop highlighting first, so it's easier to move the cursor.
8126 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008127#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 if (screen_char_attr != 0)
8129 attr = screen_char_attr;
8130 else
8131#endif
8132 attr = ScreenAttrs[off];
8133 if (screen_attr != attr)
8134 screen_stop_highlight();
8135
8136 windgoto(row, col);
8137
8138 if (screen_attr != attr)
8139 screen_start_highlight(attr);
8140
8141#ifdef FEAT_MBYTE
8142 if (enc_utf8 && ScreenLinesUC[off] != 0)
8143 {
8144 char_u buf[MB_MAXBYTES + 1];
8145
8146 /* Convert UTF-8 character to bytes and write it. */
8147
8148 buf[utfc_char2bytes(off, buf)] = NUL;
8149
8150 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008151 if (utf_ambiguous_width(ScreenLinesUC[off]))
8152 screen_cur_col = 9999;
8153 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 ++screen_cur_col;
8155 }
8156 else
8157#endif
8158 {
8159#ifdef FEAT_MBYTE
8160 out_flush_check();
8161#endif
8162 out_char(ScreenLines[off]);
8163#ifdef FEAT_MBYTE
8164 /* double-byte character in single-width cell */
8165 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8166 out_char(ScreenLines2[off]);
8167#endif
8168 }
8169
8170 screen_cur_col++;
8171}
8172
8173#ifdef FEAT_MBYTE
8174
8175/*
8176 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8177 * on the screen at position 'row' and 'col'.
8178 * The attributes of the first byte is used for all. This is required to
8179 * output the two bytes of a double-byte character with nothing in between.
8180 */
8181 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008182screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183{
8184 /* Check for illegal values (could be wrong when screen was resized). */
8185 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8186 return;
8187
8188 /* Outputting the last character on the screen may scrollup the screen.
8189 * Don't to it! Mark the character invalid (update it when scrolled up) */
8190 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8191 {
8192 ScreenAttrs[off] = (sattr_T)-1;
8193 return;
8194 }
8195
8196 /* Output the first byte normally (positions the cursor), then write the
8197 * second byte directly. */
8198 screen_char(off, row, col);
8199 out_char(ScreenLines[off + 1]);
8200 ++screen_cur_col;
8201}
8202#endif
8203
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008204#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205/*
8206 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8207 * This uses the contents of ScreenLines[] and doesn't change it.
8208 */
8209 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008210screen_draw_rectangle(
8211 int row,
8212 int col,
8213 int height,
8214 int width,
8215 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216{
8217 int r, c;
8218 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008219#ifdef FEAT_MBYTE
8220 int max_off;
8221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008223 /* Can't use ScreenLines unless initialized */
8224 if (ScreenLines == NULL)
8225 return;
8226
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 if (invert)
8228 screen_char_attr = HL_INVERSE;
8229 for (r = row; r < row + height; ++r)
8230 {
8231 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008232#ifdef FEAT_MBYTE
8233 max_off = off + screen_Columns;
8234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 for (c = col; c < col + width; ++c)
8236 {
8237#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008238 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {
8240 screen_char_2(off + c, r, c);
8241 ++c;
8242 }
8243 else
8244#endif
8245 {
8246 screen_char(off + c, r, c);
8247#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008248 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 ++c;
8250#endif
8251 }
8252 }
8253 }
8254 screen_char_attr = 0;
8255}
8256#endif
8257
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008258#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259/*
8260 * Redraw the characters for a vertically split window.
8261 */
8262 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008263redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264{
8265 int col;
8266 int width;
8267
8268# ifdef FEAT_CLIPBOARD
8269 clip_may_clear_selection(row, end - 1);
8270# endif
8271
8272 if (wp == NULL)
8273 {
8274 col = 0;
8275 width = Columns;
8276 }
8277 else
8278 {
8279 col = wp->w_wincol;
8280 width = wp->w_width;
8281 }
8282 screen_draw_rectangle(row, col, end - row, width, FALSE);
8283}
8284#endif
8285
8286/*
8287 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8288 * with character 'c1' in first column followed by 'c2' in the other columns.
8289 * Use attributes 'attr'.
8290 */
8291 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008292screen_fill(
8293 int start_row,
8294 int end_row,
8295 int start_col,
8296 int end_col,
8297 int c1,
8298 int c2,
8299 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 int row;
8302 int col;
8303 int off;
8304 int end_off;
8305 int did_delete;
8306 int c;
8307 int norm_term;
8308#if defined(FEAT_GUI) || defined(UNIX)
8309 int force_next = FALSE;
8310#endif
8311
8312 if (end_row > screen_Rows) /* safety check */
8313 end_row = screen_Rows;
8314 if (end_col > screen_Columns) /* safety check */
8315 end_col = screen_Columns;
8316 if (ScreenLines == NULL
8317 || start_row >= end_row
8318 || start_col >= end_col) /* nothing to do */
8319 return;
8320
8321 /* it's a "normal" terminal when not in a GUI or cterm */
8322 norm_term = (
8323#ifdef FEAT_GUI
8324 !gui.in_use &&
8325#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008326 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 for (row = start_row; row < end_row; ++row)
8328 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008329#ifdef FEAT_MBYTE
8330 if (has_mbyte
8331# ifdef FEAT_GUI
8332 && !gui.in_use
8333# endif
8334 )
8335 {
8336 /* When drawing over the right halve of a double-wide char clear
8337 * out the left halve. When drawing over the left halve of a
8338 * double wide-char clear out the right halve. Only needed in a
8339 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008340 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008341 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008342 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008343 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008344 }
8345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 /*
8347 * Try to use delete-line termcap code, when no attributes or in a
8348 * "normal" terminal, where a bold/italic space is just a
8349 * space.
8350 */
8351 did_delete = FALSE;
8352 if (c2 == ' '
8353 && end_col == Columns
8354 && can_clear(T_CE)
8355 && (attr == 0
8356 || (norm_term
8357 && attr <= HL_ALL
8358 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8359 {
8360 /*
8361 * check if we really need to clear something
8362 */
8363 col = start_col;
8364 if (c1 != ' ') /* don't clear first char */
8365 ++col;
8366
8367 off = LineOffset[row] + col;
8368 end_off = LineOffset[row] + end_col;
8369
8370 /* skip blanks (used often, keep it fast!) */
8371#ifdef FEAT_MBYTE
8372 if (enc_utf8)
8373 while (off < end_off && ScreenLines[off] == ' '
8374 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8375 ++off;
8376 else
8377#endif
8378 while (off < end_off && ScreenLines[off] == ' '
8379 && ScreenAttrs[off] == 0)
8380 ++off;
8381 if (off < end_off) /* something to be cleared */
8382 {
8383 col = off - LineOffset[row];
8384 screen_stop_highlight();
8385 term_windgoto(row, col);/* clear rest of this screen line */
8386 out_str(T_CE);
8387 screen_start(); /* don't know where cursor is now */
8388 col = end_col - col;
8389 while (col--) /* clear chars in ScreenLines */
8390 {
8391 ScreenLines[off] = ' ';
8392#ifdef FEAT_MBYTE
8393 if (enc_utf8)
8394 ScreenLinesUC[off] = 0;
8395#endif
8396 ScreenAttrs[off] = 0;
8397 ++off;
8398 }
8399 }
8400 did_delete = TRUE; /* the chars are cleared now */
8401 }
8402
8403 off = LineOffset[row] + start_col;
8404 c = c1;
8405 for (col = start_col; col < end_col; ++col)
8406 {
8407 if (ScreenLines[off] != c
8408#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008409 || (enc_utf8 && (int)ScreenLinesUC[off]
8410 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411#endif
8412 || ScreenAttrs[off] != attr
8413#if defined(FEAT_GUI) || defined(UNIX)
8414 || force_next
8415#endif
8416 )
8417 {
8418#if defined(FEAT_GUI) || defined(UNIX)
8419 /* The bold trick may make a single row of pixels appear in
8420 * the next character. When a bold character is removed, the
8421 * next character should be redrawn too. This happens for our
8422 * own GUI and for some xterms. */
8423 if (
8424# ifdef FEAT_GUI
8425 gui.in_use
8426# endif
8427# if defined(FEAT_GUI) && defined(UNIX)
8428 ||
8429# endif
8430# ifdef UNIX
8431 term_is_xterm
8432# endif
8433 )
8434 {
8435 if (ScreenLines[off] != ' '
8436 && (ScreenAttrs[off] > HL_ALL
8437 || ScreenAttrs[off] & HL_BOLD))
8438 force_next = TRUE;
8439 else
8440 force_next = FALSE;
8441 }
8442#endif
8443 ScreenLines[off] = c;
8444#ifdef FEAT_MBYTE
8445 if (enc_utf8)
8446 {
8447 if (c >= 0x80)
8448 {
8449 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008450 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 }
8452 else
8453 ScreenLinesUC[off] = 0;
8454 }
8455#endif
8456 ScreenAttrs[off] = attr;
8457 if (!did_delete || c != ' ')
8458 screen_char(off, row, col);
8459 }
8460 ++off;
8461 if (col == start_col)
8462 {
8463 if (did_delete)
8464 break;
8465 c = c2;
8466 }
8467 }
8468 if (end_col == Columns)
8469 LineWraps[row] = FALSE;
8470 if (row == Rows - 1) /* overwritten the command line */
8471 {
8472 redraw_cmdline = TRUE;
8473 if (c1 == ' ' && c2 == ' ')
8474 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008475 if (start_col == 0)
8476 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 }
8478 }
8479}
8480
8481/*
8482 * Check if there should be a delay. Used before clearing or redrawing the
8483 * screen or the command line.
8484 */
8485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008486check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487{
8488 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8489 && !did_wait_return
8490 && emsg_silent == 0)
8491 {
8492 out_flush();
8493 ui_delay(1000L, TRUE);
8494 emsg_on_display = FALSE;
8495 if (check_msg_scroll)
8496 msg_scroll = FALSE;
8497 }
8498}
8499
8500/*
8501 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008502 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 * Returns TRUE if there is a valid screen to write to.
8504 * Returns FALSE when starting up and screen not initialized yet.
8505 */
8506 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008507screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008509 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 return (ScreenLines != NULL);
8511}
8512
8513/*
8514 * Resize the shell to Rows and Columns.
8515 * Allocate ScreenLines[] and associated items.
8516 *
8517 * There may be some time between setting Rows and Columns and (re)allocating
8518 * ScreenLines[]. This happens when starting up and when (manually) changing
8519 * the shell size. Always use screen_Rows and screen_Columns to access items
8520 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8521 * final size of the shell is needed.
8522 */
8523 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008524screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525{
8526 int new_row, old_row;
8527#ifdef FEAT_GUI
8528 int old_Rows;
8529#endif
8530 win_T *wp;
8531 int outofmem = FALSE;
8532 int len;
8533 schar_T *new_ScreenLines;
8534#ifdef FEAT_MBYTE
8535 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008536 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008538 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539#endif
8540 sattr_T *new_ScreenAttrs;
8541 unsigned *new_LineOffset;
8542 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008543#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008544 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008545 tabpage_T *tp;
8546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008548 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008549#ifdef FEAT_AUTOCMD
8550 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008552retry:
8553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 /*
8555 * Allocation of the screen buffers is done only when the size changes and
8556 * when Rows and Columns have been set and we have started doing full
8557 * screen stuff.
8558 */
8559 if ((ScreenLines != NULL
8560 && Rows == screen_Rows
8561 && Columns == screen_Columns
8562#ifdef FEAT_MBYTE
8563 && enc_utf8 == (ScreenLinesUC != NULL)
8564 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008565 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566#endif
8567 )
8568 || Rows == 0
8569 || Columns == 0
8570 || (!full_screen && ScreenLines == NULL))
8571 return;
8572
8573 /*
8574 * It's possible that we produce an out-of-memory message below, which
8575 * will cause this function to be called again. To break the loop, just
8576 * return here.
8577 */
8578 if (entered)
8579 return;
8580 entered = TRUE;
8581
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008582 /*
8583 * Note that the window sizes are updated before reallocating the arrays,
8584 * thus we must not redraw here!
8585 */
8586 ++RedrawingDisabled;
8587
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 win_new_shellsize(); /* fit the windows in the new sized shell */
8589
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 comp_col(); /* recompute columns for shown command and ruler */
8591
8592 /*
8593 * We're changing the size of the screen.
8594 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8595 * - Move lines from the old arrays into the new arrays, clear extra
8596 * lines (unless the screen is going to be cleared).
8597 * - Free the old arrays.
8598 *
8599 * If anything fails, make ScreenLines NULL, so we don't do anything!
8600 * Continuing with the old ScreenLines may result in a crash, because the
8601 * size is wrong.
8602 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008603 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008605#ifdef FEAT_AUTOCMD
8606 if (aucmd_win != NULL)
8607 win_free_lsize(aucmd_win);
8608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
8610 new_ScreenLines = (schar_T *)lalloc((long_u)(
8611 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8612#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008613 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 if (enc_utf8)
8615 {
8616 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8617 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008618 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008619 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8621 }
8622 if (enc_dbcs == DBCS_JPNU)
8623 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8624 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8625#endif
8626 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8627 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8628 new_LineOffset = (unsigned *)lalloc((long_u)(
8629 Rows * sizeof(unsigned)), FALSE);
8630 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008631#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008632 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008635 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 {
8637 if (win_alloc_lines(wp) == FAIL)
8638 {
8639 outofmem = TRUE;
8640#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008641 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642#endif
8643 }
8644 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008645#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008646 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8647 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008648 outofmem = TRUE;
8649#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008650#ifdef FEAT_WINDOWS
8651give_up:
8652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008654#ifdef FEAT_MBYTE
8655 for (i = 0; i < p_mco; ++i)
8656 if (new_ScreenLinesC[i] == NULL)
8657 break;
8658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 if (new_ScreenLines == NULL
8660#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008661 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8663#endif
8664 || new_ScreenAttrs == NULL
8665 || new_LineOffset == NULL
8666 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008667#ifdef FEAT_WINDOWS
8668 || new_TabPageIdxs == NULL
8669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 || outofmem)
8671 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008672 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008673 {
8674 /* guess the size */
8675 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8676
8677 /* Remember we did this to avoid getting outofmem messages over
8678 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008679 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 vim_free(new_ScreenLines);
8682 new_ScreenLines = NULL;
8683#ifdef FEAT_MBYTE
8684 vim_free(new_ScreenLinesUC);
8685 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008686 for (i = 0; i < p_mco; ++i)
8687 {
8688 vim_free(new_ScreenLinesC[i]);
8689 new_ScreenLinesC[i] = NULL;
8690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 vim_free(new_ScreenLines2);
8692 new_ScreenLines2 = NULL;
8693#endif
8694 vim_free(new_ScreenAttrs);
8695 new_ScreenAttrs = NULL;
8696 vim_free(new_LineOffset);
8697 new_LineOffset = NULL;
8698 vim_free(new_LineWraps);
8699 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008700#ifdef FEAT_WINDOWS
8701 vim_free(new_TabPageIdxs);
8702 new_TabPageIdxs = NULL;
8703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 }
8705 else
8706 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008707 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008708
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 for (new_row = 0; new_row < Rows; ++new_row)
8710 {
8711 new_LineOffset[new_row] = new_row * Columns;
8712 new_LineWraps[new_row] = FALSE;
8713
8714 /*
8715 * If the screen is not going to be cleared, copy as much as
8716 * possible from the old screen to the new one and clear the rest
8717 * (used when resizing the window at the "--more--" prompt or when
8718 * executing an external command, for the GUI).
8719 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008720 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 {
8722 (void)vim_memset(new_ScreenLines + new_row * Columns,
8723 ' ', (size_t)Columns * sizeof(schar_T));
8724#ifdef FEAT_MBYTE
8725 if (enc_utf8)
8726 {
8727 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8728 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008729 for (i = 0; i < p_mco; ++i)
8730 (void)vim_memset(new_ScreenLinesC[i]
8731 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 0, (size_t)Columns * sizeof(u8char_T));
8733 }
8734 if (enc_dbcs == DBCS_JPNU)
8735 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8736 0, (size_t)Columns * sizeof(schar_T));
8737#endif
8738 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8739 0, (size_t)Columns * sizeof(sattr_T));
8740 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008741 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 {
8743 if (screen_Columns < Columns)
8744 len = screen_Columns;
8745 else
8746 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008747#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008748 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008749 * may be invalid now. Also when p_mco changes. */
8750 if (!(enc_utf8 && ScreenLinesUC == NULL)
8751 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008752#endif
8753 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8754 ScreenLines + LineOffset[old_row],
8755 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008757 if (enc_utf8 && ScreenLinesUC != NULL
8758 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 {
8760 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8761 ScreenLinesUC + LineOffset[old_row],
8762 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008763 for (i = 0; i < p_mco; ++i)
8764 mch_memmove(new_ScreenLinesC[i]
8765 + new_LineOffset[new_row],
8766 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 (size_t)len * sizeof(u8char_T));
8768 }
8769 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8770 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8771 ScreenLines2 + LineOffset[old_row],
8772 (size_t)len * sizeof(schar_T));
8773#endif
8774 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8775 ScreenAttrs + LineOffset[old_row],
8776 (size_t)len * sizeof(sattr_T));
8777 }
8778 }
8779 }
8780 /* Use the last line of the screen for the current line. */
8781 current_ScreenLine = new_ScreenLines + Rows * Columns;
8782 }
8783
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008784 free_screenlines();
8785
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 ScreenLines = new_ScreenLines;
8787#ifdef FEAT_MBYTE
8788 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008789 for (i = 0; i < p_mco; ++i)
8790 ScreenLinesC[i] = new_ScreenLinesC[i];
8791 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 ScreenLines2 = new_ScreenLines2;
8793#endif
8794 ScreenAttrs = new_ScreenAttrs;
8795 LineOffset = new_LineOffset;
8796 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008797#ifdef FEAT_WINDOWS
8798 TabPageIdxs = new_TabPageIdxs;
8799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800
8801 /* It's important that screen_Rows and screen_Columns reflect the actual
8802 * size of ScreenLines[]. Set them before calling anything. */
8803#ifdef FEAT_GUI
8804 old_Rows = screen_Rows;
8805#endif
8806 screen_Rows = Rows;
8807 screen_Columns = Columns;
8808
8809 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008810 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 screenclear2();
8812
8813#ifdef FEAT_GUI
8814 else if (gui.in_use
8815 && !gui.starting
8816 && ScreenLines != NULL
8817 && old_Rows != Rows)
8818 {
8819 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8820 /*
8821 * Adjust the position of the cursor, for when executing an external
8822 * command.
8823 */
8824 if (msg_row >= Rows) /* Rows got smaller */
8825 msg_row = Rows - 1; /* put cursor at last row */
8826 else if (Rows > old_Rows) /* Rows got bigger */
8827 msg_row += Rows - old_Rows; /* put cursor in same place */
8828 if (msg_col >= Columns) /* Columns got smaller */
8829 msg_col = Columns - 1; /* put cursor at last column */
8830 }
8831#endif
8832
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008834 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008835
8836#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008837 /*
8838 * Do not apply autocommands more than 3 times to avoid an endless loop
8839 * in case applying autocommands always changes Rows or Columns.
8840 */
8841 if (starting == 0 && ++retry_count <= 3)
8842 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008843 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008844 /* In rare cases, autocommands may have altered Rows or Columns,
8845 * jump back to check if we need to allocate the screen again. */
8846 goto retry;
8847 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849}
8850
8851 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008852free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008853{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008854#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008855 int i;
8856
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008857 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008858 for (i = 0; i < Screen_mco; ++i)
8859 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008860 vim_free(ScreenLines2);
8861#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008862 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008863 vim_free(ScreenAttrs);
8864 vim_free(LineOffset);
8865 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008866#ifdef FEAT_WINDOWS
8867 vim_free(TabPageIdxs);
8868#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008869}
8870
8871 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008872screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
8874 check_for_delay(FALSE);
8875 screenalloc(FALSE); /* allocate screen buffers if size changed */
8876 screenclear2(); /* clear the screen */
8877}
8878
8879 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008880screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881{
8882 int i;
8883
8884 if (starting == NO_SCREEN || ScreenLines == NULL
8885#ifdef FEAT_GUI
8886 || (gui.in_use && gui.starting)
8887#endif
8888 )
8889 return;
8890
8891#ifdef FEAT_GUI
8892 if (!gui.in_use)
8893#endif
8894 screen_attr = -1; /* force setting the Normal colors */
8895 screen_stop_highlight(); /* don't want highlighting here */
8896
8897#ifdef FEAT_CLIPBOARD
8898 /* disable selection without redrawing it */
8899 clip_scroll_selection(9999);
8900#endif
8901
8902 /* blank out ScreenLines */
8903 for (i = 0; i < Rows; ++i)
8904 {
8905 lineclear(LineOffset[i], (int)Columns);
8906 LineWraps[i] = FALSE;
8907 }
8908
8909 if (can_clear(T_CL))
8910 {
8911 out_str(T_CL); /* clear the display */
8912 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008913 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 }
8915 else
8916 {
8917 /* can't clear the screen, mark all chars with invalid attributes */
8918 for (i = 0; i < Rows; ++i)
8919 lineinvalid(LineOffset[i], (int)Columns);
8920 clear_cmdline = TRUE;
8921 }
8922
8923 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8924
8925 win_rest_invalid(firstwin);
8926 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008927#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008928 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 if (must_redraw == CLEAR) /* no need to clear again */
8931 must_redraw = NOT_VALID;
8932 compute_cmdrow();
8933 msg_row = cmdline_row; /* put cursor on last line for messages */
8934 msg_col = 0;
8935 screen_start(); /* don't know where cursor is now */
8936 msg_scrolled = 0; /* can't scroll back */
8937 msg_didany = FALSE;
8938 msg_didout = FALSE;
8939}
8940
8941/*
8942 * Clear one line in ScreenLines.
8943 */
8944 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008945lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
8947 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8948#ifdef FEAT_MBYTE
8949 if (enc_utf8)
8950 (void)vim_memset(ScreenLinesUC + off, 0,
8951 (size_t)width * sizeof(u8char_T));
8952#endif
8953 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8954}
8955
8956/*
8957 * Mark one line in ScreenLines invalid by setting the attributes to an
8958 * invalid value.
8959 */
8960 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008961lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
8963 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8964}
8965
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008966#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967/*
8968 * Copy part of a Screenline for vertically split window "wp".
8969 */
8970 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008971linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
8973 unsigned off_to = LineOffset[to] + wp->w_wincol;
8974 unsigned off_from = LineOffset[from] + wp->w_wincol;
8975
8976 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8977 wp->w_width * sizeof(schar_T));
8978# ifdef FEAT_MBYTE
8979 if (enc_utf8)
8980 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008981 int i;
8982
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8984 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008985 for (i = 0; i < p_mco; ++i)
8986 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8987 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 }
8989 if (enc_dbcs == DBCS_JPNU)
8990 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8991 wp->w_width * sizeof(schar_T));
8992# endif
8993 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8994 wp->w_width * sizeof(sattr_T));
8995}
8996#endif
8997
8998/*
8999 * Return TRUE if clearing with term string "p" would work.
9000 * It can't work when the string is empty or it won't set the right background.
9001 */
9002 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009003can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004{
9005 return (*p != NUL && (t_colors <= 1
9006#ifdef FEAT_GUI
9007 || gui.in_use
9008#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009009#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009010 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009011 || (!p_tgc && cterm_normal_bg_color == 0)
9012#else
9013 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009014#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009015 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016}
9017
9018/*
9019 * Reset cursor position. Use whenever cursor was moved because of outputting
9020 * something directly to the screen (shell commands) or a terminal control
9021 * code.
9022 */
9023 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009024screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025{
9026 screen_cur_row = screen_cur_col = 9999;
9027}
9028
9029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 * Move the cursor to position "row","col" in the screen.
9031 * This tries to find the most efficient way to move, minimizing the number of
9032 * characters sent to the terminal.
9033 */
9034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009035windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009037 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 int i;
9039 int plan;
9040 int cost;
9041 int wouldbe_col;
9042 int noinvcurs;
9043 char_u *bs;
9044 int goto_cost;
9045 int attr;
9046
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009047#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9049
9050#define PLAN_LE 1
9051#define PLAN_CR 2
9052#define PLAN_NL 3
9053#define PLAN_WRITE 4
9054 /* Can't use ScreenLines unless initialized */
9055 if (ScreenLines == NULL)
9056 return;
9057
9058 if (col != screen_cur_col || row != screen_cur_row)
9059 {
9060 /* Check for valid position. */
9061 if (row < 0) /* window without text lines? */
9062 row = 0;
9063 if (row >= screen_Rows)
9064 row = screen_Rows - 1;
9065 if (col >= screen_Columns)
9066 col = screen_Columns - 1;
9067
9068 /* check if no cursor movement is allowed in highlight mode */
9069 if (screen_attr && *T_MS == NUL)
9070 noinvcurs = HIGHL_COST;
9071 else
9072 noinvcurs = 0;
9073 goto_cost = GOTO_COST + noinvcurs;
9074
9075 /*
9076 * Plan how to do the positioning:
9077 * 1. Use CR to move it to column 0, same row.
9078 * 2. Use T_LE to move it a few columns to the left.
9079 * 3. Use NL to move a few lines down, column 0.
9080 * 4. Move a few columns to the right with T_ND or by writing chars.
9081 *
9082 * Don't do this if the cursor went beyond the last column, the cursor
9083 * position is unknown then (some terminals wrap, some don't )
9084 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009085 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 * characters to move the cursor to the right.
9087 */
9088 if (row >= screen_cur_row && screen_cur_col < Columns)
9089 {
9090 /*
9091 * If the cursor is in the same row, bigger col, we can use CR
9092 * or T_LE.
9093 */
9094 bs = NULL; /* init for GCC */
9095 attr = screen_attr;
9096 if (row == screen_cur_row && col < screen_cur_col)
9097 {
9098 /* "le" is preferred over "bc", because "bc" is obsolete */
9099 if (*T_LE)
9100 bs = T_LE; /* "cursor left" */
9101 else
9102 bs = T_BC; /* "backspace character (old) */
9103 if (*bs)
9104 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9105 else
9106 cost = 999;
9107 if (col + 1 < cost) /* using CR is less characters */
9108 {
9109 plan = PLAN_CR;
9110 wouldbe_col = 0;
9111 cost = 1; /* CR is just one character */
9112 }
9113 else
9114 {
9115 plan = PLAN_LE;
9116 wouldbe_col = col;
9117 }
9118 if (noinvcurs) /* will stop highlighting */
9119 {
9120 cost += noinvcurs;
9121 attr = 0;
9122 }
9123 }
9124
9125 /*
9126 * If the cursor is above where we want to be, we can use CR LF.
9127 */
9128 else if (row > screen_cur_row)
9129 {
9130 plan = PLAN_NL;
9131 wouldbe_col = 0;
9132 cost = (row - screen_cur_row) * 2; /* CR LF */
9133 if (noinvcurs) /* will stop highlighting */
9134 {
9135 cost += noinvcurs;
9136 attr = 0;
9137 }
9138 }
9139
9140 /*
9141 * If the cursor is in the same row, smaller col, just use write.
9142 */
9143 else
9144 {
9145 plan = PLAN_WRITE;
9146 wouldbe_col = screen_cur_col;
9147 cost = 0;
9148 }
9149
9150 /*
9151 * Check if any characters that need to be written have the
9152 * correct attributes. Also avoid UTF-8 characters.
9153 */
9154 i = col - wouldbe_col;
9155 if (i > 0)
9156 cost += i;
9157 if (cost < goto_cost && i > 0)
9158 {
9159 /*
9160 * Check if the attributes are correct without additionally
9161 * stopping highlighting.
9162 */
9163 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9164 while (i && *p++ == attr)
9165 --i;
9166 if (i != 0)
9167 {
9168 /*
9169 * Try if it works when highlighting is stopped here.
9170 */
9171 if (*--p == 0)
9172 {
9173 cost += noinvcurs;
9174 while (i && *p++ == 0)
9175 --i;
9176 }
9177 if (i != 0)
9178 cost = 999; /* different attributes, don't do it */
9179 }
9180#ifdef FEAT_MBYTE
9181 if (enc_utf8)
9182 {
9183 /* Don't use an UTF-8 char for positioning, it's slow. */
9184 for (i = wouldbe_col; i < col; ++i)
9185 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9186 {
9187 cost = 999;
9188 break;
9189 }
9190 }
9191#endif
9192 }
9193
9194 /*
9195 * We can do it without term_windgoto()!
9196 */
9197 if (cost < goto_cost)
9198 {
9199 if (plan == PLAN_LE)
9200 {
9201 if (noinvcurs)
9202 screen_stop_highlight();
9203 while (screen_cur_col > col)
9204 {
9205 out_str(bs);
9206 --screen_cur_col;
9207 }
9208 }
9209 else if (plan == PLAN_CR)
9210 {
9211 if (noinvcurs)
9212 screen_stop_highlight();
9213 out_char('\r');
9214 screen_cur_col = 0;
9215 }
9216 else if (plan == PLAN_NL)
9217 {
9218 if (noinvcurs)
9219 screen_stop_highlight();
9220 while (screen_cur_row < row)
9221 {
9222 out_char('\n');
9223 ++screen_cur_row;
9224 }
9225 screen_cur_col = 0;
9226 }
9227
9228 i = col - screen_cur_col;
9229 if (i > 0)
9230 {
9231 /*
9232 * Use cursor-right if it's one character only. Avoids
9233 * removing a line of pixels from the last bold char, when
9234 * using the bold trick in the GUI.
9235 */
9236 if (T_ND[0] != NUL && T_ND[1] == NUL)
9237 {
9238 while (i-- > 0)
9239 out_char(*T_ND);
9240 }
9241 else
9242 {
9243 int off;
9244
9245 off = LineOffset[row] + screen_cur_col;
9246 while (i-- > 0)
9247 {
9248 if (ScreenAttrs[off] != screen_attr)
9249 screen_stop_highlight();
9250#ifdef FEAT_MBYTE
9251 out_flush_check();
9252#endif
9253 out_char(ScreenLines[off]);
9254#ifdef FEAT_MBYTE
9255 if (enc_dbcs == DBCS_JPNU
9256 && ScreenLines[off] == 0x8e)
9257 out_char(ScreenLines2[off]);
9258#endif
9259 ++off;
9260 }
9261 }
9262 }
9263 }
9264 }
9265 else
9266 cost = 999;
9267
9268 if (cost >= goto_cost)
9269 {
9270 if (noinvcurs)
9271 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009272 if (row == screen_cur_row && (col > screen_cur_col)
9273 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 term_cursor_right(col - screen_cur_col);
9275 else
9276 term_windgoto(row, col);
9277 }
9278 screen_cur_row = row;
9279 screen_cur_col = col;
9280 }
9281}
9282
9283/*
9284 * Set cursor to its position in the current window.
9285 */
9286 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009287setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288{
9289 if (redrawing())
9290 {
9291 validate_cursor();
9292 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9293 W_WINCOL(curwin) + (
9294#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009295 /* With 'rightleft' set and the cursor on a double-wide
9296 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9298# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009299 (has_mbyte
9300 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9301 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302# endif
9303 1)) :
9304#endif
9305 curwin->w_wcol));
9306 }
9307}
9308
9309
9310/*
9311 * insert 'line_count' lines at 'row' in window 'wp'
9312 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9313 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9314 * scrolling.
9315 * Returns FAIL if the lines are not inserted, OK for success.
9316 */
9317 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009318win_ins_lines(
9319 win_T *wp,
9320 int row,
9321 int line_count,
9322 int invalid,
9323 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
9325 int did_delete;
9326 int nextrow;
9327 int lastrow;
9328 int retval;
9329
9330 if (invalid)
9331 wp->w_lines_valid = 0;
9332
9333 if (wp->w_height < 5)
9334 return FAIL;
9335
9336 if (line_count > wp->w_height - row)
9337 line_count = wp->w_height - row;
9338
9339 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9340 if (retval != MAYBE)
9341 return retval;
9342
9343 /*
9344 * If there is a next window or a status line, we first try to delete the
9345 * lines at the bottom to avoid messing what is after the window.
9346 * If this fails and there are following windows, don't do anything to avoid
9347 * messing up those windows, better just redraw.
9348 */
9349 did_delete = FALSE;
9350#ifdef FEAT_WINDOWS
9351 if (wp->w_next != NULL || wp->w_status_height)
9352 {
9353 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9354 line_count, (int)Rows, FALSE, NULL) == OK)
9355 did_delete = TRUE;
9356 else if (wp->w_next)
9357 return FAIL;
9358 }
9359#endif
9360 /*
9361 * if no lines deleted, blank the lines that will end up below the window
9362 */
9363 if (!did_delete)
9364 {
9365#ifdef FEAT_WINDOWS
9366 wp->w_redr_status = TRUE;
9367#endif
9368 redraw_cmdline = TRUE;
9369 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9370 lastrow = nextrow + line_count;
9371 if (lastrow > Rows)
9372 lastrow = Rows;
9373 screen_fill(nextrow - line_count, lastrow - line_count,
9374 W_WINCOL(wp), (int)W_ENDCOL(wp),
9375 ' ', ' ', 0);
9376 }
9377
9378 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9379 == FAIL)
9380 {
9381 /* deletion will have messed up other windows */
9382 if (did_delete)
9383 {
9384#ifdef FEAT_WINDOWS
9385 wp->w_redr_status = TRUE;
9386#endif
9387 win_rest_invalid(W_NEXT(wp));
9388 }
9389 return FAIL;
9390 }
9391
9392 return OK;
9393}
9394
9395/*
9396 * delete "line_count" window lines at "row" in window "wp"
9397 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9398 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9399 * scrolling
9400 * Return OK for success, FAIL if the lines are not deleted.
9401 */
9402 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009403win_del_lines(
9404 win_T *wp,
9405 int row,
9406 int line_count,
9407 int invalid,
9408 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
9410 int retval;
9411
9412 if (invalid)
9413 wp->w_lines_valid = 0;
9414
9415 if (line_count > wp->w_height - row)
9416 line_count = wp->w_height - row;
9417
9418 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9419 if (retval != MAYBE)
9420 return retval;
9421
9422 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9423 (int)Rows, FALSE, NULL) == FAIL)
9424 return FAIL;
9425
9426#ifdef FEAT_WINDOWS
9427 /*
9428 * If there are windows or status lines below, try to put them at the
9429 * correct place. If we can't do that, they have to be redrawn.
9430 */
9431 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9432 {
9433 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9434 line_count, (int)Rows, NULL) == FAIL)
9435 {
9436 wp->w_redr_status = TRUE;
9437 win_rest_invalid(wp->w_next);
9438 }
9439 }
9440 /*
9441 * If this is the last window and there is no status line, redraw the
9442 * command line later.
9443 */
9444 else
9445#endif
9446 redraw_cmdline = TRUE;
9447 return OK;
9448}
9449
9450/*
9451 * Common code for win_ins_lines() and win_del_lines().
9452 * Returns OK or FAIL when the work has been done.
9453 * Returns MAYBE when not finished yet.
9454 */
9455 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009456win_do_lines(
9457 win_T *wp,
9458 int row,
9459 int line_count,
9460 int mayclear,
9461 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462{
9463 int retval;
9464
9465 if (!redrawing() || line_count <= 0)
9466 return FAIL;
9467
9468 /* only a few lines left: redraw is faster */
9469 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009470#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 && wp->w_width == Columns
9472#endif
9473 )
9474 {
9475 screenclear(); /* will set wp->w_lines_valid to 0 */
9476 return FAIL;
9477 }
9478
9479 /*
9480 * Delete all remaining lines
9481 */
9482 if (row + line_count >= wp->w_height)
9483 {
9484 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9485 W_WINCOL(wp), (int)W_ENDCOL(wp),
9486 ' ', ' ', 0);
9487 return OK;
9488 }
9489
9490 /*
9491 * when scrolling, the message on the command line should be cleared,
9492 * otherwise it will stay there forever.
9493 */
9494 clear_cmdline = TRUE;
9495
9496 /*
9497 * If the terminal can set a scroll region, use that.
9498 * Always do this in a vertically split window. This will redraw from
9499 * ScreenLines[] when t_CV isn't defined. That's faster than using
9500 * win_line().
9501 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009502 * a character in the lower right corner of the scroll region may cause a
9503 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 */
9505 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009506#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 || W_WIDTH(wp) != Columns
9508#endif
9509 )
9510 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009511#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9513#endif
9514 scroll_region_set(wp, row);
9515 if (del)
9516 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9517 wp->w_height - row, FALSE, wp);
9518 else
9519 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9520 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009521#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9523#endif
9524 scroll_region_reset();
9525 return retval;
9526 }
9527
9528#ifdef FEAT_WINDOWS
9529 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9530 return FAIL;
9531#endif
9532
9533 return MAYBE;
9534}
9535
9536/*
9537 * window 'wp' and everything after it is messed up, mark it for redraw
9538 */
9539 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009540win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541{
9542#ifdef FEAT_WINDOWS
9543 while (wp != NULL)
9544#else
9545 if (wp != NULL)
9546#endif
9547 {
9548 redraw_win_later(wp, NOT_VALID);
9549#ifdef FEAT_WINDOWS
9550 wp->w_redr_status = TRUE;
9551 wp = wp->w_next;
9552#endif
9553 }
9554 redraw_cmdline = TRUE;
9555}
9556
9557/*
9558 * The rest of the routines in this file perform screen manipulations. The
9559 * given operation is performed physically on the screen. The corresponding
9560 * change is also made to the internal screen image. In this way, the editor
9561 * anticipates the effect of editing changes on the appearance of the screen.
9562 * That way, when we call screenupdate a complete redraw isn't usually
9563 * necessary. Another advantage is that we can keep adding code to anticipate
9564 * screen changes, and in the meantime, everything still works.
9565 */
9566
9567/*
9568 * types for inserting or deleting lines
9569 */
9570#define USE_T_CAL 1
9571#define USE_T_CDL 2
9572#define USE_T_AL 3
9573#define USE_T_CE 4
9574#define USE_T_DL 5
9575#define USE_T_SR 6
9576#define USE_NL 7
9577#define USE_T_CD 8
9578#define USE_REDRAW 9
9579
9580/*
9581 * insert lines on the screen and update ScreenLines[]
9582 * 'end' is the line after the scrolled part. Normally it is Rows.
9583 * When scrolling region used 'off' is the offset from the top for the region.
9584 * 'row' and 'end' are relative to the start of the region.
9585 *
9586 * return FAIL for failure, OK for success.
9587 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009588 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009589screen_ins_lines(
9590 int off,
9591 int row,
9592 int line_count,
9593 int end,
9594 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595{
9596 int i;
9597 int j;
9598 unsigned temp;
9599 int cursor_row;
9600 int type;
9601 int result_empty;
9602 int can_ce = can_clear(T_CE);
9603
9604 /*
9605 * FAIL if
9606 * - there is no valid screen
9607 * - the screen has to be redrawn completely
9608 * - the line count is less than one
9609 * - the line count is more than 'ttyscroll'
9610 */
9611 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9612 return FAIL;
9613
9614 /*
9615 * There are seven ways to insert lines:
9616 * 0. When in a vertically split window and t_CV isn't set, redraw the
9617 * characters from ScreenLines[].
9618 * 1. Use T_CD (clear to end of display) if it exists and the result of
9619 * the insert is just empty lines
9620 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9621 * present or line_count > 1. It looks better if we do all the inserts
9622 * at once.
9623 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9624 * insert is just empty lines and T_CE is not present or line_count >
9625 * 1.
9626 * 4. Use T_AL (insert line) if it exists.
9627 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9628 * just empty lines.
9629 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9630 * just empty lines.
9631 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9632 * the 'da' flag is not set or we have clear line capability.
9633 * 8. redraw the characters from ScreenLines[].
9634 *
9635 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9636 * the scrollbar for the window. It does have insert line, use that if it
9637 * exists.
9638 */
9639 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009640#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9642 type = USE_REDRAW;
9643 else
9644#endif
9645 if (can_clear(T_CD) && result_empty)
9646 type = USE_T_CD;
9647 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9648 type = USE_T_CAL;
9649 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9650 type = USE_T_CDL;
9651 else if (*T_AL != NUL)
9652 type = USE_T_AL;
9653 else if (can_ce && result_empty)
9654 type = USE_T_CE;
9655 else if (*T_DL != NUL && result_empty)
9656 type = USE_T_DL;
9657 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9658 type = USE_T_SR;
9659 else
9660 return FAIL;
9661
9662 /*
9663 * For clearing the lines screen_del_lines() is used. This will also take
9664 * care of t_db if necessary.
9665 */
9666 if (type == USE_T_CD || type == USE_T_CDL ||
9667 type == USE_T_CE || type == USE_T_DL)
9668 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9669
9670 /*
9671 * If text is retained below the screen, first clear or delete as many
9672 * lines at the bottom of the window as are about to be inserted so that
9673 * the deleted lines won't later surface during a screen_del_lines.
9674 */
9675 if (*T_DB)
9676 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9677
9678#ifdef FEAT_CLIPBOARD
9679 /* Remove a modeless selection when inserting lines halfway the screen
9680 * or not the full width of the screen. */
9681 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009682# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 || (wp != NULL && wp->w_width != Columns)
9684# endif
9685 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009686 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 else
9688 clip_scroll_selection(-line_count);
9689#endif
9690
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691#ifdef FEAT_GUI
9692 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9693 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009694 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695#endif
9696
9697 if (*T_CCS != NUL) /* cursor relative to region */
9698 cursor_row = row;
9699 else
9700 cursor_row = row + off;
9701
9702 /*
9703 * Shift LineOffset[] line_count down to reflect the inserted lines.
9704 * Clear the inserted lines in ScreenLines[].
9705 */
9706 row += off;
9707 end += off;
9708 for (i = 0; i < line_count; ++i)
9709 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009710#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 if (wp != NULL && wp->w_width != Columns)
9712 {
9713 /* need to copy part of a line */
9714 j = end - 1 - i;
9715 while ((j -= line_count) >= row)
9716 linecopy(j + line_count, j, wp);
9717 j += line_count;
9718 if (can_clear((char_u *)" "))
9719 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9720 else
9721 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9722 LineWraps[j] = FALSE;
9723 }
9724 else
9725#endif
9726 {
9727 j = end - 1 - i;
9728 temp = LineOffset[j];
9729 while ((j -= line_count) >= row)
9730 {
9731 LineOffset[j + line_count] = LineOffset[j];
9732 LineWraps[j + line_count] = LineWraps[j];
9733 }
9734 LineOffset[j + line_count] = temp;
9735 LineWraps[j + line_count] = FALSE;
9736 if (can_clear((char_u *)" "))
9737 lineclear(temp, (int)Columns);
9738 else
9739 lineinvalid(temp, (int)Columns);
9740 }
9741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742
9743 screen_stop_highlight();
9744 windgoto(cursor_row, 0);
9745
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009746#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 /* redraw the characters */
9748 if (type == USE_REDRAW)
9749 redraw_block(row, end, wp);
9750 else
9751#endif
9752 if (type == USE_T_CAL)
9753 {
9754 term_append_lines(line_count);
9755 screen_start(); /* don't know where cursor is now */
9756 }
9757 else
9758 {
9759 for (i = 0; i < line_count; i++)
9760 {
9761 if (type == USE_T_AL)
9762 {
9763 if (i && cursor_row != 0)
9764 windgoto(cursor_row, 0);
9765 out_str(T_AL);
9766 }
9767 else /* type == USE_T_SR */
9768 out_str(T_SR);
9769 screen_start(); /* don't know where cursor is now */
9770 }
9771 }
9772
9773 /*
9774 * With scroll-reverse and 'da' flag set we need to clear the lines that
9775 * have been scrolled down into the region.
9776 */
9777 if (type == USE_T_SR && *T_DA)
9778 {
9779 for (i = 0; i < line_count; ++i)
9780 {
9781 windgoto(off + i, 0);
9782 out_str(T_CE);
9783 screen_start(); /* don't know where cursor is now */
9784 }
9785 }
9786
9787#ifdef FEAT_GUI
9788 gui_can_update_cursor();
9789 if (gui.in_use)
9790 out_flush(); /* always flush after a scroll */
9791#endif
9792 return OK;
9793}
9794
9795/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009796 * Delete lines on the screen and update ScreenLines[].
9797 * "end" is the line after the scrolled part. Normally it is Rows.
9798 * When scrolling region used "off" is the offset from the top for the region.
9799 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 *
9801 * Return OK for success, FAIL if the lines are not deleted.
9802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009804screen_del_lines(
9805 int off,
9806 int row,
9807 int line_count,
9808 int end,
9809 int force, /* even when line_count > p_ttyscroll */
9810 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811{
9812 int j;
9813 int i;
9814 unsigned temp;
9815 int cursor_row;
9816 int cursor_end;
9817 int result_empty; /* result is empty until end of region */
9818 int can_delete; /* deleting line codes can be used */
9819 int type;
9820
9821 /*
9822 * FAIL if
9823 * - there is no valid screen
9824 * - the screen has to be redrawn completely
9825 * - the line count is less than one
9826 * - the line count is more than 'ttyscroll'
9827 */
9828 if (!screen_valid(TRUE) || line_count <= 0 ||
9829 (!force && line_count > p_ttyscroll))
9830 return FAIL;
9831
9832 /*
9833 * Check if the rest of the current region will become empty.
9834 */
9835 result_empty = row + line_count >= end;
9836
9837 /*
9838 * We can delete lines only when 'db' flag not set or when 'ce' option
9839 * available.
9840 */
9841 can_delete = (*T_DB == NUL || can_clear(T_CE));
9842
9843 /*
9844 * There are six ways to delete lines:
9845 * 0. When in a vertically split window and t_CV isn't set, redraw the
9846 * characters from ScreenLines[].
9847 * 1. Use T_CD if it exists and the result is empty.
9848 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9849 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9850 * none of the other ways work.
9851 * 4. Use T_CE (erase line) if the result is empty.
9852 * 5. Use T_DL (delete line) if it exists.
9853 * 6. redraw the characters from ScreenLines[].
9854 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009855#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9857 type = USE_REDRAW;
9858 else
9859#endif
9860 if (can_clear(T_CD) && result_empty)
9861 type = USE_T_CD;
9862#if defined(__BEOS__) && defined(BEOS_DR8)
9863 /*
9864 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9865 * its internal termcap... this works okay for tests which test *T_DB !=
9866 * NUL. It has the disadvantage that the user cannot use any :set t_*
9867 * command to get T_DB (back) to empty_option, only :set term=... will do
9868 * the trick...
9869 * Anyway, this hack will hopefully go away with the next OS release.
9870 * (Olaf Seibert)
9871 */
9872 else if (row == 0 && T_DB == empty_option
9873 && (line_count == 1 || *T_CDL == NUL))
9874#else
9875 else if (row == 0 && (
9876#ifndef AMIGA
9877 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9878 * up, so use delete-line command */
9879 line_count == 1 ||
9880#endif
9881 *T_CDL == NUL))
9882#endif
9883 type = USE_NL;
9884 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9885 type = USE_T_CDL;
9886 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009887#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 && (wp == NULL || wp->w_width == Columns)
9889#endif
9890 )
9891 type = USE_T_CE;
9892 else if (*T_DL != NUL && can_delete)
9893 type = USE_T_DL;
9894 else if (*T_CDL != NUL && can_delete)
9895 type = USE_T_CDL;
9896 else
9897 return FAIL;
9898
9899#ifdef FEAT_CLIPBOARD
9900 /* Remove a modeless selection when deleting lines halfway the screen or
9901 * not the full width of the screen. */
9902 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009903# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 || (wp != NULL && wp->w_width != Columns)
9905# endif
9906 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009907 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 else
9909 clip_scroll_selection(line_count);
9910#endif
9911
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912#ifdef FEAT_GUI
9913 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9914 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009915 gui_dont_update_cursor(gui.cursor_row >= row + off
9916 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917#endif
9918
9919 if (*T_CCS != NUL) /* cursor relative to region */
9920 {
9921 cursor_row = row;
9922 cursor_end = end;
9923 }
9924 else
9925 {
9926 cursor_row = row + off;
9927 cursor_end = end + off;
9928 }
9929
9930 /*
9931 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9932 * Clear the inserted lines in ScreenLines[].
9933 */
9934 row += off;
9935 end += off;
9936 for (i = 0; i < line_count; ++i)
9937 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009938#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 if (wp != NULL && wp->w_width != Columns)
9940 {
9941 /* need to copy part of a line */
9942 j = row + i;
9943 while ((j += line_count) <= end - 1)
9944 linecopy(j - line_count, j, wp);
9945 j -= line_count;
9946 if (can_clear((char_u *)" "))
9947 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9948 else
9949 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9950 LineWraps[j] = FALSE;
9951 }
9952 else
9953#endif
9954 {
9955 /* whole width, moving the line pointers is faster */
9956 j = row + i;
9957 temp = LineOffset[j];
9958 while ((j += line_count) <= end - 1)
9959 {
9960 LineOffset[j - line_count] = LineOffset[j];
9961 LineWraps[j - line_count] = LineWraps[j];
9962 }
9963 LineOffset[j - line_count] = temp;
9964 LineWraps[j - line_count] = FALSE;
9965 if (can_clear((char_u *)" "))
9966 lineclear(temp, (int)Columns);
9967 else
9968 lineinvalid(temp, (int)Columns);
9969 }
9970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971
9972 screen_stop_highlight();
9973
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009974#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 /* redraw the characters */
9976 if (type == USE_REDRAW)
9977 redraw_block(row, end, wp);
9978 else
9979#endif
9980 if (type == USE_T_CD) /* delete the lines */
9981 {
9982 windgoto(cursor_row, 0);
9983 out_str(T_CD);
9984 screen_start(); /* don't know where cursor is now */
9985 }
9986 else if (type == USE_T_CDL)
9987 {
9988 windgoto(cursor_row, 0);
9989 term_delete_lines(line_count);
9990 screen_start(); /* don't know where cursor is now */
9991 }
9992 /*
9993 * Deleting lines at top of the screen or scroll region: Just scroll
9994 * the whole screen (scroll region) up by outputting newlines on the
9995 * last line.
9996 */
9997 else if (type == USE_NL)
9998 {
9999 windgoto(cursor_end - 1, 0);
10000 for (i = line_count; --i >= 0; )
10001 out_char('\n'); /* cursor will remain on same line */
10002 }
10003 else
10004 {
10005 for (i = line_count; --i >= 0; )
10006 {
10007 if (type == USE_T_DL)
10008 {
10009 windgoto(cursor_row, 0);
10010 out_str(T_DL); /* delete a line */
10011 }
10012 else /* type == USE_T_CE */
10013 {
10014 windgoto(cursor_row + i, 0);
10015 out_str(T_CE); /* erase a line */
10016 }
10017 screen_start(); /* don't know where cursor is now */
10018 }
10019 }
10020
10021 /*
10022 * If the 'db' flag is set, we need to clear the lines that have been
10023 * scrolled up at the bottom of the region.
10024 */
10025 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10026 {
10027 for (i = line_count; i > 0; --i)
10028 {
10029 windgoto(cursor_end - i, 0);
10030 out_str(T_CE); /* erase a line */
10031 screen_start(); /* don't know where cursor is now */
10032 }
10033 }
10034
10035#ifdef FEAT_GUI
10036 gui_can_update_cursor();
10037 if (gui.in_use)
10038 out_flush(); /* always flush after a scroll */
10039#endif
10040
10041 return OK;
10042}
10043
10044/*
10045 * show the current mode and ruler
10046 *
10047 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10048 * If clear_cmdline is FALSE there may be a message there that needs to be
10049 * cleared only if a mode is shown.
10050 * Return the length of the message (0 if no message).
10051 */
10052 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010053showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054{
10055 int need_clear;
10056 int length = 0;
10057 int do_mode;
10058 int attr;
10059 int nwr_save;
10060#ifdef FEAT_INS_EXPAND
10061 int sub_attr;
10062#endif
10063
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010064 do_mode = ((p_smd && msg_silent == 0)
10065 && ((State & INSERT)
10066 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010067 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 if (do_mode || Recording)
10069 {
10070 /*
10071 * Don't show mode right now, when not redrawing or inside a mapping.
10072 * Call char_avail() only when we are going to show something, because
10073 * it takes a bit of time.
10074 */
10075 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10076 {
10077 redraw_cmdline = TRUE; /* show mode later */
10078 return 0;
10079 }
10080
10081 nwr_save = need_wait_return;
10082
10083 /* wait a bit before overwriting an important message */
10084 check_for_delay(FALSE);
10085
10086 /* if the cmdline is more than one line high, erase top lines */
10087 need_clear = clear_cmdline;
10088 if (clear_cmdline && cmdline_row < Rows - 1)
10089 msg_clr_cmdline(); /* will reset clear_cmdline */
10090
10091 /* Position on the last line in the window, column 0 */
10092 msg_pos_mode();
10093 cursor_off();
10094 attr = hl_attr(HLF_CM); /* Highlight mode */
10095 if (do_mode)
10096 {
10097 MSG_PUTS_ATTR("--", attr);
10098#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010099 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010100# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010101 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010102# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010103 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010104# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010105 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010106# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 MSG_PUTS_ATTR(" IM", attr);
10108# else
10109 MSG_PUTS_ATTR(" XIM", attr);
10110# endif
10111#endif
10112#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10113 if (gui.in_use)
10114 {
10115 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010116 {
10117 /* HANGUL */
10118 if (enc_utf8)
10119 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10120 else
10121 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 }
10124#endif
10125#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010126 /* CTRL-X in Insert mode */
10127 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 {
10129 /* These messages can get long, avoid a wrap in a narrow
10130 * window. Prefer showing edit_submode_extra. */
10131 length = (Rows - msg_row) * Columns - 3;
10132 if (edit_submode_extra != NULL)
10133 length -= vim_strsize(edit_submode_extra);
10134 if (length > 0)
10135 {
10136 if (edit_submode_pre != NULL)
10137 length -= vim_strsize(edit_submode_pre);
10138 if (length - vim_strsize(edit_submode) > 0)
10139 {
10140 if (edit_submode_pre != NULL)
10141 msg_puts_attr(edit_submode_pre, attr);
10142 msg_puts_attr(edit_submode, attr);
10143 }
10144 if (edit_submode_extra != NULL)
10145 {
10146 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10147 if ((int)edit_submode_highl < (int)HLF_COUNT)
10148 sub_attr = hl_attr(edit_submode_highl);
10149 else
10150 sub_attr = attr;
10151 msg_puts_attr(edit_submode_extra, sub_attr);
10152 }
10153 }
10154 length = 0;
10155 }
10156 else
10157#endif
10158 {
10159#ifdef FEAT_VREPLACE
10160 if (State & VREPLACE_FLAG)
10161 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10162 else
10163#endif
10164 if (State & REPLACE_FLAG)
10165 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10166 else if (State & INSERT)
10167 {
10168#ifdef FEAT_RIGHTLEFT
10169 if (p_ri)
10170 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10171#endif
10172 MSG_PUTS_ATTR(_(" INSERT"), attr);
10173 }
10174 else if (restart_edit == 'I')
10175 MSG_PUTS_ATTR(_(" (insert)"), attr);
10176 else if (restart_edit == 'R')
10177 MSG_PUTS_ATTR(_(" (replace)"), attr);
10178 else if (restart_edit == 'V')
10179 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10180#ifdef FEAT_RIGHTLEFT
10181 if (p_hkmap)
10182 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10183# ifdef FEAT_FKMAP
10184 if (p_fkmap)
10185 MSG_PUTS_ATTR(farsi_text_5, attr);
10186# endif
10187#endif
10188#ifdef FEAT_KEYMAP
10189 if (State & LANGMAP)
10190 {
10191# ifdef FEAT_ARABIC
10192 if (curwin->w_p_arab)
10193 MSG_PUTS_ATTR(_(" Arabic"), attr);
10194 else
10195# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010196 if (get_keymap_str(curwin, (char_u *)" (%s)",
10197 NameBuff, MAXPATHL))
10198 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 }
10200#endif
10201 if ((State & INSERT) && p_paste)
10202 MSG_PUTS_ATTR(_(" (paste)"), attr);
10203
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 if (VIsual_active)
10205 {
10206 char *p;
10207
10208 /* Don't concatenate separate words to avoid translation
10209 * problems. */
10210 switch ((VIsual_select ? 4 : 0)
10211 + (VIsual_mode == Ctrl_V) * 2
10212 + (VIsual_mode == 'V'))
10213 {
10214 case 0: p = N_(" VISUAL"); break;
10215 case 1: p = N_(" VISUAL LINE"); break;
10216 case 2: p = N_(" VISUAL BLOCK"); break;
10217 case 4: p = N_(" SELECT"); break;
10218 case 5: p = N_(" SELECT LINE"); break;
10219 default: p = N_(" SELECT BLOCK"); break;
10220 }
10221 MSG_PUTS_ATTR(_(p), attr);
10222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 MSG_PUTS_ATTR(" --", attr);
10224 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010225
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 need_clear = TRUE;
10227 }
10228 if (Recording
10229#ifdef FEAT_INS_EXPAND
10230 && edit_submode == NULL /* otherwise it gets too long */
10231#endif
10232 )
10233 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010234 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 need_clear = TRUE;
10236 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010237
10238 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 if (need_clear || clear_cmdline)
10240 msg_clr_eos();
10241 msg_didout = FALSE; /* overwrite this message */
10242 length = msg_col;
10243 msg_col = 0;
10244 need_wait_return = nwr_save; /* never ask for hit-return for this */
10245 }
10246 else if (clear_cmdline && msg_silent == 0)
10247 /* Clear the whole command line. Will reset "clear_cmdline". */
10248 msg_clr_cmdline();
10249
10250#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 /* In Visual mode the size of the selected area must be redrawn. */
10252 if (VIsual_active)
10253 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254
10255 /* If the last window has no status line, the ruler is after the mode
10256 * message and must be redrawn */
10257 if (redrawing()
10258# ifdef FEAT_WINDOWS
10259 && lastwin->w_status_height == 0
10260# endif
10261 )
10262 win_redr_ruler(lastwin, TRUE);
10263#endif
10264 redraw_cmdline = FALSE;
10265 clear_cmdline = FALSE;
10266
10267 return length;
10268}
10269
10270/*
10271 * Position for a mode message.
10272 */
10273 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010274msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275{
10276 msg_col = 0;
10277 msg_row = Rows - 1;
10278}
10279
10280/*
10281 * Delete mode message. Used when ESC is typed which is expected to end
10282 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010283 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 */
10285 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010286unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287{
10288 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010289 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 */
10291 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10292 redraw_cmdline = TRUE; /* delete mode later */
10293 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010294 clearmode();
10295}
10296
10297/*
10298 * Clear the mode message.
10299 */
10300 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010301clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010302{
10303 msg_pos_mode();
10304 if (Recording)
10305 recording_mode(hl_attr(HLF_CM));
10306 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307}
10308
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010309 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010310recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010311{
10312 MSG_PUTS_ATTR(_("recording"), attr);
10313 if (!shortmess(SHM_RECORDING))
10314 {
10315 char_u s[4];
10316 sprintf((char *)s, " @%c", Recording);
10317 MSG_PUTS_ATTR(s, attr);
10318 }
10319}
10320
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010321#if defined(FEAT_WINDOWS)
10322/*
10323 * Draw the tab pages line at the top of the Vim window.
10324 */
10325 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010326draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010327{
10328 int tabcount = 0;
10329 tabpage_T *tp;
10330 int tabwidth;
10331 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010332 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010333 int attr;
10334 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010335 win_T *cwp;
10336 int wincount;
10337 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010338 int c;
10339 int len;
10340 int attr_sel = hl_attr(HLF_TPS);
10341 int attr_nosel = hl_attr(HLF_TP);
10342 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010343 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010344 int room;
10345 int use_sep_chars = (t_colors < 8
10346#ifdef FEAT_GUI
10347 && !gui.in_use
10348#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010349#ifdef FEAT_TERMGUICOLORS
10350 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010351#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010352 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010353
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010354 if (ScreenLines == NULL)
10355 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010356 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010357
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010358#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010359 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010360 if (gui_use_tabline())
10361 {
10362 gui_update_tabline();
10363 return;
10364 }
10365#endif
10366
10367 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010368 return;
10369
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010370#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010371
10372 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10373 for (scol = 0; scol < Columns; ++scol)
10374 TabPageIdxs[scol] = 0;
10375
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010376 /* Use the 'tabline' option if it's set. */
10377 if (*p_tal != NUL)
10378 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010379 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010380
10381 /* Check for an error. If there is one we would loop in redrawing the
10382 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010383 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010384 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010385 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010386 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010387 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010388 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010389 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010390 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010391#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010392 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010393 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010394 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010395
Bram Moolenaar238a5642006-02-21 22:12:05 +000010396 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10397 if (tabwidth < 6)
10398 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010399
Bram Moolenaar238a5642006-02-21 22:12:05 +000010400 attr = attr_nosel;
10401 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010402 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010403 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10404 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010405 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010406 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010407
Bram Moolenaar238a5642006-02-21 22:12:05 +000010408 if (tp->tp_topframe == topframe)
10409 attr = attr_sel;
10410 if (use_sep_chars && col > 0)
10411 screen_putchar('|', 0, col++, attr);
10412
10413 if (tp->tp_topframe != topframe)
10414 attr = attr_nosel;
10415
10416 screen_putchar(' ', 0, col++, attr);
10417
10418 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010419 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010420 cwp = curwin;
10421 wp = firstwin;
10422 }
10423 else
10424 {
10425 cwp = tp->tp_curwin;
10426 wp = tp->tp_firstwin;
10427 }
10428
10429 modified = FALSE;
10430 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10431 if (bufIsChanged(wp->w_buffer))
10432 modified = TRUE;
10433 if (modified || wincount > 1)
10434 {
10435 if (wincount > 1)
10436 {
10437 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010438 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010439 if (col + len >= Columns - 3)
10440 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010441 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010442#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010443 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010444#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010445 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010446#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010447 );
10448 col += len;
10449 }
10450 if (modified)
10451 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10452 screen_putchar(' ', 0, col++, attr);
10453 }
10454
10455 room = scol - col + tabwidth - 1;
10456 if (room > 0)
10457 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010458 /* Get buffer name in NameBuff[] */
10459 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010460 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010461 len = vim_strsize(NameBuff);
10462 p = NameBuff;
10463#ifdef FEAT_MBYTE
10464 if (has_mbyte)
10465 while (len > room)
10466 {
10467 len -= ptr2cells(p);
10468 mb_ptr_adv(p);
10469 }
10470 else
10471#endif
10472 if (len > room)
10473 {
10474 p += len - room;
10475 len = room;
10476 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010477 if (len > Columns - col - 1)
10478 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010479
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010480 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010481 col += len;
10482 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010483 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010484
10485 /* Store the tab page number in TabPageIdxs[], so that
10486 * jump_to_mouse() knows where each one is. */
10487 ++tabcount;
10488 while (scol < col)
10489 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010490 }
10491
Bram Moolenaar238a5642006-02-21 22:12:05 +000010492 if (use_sep_chars)
10493 c = '_';
10494 else
10495 c = ' ';
10496 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010497
10498 /* Put an "X" for closing the current tab if there are several. */
10499 if (first_tabpage->tp_next != NULL)
10500 {
10501 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10502 TabPageIdxs[Columns - 1] = -999;
10503 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010504 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010505
10506 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10507 * set. */
10508 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010509}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010510
10511/*
10512 * Get buffer name for "buf" into NameBuff[].
10513 * Takes care of special buffer names and translates special characters.
10514 */
10515 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010516get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010517{
10518 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010519 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010520 else
10521 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10522 trans_characters(NameBuff, MAXPATHL);
10523}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010524#endif
10525
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10527/*
10528 * Get the character to use in a status line. Get its attributes in "*attr".
10529 */
10530 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010531fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532{
10533 int fill;
10534 if (is_curwin)
10535 {
10536 *attr = hl_attr(HLF_S);
10537 fill = fill_stl;
10538 }
10539 else
10540 {
10541 *attr = hl_attr(HLF_SNC);
10542 fill = fill_stlnc;
10543 }
10544 /* Use fill when there is highlighting, and highlighting of current
10545 * window differs, or the fillchars differ, or this is not the
10546 * current window */
10547 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010548 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 || (fill_stl != fill_stlnc)))
10550 return fill;
10551 if (is_curwin)
10552 return '^';
10553 return '=';
10554}
10555#endif
10556
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010557#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558/*
10559 * Get the character to use in a separator between vertically split windows.
10560 * Get its attributes in "*attr".
10561 */
10562 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010563fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564{
10565 *attr = hl_attr(HLF_C);
10566 if (*attr == 0 && fill_vert == ' ')
10567 return '|';
10568 else
10569 return fill_vert;
10570}
10571#endif
10572
10573/*
10574 * Return TRUE if redrawing should currently be done.
10575 */
10576 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010577redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578{
10579 return (!RedrawingDisabled
10580 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10581}
10582
10583/*
10584 * Return TRUE if printing messages should currently be done.
10585 */
10586 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010587messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588{
10589 return (!(p_lz && char_avail() && !KeyTyped));
10590}
10591
10592/*
10593 * Show current status info in ruler and various other places
10594 * If always is FALSE, only show ruler if position has changed.
10595 */
10596 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010597showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598{
10599 if (!always && !redrawing())
10600 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010601#ifdef FEAT_INS_EXPAND
10602 if (pum_visible())
10603 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010604# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010605 /* Don't redraw right now, do it later. */
10606 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010607# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010608 return;
10609 }
10610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010612 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010614 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 else
10617#endif
10618#ifdef FEAT_CMDL_INFO
10619 win_redr_ruler(curwin, always);
10620#endif
10621
10622#ifdef FEAT_TITLE
10623 if (need_maketitle
10624# ifdef FEAT_STL_OPT
10625 || (p_icon && (stl_syntax & STL_IN_ICON))
10626 || (p_title && (stl_syntax & STL_IN_TITLE))
10627# endif
10628 )
10629 maketitle();
10630#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010631#ifdef FEAT_WINDOWS
10632 /* Redraw the tab pages line if needed. */
10633 if (redraw_tabline)
10634 draw_tabline();
10635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636}
10637
10638#ifdef FEAT_CMDL_INFO
10639 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010640win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010642#define RULER_BUF_LEN 70
10643 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 int row;
10645 int fillchar;
10646 int attr;
10647 int empty_line = FALSE;
10648 colnr_T virtcol;
10649 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010650 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010652#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 int this_ru_col;
10654 int off = 0;
10655 int width = Columns;
10656# define WITH_OFF(x) x
10657# define WITH_WIDTH(x) x
10658#else
10659# define WITH_OFF(x) 0
10660# define WITH_WIDTH(x) Columns
10661# define this_ru_col ru_col
10662#endif
10663
10664 /* If 'ruler' off or redrawing disabled, don't do anything */
10665 if (!p_ru)
10666 return;
10667
10668 /*
10669 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10670 * after deleting lines, before cursor.lnum is corrected.
10671 */
10672 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10673 return;
10674
10675#ifdef FEAT_INS_EXPAND
10676 /* Don't draw the ruler while doing insert-completion, it might overwrite
10677 * the (long) mode message. */
10678# ifdef FEAT_WINDOWS
10679 if (wp == lastwin && lastwin->w_status_height == 0)
10680# endif
10681 if (edit_submode != NULL)
10682 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010683 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10684 if (pum_visible())
10685 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686#endif
10687
10688#ifdef FEAT_STL_OPT
10689 if (*p_ruf)
10690 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010691 int save_called_emsg = called_emsg;
10692
10693 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010695 if (called_emsg)
10696 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010697 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010698 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 return;
10700 }
10701#endif
10702
10703 /*
10704 * Check if not in Insert mode and the line is empty (will show "0-1").
10705 */
10706 if (!(State & INSERT)
10707 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10708 empty_line = TRUE;
10709
10710 /*
10711 * Only draw the ruler when something changed.
10712 */
10713 validate_virtcol_win(wp);
10714 if ( redraw_cmdline
10715 || always
10716 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10717 || wp->w_cursor.col != wp->w_ru_cursor.col
10718 || wp->w_virtcol != wp->w_ru_virtcol
10719#ifdef FEAT_VIRTUALEDIT
10720 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10721#endif
10722 || wp->w_topline != wp->w_ru_topline
10723 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10724#ifdef FEAT_DIFF
10725 || wp->w_topfill != wp->w_ru_topfill
10726#endif
10727 || empty_line != wp->w_ru_empty)
10728 {
10729 cursor_off();
10730#ifdef FEAT_WINDOWS
10731 if (wp->w_status_height)
10732 {
10733 row = W_WINROW(wp) + wp->w_height;
10734 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 off = W_WINCOL(wp);
10736 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 }
10738 else
10739#endif
10740 {
10741 row = Rows - 1;
10742 fillchar = ' ';
10743 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010744#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 width = Columns;
10746 off = 0;
10747#endif
10748 }
10749
10750 /* In list mode virtcol needs to be recomputed */
10751 virtcol = wp->w_virtcol;
10752 if (wp->w_p_list && lcs_tab1 == NUL)
10753 {
10754 wp->w_p_list = FALSE;
10755 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10756 wp->w_p_list = TRUE;
10757 }
10758
10759 /*
10760 * Some sprintfs return the length, some return a pointer.
10761 * To avoid portability problems we use strlen() here.
10762 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010763 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10765 ? 0L
10766 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010767 len = STRLEN(buffer);
10768 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10770 (int)virtcol + 1);
10771
10772 /*
10773 * Add a "50%" if there is room for it.
10774 * On the last line, don't print in the last column (scrolls the
10775 * screen up on some terminals).
10776 */
10777 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010778 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 o = i + vim_strsize(buffer + i + 1);
10780#ifdef FEAT_WINDOWS
10781 if (wp->w_status_height == 0) /* can't use last char of screen */
10782#endif
10783 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010784#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 this_ru_col = ru_col - (Columns - width);
10786 if (this_ru_col < 0)
10787 this_ru_col = 0;
10788#endif
10789 /* Never use more than half the window/screen width, leave the other
10790 * half for the filename. */
10791 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10792 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10793 if (this_ru_col + o < WITH_WIDTH(width))
10794 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010795 /* need at least 3 chars left for get_rel_pos() + NUL */
10796 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 {
10798#ifdef FEAT_MBYTE
10799 if (has_mbyte)
10800 i += (*mb_char2bytes)(fillchar, buffer + i);
10801 else
10802#endif
10803 buffer[i++] = fillchar;
10804 ++o;
10805 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010806 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 }
10808 /* Truncate at window boundary. */
10809#ifdef FEAT_MBYTE
10810 if (has_mbyte)
10811 {
10812 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010813 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 {
10815 o += (*mb_ptr2cells)(buffer + i);
10816 if (this_ru_col + o > WITH_WIDTH(width))
10817 {
10818 buffer[i] = NUL;
10819 break;
10820 }
10821 }
10822 }
10823 else
10824#endif
10825 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10826 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10827
10828 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10829 i = redraw_cmdline;
10830 screen_fill(row, row + 1,
10831 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10832 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10833 fillchar, fillchar, attr);
10834 /* don't redraw the cmdline because of showing the ruler */
10835 redraw_cmdline = i;
10836 wp->w_ru_cursor = wp->w_cursor;
10837 wp->w_ru_virtcol = wp->w_virtcol;
10838 wp->w_ru_empty = empty_line;
10839 wp->w_ru_topline = wp->w_topline;
10840 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10841#ifdef FEAT_DIFF
10842 wp->w_ru_topfill = wp->w_topfill;
10843#endif
10844 }
10845}
10846#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010847
10848#if defined(FEAT_LINEBREAK) || defined(PROTO)
10849/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010850 * Return the width of the 'number' and 'relativenumber' column.
10851 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010852 * Otherwise it depends on 'numberwidth' and the line count.
10853 */
10854 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010855number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010856{
10857 int n;
10858 linenr_T lnum;
10859
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010860 if (wp->w_p_rnu && !wp->w_p_nu)
10861 /* cursor line shows "0" */
10862 lnum = wp->w_height;
10863 else
10864 /* cursor line shows absolute line number */
10865 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010866
Bram Moolenaar6b314672015-03-20 15:42:10 +010010867 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010868 return wp->w_nrwidth_width;
10869 wp->w_nrwidth_line_count = lnum;
10870
10871 n = 0;
10872 do
10873 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010874 lnum /= 10;
10875 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010876 } while (lnum > 0);
10877
10878 /* 'numberwidth' gives the minimal width plus one */
10879 if (n < wp->w_p_nuw - 1)
10880 n = wp->w_p_nuw - 1;
10881
10882 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010883 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010884 return n;
10885}
10886#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010887
10888/*
10889 * Return the current cursor column. This is the actual position on the
10890 * screen. First column is 0.
10891 */
10892 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010893screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010894{
10895 return screen_cur_col;
10896}
10897
10898/*
10899 * Return the current cursor row. This is the actual position on the screen.
10900 * First row is 0.
10901 */
10902 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010903screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010904{
10905 return screen_cur_row;
10906}