blob: e37bf483825e45b593f96c3b54e9db299ffb332a [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 Moolenaar860cae12010-06-05 23:22:07 +0200780#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200781/*
782 * Return TRUE if the cursor line in window "wp" may be concealed, according
783 * to the 'concealcursor' option.
784 */
785 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100786conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200787{
788 int c;
789
790 if (*wp->w_p_cocu == NUL)
791 return FALSE;
792 if (get_real_state() & VISUAL)
793 c = 'v';
794 else if (State & INSERT)
795 c = 'i';
796 else if (State & NORMAL)
797 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200798 else if (State & CMDLINE)
799 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200800 else
801 return FALSE;
802 return vim_strchr(wp->w_p_cocu, c) != NULL;
803}
804
805/*
806 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
807 */
808 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100809conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200810{
811 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
812 {
813 need_cursor_line_redraw = TRUE;
814 /* Need to recompute cursor column, e.g., when starting Visual mode
815 * without concealing. */
816 curs_columns(TRUE);
817 }
818}
819
Bram Moolenaar860cae12010-06-05 23:22:07 +0200820 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100821update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200822{
823 int row;
824 int j;
825
Bram Moolenaar908be432016-05-24 10:51:30 +0200826 /* Don't do anything if the screen structures are (not yet) valid. */
827 if (!screen_valid(TRUE))
828 return;
829
Bram Moolenaar860cae12010-06-05 23:22:07 +0200830 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200831 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200832 {
833# ifdef FEAT_GUI
834 /* Remove the cursor before starting to do anything, because scrolling
835 * may make it difficult to redraw the text under it. */
836 if (gui.in_use)
837 gui_undraw_cursor();
838# endif
839 row = 0;
840 for (j = 0; j < wp->w_lines_valid; ++j)
841 {
842 if (lnum == wp->w_lines[j].wl_lnum)
843 {
844 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200845# ifdef FEAT_SEARCH_EXTRA
846 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200847 start_search_hl();
848 prepare_search_hl(wp, lnum);
849# endif
850 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
851# if defined(FEAT_SEARCH_EXTRA)
852 end_search_hl();
853# endif
854 break;
855 }
856 row += wp->w_lines[j].wl_size;
857 }
858# ifdef FEAT_GUI
859 /* Redraw the cursor */
860 if (gui.in_use)
861 {
862 out_flush(); /* required before updating the cursor */
863 gui_update_cursor(FALSE, FALSE);
864 }
865# endif
866 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200867 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200868}
869#endif
870
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100872static void update_prepare(void);
873static void update_finish(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875/*
876 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000877 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 */
879 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100880update_prepare(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881{
882 cursor_off();
883 updating_screen = TRUE;
884#ifdef FEAT_GUI
885 /* Remove the cursor before starting to do anything, because scrolling may
886 * make it difficult to redraw the text under it. */
887 if (gui.in_use)
888 gui_undraw_cursor();
889#endif
890#ifdef FEAT_SEARCH_EXTRA
891 start_search_hl();
892#endif
893}
894
895/*
896 * Finish updating one or more windows.
897 */
898 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100899update_finish(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900{
901 if (redraw_cmdline)
902 showmode();
903
904# ifdef FEAT_SEARCH_EXTRA
905 end_search_hl();
906# endif
907
908 updating_screen = FALSE;
909
910# ifdef FEAT_GUI
911 gui_may_resize_shell();
912
913 /* Redraw the cursor and update the scrollbars when all screen updating is
914 * done. */
915 if (gui.in_use)
916 {
917 out_flush(); /* required before updating the cursor */
918 gui_update_cursor(FALSE, FALSE);
919 gui_update_scrollbars(FALSE);
920 }
921# endif
922}
923#endif
924
925#if defined(FEAT_SIGNS) || defined(PROTO)
926 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100927update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 win_T *wp;
930 int doit = FALSE;
931
932# ifdef FEAT_FOLDING
933 win_foldinfo.fi_level = 0;
934# endif
935
936 /* update/delete a specific mark */
937 FOR_ALL_WINDOWS(wp)
938 {
939 if (buf != NULL && lnum > 0)
940 {
941 if (wp->w_buffer == buf && lnum >= wp->w_topline
942 && lnum < wp->w_botline)
943 {
944 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
945 wp->w_redraw_top = lnum;
946 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
947 wp->w_redraw_bot = lnum;
948 redraw_win_later(wp, VALID);
949 }
950 }
951 else
952 redraw_win_later(wp, VALID);
953 if (wp->w_redr_type != 0)
954 doit = TRUE;
955 }
956
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100957 /* Return when there is nothing to do, screen updating is already
958 * happening (recursive call) or still starting up. */
959 if (!doit || updating_screen
960#ifdef FEAT_GUI
961 || gui.starting
962#endif
963 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 return;
965
966 /* update all windows that need updating */
967 update_prepare();
968
969# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +0200970 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
972 if (wp->w_redr_type != 0)
973 win_update(wp);
974 if (wp->w_redr_status)
975 win_redr_status(wp);
976 }
977# else
978 if (curwin->w_redr_type != 0)
979 win_update(curwin);
980# endif
981
982 update_finish();
983}
984#endif
985
986
987#if defined(FEAT_GUI) || defined(PROTO)
988/*
989 * Update a single window, its status line and maybe the command line msg.
990 * Used for the GUI scrollbar.
991 */
992 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100993updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000995 /* return if already busy updating */
996 if (updating_screen)
997 return;
998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 update_prepare();
1000
1001#ifdef FEAT_CLIPBOARD
1002 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001003 if (clip_star.available && clip_isautosel_star())
1004 clip_update_selection(&clip_star);
1005 if (clip_plus.available && clip_isautosel_plus())
1006 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001010
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001012 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001013 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001014 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (wp->w_redr_status
1017# ifdef FEAT_CMDL_INFO
1018 || p_ru
1019# endif
1020# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001021 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022# endif
1023 )
1024 win_redr_status(wp);
1025#endif
1026
1027 update_finish();
1028}
1029#endif
1030
1031/*
1032 * Update a single window.
1033 *
1034 * This may cause the windows below it also to be redrawn (when clearing the
1035 * screen or scrolling lines).
1036 *
1037 * How the window is redrawn depends on wp->w_redr_type. Each type also
1038 * implies the one below it.
1039 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001040 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1042 * INVERTED redraw the changed part of the Visual area
1043 * INVERTED_ALL redraw the whole Visual area
1044 * VALID 1. scroll up/down to adjust for a changed w_topline
1045 * 2. update lines at the top when scrolled down
1046 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001047 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 * b_mod_top and b_mod_bot.
1049 * - if wp->w_redraw_top non-zero, redraw lines between
1050 * wp->w_redraw_top and wp->w_redr_bot.
1051 * - continue redrawing when syntax status is invalid.
1052 * 4. if scrolled up, update lines at the bottom.
1053 * This results in three areas that may need updating:
1054 * top: from first row to top_end (when scrolled down)
1055 * mid: from mid_start to mid_end (update inversion or changed text)
1056 * bot: from bot_start to last row (when scrolled up)
1057 */
1058 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001059win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
1061 buf_T *buf = wp->w_buffer;
1062 int type;
1063 int top_end = 0; /* Below last row of the top area that needs
1064 updating. 0 when no top area updating. */
1065 int mid_start = 999;/* first row of the mid area that needs
1066 updating. 999 when no mid area updating. */
1067 int mid_end = 0; /* Below last row of the mid area that needs
1068 updating. 0 when no mid area updating. */
1069 int bot_start = 999;/* first row of the bot area that needs
1070 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 int scrolled_down = FALSE; /* TRUE when scrolled down when
1072 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001074 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 int top_to_mod = FALSE; /* redraw above mod_top */
1076#endif
1077
1078 int row; /* current window row to display */
1079 linenr_T lnum; /* current buffer lnum to display */
1080 int idx; /* current index in w_lines[] */
1081 int srow; /* starting row of the current line */
1082
1083 int eof = FALSE; /* if TRUE, we hit the end of the file */
1084 int didline = FALSE; /* if TRUE, we finished the last line */
1085 int i;
1086 long j;
1087 static int recursive = FALSE; /* being called recursively */
1088 int old_botline = wp->w_botline;
1089#ifdef FEAT_FOLDING
1090 long fold_count;
1091#endif
1092#ifdef FEAT_SYN_HL
1093 /* remember what happened to the previous line, to know if
1094 * check_visual_highlight() can be used */
1095#define DID_NONE 1 /* didn't update a line */
1096#define DID_LINE 2 /* updated a normal line */
1097#define DID_FOLD 3 /* updated a folded line */
1098 int did_update = DID_NONE;
1099 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1100#endif
1101 linenr_T mod_top = 0;
1102 linenr_T mod_bot = 0;
1103#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1104 int save_got_int;
1105#endif
1106
1107 type = wp->w_redr_type;
1108
1109 if (type == NOT_VALID)
1110 {
1111#ifdef FEAT_WINDOWS
1112 wp->w_redr_status = TRUE;
1113#endif
1114 wp->w_lines_valid = 0;
1115 }
1116
1117 /* Window is zero-height: nothing to draw. */
1118 if (wp->w_height == 0)
1119 {
1120 wp->w_redr_type = 0;
1121 return;
1122 }
1123
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001124#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 /* Window is zero-width: Only need to draw the separator. */
1126 if (wp->w_width == 0)
1127 {
1128 /* draw the vertical separator right of this window */
1129 draw_vsep_win(wp, 0);
1130 wp->w_redr_type = 0;
1131 return;
1132 }
1133#endif
1134
1135#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001136 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#endif
1138
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001139#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001140 /* Force redraw when width of 'number' or 'relativenumber' column
1141 * changes. */
1142 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001143 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001144 {
1145 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001146 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001147 }
1148 else
1149#endif
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1152 {
1153 /*
1154 * When there are both inserted/deleted lines and specific lines to be
1155 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1156 * everything (only happens when redrawing is off for while).
1157 */
1158 type = NOT_VALID;
1159 }
1160 else
1161 {
1162 /*
1163 * Set mod_top to the first line that needs displaying because of
1164 * changes. Set mod_bot to the first line after the changes.
1165 */
1166 mod_top = wp->w_redraw_top;
1167 if (wp->w_redraw_bot != 0)
1168 mod_bot = wp->w_redraw_bot + 1;
1169 else
1170 mod_bot = 0;
1171 wp->w_redraw_top = 0; /* reset for next time */
1172 wp->w_redraw_bot = 0;
1173 if (buf->b_mod_set)
1174 {
1175 if (mod_top == 0 || mod_top > buf->b_mod_top)
1176 {
1177 mod_top = buf->b_mod_top;
1178#ifdef FEAT_SYN_HL
1179 /* Need to redraw lines above the change that may be included
1180 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001181 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001183 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 if (mod_top < 1)
1185 mod_top = 1;
1186 }
1187#endif
1188 }
1189 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1190 mod_bot = buf->b_mod_bot;
1191
1192#ifdef FEAT_SEARCH_EXTRA
1193 /* When 'hlsearch' is on and using a multi-line search pattern, a
1194 * change in one line may make the Search highlighting in a
1195 * previous line invalid. Simple solution: redraw all visible
1196 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001197 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001199 if (search_hl.rm.regprog != NULL
1200 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001202 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001203 {
1204 cur = wp->w_match_head;
1205 while (cur != NULL)
1206 {
1207 if (cur->match.regprog != NULL
1208 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001209 {
1210 top_to_mod = TRUE;
1211 break;
1212 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001213 cur = cur->next;
1214 }
1215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216#endif
1217 }
1218#ifdef FEAT_FOLDING
1219 if (mod_top != 0 && hasAnyFolding(wp))
1220 {
1221 linenr_T lnumt, lnumb;
1222
1223 /*
1224 * A change in a line can cause lines above it to become folded or
1225 * unfolded. Find the top most buffer line that may be affected.
1226 * If the line was previously folded and displayed, get the first
1227 * line of that fold. If the line is folded now, get the first
1228 * folded line. Use the minimum of these two.
1229 */
1230
1231 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1232 * the line below it. If there is no valid entry, use w_topline.
1233 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1234 * to this line. If there is no valid entry, use MAXLNUM. */
1235 lnumt = wp->w_topline;
1236 lnumb = MAXLNUM;
1237 for (i = 0; i < wp->w_lines_valid; ++i)
1238 if (wp->w_lines[i].wl_valid)
1239 {
1240 if (wp->w_lines[i].wl_lastlnum < mod_top)
1241 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1242 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1243 {
1244 lnumb = wp->w_lines[i].wl_lnum;
1245 /* When there is a fold column it might need updating
1246 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001247 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 ++lnumb;
1249 }
1250 }
1251
1252 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1253 if (mod_top > lnumt)
1254 mod_top = lnumt;
1255
1256 /* Now do the same for the bottom line (one above mod_bot). */
1257 --mod_bot;
1258 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1259 ++mod_bot;
1260 if (mod_bot < lnumb)
1261 mod_bot = lnumb;
1262 }
1263#endif
1264
1265 /* When a change starts above w_topline and the end is below
1266 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001267 * If the end of the change is above w_topline: do like no change was
1268 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (mod_top != 0 && mod_top < wp->w_topline)
1270 {
1271 if (mod_bot > wp->w_topline)
1272 mod_top = wp->w_topline;
1273#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001274 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 top_end = 1;
1276#endif
1277 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001278
1279 /* When line numbers are displayed need to redraw all lines below
1280 * inserted/deleted lines. */
1281 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1282 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284
1285 /*
1286 * When only displaying the lines at the top, set top_end. Used when
1287 * window has scrolled down for msg_scrolled.
1288 */
1289 if (type == REDRAW_TOP)
1290 {
1291 j = 0;
1292 for (i = 0; i < wp->w_lines_valid; ++i)
1293 {
1294 j += wp->w_lines[i].wl_size;
1295 if (j >= wp->w_upd_rows)
1296 {
1297 top_end = j;
1298 break;
1299 }
1300 }
1301 if (top_end == 0)
1302 /* not found (cannot happen?): redraw everything */
1303 type = NOT_VALID;
1304 else
1305 /* top area defined, the rest is VALID */
1306 type = VALID;
1307 }
1308
Bram Moolenaar367329b2007-08-30 11:53:22 +00001309 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001310 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1311 * non-zero and thus not FALSE) will indicate that screenclear() was not
1312 * called. */
1313 if (screen_cleared)
1314 screen_cleared = MAYBE;
1315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 /*
1317 * If there are no changes on the screen that require a complete redraw,
1318 * handle three cases:
1319 * 1: we are off the top of the screen by a few lines: scroll down
1320 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1321 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1322 * w_lines[] that needs updating.
1323 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001324 if ((type == VALID || type == SOME_VALID
1325 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326#ifdef FEAT_DIFF
1327 && !wp->w_botfill && !wp->w_old_botfill
1328#endif
1329 )
1330 {
1331 if (mod_top != 0 && wp->w_topline == mod_top)
1332 {
1333 /*
1334 * w_topline is the first changed line, the scrolling will be done
1335 * further down.
1336 */
1337 }
1338 else if (wp->w_lines[0].wl_valid
1339 && (wp->w_topline < wp->w_lines[0].wl_lnum
1340#ifdef FEAT_DIFF
1341 || (wp->w_topline == wp->w_lines[0].wl_lnum
1342 && wp->w_topfill > wp->w_old_topfill)
1343#endif
1344 ))
1345 {
1346 /*
1347 * New topline is above old topline: May scroll down.
1348 */
1349#ifdef FEAT_FOLDING
1350 if (hasAnyFolding(wp))
1351 {
1352 linenr_T ln;
1353
1354 /* count the number of lines we are off, counting a sequence
1355 * of folded lines as one */
1356 j = 0;
1357 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1358 {
1359 ++j;
1360 if (j >= wp->w_height - 2)
1361 break;
1362 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1363 }
1364 }
1365 else
1366#endif
1367 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1368 if (j < wp->w_height - 2) /* not too far off */
1369 {
1370 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1371#ifdef FEAT_DIFF
1372 /* insert extra lines for previously invisible filler lines */
1373 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1374 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1375 - wp->w_old_topfill;
1376#endif
1377 if (i < wp->w_height - 2) /* less than a screen off */
1378 {
1379 /*
1380 * Try to insert the correct number of lines.
1381 * If not the last window, delete the lines at the bottom.
1382 * win_ins_lines may fail when the terminal can't do it.
1383 */
1384 if (i > 0)
1385 check_for_delay(FALSE);
1386 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1387 {
1388 if (wp->w_lines_valid != 0)
1389 {
1390 /* Need to update rows that are new, stop at the
1391 * first one that scrolled down. */
1392 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
1395 /* Move the entries that were scrolled, disable
1396 * the entries for the lines to be redrawn. */
1397 if ((wp->w_lines_valid += j) > wp->w_height)
1398 wp->w_lines_valid = wp->w_height;
1399 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1400 wp->w_lines[idx] = wp->w_lines[idx - j];
1401 while (idx >= 0)
1402 wp->w_lines[idx--].wl_valid = FALSE;
1403 }
1404 }
1405 else
1406 mid_start = 0; /* redraw all lines */
1407 }
1408 else
1409 mid_start = 0; /* redraw all lines */
1410 }
1411 else
1412 mid_start = 0; /* redraw all lines */
1413 }
1414 else
1415 {
1416 /*
1417 * New topline is at or below old topline: May scroll up.
1418 * When topline didn't change, find first entry in w_lines[] that
1419 * needs updating.
1420 */
1421
1422 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1423 j = -1;
1424 row = 0;
1425 for (i = 0; i < wp->w_lines_valid; i++)
1426 {
1427 if (wp->w_lines[i].wl_valid
1428 && wp->w_lines[i].wl_lnum == wp->w_topline)
1429 {
1430 j = i;
1431 break;
1432 }
1433 row += wp->w_lines[i].wl_size;
1434 }
1435 if (j == -1)
1436 {
1437 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1438 * lines */
1439 mid_start = 0;
1440 }
1441 else
1442 {
1443 /*
1444 * Try to delete the correct number of lines.
1445 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1446 */
1447#ifdef FEAT_DIFF
1448 /* If the topline didn't change, delete old filler lines,
1449 * otherwise delete filler lines of the new topline... */
1450 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1451 row += wp->w_old_topfill;
1452 else
1453 row += diff_check_fill(wp, wp->w_topline);
1454 /* ... but don't delete new filler lines. */
1455 row -= wp->w_topfill;
1456#endif
1457 if (row > 0)
1458 {
1459 check_for_delay(FALSE);
1460 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1461 bot_start = wp->w_height - row;
1462 else
1463 mid_start = 0; /* redraw all lines */
1464 }
1465 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1466 {
1467 /*
1468 * Skip the lines (below the deleted lines) that are still
1469 * valid and don't need redrawing. Copy their info
1470 * upwards, to compensate for the deleted lines. Set
1471 * bot_start to the first row that needs redrawing.
1472 */
1473 bot_start = 0;
1474 idx = 0;
1475 for (;;)
1476 {
1477 wp->w_lines[idx] = wp->w_lines[j];
1478 /* stop at line that didn't fit, unless it is still
1479 * valid (no lines deleted) */
1480 if (row > 0 && bot_start + row
1481 + (int)wp->w_lines[j].wl_size > wp->w_height)
1482 {
1483 wp->w_lines_valid = idx + 1;
1484 break;
1485 }
1486 bot_start += wp->w_lines[idx++].wl_size;
1487
1488 /* stop at the last valid entry in w_lines[].wl_size */
1489 if (++j >= wp->w_lines_valid)
1490 {
1491 wp->w_lines_valid = idx;
1492 break;
1493 }
1494 }
1495#ifdef FEAT_DIFF
1496 /* Correct the first entry for filler lines at the top
1497 * when it won't get updated below. */
1498 if (wp->w_p_diff && bot_start > 0)
1499 wp->w_lines[0].wl_size =
1500 plines_win_nofill(wp, wp->w_topline, TRUE)
1501 + wp->w_topfill;
1502#endif
1503 }
1504 }
1505 }
1506
1507 /* When starting redraw in the first line, redraw all lines. When
1508 * there is only one window it's probably faster to clear the screen
1509 * first. */
1510 if (mid_start == 0)
1511 {
1512 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001513 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001514 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001515 /* Clear the screen when it was not done by win_del_lines() or
1516 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1517 * then. */
1518 if (screen_cleared != TRUE)
1519 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001520#ifdef FEAT_WINDOWS
1521 /* The screen was cleared, redraw the tab pages line. */
1522 if (redraw_tabline)
1523 draw_tabline();
1524#endif
1525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001527
1528 /* When win_del_lines() or win_ins_lines() caused the screen to be
1529 * cleared (only happens for the first window) or when screenclear()
1530 * was called directly above, "must_redraw" will have been set to
1531 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1532 if (screen_cleared == TRUE)
1533 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
1535 else
1536 {
1537 /* Not VALID or INVERTED: redraw all lines. */
1538 mid_start = 0;
1539 mid_end = wp->w_height;
1540 }
1541
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001542 if (type == SOME_VALID)
1543 {
1544 /* SOME_VALID: redraw all lines. */
1545 mid_start = 0;
1546 mid_end = wp->w_height;
1547 type = NOT_VALID;
1548 }
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 /* check if we are updating or removing the inverted part */
1551 if ((VIsual_active && buf == curwin->w_buffer)
1552 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1553 {
1554 linenr_T from, to;
1555
1556 if (VIsual_active)
1557 {
1558 if (VIsual_active
1559 && (VIsual_mode != wp->w_old_visual_mode
1560 || type == INVERTED_ALL))
1561 {
1562 /*
1563 * If the type of Visual selection changed, redraw the whole
1564 * selection. Also when the ownership of the X selection is
1565 * gained or lost.
1566 */
1567 if (curwin->w_cursor.lnum < VIsual.lnum)
1568 {
1569 from = curwin->w_cursor.lnum;
1570 to = VIsual.lnum;
1571 }
1572 else
1573 {
1574 from = VIsual.lnum;
1575 to = curwin->w_cursor.lnum;
1576 }
1577 /* redraw more when the cursor moved as well */
1578 if (wp->w_old_cursor_lnum < from)
1579 from = wp->w_old_cursor_lnum;
1580 if (wp->w_old_cursor_lnum > to)
1581 to = wp->w_old_cursor_lnum;
1582 if (wp->w_old_visual_lnum < from)
1583 from = wp->w_old_visual_lnum;
1584 if (wp->w_old_visual_lnum > to)
1585 to = wp->w_old_visual_lnum;
1586 }
1587 else
1588 {
1589 /*
1590 * Find the line numbers that need to be updated: The lines
1591 * between the old cursor position and the current cursor
1592 * position. Also check if the Visual position changed.
1593 */
1594 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1595 {
1596 from = curwin->w_cursor.lnum;
1597 to = wp->w_old_cursor_lnum;
1598 }
1599 else
1600 {
1601 from = wp->w_old_cursor_lnum;
1602 to = curwin->w_cursor.lnum;
1603 if (from == 0) /* Visual mode just started */
1604 from = to;
1605 }
1606
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001607 if (VIsual.lnum != wp->w_old_visual_lnum
1608 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {
1610 if (wp->w_old_visual_lnum < from
1611 && wp->w_old_visual_lnum != 0)
1612 from = wp->w_old_visual_lnum;
1613 if (wp->w_old_visual_lnum > to)
1614 to = wp->w_old_visual_lnum;
1615 if (VIsual.lnum < from)
1616 from = VIsual.lnum;
1617 if (VIsual.lnum > to)
1618 to = VIsual.lnum;
1619 }
1620 }
1621
1622 /*
1623 * If in block mode and changed column or curwin->w_curswant:
1624 * update all lines.
1625 * First compute the actual start and end column.
1626 */
1627 if (VIsual_mode == Ctrl_V)
1628 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001629 colnr_T fromc, toc;
1630#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1631 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaar404406a2014-10-09 13:24:43 +02001633 if (curwin->w_p_lbr)
1634 ve_flags = VE_ALL;
1635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001637#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1638 ve_flags = save_ve_flags;
1639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 ++toc;
1641 if (curwin->w_curswant == MAXCOL)
1642 toc = MAXCOL;
1643
1644 if (fromc != wp->w_old_cursor_fcol
1645 || toc != wp->w_old_cursor_lcol)
1646 {
1647 if (from > VIsual.lnum)
1648 from = VIsual.lnum;
1649 if (to < VIsual.lnum)
1650 to = VIsual.lnum;
1651 }
1652 wp->w_old_cursor_fcol = fromc;
1653 wp->w_old_cursor_lcol = toc;
1654 }
1655 }
1656 else
1657 {
1658 /* Use the line numbers of the old Visual area. */
1659 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1660 {
1661 from = wp->w_old_cursor_lnum;
1662 to = wp->w_old_visual_lnum;
1663 }
1664 else
1665 {
1666 from = wp->w_old_visual_lnum;
1667 to = wp->w_old_cursor_lnum;
1668 }
1669 }
1670
1671 /*
1672 * There is no need to update lines above the top of the window.
1673 */
1674 if (from < wp->w_topline)
1675 from = wp->w_topline;
1676
1677 /*
1678 * If we know the value of w_botline, use it to restrict the update to
1679 * the lines that are visible in the window.
1680 */
1681 if (wp->w_valid & VALID_BOTLINE)
1682 {
1683 if (from >= wp->w_botline)
1684 from = wp->w_botline - 1;
1685 if (to >= wp->w_botline)
1686 to = wp->w_botline - 1;
1687 }
1688
1689 /*
1690 * Find the minimal part to be updated.
1691 * Watch out for scrolling that made entries in w_lines[] invalid.
1692 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1693 * top_end; need to redraw from top_end to the "to" line.
1694 * A middle mouse click with a Visual selection may change the text
1695 * above the Visual area and reset wl_valid, do count these for
1696 * mid_end (in srow).
1697 */
1698 if (mid_start > 0)
1699 {
1700 lnum = wp->w_topline;
1701 idx = 0;
1702 srow = 0;
1703 if (scrolled_down)
1704 mid_start = top_end;
1705 else
1706 mid_start = 0;
1707 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1708 {
1709 if (wp->w_lines[idx].wl_valid)
1710 mid_start += wp->w_lines[idx].wl_size;
1711 else if (!scrolled_down)
1712 srow += wp->w_lines[idx].wl_size;
1713 ++idx;
1714# ifdef FEAT_FOLDING
1715 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1716 lnum = wp->w_lines[idx].wl_lnum;
1717 else
1718# endif
1719 ++lnum;
1720 }
1721 srow += mid_start;
1722 mid_end = wp->w_height;
1723 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1724 {
1725 if (wp->w_lines[idx].wl_valid
1726 && wp->w_lines[idx].wl_lnum >= to + 1)
1727 {
1728 /* Only update until first row of this line */
1729 mid_end = srow;
1730 break;
1731 }
1732 srow += wp->w_lines[idx].wl_size;
1733 }
1734 }
1735 }
1736
1737 if (VIsual_active && buf == curwin->w_buffer)
1738 {
1739 wp->w_old_visual_mode = VIsual_mode;
1740 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1741 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001742 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 wp->w_old_curswant = curwin->w_curswant;
1744 }
1745 else
1746 {
1747 wp->w_old_visual_mode = 0;
1748 wp->w_old_cursor_lnum = 0;
1749 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001750 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
1753#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1754 /* reset got_int, otherwise regexp won't work */
1755 save_got_int = got_int;
1756 got_int = 0;
1757#endif
1758#ifdef FEAT_FOLDING
1759 win_foldinfo.fi_level = 0;
1760#endif
1761
1762 /*
1763 * Update all the window rows.
1764 */
1765 idx = 0; /* first entry in w_lines[].wl_size */
1766 row = 0;
1767 srow = 0;
1768 lnum = wp->w_topline; /* first line shown in window */
1769 for (;;)
1770 {
1771 /* stop updating when reached the end of the window (check for _past_
1772 * the end of the window is at the end of the loop) */
1773 if (row == wp->w_height)
1774 {
1775 didline = TRUE;
1776 break;
1777 }
1778
1779 /* stop updating when hit the end of the file */
1780 if (lnum > buf->b_ml.ml_line_count)
1781 {
1782 eof = TRUE;
1783 break;
1784 }
1785
1786 /* Remember the starting row of the line that is going to be dealt
1787 * with. It is used further down when the line doesn't fit. */
1788 srow = row;
1789
1790 /*
1791 * Update a line when it is in an area that needs updating, when it
1792 * has changes or w_lines[idx] is invalid.
1793 * bot_start may be halfway a wrapped line after using
1794 * win_del_lines(), check if the current line includes it.
1795 * When syntax folding is being used, the saved syntax states will
1796 * already have been updated, we can't see where the syntax state is
1797 * the same again, just update until the end of the window.
1798 */
1799 if (row < top_end
1800 || (row >= mid_start && row < mid_end)
1801#ifdef FEAT_SEARCH_EXTRA
1802 || top_to_mod
1803#endif
1804 || idx >= wp->w_lines_valid
1805 || (row + wp->w_lines[idx].wl_size > bot_start)
1806 || (mod_top != 0
1807 && (lnum == mod_top
1808 || (lnum >= mod_top
1809 && (lnum < mod_bot
1810#ifdef FEAT_SYN_HL
1811 || did_update == DID_FOLD
1812 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001813 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 && (
1815# ifdef FEAT_FOLDING
1816 (foldmethodIsSyntax(wp)
1817 && hasAnyFolding(wp)) ||
1818# endif
1819 syntax_check_changed(lnum)))
1820#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001821#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001822 /* match in fixed position might need redraw
1823 * if lines were inserted or deleted */
1824 || (wp->w_match_head != NULL
1825 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 )))))
1828 {
1829#ifdef FEAT_SEARCH_EXTRA
1830 if (lnum == mod_top)
1831 top_to_mod = FALSE;
1832#endif
1833
1834 /*
1835 * When at start of changed lines: May scroll following lines
1836 * up or down to minimize redrawing.
1837 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001838 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 */
1840 if (lnum == mod_top
1841 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001842 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 {
1844 int old_rows = 0;
1845 int new_rows = 0;
1846 int xtra_rows;
1847 linenr_T l;
1848
1849 /* Count the old number of window rows, using w_lines[], which
1850 * should still contain the sizes for the lines as they are
1851 * currently displayed. */
1852 for (i = idx; i < wp->w_lines_valid; ++i)
1853 {
1854 /* Only valid lines have a meaningful wl_lnum. Invalid
1855 * lines are part of the changed area. */
1856 if (wp->w_lines[i].wl_valid
1857 && wp->w_lines[i].wl_lnum == mod_bot)
1858 break;
1859 old_rows += wp->w_lines[i].wl_size;
1860#ifdef FEAT_FOLDING
1861 if (wp->w_lines[i].wl_valid
1862 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1863 {
1864 /* Must have found the last valid entry above mod_bot.
1865 * Add following invalid entries. */
1866 ++i;
1867 while (i < wp->w_lines_valid
1868 && !wp->w_lines[i].wl_valid)
1869 old_rows += wp->w_lines[i++].wl_size;
1870 break;
1871 }
1872#endif
1873 }
1874
1875 if (i >= wp->w_lines_valid)
1876 {
1877 /* We can't find a valid line below the changed lines,
1878 * need to redraw until the end of the window.
1879 * Inserting/deleting lines has no use. */
1880 bot_start = 0;
1881 }
1882 else
1883 {
1884 /* Able to count old number of rows: Count new window
1885 * rows, and may insert/delete lines */
1886 j = idx;
1887 for (l = lnum; l < mod_bot; ++l)
1888 {
1889#ifdef FEAT_FOLDING
1890 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1891 ++new_rows;
1892 else
1893#endif
1894#ifdef FEAT_DIFF
1895 if (l == wp->w_topline)
1896 new_rows += plines_win_nofill(wp, l, TRUE)
1897 + wp->w_topfill;
1898 else
1899#endif
1900 new_rows += plines_win(wp, l, TRUE);
1901 ++j;
1902 if (new_rows > wp->w_height - row - 2)
1903 {
1904 /* it's getting too much, must redraw the rest */
1905 new_rows = 9999;
1906 break;
1907 }
1908 }
1909 xtra_rows = new_rows - old_rows;
1910 if (xtra_rows < 0)
1911 {
1912 /* May scroll text up. If there is not enough
1913 * remaining text or scrolling fails, must redraw the
1914 * rest. If scrolling works, must redraw the text
1915 * below the scrolled text. */
1916 if (row - xtra_rows >= wp->w_height - 2)
1917 mod_bot = MAXLNUM;
1918 else
1919 {
1920 check_for_delay(FALSE);
1921 if (win_del_lines(wp, row,
1922 -xtra_rows, FALSE, FALSE) == FAIL)
1923 mod_bot = MAXLNUM;
1924 else
1925 bot_start = wp->w_height + xtra_rows;
1926 }
1927 }
1928 else if (xtra_rows > 0)
1929 {
1930 /* May scroll text down. If there is not enough
1931 * remaining text of scrolling fails, must redraw the
1932 * rest. */
1933 if (row + xtra_rows >= wp->w_height - 2)
1934 mod_bot = MAXLNUM;
1935 else
1936 {
1937 check_for_delay(FALSE);
1938 if (win_ins_lines(wp, row + old_rows,
1939 xtra_rows, FALSE, FALSE) == FAIL)
1940 mod_bot = MAXLNUM;
1941 else if (top_end > row + old_rows)
1942 /* Scrolled the part at the top that requires
1943 * updating down. */
1944 top_end += xtra_rows;
1945 }
1946 }
1947
1948 /* When not updating the rest, may need to move w_lines[]
1949 * entries. */
1950 if (mod_bot != MAXLNUM && i != j)
1951 {
1952 if (j < i)
1953 {
1954 int x = row + new_rows;
1955
1956 /* move entries in w_lines[] upwards */
1957 for (;;)
1958 {
1959 /* stop at last valid entry in w_lines[] */
1960 if (i >= wp->w_lines_valid)
1961 {
1962 wp->w_lines_valid = j;
1963 break;
1964 }
1965 wp->w_lines[j] = wp->w_lines[i];
1966 /* stop at a line that won't fit */
1967 if (x + (int)wp->w_lines[j].wl_size
1968 > wp->w_height)
1969 {
1970 wp->w_lines_valid = j + 1;
1971 break;
1972 }
1973 x += wp->w_lines[j++].wl_size;
1974 ++i;
1975 }
1976 if (bot_start > x)
1977 bot_start = x;
1978 }
1979 else /* j > i */
1980 {
1981 /* move entries in w_lines[] downwards */
1982 j -= i;
1983 wp->w_lines_valid += j;
1984 if (wp->w_lines_valid > wp->w_height)
1985 wp->w_lines_valid = wp->w_height;
1986 for (i = wp->w_lines_valid; i - j >= idx; --i)
1987 wp->w_lines[i] = wp->w_lines[i - j];
1988
1989 /* The w_lines[] entries for inserted lines are
1990 * now invalid, but wl_size may be used above.
1991 * Reset to zero. */
1992 while (i >= idx)
1993 {
1994 wp->w_lines[i].wl_size = 0;
1995 wp->w_lines[i--].wl_valid = FALSE;
1996 }
1997 }
1998 }
1999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 }
2001
2002#ifdef FEAT_FOLDING
2003 /*
2004 * When lines are folded, display one line for all of them.
2005 * Otherwise, display normally (can be several display lines when
2006 * 'wrap' is on).
2007 */
2008 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2009 if (fold_count != 0)
2010 {
2011 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2012 ++row;
2013 --fold_count;
2014 wp->w_lines[idx].wl_folded = TRUE;
2015 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2016# ifdef FEAT_SYN_HL
2017 did_update = DID_FOLD;
2018# endif
2019 }
2020 else
2021#endif
2022 if (idx < wp->w_lines_valid
2023 && wp->w_lines[idx].wl_valid
2024 && wp->w_lines[idx].wl_lnum == lnum
2025 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002026 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 && srow + wp->w_lines[idx].wl_size > wp->w_height
2028#ifdef FEAT_DIFF
2029 && diff_check_fill(wp, lnum) == 0
2030#endif
2031 )
2032 {
2033 /* This line is not going to fit. Don't draw anything here,
2034 * will draw "@ " lines below. */
2035 row = wp->w_height + 1;
2036 }
2037 else
2038 {
2039#ifdef FEAT_SEARCH_EXTRA
2040 prepare_search_hl(wp, lnum);
2041#endif
2042#ifdef FEAT_SYN_HL
2043 /* Let the syntax stuff know we skipped a few lines. */
2044 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002045 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 syntax_end_parsing(syntax_last_parsed + 1);
2047#endif
2048
2049 /*
2050 * Display one line.
2051 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002052 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054#ifdef FEAT_FOLDING
2055 wp->w_lines[idx].wl_folded = FALSE;
2056 wp->w_lines[idx].wl_lastlnum = lnum;
2057#endif
2058#ifdef FEAT_SYN_HL
2059 did_update = DID_LINE;
2060 syntax_last_parsed = lnum;
2061#endif
2062 }
2063
2064 wp->w_lines[idx].wl_lnum = lnum;
2065 wp->w_lines[idx].wl_valid = TRUE;
2066 if (row > wp->w_height) /* past end of screen */
2067 {
2068 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002069 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2071 ++idx;
2072 break;
2073 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002074 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 wp->w_lines[idx].wl_size = row - srow;
2076 ++idx;
2077#ifdef FEAT_FOLDING
2078 lnum += fold_count + 1;
2079#else
2080 ++lnum;
2081#endif
2082 }
2083 else
2084 {
2085 /* This line does not need updating, advance to the next one */
2086 row += wp->w_lines[idx++].wl_size;
2087 if (row > wp->w_height) /* past end of screen */
2088 break;
2089#ifdef FEAT_FOLDING
2090 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2091#else
2092 ++lnum;
2093#endif
2094#ifdef FEAT_SYN_HL
2095 did_update = DID_NONE;
2096#endif
2097 }
2098
2099 if (lnum > buf->b_ml.ml_line_count)
2100 {
2101 eof = TRUE;
2102 break;
2103 }
2104 }
2105 /*
2106 * End of loop over all window lines.
2107 */
2108
2109
2110 if (idx > wp->w_lines_valid)
2111 wp->w_lines_valid = idx;
2112
2113#ifdef FEAT_SYN_HL
2114 /*
2115 * Let the syntax stuff know we stop parsing here.
2116 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002117 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 syntax_end_parsing(syntax_last_parsed + 1);
2119#endif
2120
2121 /*
2122 * If we didn't hit the end of the file, and we didn't finish the last
2123 * line we were working on, then the line didn't fit.
2124 */
2125 wp->w_empty_rows = 0;
2126#ifdef FEAT_DIFF
2127 wp->w_filler_rows = 0;
2128#endif
2129 if (!eof && !didline)
2130 {
2131 if (lnum == wp->w_topline)
2132 {
2133 /*
2134 * Single line that does not fit!
2135 * Don't overwrite it, it can be edited.
2136 */
2137 wp->w_botline = lnum + 1;
2138 }
2139#ifdef FEAT_DIFF
2140 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2141 {
2142 /* Window ends in filler lines. */
2143 wp->w_botline = lnum;
2144 wp->w_filler_rows = wp->w_height - srow;
2145 }
2146#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002147 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2148 {
2149 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2150
2151 /*
2152 * Last line isn't finished: Display "@@@" in the last screen line.
2153 */
2154 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
2155 hl_attr(HLF_AT));
2156 screen_fill(scr_row, scr_row + 1,
2157 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
2158 '@', ' ', hl_attr(HLF_AT));
2159 set_empty_rows(wp, srow);
2160 wp->w_botline = lnum;
2161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2163 {
2164 /*
2165 * Last line isn't finished: Display "@@@" at the end.
2166 */
2167 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2168 W_WINROW(wp) + wp->w_height,
2169 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2170 '@', '@', hl_attr(HLF_AT));
2171 set_empty_rows(wp, srow);
2172 wp->w_botline = lnum;
2173 }
2174 else
2175 {
2176 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2177 wp->w_botline = lnum;
2178 }
2179 }
2180 else
2181 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002182#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 draw_vsep_win(wp, row);
2184#endif
2185 if (eof) /* we hit the end of the file */
2186 {
2187 wp->w_botline = buf->b_ml.ml_line_count + 1;
2188#ifdef FEAT_DIFF
2189 j = diff_check_fill(wp, wp->w_botline);
2190 if (j > 0 && !wp->w_botfill)
2191 {
2192 /*
2193 * Display filler lines at the end of the file
2194 */
2195 if (char2cells(fill_diff) > 1)
2196 i = '-';
2197 else
2198 i = fill_diff;
2199 if (row + j > wp->w_height)
2200 j = wp->w_height - row;
2201 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2202 row += j;
2203 }
2204#endif
2205 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002206 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 wp->w_botline = lnum;
2208
2209 /* make sure the rest of the screen is blank */
2210 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002211 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 }
2213
2214 /* Reset the type of redrawing required, the window has been updated. */
2215 wp->w_redr_type = 0;
2216#ifdef FEAT_DIFF
2217 wp->w_old_topfill = wp->w_topfill;
2218 wp->w_old_botfill = wp->w_botfill;
2219#endif
2220
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002221 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
2223 /*
2224 * There is a trick with w_botline. If we invalidate it on each
2225 * change that might modify it, this will cause a lot of expensive
2226 * calls to plines() in update_topline() each time. Therefore the
2227 * value of w_botline is often approximated, and this value is used to
2228 * compute the value of w_topline. If the value of w_botline was
2229 * wrong, check that the value of w_topline is correct (cursor is on
2230 * the visible part of the text). If it's not, we need to redraw
2231 * again. Mostly this just means scrolling up a few lines, so it
2232 * doesn't look too bad. Only do this for the current window (where
2233 * changes are relevant).
2234 */
2235 wp->w_valid |= VALID_BOTLINE;
2236 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2237 {
2238 recursive = TRUE;
2239 curwin->w_valid &= ~VALID_TOPLINE;
2240 update_topline(); /* may invalidate w_botline again */
2241 if (must_redraw != 0)
2242 {
2243 /* Don't update for changes in buffer again. */
2244 i = curbuf->b_mod_set;
2245 curbuf->b_mod_set = FALSE;
2246 win_update(curwin);
2247 must_redraw = 0;
2248 curbuf->b_mod_set = i;
2249 }
2250 recursive = FALSE;
2251 }
2252 }
2253
2254#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2255 /* restore got_int, unless CTRL-C was hit while redrawing */
2256 if (!got_int)
2257 got_int = save_got_int;
2258#endif
2259}
2260
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261/*
2262 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2263 * as the filler character.
2264 */
2265 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002266win_draw_end(
2267 win_T *wp,
2268 int c1,
2269 int c2,
2270 int row,
2271 int endrow,
2272 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
2274#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2275 int n = 0;
2276# define FDC_OFF n
2277#else
2278# define FDC_OFF 0
2279#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002280#ifdef FEAT_FOLDING
2281 int fdc = compute_foldcolumn(wp, 0);
2282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
2284#ifdef FEAT_RIGHTLEFT
2285 if (wp->w_p_rl)
2286 {
2287 /* No check for cmdline window: should never be right-left. */
2288# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002289 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
2291 if (n > 0)
2292 {
2293 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002294 if (n > W_WIDTH(wp))
2295 n = 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) - n, (int)W_ENDCOL(wp),
2298 ' ', ' ', hl_attr(HLF_FC));
2299 }
2300# endif
2301# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002302 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 int nn = n + 2;
2305
2306 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002307 if (nn > W_WIDTH(wp))
2308 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2310 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2311 ' ', ' ', hl_attr(HLF_SC));
2312 n = nn;
2313 }
2314# endif
2315 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2316 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2317 c2, c2, hl_attr(hl));
2318 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2319 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2320 c1, c2, hl_attr(hl));
2321 }
2322 else
2323#endif
2324 {
2325#ifdef FEAT_CMDWIN
2326 if (cmdwin_type != 0 && wp == curwin)
2327 {
2328 /* draw the cmdline character in the leftmost column */
2329 n = 1;
2330 if (n > wp->w_width)
2331 n = wp->w_width;
2332 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2333 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2334 cmdwin_type, ' ', hl_attr(HLF_AT));
2335 }
2336#endif
2337#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002338 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002340 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
2342 /* draw the fold column at the left */
2343 if (nn > W_WIDTH(wp))
2344 nn = W_WIDTH(wp);
2345 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2346 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2347 ' ', ' ', hl_attr(HLF_FC));
2348 n = nn;
2349 }
2350#endif
2351#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002352 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
2354 int nn = n + 2;
2355
2356 /* draw the sign column after the fold column */
2357 if (nn > W_WIDTH(wp))
2358 nn = W_WIDTH(wp);
2359 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2360 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2361 ' ', ' ', hl_attr(HLF_SC));
2362 n = nn;
2363 }
2364#endif
2365 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2366 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2367 c1, c2, hl_attr(hl));
2368 }
2369 set_empty_rows(wp, row);
2370}
2371
Bram Moolenaar1a384422010-07-14 19:53:30 +02002372#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002373static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002374
2375/*
2376 * Advance **color_cols and return TRUE when there are columns to draw.
2377 */
2378 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002379advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002380{
2381 while (**color_cols >= 0 && vcol > **color_cols)
2382 ++*color_cols;
2383 return (**color_cols >= 0);
2384}
2385#endif
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#ifdef FEAT_FOLDING
2388/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002389 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2390 * space is available for window "wp", minus "col".
2391 */
2392 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002393compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002394{
2395 int fdc = wp->w_p_fdc;
2396 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2397 int wwidth = W_WIDTH(wp);
2398
2399 if (fdc > wwidth - (col + wmw))
2400 fdc = wwidth - (col + wmw);
2401 return fdc;
2402}
2403
2404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 * Display one folded line.
2406 */
2407 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002408fold_line(
2409 win_T *wp,
2410 long fold_count,
2411 foldinfo_T *foldinfo,
2412 linenr_T lnum,
2413 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002415 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 pos_T *top, *bot;
2417 linenr_T lnume = lnum + fold_count - 1;
2418 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002419 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 int col;
2422 int txtcol;
2423 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002424 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 /* Build the fold line:
2427 * 1. Add the cmdwin_type for the command-line window
2428 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002429 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 * 4. Compose the text
2431 * 5. Add the text
2432 * 6. set highlighting for the Visual area an other text
2433 */
2434 col = 0;
2435
2436 /*
2437 * 1. Add the cmdwin_type for the command-line window
2438 * Ignores 'rightleft', this window is never right-left.
2439 */
2440#ifdef FEAT_CMDWIN
2441 if (cmdwin_type != 0 && wp == curwin)
2442 {
2443 ScreenLines[off] = cmdwin_type;
2444 ScreenAttrs[off] = hl_attr(HLF_AT);
2445#ifdef FEAT_MBYTE
2446 if (enc_utf8)
2447 ScreenLinesUC[off] = 0;
2448#endif
2449 ++col;
2450 }
2451#endif
2452
2453 /*
2454 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002455 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002457 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 if (fdc > 0)
2459 {
2460 fill_foldcolumn(buf, wp, TRUE, lnum);
2461#ifdef FEAT_RIGHTLEFT
2462 if (wp->w_p_rl)
2463 {
2464 int i;
2465
2466 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2467 hl_attr(HLF_FC));
2468 /* reverse the fold column */
2469 for (i = 0; i < fdc; ++i)
2470 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2471 }
2472 else
2473#endif
2474 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2475 col += fdc;
2476 }
2477
2478#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002479# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2480 for (ri = 0; ri < l; ++ri) \
2481 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2482 else \
2483 for (ri = 0; ri < l; ++ri) \
2484 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002486# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2487 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488#endif
2489
Bram Moolenaar64486672010-05-16 15:46:46 +02002490 /* Set all attributes of the 'number' or 'relativenumber' column and the
2491 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002492 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494#ifdef FEAT_SIGNS
2495 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002496 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 {
2498 len = W_WIDTH(wp) - col;
2499 if (len > 0)
2500 {
2501 if (len > 2)
2502 len = 2;
2503# ifdef FEAT_RIGHTLEFT
2504 if (wp->w_p_rl)
2505 /* the line number isn't reversed */
2506 copy_text_attr(off + W_WIDTH(wp) - len - col,
2507 (char_u *)" ", len, hl_attr(HLF_FL));
2508 else
2509# endif
2510 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2511 col += len;
2512 }
2513 }
2514#endif
2515
2516 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002517 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002519 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 len = W_WIDTH(wp) - col;
2522 if (len > 0)
2523 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002524 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002525 long num;
2526 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002527
2528 if (len > w + 1)
2529 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002530
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002531 if (wp->w_p_nu && !wp->w_p_rnu)
2532 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002533 num = (long)lnum;
2534 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002535 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002536 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002537 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002538 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002539 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002540 /* 'number' + 'relativenumber': cursor line shows absolute
2541 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002542 num = lnum;
2543 fmt = "%-*ld ";
2544 }
2545 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002546
Bram Moolenaar700e7342013-01-30 12:31:36 +01002547 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548#ifdef FEAT_RIGHTLEFT
2549 if (wp->w_p_rl)
2550 /* the line number isn't reversed */
2551 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2552 hl_attr(HLF_FL));
2553 else
2554#endif
2555 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2556 col += len;
2557 }
2558 }
2559
2560 /*
2561 * 4. Compose the folded-line string with 'foldtext', if set.
2562 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002563 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
2565 txtcol = col; /* remember where text starts */
2566
2567 /*
2568 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2569 * Right-left text is put in columns 0 - number-col, normal text is put
2570 * in columns number-col - window-width.
2571 */
2572#ifdef FEAT_MBYTE
2573 if (has_mbyte)
2574 {
2575 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002576 int u8c, u8cc[MAX_MCO];
2577 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 int idx;
2579 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002580 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581# ifdef FEAT_ARABIC
2582 int prev_c = 0; /* previous Arabic character */
2583 int prev_c1 = 0; /* first composing char for prev_c */
2584# endif
2585
2586# ifdef FEAT_RIGHTLEFT
2587 if (wp->w_p_rl)
2588 idx = off;
2589 else
2590# endif
2591 idx = off + col;
2592
2593 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2594 for (p = text; *p != NUL; )
2595 {
2596 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002597 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 if (col + cells > W_WIDTH(wp)
2599# ifdef FEAT_RIGHTLEFT
2600 - (wp->w_p_rl ? col : 0)
2601# endif
2602 )
2603 break;
2604 ScreenLines[idx] = *p;
2605 if (enc_utf8)
2606 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002607 u8c = utfc_ptr2char(p, u8cc);
2608 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
2610 ScreenLinesUC[idx] = 0;
2611#ifdef FEAT_ARABIC
2612 prev_c = u8c;
2613#endif
2614 }
2615 else
2616 {
2617#ifdef FEAT_ARABIC
2618 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2619 {
2620 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002621 int pc, pc1, nc;
2622 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 int firstbyte = *p;
2624
2625 /* The idea of what is the previous and next
2626 * character depends on 'rightleft'. */
2627 if (wp->w_p_rl)
2628 {
2629 pc = prev_c;
2630 pc1 = prev_c1;
2631 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002632 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
2634 else
2635 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002636 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002638 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
2640 prev_c = u8c;
2641
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002642 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 pc, pc1, nc);
2644 ScreenLines[idx] = firstbyte;
2645 }
2646 else
2647 prev_c = u8c;
2648#endif
2649 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002650#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (u8c >= 0x10000)
2652 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2653 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002656 for (i = 0; i < Screen_mco; ++i)
2657 {
2658 ScreenLinesC[i][idx] = u8cc[i];
2659 if (u8cc[i] == 0)
2660 break;
2661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663 if (cells > 1)
2664 ScreenLines[idx + 1] = 0;
2665 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002666 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2667 /* double-byte single width character */
2668 ScreenLines2[idx] = p[1];
2669 else if (cells > 1)
2670 /* double-width character */
2671 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 col += cells;
2673 idx += cells;
2674 p += c_len;
2675 }
2676 }
2677 else
2678#endif
2679 {
2680 len = (int)STRLEN(text);
2681 if (len > W_WIDTH(wp) - col)
2682 len = W_WIDTH(wp) - col;
2683 if (len > 0)
2684 {
2685#ifdef FEAT_RIGHTLEFT
2686 if (wp->w_p_rl)
2687 STRNCPY(current_ScreenLine, text, len);
2688 else
2689#endif
2690 STRNCPY(current_ScreenLine + col, text, len);
2691 col += len;
2692 }
2693 }
2694
2695 /* Fill the rest of the line with the fold filler */
2696#ifdef FEAT_RIGHTLEFT
2697 if (wp->w_p_rl)
2698 col -= txtcol;
2699#endif
2700 while (col < W_WIDTH(wp)
2701#ifdef FEAT_RIGHTLEFT
2702 - (wp->w_p_rl ? txtcol : 0)
2703#endif
2704 )
2705 {
2706#ifdef FEAT_MBYTE
2707 if (enc_utf8)
2708 {
2709 if (fill_fold >= 0x80)
2710 {
2711 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002712 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 else
2715 ScreenLinesUC[off + col] = 0;
2716 }
2717#endif
2718 ScreenLines[off + col++] = fill_fold;
2719 }
2720
2721 if (text != buf)
2722 vim_free(text);
2723
2724 /*
2725 * 6. set highlighting for the Visual area an other text.
2726 * If all folded lines are in the Visual area, highlight the line.
2727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2729 {
2730 if (ltoreq(curwin->w_cursor, VIsual))
2731 {
2732 /* Visual is after curwin->w_cursor */
2733 top = &curwin->w_cursor;
2734 bot = &VIsual;
2735 }
2736 else
2737 {
2738 /* Visual is before curwin->w_cursor */
2739 top = &VIsual;
2740 bot = &curwin->w_cursor;
2741 }
2742 if (lnum >= top->lnum
2743 && lnume <= bot->lnum
2744 && (VIsual_mode != 'v'
2745 || ((lnum > top->lnum
2746 || (lnum == top->lnum
2747 && top->col == 0))
2748 && (lnume < bot->lnum
2749 || (lnume == bot->lnum
2750 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002751 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 {
2753 if (VIsual_mode == Ctrl_V)
2754 {
2755 /* Visual block mode: highlight the chars part of the block */
2756 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2757 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002758 if (wp->w_old_cursor_lcol != MAXCOL
2759 && wp->w_old_cursor_lcol + txtcol
2760 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 len = wp->w_old_cursor_lcol;
2762 else
2763 len = W_WIDTH(wp) - txtcol;
2764 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002765 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767 }
2768 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002771 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
2774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002776#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002777 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2778 if (wp->w_p_cc_cols)
2779 {
2780 int i = 0;
2781 int j = wp->w_p_cc_cols[i];
2782 int old_txtcol = txtcol;
2783
2784 while (j > -1)
2785 {
2786 txtcol += j;
2787 if (wp->w_p_wrap)
2788 txtcol -= wp->w_skipcol;
2789 else
2790 txtcol -= wp->w_leftcol;
2791 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2792 ScreenAttrs[off + txtcol] = hl_combine_attr(
2793 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2794 txtcol = old_txtcol;
2795 j = wp->w_p_cc_cols[++i];
2796 }
2797 }
2798
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002799 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002800 if (wp->w_p_cuc)
2801 {
2802 txtcol += wp->w_virtcol;
2803 if (wp->w_p_wrap)
2804 txtcol -= wp->w_skipcol;
2805 else
2806 txtcol -= wp->w_leftcol;
2807 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2808 ScreenAttrs[off + txtcol] = hl_combine_attr(
2809 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2810 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2814 (int)W_WIDTH(wp), FALSE);
2815
2816 /*
2817 * Update w_cline_height and w_cline_folded if the cursor line was
2818 * updated (saves a call to plines() later).
2819 */
2820 if (wp == curwin
2821 && lnum <= curwin->w_cursor.lnum
2822 && lnume >= curwin->w_cursor.lnum)
2823 {
2824 curwin->w_cline_row = row;
2825 curwin->w_cline_height = 1;
2826 curwin->w_cline_folded = TRUE;
2827 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2828 }
2829}
2830
2831/*
2832 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2833 */
2834 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002835copy_text_attr(
2836 int off,
2837 char_u *buf,
2838 int len,
2839 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002841 int i;
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 mch_memmove(ScreenLines + off, buf, (size_t)len);
2844# ifdef FEAT_MBYTE
2845 if (enc_utf8)
2846 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2847# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002848 for (i = 0; i < len; ++i)
2849 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850}
2851
2852/*
2853 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002854 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 */
2856 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002857fill_foldcolumn(
2858 char_u *p,
2859 win_T *wp,
2860 int closed, /* TRUE of FALSE */
2861 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 int i = 0;
2864 int level;
2865 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002866 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002867 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
2869 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002870 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
2872 level = win_foldinfo.fi_level;
2873 if (level > 0)
2874 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002875 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002876 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 /* If the column is too narrow, we start at the lowest level that
2879 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002880 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (first_level < 1)
2882 first_level = 1;
2883
Bram Moolenaar1c934292015-01-27 16:39:29 +01002884 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
2886 if (win_foldinfo.fi_lnum == lnum
2887 && first_level + i >= win_foldinfo.fi_low_level)
2888 p[i] = '-';
2889 else if (first_level == 1)
2890 p[i] = '|';
2891 else if (first_level + i <= 9)
2892 p[i] = '0' + first_level + i;
2893 else
2894 p[i] = '>';
2895 if (first_level + i == level)
2896 break;
2897 }
2898 }
2899 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002900 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901}
2902#endif /* FEAT_FOLDING */
2903
2904/*
2905 * Display line "lnum" of window 'wp' on the screen.
2906 * Start at row "startrow", stop when "endrow" is reached.
2907 * wp->w_virtcol needs to be valid.
2908 *
2909 * Return the number of last row the line occupies.
2910 */
2911 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002912win_line(
2913 win_T *wp,
2914 linenr_T lnum,
2915 int startrow,
2916 int endrow,
2917 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
2919 int col; /* visual column on screen */
2920 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2921 int c = 0; /* init for GCC */
2922 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002923#ifdef FEAT_LINEBREAK
2924 long vcol_sbr = -1; /* virtual column after showbreak */
2925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 long vcol_prev = -1; /* "vcol" of previous character */
2927 char_u *line; /* current line */
2928 char_u *ptr; /* current position in "line" */
2929 int row; /* row in the window, excl w_winrow */
2930 int screen_row; /* row on the screen, incl w_winrow */
2931
2932 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2933 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002934 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002935 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 int c_extra = NUL; /* extra chars, all the same */
2937 int extra_attr = 0; /* attributes when n_extra != 0 */
2938 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2939 displaying lcs_eol at end-of-line */
2940 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2941 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2942
2943 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2944 int saved_n_extra = 0;
2945 char_u *saved_p_extra = NULL;
2946 int saved_c_extra = 0;
2947 int saved_char_attr = 0;
2948
2949 int n_attr = 0; /* chars with special attr */
2950 int saved_attr2 = 0; /* char_attr saved for n_attr */
2951 int n_attr3 = 0; /* chars with overruling special attr */
2952 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2953
2954 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2955
2956 int fromcol, tocol; /* start/end of inverting */
2957 int fromcol_prev = -2; /* start of inverting after cursor */
2958 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002960 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 pos_T pos;
2962 long v;
2963
2964 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002965 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2967 in this line */
2968 int attr = 0; /* attributes for area highlighting */
2969 int area_attr = 0; /* attributes desired by highlighting */
2970 int search_attr = 0; /* attributes desired by 'hlsearch' */
2971#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002972 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 int syntax_attr = 0; /* attributes desired by syntax */
2974 int has_syntax = FALSE; /* this buffer has syntax highl. */
2975 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002976 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002977 int draw_color_col = FALSE; /* highlight colorcolumn */
2978 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002979#endif
2980#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002981 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002982# define SPWORDLEN 150
2983 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002984 int nextlinecol = 0; /* column where nextline[] starts */
2985 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002986 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002987 int spell_attr = 0; /* attributes desired by spelling */
2988 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002989 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2990 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002991 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002992 static int cap_col = -1; /* column to check for Cap word */
2993 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002994 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995#endif
2996 int extra_check; /* has syntax or linebreak */
2997#ifdef FEAT_MBYTE
2998 int multi_attr = 0; /* attributes desired by multibyte */
2999 int mb_l = 1; /* multi-byte byte length */
3000 int mb_c = 0; /* decoded multi-byte character */
3001 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003002 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003#endif
3004#ifdef FEAT_DIFF
3005 int filler_lines; /* nr of filler lines to be drawn */
3006 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003007 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 int change_start = MAXCOL; /* first col of changed area */
3009 int change_end = -1; /* last col of changed area */
3010#endif
3011 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3012#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003013 int need_showbreak = FALSE; /* overlong line, skipping first x
3014 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003016#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3017 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003019 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020#endif
3021#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003022 matchitem_T *cur; /* points to the match list */
3023 match_T *shl; /* points to search_hl or a match */
3024 int shl_flag; /* flag to indicate whether search_hl
3025 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003026 int pos_inprogress; /* marks that position match search is
3027 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003028 int prevcol_hl_flag; /* flag to indicate whether prevcol
3029 equals startcol of search_hl or one
3030 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031#endif
3032#ifdef FEAT_ARABIC
3033 int prev_c = 0; /* previous Arabic character */
3034 int prev_c1 = 0; /* first composing char for prev_c */
3035#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003036#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003037 int did_line_attr = 0;
3038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
3040 /* draw_state: items that are drawn in sequence: */
3041#define WL_START 0 /* nothing done yet */
3042#ifdef FEAT_CMDWIN
3043# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3044#else
3045# define WL_CMDLINE WL_START
3046#endif
3047#ifdef FEAT_FOLDING
3048# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3049#else
3050# define WL_FOLD WL_CMDLINE
3051#endif
3052#ifdef FEAT_SIGNS
3053# define WL_SIGN WL_FOLD + 1 /* column for signs */
3054#else
3055# define WL_SIGN WL_FOLD /* column for signs */
3056#endif
3057#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003058#ifdef FEAT_LINEBREAK
3059# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003061# define WL_BRI WL_NR
3062#endif
3063#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3064# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3065#else
3066# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067#endif
3068#define WL_LINE WL_SBR + 1 /* text in the line */
3069 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003070#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 int feedback_col = 0;
3072 int feedback_old_attr = -1;
3073#endif
3074
Bram Moolenaar860cae12010-06-05 23:22:07 +02003075#ifdef FEAT_CONCEAL
3076 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003077 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003078 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003079 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003080 int is_concealing = FALSE;
3081 int boguscols = 0; /* nonexistent columns added to force
3082 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003083 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003084 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003085 int match_conc = 0; /* cchar for match functions */
3086 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003087 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003088# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003089# define FIX_FOR_BOGUSCOLS \
3090 { \
3091 n_extra += vcol_off; \
3092 vcol -= vcol_off; \
3093 vcol_off = 0; \
3094 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003095 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003096 boguscols = 0; \
3097 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003098#else
3099# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
3102 if (startrow > endrow) /* past the end already! */
3103 return startrow;
3104
3105 row = startrow;
3106 screen_row = row + W_WINROW(wp);
3107
3108 /*
3109 * To speed up the loop below, set extra_check when there is linebreak,
3110 * trailing white space and/or syntax processing to be done.
3111 */
3112#ifdef FEAT_LINEBREAK
3113 extra_check = wp->w_p_lbr;
3114#else
3115 extra_check = 0;
3116#endif
3117#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003118 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
3120 /* Prepare for syntax highlighting in this line. When there is an
3121 * error, stop syntax highlighting. */
3122 save_did_emsg = did_emsg;
3123 did_emsg = FALSE;
3124 syntax_start(wp, lnum);
3125 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003126 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 else
3128 {
3129 did_emsg = save_did_emsg;
3130 has_syntax = TRUE;
3131 extra_check = TRUE;
3132 }
3133 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003134
3135 /* Check for columns to display for 'colorcolumn'. */
3136 color_cols = wp->w_p_cc_cols;
3137 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003138 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003139#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003140
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003141#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003142 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003143 && *wp->w_s->b_p_spl != NUL
3144 && wp->w_s->b_langp.ga_len > 0
3145 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003146 {
3147 /* Prepare for spell checking. */
3148 has_spell = TRUE;
3149 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003150
3151 /* Get the start of the next line, so that words that wrap to the next
3152 * line are found too: "et<line-break>al.".
3153 * Trick: skip a few chars for C/shell/Vim comments */
3154 nextline[SPWORDLEN] = NUL;
3155 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3156 {
3157 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3158 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3159 }
3160
3161 /* When a word wrapped from the previous line the start of the current
3162 * line is valid. */
3163 if (lnum == checked_lnum)
3164 cur_checked_col = checked_col;
3165 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003166
3167 /* When there was a sentence end in the previous line may require a
3168 * word starting with capital in this line. In line 1 always check
3169 * the first word. */
3170 if (lnum != capcol_lnum)
3171 cap_col = -1;
3172 if (lnum == 1)
3173 cap_col = 0;
3174 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176#endif
3177
3178 /*
3179 * handle visual active in this window
3180 */
3181 fromcol = -10;
3182 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3184 {
3185 /* Visual is after curwin->w_cursor */
3186 if (ltoreq(curwin->w_cursor, VIsual))
3187 {
3188 top = &curwin->w_cursor;
3189 bot = &VIsual;
3190 }
3191 else /* Visual is before curwin->w_cursor */
3192 {
3193 top = &VIsual;
3194 bot = &curwin->w_cursor;
3195 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003196 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (VIsual_mode == Ctrl_V) /* block mode */
3198 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003199 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
3201 fromcol = wp->w_old_cursor_fcol;
3202 tocol = wp->w_old_cursor_lcol;
3203 }
3204 }
3205 else /* non-block mode */
3206 {
3207 if (lnum > top->lnum && lnum <= bot->lnum)
3208 fromcol = 0;
3209 else if (lnum == top->lnum)
3210 {
3211 if (VIsual_mode == 'V') /* linewise */
3212 fromcol = 0;
3213 else
3214 {
3215 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3216 if (gchar_pos(top) == NUL)
3217 tocol = fromcol + 1;
3218 }
3219 }
3220 if (VIsual_mode != 'V' && lnum == bot->lnum)
3221 {
3222 if (*p_sel == 'e' && bot->col == 0
3223#ifdef FEAT_VIRTUALEDIT
3224 && bot->coladd == 0
3225#endif
3226 )
3227 {
3228 fromcol = -10;
3229 tocol = MAXCOL;
3230 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003231 else if (bot->col == MAXCOL)
3232 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 else
3234 {
3235 pos = *bot;
3236 if (*p_sel == 'e')
3237 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3238 else
3239 {
3240 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3241 ++tocol;
3242 }
3243 }
3244 }
3245 }
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 /* Check if the character under the cursor should not be inverted */
3248 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003249#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 )
3253 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255 /* if inverting in this line set area_highlighting */
3256 if (fromcol >= 0)
3257 {
3258 area_highlighting = TRUE;
3259 attr = hl_attr(HLF_V);
3260#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003261 if ((clip_star.available && !clip_star.owned
3262 && clip_isautosel_star())
3263 || (clip_plus.available && !clip_plus.owned
3264 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 attr = hl_attr(HLF_VNC);
3266#endif
3267 }
3268 }
3269
3270 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003271 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003273 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 && wp == curwin
3275 && lnum >= curwin->w_cursor.lnum
3276 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3277 {
3278 if (lnum == curwin->w_cursor.lnum)
3279 getvcol(curwin, &(curwin->w_cursor),
3280 (colnr_T *)&fromcol, NULL, NULL);
3281 else
3282 fromcol = 0;
3283 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3284 {
3285 pos.lnum = lnum;
3286 pos.col = search_match_endcol;
3287 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3288 }
3289 else
3290 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003291 /* do at least one character; happens when past end of line */
3292 if (fromcol == tocol)
3293 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 area_highlighting = TRUE;
3295 attr = hl_attr(HLF_I);
3296 }
3297
3298#ifdef FEAT_DIFF
3299 filler_lines = diff_check(wp, lnum);
3300 if (filler_lines < 0)
3301 {
3302 if (filler_lines == -1)
3303 {
3304 if (diff_find_change(wp, lnum, &change_start, &change_end))
3305 diff_hlf = HLF_ADD; /* added line */
3306 else if (change_start == 0)
3307 diff_hlf = HLF_TXD; /* changed text */
3308 else
3309 diff_hlf = HLF_CHD; /* changed line */
3310 }
3311 else
3312 diff_hlf = HLF_ADD; /* added line */
3313 filler_lines = 0;
3314 area_highlighting = TRUE;
3315 }
3316 if (lnum == wp->w_topline)
3317 filler_lines = wp->w_topfill;
3318 filler_todo = filler_lines;
3319#endif
3320
3321#ifdef LINE_ATTR
3322# ifdef FEAT_SIGNS
3323 /* If this line has a sign with line highlighting set line_attr. */
3324 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3325 if (v != 0)
3326 line_attr = sign_get_attr((int)v, TRUE);
3327# endif
3328# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3329 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003330 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 line_attr = hl_attr(HLF_L);
3332# endif
3333 if (line_attr != 0)
3334 area_highlighting = TRUE;
3335#endif
3336
3337 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3338 ptr = line;
3339
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003340#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003341 if (has_spell)
3342 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003343 /* For checking first word with a capital skip white space. */
3344 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003345 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003346
Bram Moolenaar30abd282005-06-22 22:35:10 +00003347 /* To be able to spell-check over line boundaries copy the end of the
3348 * current line into nextline[]. Above the start of the next line was
3349 * copied to nextline[SPWORDLEN]. */
3350 if (nextline[SPWORDLEN] == NUL)
3351 {
3352 /* No next line or it is empty. */
3353 nextlinecol = MAXCOL;
3354 nextline_idx = 0;
3355 }
3356 else
3357 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003358 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003359 if (v < SPWORDLEN)
3360 {
3361 /* Short line, use it completely and append the start of the
3362 * next line. */
3363 nextlinecol = 0;
3364 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003365 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003366 nextline_idx = v + 1;
3367 }
3368 else
3369 {
3370 /* Long line, use only the last SPWORDLEN bytes. */
3371 nextlinecol = v - SPWORDLEN;
3372 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3373 nextline_idx = SPWORDLEN + 1;
3374 }
3375 }
3376 }
3377#endif
3378
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003379 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003381 if (lcs_space || lcs_trail)
3382 extra_check = TRUE;
3383 /* find start of trailing whitespace */
3384 if (lcs_trail)
3385 {
3386 trailcol = (colnr_T)STRLEN(ptr);
3387 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3388 --trailcol;
3389 trailcol += (colnr_T) (ptr - line);
3390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
3392
3393 /*
3394 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3395 * first character to be displayed.
3396 */
3397 if (wp->w_p_wrap)
3398 v = wp->w_skipcol;
3399 else
3400 v = wp->w_leftcol;
3401 if (v > 0)
3402 {
3403#ifdef FEAT_MBYTE
3404 char_u *prev_ptr = ptr;
3405#endif
3406 while (vcol < v && *ptr != NUL)
3407 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003408 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 vcol += c;
3410#ifdef FEAT_MBYTE
3411 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003413 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 }
3415
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003416 /* When:
3417 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003418 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003419 * - 'virtualedit' is set, or
3420 * - the visual mode is active,
3421 * the end of the line may be before the start of the displayed part.
3422 */
3423 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003424#ifdef FEAT_SYN_HL
3425 wp->w_p_cuc || draw_color_col ||
3426#endif
3427#ifdef FEAT_VIRTUALEDIT
3428 virtual_active() ||
3429#endif
3430 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435 /* Handle a character that's not completely on the screen: Put ptr at
3436 * that character but skip the first few screen characters. */
3437 if (vcol > v)
3438 {
3439 vcol -= c;
3440#ifdef FEAT_MBYTE
3441 ptr = prev_ptr;
3442#else
3443 --ptr;
3444#endif
3445 n_skip = v - vcol;
3446 }
3447
3448 /*
3449 * Adjust for when the inverted text is before the screen,
3450 * and when the start of the inverted text is before the screen.
3451 */
3452 if (tocol <= vcol)
3453 fromcol = 0;
3454 else if (fromcol >= 0 && fromcol < vcol)
3455 fromcol = vcol;
3456
3457#ifdef FEAT_LINEBREAK
3458 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3459 if (wp->w_p_wrap)
3460 need_showbreak = TRUE;
3461#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003462#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003463 /* When spell checking a word we need to figure out the start of the
3464 * word and if it's badly spelled or not. */
3465 if (has_spell)
3466 {
3467 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003468 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003469 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003470
3471 pos = wp->w_cursor;
3472 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003473 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003474 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003475
3476 /* spell_move_to() may call ml_get() and make "line" invalid */
3477 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3478 ptr = line + linecol;
3479
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003480 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003481 {
3482 /* no bad word found at line start, don't check until end of a
3483 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003484 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003485 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003486 }
3487 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003488 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003489 /* bad word found, use attributes until end of word */
3490 word_end = wp->w_cursor.col + len + 1;
3491
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003492 /* Turn index into actual attributes. */
3493 if (spell_hlf != HLF_COUNT)
3494 spell_attr = highlight_attr[spell_hlf];
3495 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003496 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003497
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003498# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003499 /* Need to restart syntax highlighting for this line. */
3500 if (has_syntax)
3501 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003502# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003503 }
3504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506
3507 /*
3508 * Correct highlighting for cursor that can't be disabled.
3509 * Avoids having to check this for each character.
3510 */
3511 if (fromcol >= 0)
3512 {
3513 if (noinvcur)
3514 {
3515 if ((colnr_T)fromcol == wp->w_virtcol)
3516 {
3517 /* highlighting starts at cursor, let it start just after the
3518 * cursor */
3519 fromcol_prev = fromcol;
3520 fromcol = -1;
3521 }
3522 else if ((colnr_T)fromcol < wp->w_virtcol)
3523 /* restart highlighting after the cursor */
3524 fromcol_prev = wp->w_virtcol;
3525 }
3526 if (fromcol >= tocol)
3527 fromcol = -1;
3528 }
3529
3530#ifdef FEAT_SEARCH_EXTRA
3531 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003532 * Handle highlighting the last used search pattern and matches.
3533 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003535 cur = wp->w_match_head;
3536 shl_flag = FALSE;
3537 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003539 if (shl_flag == FALSE)
3540 {
3541 shl = &search_hl;
3542 shl_flag = TRUE;
3543 }
3544 else
3545 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003546 shl->startcol = MAXCOL;
3547 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003549 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003550 v = (long)(ptr - line);
3551 if (cur != NULL)
3552 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003553 next_search_hl(wp, shl, lnum, (colnr_T)v,
3554 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003555
3556 /* Need to get the line again, a multi-line regexp may have made it
3557 * invalid. */
3558 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3559 ptr = line + v;
3560
3561 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003563 if (shl->lnum == lnum)
3564 shl->startcol = shl->rm.startpos[0].col;
3565 else
3566 shl->startcol = 0;
3567 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3568 - shl->rm.startpos[0].lnum)
3569 shl->endcol = shl->rm.endpos[0].col;
3570 else
3571 shl->endcol = MAXCOL;
3572 /* Highlight one character for an empty match. */
3573 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003576 if (has_mbyte && line[shl->endcol] != NUL)
3577 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3578 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003580 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003582 if ((long)shl->startcol < v) /* match at leftcol */
3583 {
3584 shl->attr_cur = shl->attr;
3585 search_attr = shl->attr;
3586 }
3587 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003589 if (shl != &search_hl && cur != NULL)
3590 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
3592#endif
3593
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003594#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003595 /* Cursor line highlighting for 'cursorline' in the current window. Not
3596 * when Visual mode is active, because it's not clear what is selected
3597 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003598 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003599 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003600 {
3601 line_attr = hl_attr(HLF_CUL);
3602 area_highlighting = TRUE;
3603 }
3604#endif
3605
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003606 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 col = 0;
3608#ifdef FEAT_RIGHTLEFT
3609 if (wp->w_p_rl)
3610 {
3611 /* Rightleft window: process the text in the normal direction, but put
3612 * it in current_ScreenLine[] from right to left. Start at the
3613 * rightmost column of the window. */
3614 col = W_WIDTH(wp) - 1;
3615 off += col;
3616 }
3617#endif
3618
3619 /*
3620 * Repeat for the whole displayed line.
3621 */
3622 for (;;)
3623 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003624#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003625 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 /* Skip this quickly when working on the text. */
3628 if (draw_state != WL_LINE)
3629 {
3630#ifdef FEAT_CMDWIN
3631 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3632 {
3633 draw_state = WL_CMDLINE;
3634 if (cmdwin_type != 0 && wp == curwin)
3635 {
3636 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003638 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 char_attr = hl_attr(HLF_AT);
3640 }
3641 }
3642#endif
3643
3644#ifdef FEAT_FOLDING
3645 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3646 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003647 int fdc = compute_foldcolumn(wp, 0);
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003650 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003652 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003653 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003654 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003655 p_extra_free = alloc(12 + 1);
3656
3657 if (p_extra_free != NULL)
3658 {
3659 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3660 n_extra = fdc;
3661 p_extra_free[n_extra] = NUL;
3662 p_extra = p_extra_free;
3663 c_extra = NUL;
3664 char_attr = hl_attr(HLF_FC);
3665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667 }
3668#endif
3669
3670#ifdef FEAT_SIGNS
3671 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3672 {
3673 draw_state = WL_SIGN;
3674 /* Show the sign column when there are any signs in this
3675 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003676 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003678 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003680 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681# endif
3682
3683 /* Draw two cells with the sign value or blank. */
3684 c_extra = ' ';
3685 char_attr = hl_attr(HLF_SC);
3686 n_extra = 2;
3687
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003688 if (row == startrow
3689#ifdef FEAT_DIFF
3690 + filler_lines && filler_todo <= 0
3691#endif
3692 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
3694 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3695 SIGN_TEXT);
3696# ifdef FEAT_SIGN_ICONS
3697 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3698 SIGN_ICON);
3699 if (gui.in_use && icon_sign != 0)
3700 {
3701 /* Use the image in this position. */
3702 c_extra = SIGN_BYTE;
3703# ifdef FEAT_NETBEANS_INTG
3704 if (buf_signcount(wp->w_buffer, lnum) > 1)
3705 c_extra = MULTISIGN_BYTE;
3706# endif
3707 char_attr = icon_sign;
3708 }
3709 else
3710# endif
3711 if (text_sign != 0)
3712 {
3713 p_extra = sign_get_text(text_sign);
3714 if (p_extra != NULL)
3715 {
3716 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003717 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
3719 char_attr = sign_get_attr(text_sign, FALSE);
3720 }
3721 }
3722 }
3723 }
3724#endif
3725
3726 if (draw_state == WL_NR - 1 && n_extra == 0)
3727 {
3728 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003729 /* Display the absolute or relative line number. After the
3730 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3731 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 && (row == startrow
3733#ifdef FEAT_DIFF
3734 + filler_lines
3735#endif
3736 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3737 {
3738 /* Draw the line number (empty space after wrapping). */
3739 if (row == startrow
3740#ifdef FEAT_DIFF
3741 + filler_lines
3742#endif
3743 )
3744 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003745 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003746 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003747
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003748 if (wp->w_p_nu && !wp->w_p_rnu)
3749 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003750 num = (long)lnum;
3751 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003752 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003753 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003754 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003755 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003756 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003757 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003758 num = lnum;
3759 fmt = "%-*ld ";
3760 }
3761 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003762
Bram Moolenaar700e7342013-01-30 12:31:36 +01003763 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003764 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 if (wp->w_skipcol > 0)
3766 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3767 *p_extra = '-';
3768#ifdef FEAT_RIGHTLEFT
3769 if (wp->w_p_rl) /* reverse line numbers */
3770 rl_mirror(extra);
3771#endif
3772 p_extra = extra;
3773 c_extra = NUL;
3774 }
3775 else
3776 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003777 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003779#ifdef FEAT_SYN_HL
3780 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003781 * the current line differently.
3782 * TODO: Can we use CursorLine instead of CursorLineNr
3783 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003784 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003785 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003786 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789 }
3790
Bram Moolenaar597a4222014-06-25 14:39:50 +02003791#ifdef FEAT_LINEBREAK
3792 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3793 && n_extra == 0 && *p_sbr != NUL)
3794 /* draw indent after showbreak value */
3795 draw_state = WL_BRI;
3796 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3797 /* After the showbreak, draw the breakindent */
3798 draw_state = WL_BRI - 1;
3799
3800 /* draw 'breakindent': indent wrapped text accordingly */
3801 if (draw_state == WL_BRI - 1 && n_extra == 0)
3802 {
3803 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003804 /* if need_showbreak is set, breakindent also applies */
3805 if (wp->w_p_bri && n_extra == 0
3806 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003807# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003808 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003809# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003810 )
3811 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003812 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003813# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003814 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003815 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003816 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003817# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003818 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3819 char_attr = hl_combine_attr(char_attr,
3820 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003821# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003822 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003823# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003824 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003825 c_extra = ' ';
3826 n_extra = get_breakindent_win(wp,
3827 ml_get_buf(wp->w_buffer, lnum, FALSE));
3828 /* Correct end of highlighted area for 'breakindent',
3829 * required when 'linebreak' is also set. */
3830 if (tocol == vcol)
3831 tocol += n_extra;
3832 }
3833 }
3834#endif
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3837 if (draw_state == WL_SBR - 1 && n_extra == 0)
3838 {
3839 draw_state = WL_SBR;
3840# ifdef FEAT_DIFF
3841 if (filler_todo > 0)
3842 {
3843 /* Draw "deleted" diff line(s). */
3844 if (char2cells(fill_diff) > 1)
3845 c_extra = '-';
3846 else
3847 c_extra = fill_diff;
3848# ifdef FEAT_RIGHTLEFT
3849 if (wp->w_p_rl)
3850 n_extra = col + 1;
3851 else
3852# endif
3853 n_extra = W_WIDTH(wp) - col;
3854 char_attr = hl_attr(HLF_DED);
3855 }
3856# endif
3857# ifdef FEAT_LINEBREAK
3858 if (*p_sbr != NUL && need_showbreak)
3859 {
3860 /* Draw 'showbreak' at the start of each broken line. */
3861 p_extra = p_sbr;
3862 c_extra = NUL;
3863 n_extra = (int)STRLEN(p_sbr);
3864 char_attr = hl_attr(HLF_AT);
3865 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003866 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 /* Correct end of highlighted area for 'showbreak',
3868 * required when 'linebreak' is also set. */
3869 if (tocol == vcol)
3870 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003871#ifdef FEAT_SYN_HL
3872 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003873 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003874 char_attr = hl_combine_attr(char_attr,
3875 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 }
3878# endif
3879 }
3880#endif
3881
3882 if (draw_state == WL_LINE - 1 && n_extra == 0)
3883 {
3884 draw_state = WL_LINE;
3885 if (saved_n_extra)
3886 {
3887 /* Continue item from end of wrapped line. */
3888 n_extra = saved_n_extra;
3889 c_extra = saved_c_extra;
3890 p_extra = saved_p_extra;
3891 char_attr = saved_char_attr;
3892 }
3893 else
3894 char_attr = 0;
3895 }
3896 }
3897
3898 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003899 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003900 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901#ifdef FEAT_DIFF
3902 && filler_todo <= 0
3903#endif
3904 )
3905 {
3906 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3907 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003908 /* Pretend we have finished updating the window. Except when
3909 * 'cursorcolumn' is set. */
3910#ifdef FEAT_SYN_HL
3911 if (wp->w_p_cuc)
3912 row = wp->w_cline_row + wp->w_cline_height;
3913 else
3914#endif
3915 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 break;
3917 }
3918
3919 if (draw_state == WL_LINE && area_highlighting)
3920 {
3921 /* handle Visual or match highlighting in this line */
3922 if (vcol == fromcol
3923#ifdef FEAT_MBYTE
3924 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3925 && (*mb_ptr2cells)(ptr) > 1)
3926#endif
3927 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003928 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 && vcol < tocol))
3930 area_attr = attr; /* start highlighting */
3931 else if (area_attr != 0
3932 && (vcol == tocol
3933 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
3936#ifdef FEAT_SEARCH_EXTRA
3937 if (!n_extra)
3938 {
3939 /*
3940 * Check for start/end of search pattern match.
3941 * After end, check for start/end of next match.
3942 * When another match, have to check for start again.
3943 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003944 * Do this for 'search_hl' and the match list (ordered by
3945 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003947 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003948 cur = wp->w_match_head;
3949 shl_flag = FALSE;
3950 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003952 if (shl_flag == FALSE
3953 && ((cur != NULL
3954 && cur->priority > SEARCH_HL_PRIORITY)
3955 || cur == NULL))
3956 {
3957 shl = &search_hl;
3958 shl_flag = TRUE;
3959 }
3960 else
3961 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003962 if (cur != NULL)
3963 cur->pos.cur = 0;
3964 pos_inprogress = TRUE;
3965 while (shl->rm.regprog != NULL
3966 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003968 if (shl->startcol != MAXCOL
3969 && v >= (long)shl->startcol
3970 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003972#ifdef FEAT_MBYTE
3973 int tmp_col = v + MB_PTR2LEN(ptr);
3974
3975 if (shl->endcol < tmp_col)
3976 shl->endcol = tmp_col;
3977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003979#ifdef FEAT_CONCEAL
3980 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3981 == cur->hlg_id)
3982 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02003983 has_match_conc =
3984 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003985 match_conc = cur->conceal_char;
3986 }
3987 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02003988 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003991 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 {
3993 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003994 next_search_hl(wp, shl, lnum, (colnr_T)v,
3995 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003996 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003997 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999 /* Need to get the line again, a multi-line regexp
4000 * may have made it invalid. */
4001 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4002 ptr = line + v;
4003
4004 if (shl->lnum == lnum)
4005 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004006 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004008 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004010 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004012 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 {
4014 /* highlight empty match, try again after
4015 * it */
4016#ifdef FEAT_MBYTE
4017 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004018 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004019 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 else
4021#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004022 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
4024
4025 /* Loop to check if the match starts at the
4026 * current position */
4027 continue;
4028 }
4029 }
4030 break;
4031 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004032 if (shl != &search_hl && cur != NULL)
4033 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004035
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004036 /* Use attributes from match with highest priority among
4037 * 'search_hl' and the match list. */
4038 search_attr = search_hl.attr_cur;
4039 cur = wp->w_match_head;
4040 shl_flag = FALSE;
4041 while (cur != NULL || shl_flag == FALSE)
4042 {
4043 if (shl_flag == FALSE
4044 && ((cur != NULL
4045 && cur->priority > SEARCH_HL_PRIORITY)
4046 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004047 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004048 shl = &search_hl;
4049 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004050 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004051 else
4052 shl = &cur->hl;
4053 if (shl->attr_cur != 0)
4054 search_attr = shl->attr_cur;
4055 if (shl != &search_hl && cur != NULL)
4056 cur = cur->next;
4057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
4059#endif
4060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004062 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004064 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4065 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004067 if (diff_hlf == HLF_TXD && ptr - line > change_end
4068 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004070 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004071 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4072 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004075
4076 /* Decide which of the highlight attributes to use. */
4077 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004078#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004079 if (area_attr != 0)
4080 char_attr = hl_combine_attr(line_attr, area_attr);
4081 else if (search_attr != 0)
4082 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004083 /* Use line_attr when not in the Visual or 'incsearch' area
4084 * (area_attr may be 0 when "noinvcur" is set). */
4085 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004086 || vcol < fromcol || vcol_prev < fromcol_prev
4087 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004088 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004089#else
4090 if (area_attr != 0)
4091 char_attr = area_attr;
4092 else if (search_attr != 0)
4093 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004094#endif
4095 else
4096 {
4097 attr_pri = FALSE;
4098#ifdef FEAT_SYN_HL
4099 if (has_syntax)
4100 char_attr = syntax_attr;
4101 else
4102#endif
4103 char_attr = 0;
4104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106
4107 /*
4108 * Get the next character to put on the screen.
4109 */
4110 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004111 * The "p_extra" points to the extra stuff that is inserted to
4112 * represent special characters (non-printable stuff) and other
4113 * things. When all characters are the same, c_extra is used.
4114 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4115 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4117 */
4118 if (n_extra > 0)
4119 {
4120 if (c_extra != NUL)
4121 {
4122 c = c_extra;
4123#ifdef FEAT_MBYTE
4124 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4125 if (enc_utf8 && (*mb_char2len)(c) > 1)
4126 {
4127 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004128 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004129 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131 else
4132 mb_utf8 = FALSE;
4133#endif
4134 }
4135 else
4136 {
4137 c = *p_extra;
4138#ifdef FEAT_MBYTE
4139 if (has_mbyte)
4140 {
4141 mb_c = c;
4142 if (enc_utf8)
4143 {
4144 /* If the UTF-8 character is more than one byte:
4145 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004146 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 mb_utf8 = FALSE;
4148 if (mb_l > n_extra)
4149 mb_l = 1;
4150 else if (mb_l > 1)
4151 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004152 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004154 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156 }
4157 else
4158 {
4159 /* if this is a DBCS character, put it in "mb_c" */
4160 mb_l = MB_BYTE2LEN(c);
4161 if (mb_l >= n_extra)
4162 mb_l = 1;
4163 else if (mb_l > 1)
4164 mb_c = (c << 8) + p_extra[1];
4165 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004166 if (mb_l == 0) /* at the NUL at end-of-line */
4167 mb_l = 1;
4168
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 /* If a double-width char doesn't fit display a '>' in the
4170 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004171 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172# ifdef FEAT_RIGHTLEFT
4173 wp->w_p_rl ? (col <= 0) :
4174# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004175 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 && (*mb_char2cells)(mb_c) == 2)
4177 {
4178 c = '>';
4179 mb_c = c;
4180 mb_l = 1;
4181 mb_utf8 = FALSE;
4182 multi_attr = hl_attr(HLF_AT);
4183 /* put the pointer back to output the double-width
4184 * character at the start of the next line. */
4185 ++n_extra;
4186 --p_extra;
4187 }
4188 else
4189 {
4190 n_extra -= mb_l - 1;
4191 p_extra += mb_l - 1;
4192 }
4193 }
4194#endif
4195 ++p_extra;
4196 }
4197 --n_extra;
4198 }
4199 else
4200 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004201 if (p_extra_free != NULL)
4202 {
4203 vim_free(p_extra_free);
4204 p_extra_free = NULL;
4205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 /*
4207 * Get a character from the line itself.
4208 */
4209 c = *ptr;
4210#ifdef FEAT_MBYTE
4211 if (has_mbyte)
4212 {
4213 mb_c = c;
4214 if (enc_utf8)
4215 {
4216 /* If the UTF-8 character is more than one byte: Decode it
4217 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004218 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 mb_utf8 = FALSE;
4220 if (mb_l > 1)
4221 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004222 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 /* Overlong encoded ASCII or ASCII with composing char
4224 * is displayed normally, except a NUL. */
4225 if (mb_c < 0x80)
4226 c = mb_c;
4227 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004228
4229 /* At start of the line we can have a composing char.
4230 * Draw it as a space with a composing char. */
4231 if (utf_iscomposing(mb_c))
4232 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004233 int i;
4234
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004235 for (i = Screen_mco - 1; i > 0; --i)
4236 u8cc[i] = u8cc[i - 1];
4237 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004238 mb_c = ' ';
4239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241
4242 if ((mb_l == 1 && c >= 0x80)
4243 || (mb_l >= 1 && mb_c == 0)
4244 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004245# ifdef UNICODE16
4246 || mb_c >= 0x10000
4247# endif
4248 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 /*
4251 * Illegal UTF-8 byte: display as <xx>.
4252 * Non-BMP character : display as ? or fullwidth ?.
4253 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004254# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 {
4258 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004259# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (wp->w_p_rl) /* reverse */
4261 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004262# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004264# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 else if (utf_char2cells(mb_c) != 2)
4266 STRCPY(extra, "?");
4267 else
4268 /* 0xff1f in UTF-8: full-width '?' */
4269 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
4272 p_extra = extra;
4273 c = *p_extra;
4274 mb_c = mb_ptr2char_adv(&p_extra);
4275 mb_utf8 = (c >= 0x80);
4276 n_extra = (int)STRLEN(p_extra);
4277 c_extra = NUL;
4278 if (area_attr == 0 && search_attr == 0)
4279 {
4280 n_attr = n_extra + 1;
4281 extra_attr = hl_attr(HLF_8);
4282 saved_attr2 = char_attr; /* save current attr */
4283 }
4284 }
4285 else if (mb_l == 0) /* at the NUL at end-of-line */
4286 mb_l = 1;
4287#ifdef FEAT_ARABIC
4288 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4289 {
4290 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004291 int pc, pc1, nc;
4292 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
4294 /* The idea of what is the previous and next
4295 * character depends on 'rightleft'. */
4296 if (wp->w_p_rl)
4297 {
4298 pc = prev_c;
4299 pc1 = prev_c1;
4300 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004301 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 else
4304 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004305 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004307 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
4309 prev_c = mb_c;
4310
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004311 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313 else
4314 prev_c = mb_c;
4315#endif
4316 }
4317 else /* enc_dbcs */
4318 {
4319 mb_l = MB_BYTE2LEN(c);
4320 if (mb_l == 0) /* at the NUL at end-of-line */
4321 mb_l = 1;
4322 else if (mb_l > 1)
4323 {
4324 /* We assume a second byte below 32 is illegal.
4325 * Hopefully this is OK for all double-byte encodings!
4326 */
4327 if (ptr[1] >= 32)
4328 mb_c = (c << 8) + ptr[1];
4329 else
4330 {
4331 if (ptr[1] == NUL)
4332 {
4333 /* head byte at end of line */
4334 mb_l = 1;
4335 transchar_nonprint(extra, c);
4336 }
4337 else
4338 {
4339 /* illegal tail byte */
4340 mb_l = 2;
4341 STRCPY(extra, "XX");
4342 }
4343 p_extra = extra;
4344 n_extra = (int)STRLEN(extra) - 1;
4345 c_extra = NUL;
4346 c = *p_extra++;
4347 if (area_attr == 0 && search_attr == 0)
4348 {
4349 n_attr = n_extra + 1;
4350 extra_attr = hl_attr(HLF_8);
4351 saved_attr2 = char_attr; /* save current attr */
4352 }
4353 mb_c = c;
4354 }
4355 }
4356 }
4357 /* If a double-width char doesn't fit display a '>' in the
4358 * last column; the character is displayed at the start of the
4359 * next line. */
4360 if ((
4361# ifdef FEAT_RIGHTLEFT
4362 wp->w_p_rl ? (col <= 0) :
4363# endif
4364 (col >= W_WIDTH(wp) - 1))
4365 && (*mb_char2cells)(mb_c) == 2)
4366 {
4367 c = '>';
4368 mb_c = c;
4369 mb_utf8 = FALSE;
4370 mb_l = 1;
4371 multi_attr = hl_attr(HLF_AT);
4372 /* Put pointer back so that the character will be
4373 * displayed at the start of the next line. */
4374 --ptr;
4375 }
4376 else if (*ptr != NUL)
4377 ptr += mb_l - 1;
4378
4379 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004380 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004381 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004382 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004385 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 c = ' ';
4387 if (area_attr == 0 && search_attr == 0)
4388 {
4389 n_attr = n_extra + 1;
4390 extra_attr = hl_attr(HLF_AT);
4391 saved_attr2 = char_attr; /* save current attr */
4392 }
4393 mb_c = c;
4394 mb_utf8 = FALSE;
4395 mb_l = 1;
4396 }
4397
4398 }
4399#endif
4400 ++ptr;
4401
4402 if (extra_check)
4403 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004404#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004405 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004406#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004407
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004408#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 /* Get syntax attribute, unless still at the start of the line
4410 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004411 v = (long)(ptr - line);
4412 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
4414 /* Get the syntax attribute for the character. If there
4415 * is an error, disable syntax highlighting. */
4416 save_did_emsg = did_emsg;
4417 did_emsg = FALSE;
4418
Bram Moolenaar217ad922005-03-20 22:37:15 +00004419 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004420# ifdef FEAT_SPELL
4421 has_spell ? &can_spell :
4422# endif
4423 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004426 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004427 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004428 has_syntax = FALSE;
4429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 else
4431 did_emsg = save_did_emsg;
4432
4433 /* Need to get the line again, a multi-line regexp may
4434 * have made it invalid. */
4435 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4436 ptr = line + v;
4437
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004438 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004440 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004441 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004442# ifdef FEAT_CONCEAL
4443 /* no concealing past the end of the line, it interferes
4444 * with line highlighting */
4445 if (c == NUL)
4446 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004447 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004448 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004451#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004452
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004453#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004454 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004455 * Only do this when there is no syntax highlighting, the
4456 * @Spell cluster is not used or the current syntax item
4457 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004458 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004459 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004460 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004461# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004462 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004463 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004464# endif
4465 if (c != 0 && (
4466# ifdef FEAT_SYN_HL
4467 !has_syntax ||
4468# endif
4469 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004470 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004471 char_u *prev_ptr, *p;
4472 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004473 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004474# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004475 if (has_mbyte)
4476 {
4477 prev_ptr = ptr - mb_l;
4478 v -= mb_l - 1;
4479 }
4480 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004481# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004482 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004483
4484 /* Use nextline[] if possible, it has the start of the
4485 * next line concatenated. */
4486 if ((prev_ptr - line) - nextlinecol >= 0)
4487 p = nextline + (prev_ptr - line) - nextlinecol;
4488 else
4489 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004490 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004491 len = spell_check(wp, p, &spell_hlf, &cap_col,
4492 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004493 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004494
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004495 /* In Insert mode only highlight a word that
4496 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004497 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004498 && (State & INSERT) != 0
4499 && wp->w_cursor.lnum == lnum
4500 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004501 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004502 && wp->w_cursor.col < (colnr_T)word_end)
4503 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004504 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004505 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004506 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004507
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004508 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004509 && (p - nextline) + len > nextline_idx)
4510 {
4511 /* Remember that the good word continues at the
4512 * start of the next line. */
4513 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004514 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004515 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004516
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004517 /* Turn index into actual attributes. */
4518 if (spell_hlf != HLF_COUNT)
4519 spell_attr = highlight_attr[spell_hlf];
4520
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004521 if (cap_col > 0)
4522 {
4523 if (p != prev_ptr
4524 && (p - nextline) + cap_col >= nextline_idx)
4525 {
4526 /* Remember that the word in the next line
4527 * must start with a capital. */
4528 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004529 cap_col = (int)((p - nextline) + cap_col
4530 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004531 }
4532 else
4533 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004534 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004535 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004536 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004537 }
4538 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004539 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004540 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004541 char_attr = hl_combine_attr(char_attr, spell_attr);
4542 else
4543 char_attr = hl_combine_attr(spell_attr, char_attr);
4544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545#endif
4546#ifdef FEAT_LINEBREAK
4547 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004548 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004550 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004552# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004553 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004554# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004555 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004557 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004559 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004560
Bram Moolenaar597a4222014-06-25 14:39:50 +02004561 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004562 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004563 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004564 if (c == TAB && n_extra + col > W_WIDTH(wp))
4565 n_extra = (int)wp->w_buffer->b_p_ts
4566 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4567
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004568# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004569 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004570# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004574 {
4575#ifdef FEAT_CONCEAL
4576 if (c == TAB)
4577 /* See "Tab alignment" below. */
4578 FIX_FOR_BOGUSCOLS;
4579#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004580 if (!wp->w_p_list)
4581 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
4584#endif
4585
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004586 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4587 */
4588 if (wp->w_p_list
4589 && (((c == 160
4590#ifdef FEAT_MBYTE
4591 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4592#endif
4593 ) && lcs_nbsp)
4594 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4595 {
4596 c = (c == ' ') ? lcs_space : lcs_nbsp;
4597 if (area_attr == 0 && search_attr == 0)
4598 {
4599 n_attr = 1;
4600 extra_attr = hl_attr(HLF_8);
4601 saved_attr2 = char_attr; /* save current attr */
4602 }
4603#ifdef FEAT_MBYTE
4604 mb_c = c;
4605 if (enc_utf8 && (*mb_char2len)(c) > 1)
4606 {
4607 mb_utf8 = TRUE;
4608 u8cc[0] = 0;
4609 c = 0xc0;
4610 }
4611 else
4612 mb_utf8 = FALSE;
4613#endif
4614 }
4615
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4617 {
4618 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004619 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
4621 n_attr = 1;
4622 extra_attr = hl_attr(HLF_8);
4623 saved_attr2 = char_attr; /* save current attr */
4624 }
4625#ifdef FEAT_MBYTE
4626 mb_c = c;
4627 if (enc_utf8 && (*mb_char2len)(c) > 1)
4628 {
4629 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004630 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004631 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
4633 else
4634 mb_utf8 = FALSE;
4635#endif
4636 }
4637 }
4638
4639 /*
4640 * Handling of non-printable characters.
4641 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004642 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
4644 /*
4645 * when getting a character from the file, we may have to
4646 * turn it into something else on the way to putting it
4647 * into "ScreenLines".
4648 */
4649 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4650 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004651 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004652 long vcol_adjusted = vcol; /* removed showbreak length */
4653#ifdef FEAT_LINEBREAK
4654 /* only adjust the tab_len, when at the first column
4655 * after the showbreak value was drawn */
4656 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4657 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004660 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004661 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4662
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004663#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004664 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004665#endif
4666 /* tab amount depends on current column */
4667 n_extra = tab_len;
4668#ifdef FEAT_LINEBREAK
4669 else
4670 {
4671 char_u *p;
4672 int len = n_extra;
4673 int i;
4674 int saved_nextra = n_extra;
4675
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004676#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004677 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004678 /* there are characters to conceal */
4679 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004680 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4681 */
4682 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4683 && n_extra > tab_len)
4684 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004685#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004686
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004687 /* if n_extra > 0, it gives the number of chars, to
4688 * use for a tab, else we need to calculate the width
4689 * for a tab */
4690#ifdef FEAT_MBYTE
4691 len = (tab_len * mb_char2len(lcs_tab2));
4692 if (n_extra > 0)
4693 len += n_extra - tab_len;
4694#endif
4695 c = lcs_tab1;
4696 p = alloc((unsigned)(len + 1));
4697 vim_memset(p, ' ', len);
4698 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004699 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004700 p_extra_free = p;
4701 for (i = 0; i < tab_len; i++)
4702 {
4703#ifdef FEAT_MBYTE
4704 mb_char2bytes(lcs_tab2, p);
4705 p += mb_char2len(lcs_tab2);
4706 n_extra += mb_char2len(lcs_tab2)
4707 - (saved_nextra > 0 ? 1 : 0);
4708#else
4709 p[i] = lcs_tab2;
4710#endif
4711 }
4712 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004713#ifdef FEAT_CONCEAL
4714 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4715 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004716 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004717 n_extra -= vcol_off;
4718#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004719 }
4720#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004721#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004722 {
4723 int vc_saved = vcol_off;
4724
4725 /* Tab alignment should be identical regardless of
4726 * 'conceallevel' value. So tab compensates of all
4727 * previous concealed characters, and thus resets
4728 * vcol_off and boguscols accumulated so far in the
4729 * line. Note that the tab can be longer than
4730 * 'tabstop' when there are concealed characters. */
4731 FIX_FOR_BOGUSCOLS;
4732
4733 /* Make sure, the highlighting for the tab char will be
4734 * correctly set further below (effectively reverts the
4735 * FIX_FOR_BOGSUCOLS macro */
4736 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004737 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004738 tab_len += vc_saved;
4739 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741#ifdef FEAT_MBYTE
4742 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4743#endif
4744 if (wp->w_p_list)
4745 {
4746 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004747#ifdef FEAT_LINEBREAK
4748 if (wp->w_p_lbr)
4749 c_extra = NUL; /* using p_extra from above */
4750 else
4751#endif
4752 c_extra = lcs_tab2;
4753 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 extra_attr = hl_attr(HLF_8);
4755 saved_attr2 = char_attr; /* save current attr */
4756#ifdef FEAT_MBYTE
4757 mb_c = c;
4758 if (enc_utf8 && (*mb_char2len)(c) > 1)
4759 {
4760 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004761 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004762 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
4764#endif
4765 }
4766 else
4767 {
4768 c_extra = ' ';
4769 c = ' ';
4770 }
4771 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004772 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004773 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004774 || ((fromcol >= 0 || fromcol_prev >= 0)
4775 && tocol > vcol
4776 && VIsual_mode != Ctrl_V
4777 && (
4778# ifdef FEAT_RIGHTLEFT
4779 wp->w_p_rl ? (col >= 0) :
4780# endif
4781 (col < W_WIDTH(wp)))
4782 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004783 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004784 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004785 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004787 /* Display a '$' after the line or highlight an extra
4788 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4790 /* For a diff line the highlighting continues after the
4791 * "$". */
4792 if (
4793# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004794 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795# ifdef LINE_ATTR
4796 &&
4797# endif
4798# endif
4799# ifdef LINE_ATTR
4800 line_attr == 0
4801# endif
4802 )
4803#endif
4804 {
4805#ifdef FEAT_VIRTUALEDIT
4806 /* In virtualedit, visual selections may extend
4807 * beyond end of line. */
4808 if (area_highlighting && virtual_active()
4809 && tocol != MAXCOL && vcol < tocol)
4810 n_extra = 0;
4811 else
4812#endif
4813 {
4814 p_extra = at_end_str;
4815 n_extra = 1;
4816 c_extra = NUL;
4817 }
4818 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004819 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004820 c = lcs_eol;
4821 else
4822 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 lcs_eol_one = -1;
4824 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004825 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 {
4827 extra_attr = hl_attr(HLF_AT);
4828 n_attr = 1;
4829 }
4830#ifdef FEAT_MBYTE
4831 mb_c = c;
4832 if (enc_utf8 && (*mb_char2len)(c) > 1)
4833 {
4834 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004835 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004836 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838 else
4839 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4840#endif
4841 }
4842 else if (c != NUL)
4843 {
4844 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004845 if (n_extra == 0)
4846 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847#ifdef FEAT_RIGHTLEFT
4848 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4849 rl_mirror(p_extra); /* reverse "<12>" */
4850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004852#ifdef FEAT_LINEBREAK
4853 if (wp->w_p_lbr)
4854 {
4855 char_u *p;
4856
4857 c = *p_extra;
4858 p = alloc((unsigned)n_extra + 1);
4859 vim_memset(p, ' ', n_extra);
4860 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4861 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004862 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004863 p_extra_free = p_extra = p;
4864 }
4865 else
4866#endif
4867 {
4868 n_extra = byte2cells(c) - 1;
4869 c = *p_extra++;
4870 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004871 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
4873 n_attr = n_extra + 1;
4874 extra_attr = hl_attr(HLF_8);
4875 saved_attr2 = char_attr; /* save current attr */
4876 }
4877#ifdef FEAT_MBYTE
4878 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4879#endif
4880 }
4881#ifdef FEAT_VIRTUALEDIT
4882 else if (VIsual_active
4883 && (VIsual_mode == Ctrl_V
4884 || VIsual_mode == 'v')
4885 && virtual_active()
4886 && tocol != MAXCOL
4887 && vcol < tocol
4888 && (
4889# ifdef FEAT_RIGHTLEFT
4890 wp->w_p_rl ? (col >= 0) :
4891# endif
4892 (col < W_WIDTH(wp))))
4893 {
4894 c = ' ';
4895 --ptr; /* put it back at the NUL */
4896 }
4897#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004898#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 else if ((
4900# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004901 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 ) && (
4905# ifdef FEAT_RIGHTLEFT
4906 wp->w_p_rl ? (col >= 0) :
4907# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004908 (col
4909# ifdef FEAT_CONCEAL
4910 - boguscols
4911# endif
4912 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
4914 /* Highlight until the right side of the window */
4915 c = ' ';
4916 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004917
4918 /* Remember we do the char for line highlighting. */
4919 ++did_line_attr;
4920
4921 /* don't do search HL for the rest of the line */
4922 if (line_attr != 0 && char_attr == search_attr && col > 0)
4923 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924# ifdef FEAT_DIFF
4925 if (diff_hlf == HLF_TXD)
4926 {
4927 diff_hlf = HLF_CHD;
4928 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004931 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4932 char_attr = hl_combine_attr(char_attr,
4933 hl_attr(HLF_CUL));
4934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
4936# endif
4937 }
4938#endif
4939 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004940
4941#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004942 if ( wp->w_p_cole > 0
4943 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004944 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02004945 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004946 && !(lnum_in_visual_area
4947 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004948 {
4949 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02004950 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004951 && (syn_get_sub_char() != NUL || match_conc
4952 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004953 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004954 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004955 /* First time at this concealed item: display one
4956 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004957 if (match_conc)
4958 c = match_conc;
4959 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004960 c = syn_get_sub_char();
4961 else if (lcs_conceal != NUL)
4962 c = lcs_conceal;
4963 else
4964 c = ' ';
4965
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004966 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004967
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004968 if (n_extra > 0)
4969 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004970 vcol += n_extra;
4971 if (wp->w_p_wrap && n_extra > 0)
4972 {
4973# ifdef FEAT_RIGHTLEFT
4974 if (wp->w_p_rl)
4975 {
4976 col -= n_extra;
4977 boguscols -= n_extra;
4978 }
4979 else
4980# endif
4981 {
4982 boguscols += n_extra;
4983 col += n_extra;
4984 }
4985 }
4986 n_extra = 0;
4987 n_attr = 0;
4988 }
4989 else if (n_skip == 0)
4990 {
4991 is_concealing = TRUE;
4992 n_skip = 1;
4993 }
4994# ifdef FEAT_MBYTE
4995 mb_c = c;
4996 if (enc_utf8 && (*mb_char2len)(c) > 1)
4997 {
4998 mb_utf8 = TRUE;
4999 u8cc[0] = 0;
5000 c = 0xc0;
5001 }
5002 else
5003 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5004# endif
5005 }
5006 else
5007 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005008 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005009 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005010 }
5011#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005014#ifdef FEAT_CONCEAL
5015 /* In the cursor line and we may be concealing characters: correct
5016 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005017 if (!did_wcol && draw_state == WL_LINE
5018 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005019 && conceal_cursor_line(wp)
5020 && (int)wp->w_virtcol <= vcol + n_skip)
5021 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005022# ifdef FEAT_RIGHTLEFT
5023 if (wp->w_p_rl)
5024 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5025 else
5026# endif
5027 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005028 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005029 did_wcol = TRUE;
5030 }
5031#endif
5032
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 /* Don't override visual selection highlighting. */
5034 if (n_attr > 0
5035 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005036 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 char_attr = extra_attr;
5038
Bram Moolenaar81695252004-12-29 20:58:21 +00005039#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 /* XIM don't send preedit_start and preedit_end, but they send
5041 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5042 * im_is_preediting() here. */
5043 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005044 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 && (State & INSERT)
5046 && !p_imdisable
5047 && im_is_preediting()
5048 && draw_state == WL_LINE)
5049 {
5050 colnr_T tcol;
5051
5052 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005053 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 else
5055 tcol = preedit_end_col;
5056 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5057 {
5058 if (feedback_old_attr < 0)
5059 {
5060 feedback_col = 0;
5061 feedback_old_attr = char_attr;
5062 }
5063 char_attr = im_get_feedback_attr(feedback_col);
5064 if (char_attr < 0)
5065 char_attr = feedback_old_attr;
5066 feedback_col++;
5067 }
5068 else if (feedback_old_attr >= 0)
5069 {
5070 char_attr = feedback_old_attr;
5071 feedback_old_attr = -1;
5072 feedback_col = 0;
5073 }
5074 }
5075#endif
5076 /*
5077 * Handle the case where we are in column 0 but not on the first
5078 * character of the line and the user wants us to show us a
5079 * special character (via 'listchars' option "precedes:<char>".
5080 */
5081 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005082 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5084#ifdef FEAT_DIFF
5085 && filler_todo <= 0
5086#endif
5087 && draw_state > WL_NR
5088 && c != NUL)
5089 {
5090 c = lcs_prec;
5091 lcs_prec_todo = NUL;
5092#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005093 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5094 {
5095 /* Double-width character being overwritten by the "precedes"
5096 * character, need to fill up half the character. */
5097 c_extra = MB_FILLER_CHAR;
5098 n_extra = 1;
5099 n_attr = 2;
5100 extra_attr = hl_attr(HLF_AT);
5101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 mb_c = c;
5103 if (enc_utf8 && (*mb_char2len)(c) > 1)
5104 {
5105 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005106 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005107 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 else
5110 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5111#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005112 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
5114 saved_attr3 = char_attr; /* save current attr */
5115 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5116 n_attr3 = 1;
5117 }
5118 }
5119
5120 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005121 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005123 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005124#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005125 || did_line_attr == 1
5126#endif
5127 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005129#ifdef FEAT_SEARCH_EXTRA
5130 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005131
5132 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005133 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005134 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005135#endif
5136
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005137 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 * highlight match at end of line. If it's beyond the last
5139 * char on the screen, just overwrite that one (tricky!) Not
5140 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005141#ifdef FEAT_SEARCH_EXTRA
5142 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005143 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005144 prevcol_hl_flag = TRUE;
5145 else
5146 {
5147 cur = wp->w_match_head;
5148 while (cur != NULL)
5149 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005150 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005151 {
5152 prevcol_hl_flag = TRUE;
5153 break;
5154 }
5155 cur = cur->next;
5156 }
5157 }
5158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005160 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005161 && (VIsual_mode != Ctrl_V
5162 || lnum == VIsual.lnum
5163 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005164 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165#ifdef FEAT_SEARCH_EXTRA
5166 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005167 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005168# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005169 && did_line_attr <= 1
5170# endif
5171 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172#endif
5173 ))
5174 {
5175 int n = 0;
5176
5177#ifdef FEAT_RIGHTLEFT
5178 if (wp->w_p_rl)
5179 {
5180 if (col < 0)
5181 n = 1;
5182 }
5183 else
5184#endif
5185 {
5186 if (col >= W_WIDTH(wp))
5187 n = -1;
5188 }
5189 if (n != 0)
5190 {
5191 /* At the window boundary, highlight the last character
5192 * instead (better than nothing). */
5193 off += n;
5194 col += n;
5195 }
5196 else
5197 {
5198 /* Add a blank character to highlight. */
5199 ScreenLines[off] = ' ';
5200#ifdef FEAT_MBYTE
5201 if (enc_utf8)
5202 ScreenLinesUC[off] = 0;
5203#endif
5204 }
5205#ifdef FEAT_SEARCH_EXTRA
5206 if (area_attr == 0)
5207 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005208 /* Use attributes from match with highest priority among
5209 * 'search_hl' and the match list. */
5210 char_attr = search_hl.attr;
5211 cur = wp->w_match_head;
5212 shl_flag = FALSE;
5213 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005214 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005215 if (shl_flag == FALSE
5216 && ((cur != NULL
5217 && cur->priority > SEARCH_HL_PRIORITY)
5218 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005219 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005220 shl = &search_hl;
5221 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005222 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005223 else
5224 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005225 if ((ptr - line) - 1 == (long)shl->startcol
5226 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005227 char_attr = shl->attr;
5228 if (shl != &search_hl && cur != NULL)
5229 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
5232#endif
5233 ScreenAttrs[off] = char_attr;
5234#ifdef FEAT_RIGHTLEFT
5235 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005238 --off;
5239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 else
5241#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005244 ++off;
5245 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005246 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005247#ifdef FEAT_SYN_HL
5248 eol_hl_off = 1;
5249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
Bram Moolenaar91170f82006-05-05 21:15:17 +00005253 /*
5254 * At end of the text line.
5255 */
5256 if (c == NUL)
5257 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005258#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005259 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5260 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005261 {
5262 /* highlight last char after line */
5263 --col;
5264 --off;
5265 --vcol;
5266 }
5267
Bram Moolenaar1a384422010-07-14 19:53:30 +02005268 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005269 if (wp->w_p_wrap)
5270 v = wp->w_skipcol;
5271 else
5272 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005273
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005274 /* check if line ends before left margin */
5275 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005276 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005277#ifdef FEAT_CONCEAL
5278 /* Get rid of the boguscols now, we want to draw until the right
5279 * edge for 'cursorcolumn'. */
5280 col -= boguscols;
5281 boguscols = 0;
5282#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005283
5284 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005285 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005286
5287 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005288 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5289 && (int)wp->w_virtcol <
5290 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005291 && lnum != wp->w_cursor.lnum)
5292 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005293# ifdef FEAT_RIGHTLEFT
5294 && !wp->w_p_rl
5295# endif
5296 )
5297 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005298 int rightmost_vcol = 0;
5299 int i;
5300
5301 if (wp->w_p_cuc)
5302 rightmost_vcol = wp->w_virtcol;
5303 if (draw_color_col)
5304 /* determine rightmost colorcolumn to possibly draw */
5305 for (i = 0; color_cols[i] >= 0; ++i)
5306 if (rightmost_vcol < color_cols[i])
5307 rightmost_vcol = color_cols[i];
5308
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005309 while (col < W_WIDTH(wp))
5310 {
5311 ScreenLines[off] = ' ';
5312#ifdef FEAT_MBYTE
5313 if (enc_utf8)
5314 ScreenLinesUC[off] = 0;
5315#endif
5316 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005317 if (draw_color_col)
5318 draw_color_col = advance_color_col(VCOL_HLC,
5319 &color_cols);
5320
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005321 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005322 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005323 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005324 ScreenAttrs[off++] = hl_attr(HLF_MC);
5325 else
5326 ScreenAttrs[off++] = 0;
5327
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005328 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005329 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005330
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005331 ++vcol;
5332 }
5333 }
5334#endif
5335
Bram Moolenaar860cae12010-06-05 23:22:07 +02005336 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5337 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 row++;
5339
5340 /*
5341 * Update w_cline_height and w_cline_folded if the cursor line was
5342 * updated (saves a call to plines() later).
5343 */
5344 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5345 {
5346 curwin->w_cline_row = startrow;
5347 curwin->w_cline_height = row - startrow;
5348#ifdef FEAT_FOLDING
5349 curwin->w_cline_folded = FALSE;
5350#endif
5351 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5352 }
5353
5354 break;
5355 }
5356
5357 /* line continues beyond line end */
5358 if (lcs_ext
5359 && !wp->w_p_wrap
5360#ifdef FEAT_DIFF
5361 && filler_todo <= 0
5362#endif
5363 && (
5364#ifdef FEAT_RIGHTLEFT
5365 wp->w_p_rl ? col == 0 :
5366#endif
5367 col == W_WIDTH(wp) - 1)
5368 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005369 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5371 {
5372 c = lcs_ext;
5373 char_attr = hl_attr(HLF_AT);
5374#ifdef FEAT_MBYTE
5375 mb_c = c;
5376 if (enc_utf8 && (*mb_char2len)(c) > 1)
5377 {
5378 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005379 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005380 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 }
5382 else
5383 mb_utf8 = FALSE;
5384#endif
5385 }
5386
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005387#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005388 /* advance to the next 'colorcolumn' */
5389 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005390 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005391
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005392 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005393 * highlight the cursor position itself.
5394 * Also highlight the 'colorcolumn' if it is different than
5395 * 'cursorcolumn' */
5396 vcol_save_attr = -1;
5397 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005398 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005399 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005400 && lnum != wp->w_cursor.lnum)
5401 {
5402 vcol_save_attr = char_attr;
5403 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5404 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005405 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005406 {
5407 vcol_save_attr = char_attr;
5408 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5409 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005410 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005411#endif
5412
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 /*
5414 * Store character to be displayed.
5415 * Skip characters that are left of the screen for 'nowrap'.
5416 */
5417 vcol_prev = vcol;
5418 if (draw_state < WL_LINE || n_skip <= 0)
5419 {
5420 /*
5421 * Store the character.
5422 */
5423#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5424 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5425 {
5426 /* A double-wide character is: put first halve in left cell. */
5427 --off;
5428 --col;
5429 }
5430#endif
5431 ScreenLines[off] = c;
5432#ifdef FEAT_MBYTE
5433 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005434 {
5435 if ((mb_c & 0xff00) == 0x8e00)
5436 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 else if (enc_utf8)
5440 {
5441 if (mb_utf8)
5442 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005443 int i;
5444
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005446 if ((c & 0xff) == 0)
5447 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005448 for (i = 0; i < Screen_mco; ++i)
5449 {
5450 ScreenLinesC[i][off] = u8cc[i];
5451 if (u8cc[i] == 0)
5452 break;
5453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 }
5455 else
5456 ScreenLinesUC[off] = 0;
5457 }
5458 if (multi_attr)
5459 {
5460 ScreenAttrs[off] = multi_attr;
5461 multi_attr = 0;
5462 }
5463 else
5464#endif
5465 ScreenAttrs[off] = char_attr;
5466
5467#ifdef FEAT_MBYTE
5468 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5469 {
5470 /* Need to fill two screen columns. */
5471 ++off;
5472 ++col;
5473 if (enc_utf8)
5474 /* UTF-8: Put a 0 in the second screen char. */
5475 ScreenLines[off] = 0;
5476 else
5477 /* DBCS: Put second byte in the second screen char. */
5478 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005479 if (draw_state > WL_NR
5480#ifdef FEAT_DIFF
5481 && filler_todo <= 0
5482#endif
5483 )
5484 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 /* When "tocol" is halfway a character, set it to the end of
5486 * the character, otherwise highlighting won't stop. */
5487 if (tocol == vcol)
5488 ++tocol;
5489#ifdef FEAT_RIGHTLEFT
5490 if (wp->w_p_rl)
5491 {
5492 /* now it's time to backup one cell */
5493 --off;
5494 --col;
5495 }
5496#endif
5497 }
5498#endif
5499#ifdef FEAT_RIGHTLEFT
5500 if (wp->w_p_rl)
5501 {
5502 --off;
5503 --col;
5504 }
5505 else
5506#endif
5507 {
5508 ++off;
5509 ++col;
5510 }
5511 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005512#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005513 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005514 {
5515 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005516 ++vcol_off;
5517 if (n_extra > 0)
5518 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005519 if (wp->w_p_wrap)
5520 {
5521 /*
5522 * Special voodoo required if 'wrap' is on.
5523 *
5524 * Advance the column indicator to force the line
5525 * drawing to wrap early. This will make the line
5526 * take up the same screen space when parts are concealed,
5527 * so that cursor line computations aren't messed up.
5528 *
5529 * To avoid the fictitious advance of 'col' causing
5530 * trailing junk to be written out of the screen line
5531 * we are building, 'boguscols' keeps track of the number
5532 * of bad columns we have advanced.
5533 */
5534 if (n_extra > 0)
5535 {
5536 vcol += n_extra;
5537# ifdef FEAT_RIGHTLEFT
5538 if (wp->w_p_rl)
5539 {
5540 col -= n_extra;
5541 boguscols -= n_extra;
5542 }
5543 else
5544# endif
5545 {
5546 col += n_extra;
5547 boguscols += n_extra;
5548 }
5549 n_extra = 0;
5550 n_attr = 0;
5551 }
5552
5553
5554# ifdef FEAT_MBYTE
5555 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5556 {
5557 /* Need to fill two screen columns. */
5558# ifdef FEAT_RIGHTLEFT
5559 if (wp->w_p_rl)
5560 {
5561 --boguscols;
5562 --col;
5563 }
5564 else
5565# endif
5566 {
5567 ++boguscols;
5568 ++col;
5569 }
5570 }
5571# endif
5572
5573# ifdef FEAT_RIGHTLEFT
5574 if (wp->w_p_rl)
5575 {
5576 --boguscols;
5577 --col;
5578 }
5579 else
5580# endif
5581 {
5582 ++boguscols;
5583 ++col;
5584 }
5585 }
5586 else
5587 {
5588 if (n_extra > 0)
5589 {
5590 vcol += n_extra;
5591 n_extra = 0;
5592 n_attr = 0;
5593 }
5594 }
5595
5596 }
5597#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 else
5599 --n_skip;
5600
Bram Moolenaar64486672010-05-16 15:46:46 +02005601 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5602 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005603 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604#ifdef FEAT_DIFF
5605 && filler_todo <= 0
5606#endif
5607 )
5608 ++vcol;
5609
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005610#ifdef FEAT_SYN_HL
5611 if (vcol_save_attr >= 0)
5612 char_attr = vcol_save_attr;
5613#endif
5614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 /* restore attributes after "predeces" in 'listchars' */
5616 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5617 char_attr = saved_attr3;
5618
5619 /* restore attributes after last 'listchars' or 'number' char */
5620 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5621 char_attr = saved_attr2;
5622
5623 /*
5624 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005625 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 */
5627 if ((
5628#ifdef FEAT_RIGHTLEFT
5629 wp->w_p_rl ? (col < 0) :
5630#endif
5631 (col >= W_WIDTH(wp)))
5632 && (*ptr != NUL
5633#ifdef FEAT_DIFF
5634 || filler_todo > 0
5635#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005636 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5638 )
5639 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005640#ifdef FEAT_CONCEAL
5641 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5642 (int)W_WIDTH(wp), wp->w_p_rl);
5643 boguscols = 0;
5644#else
5645 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5646 (int)W_WIDTH(wp), wp->w_p_rl);
5647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 ++row;
5649 ++screen_row;
5650
5651 /* When not wrapping and finished diff lines, or when displayed
5652 * '$' and highlighting until last column, break here. */
5653 if ((!wp->w_p_wrap
5654#ifdef FEAT_DIFF
5655 && filler_todo <= 0
5656#endif
5657 ) || lcs_eol_one == -1)
5658 break;
5659
5660 /* When the window is too narrow draw all "@" lines. */
5661 if (draw_state != WL_LINE
5662#ifdef FEAT_DIFF
5663 && filler_todo <= 0
5664#endif
5665 )
5666 {
5667 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005668#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 draw_vsep_win(wp, row);
5670#endif
5671 row = endrow;
5672 }
5673
5674 /* When line got too long for screen break here. */
5675 if (row == endrow)
5676 {
5677 ++row;
5678 break;
5679 }
5680
5681 if (screen_cur_row == screen_row - 1
5682#ifdef FEAT_DIFF
5683 && filler_todo <= 0
5684#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005685#ifdef FEAT_WINDOWS
5686 && W_WIDTH(wp) == Columns
5687#endif
5688 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
5690 /* Remember that the line wraps, used for modeless copy. */
5691 LineWraps[screen_row - 1] = TRUE;
5692
5693 /*
5694 * Special trick to make copy/paste of wrapped lines work with
5695 * xterm/screen: write an extra character beyond the end of
5696 * the line. This will work with all terminal types
5697 * (regardless of the xn,am settings).
5698 * Only do this on a fast tty.
5699 * Only do this if the cursor is on the current line
5700 * (something has been written in it).
5701 * Don't do this for the GUI.
5702 * Don't do this for double-width characters.
5703 * Don't do this for a window not at the right screen border.
5704 */
5705 if (p_tf
5706#ifdef FEAT_GUI
5707 && !gui.in_use
5708#endif
5709#ifdef FEAT_MBYTE
5710 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005711 && ((*mb_off2cells)(LineOffset[screen_row],
5712 LineOffset[screen_row] + screen_Columns)
5713 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005715 + (int)Columns - 2,
5716 LineOffset[screen_row] + screen_Columns)
5717 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718#endif
5719 )
5720 {
5721 /* First make sure we are at the end of the screen line,
5722 * then output the same character again to let the
5723 * terminal know about the wrap. If the terminal doesn't
5724 * auto-wrap, we overwrite the character. */
5725 if (screen_cur_col != W_WIDTH(wp))
5726 screen_char(LineOffset[screen_row - 1]
5727 + (unsigned)Columns - 1,
5728 screen_row - 1, (int)(Columns - 1));
5729
5730#ifdef FEAT_MBYTE
5731 /* When there is a multi-byte character, just output a
5732 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005733 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5734 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 out_char(' ');
5736 else
5737#endif
5738 out_char(ScreenLines[LineOffset[screen_row - 1]
5739 + (Columns - 1)]);
5740 /* force a redraw of the first char on the next line */
5741 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5742 screen_start(); /* don't know where cursor is now */
5743 }
5744 }
5745
5746 col = 0;
5747 off = (unsigned)(current_ScreenLine - ScreenLines);
5748#ifdef FEAT_RIGHTLEFT
5749 if (wp->w_p_rl)
5750 {
5751 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5752 off += col;
5753 }
5754#endif
5755
5756 /* reset the drawing state for the start of a wrapped line */
5757 draw_state = WL_START;
5758 saved_n_extra = n_extra;
5759 saved_p_extra = p_extra;
5760 saved_c_extra = c_extra;
5761 saved_char_attr = char_attr;
5762 n_extra = 0;
5763 lcs_prec_todo = lcs_prec;
5764#ifdef FEAT_LINEBREAK
5765# ifdef FEAT_DIFF
5766 if (filler_todo <= 0)
5767# endif
5768 need_showbreak = TRUE;
5769#endif
5770#ifdef FEAT_DIFF
5771 --filler_todo;
5772 /* When the filler lines are actually below the last line of the
5773 * file, don't draw the line itself, break here. */
5774 if (filler_todo == 0 && wp->w_botfill)
5775 break;
5776#endif
5777 }
5778
5779 } /* for every character in the line */
5780
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005781#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005782 /* After an empty line check first word for capital. */
5783 if (*skipwhite(line) == NUL)
5784 {
5785 capcol_lnum = lnum + 1;
5786 cap_col = 0;
5787 }
5788#endif
5789
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005790 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 return row;
5792}
5793
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005794#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005795static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005796
5797/*
5798 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005799 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005800 */
5801 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005802comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005803{
5804 int i;
5805
5806 for (i = 0; i < Screen_mco; ++i)
5807 {
5808 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5809 return TRUE;
5810 if (ScreenLinesC[i][off_from] == 0)
5811 break;
5812 }
5813 return FALSE;
5814}
5815#endif
5816
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817/*
5818 * Check whether the given character needs redrawing:
5819 * - the (first byte of the) character is different
5820 * - the attributes are different
5821 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005822 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 */
5824 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005825char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826{
5827 if (cols > 0
5828 && ((ScreenLines[off_from] != ScreenLines[off_to]
5829 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5830
5831#ifdef FEAT_MBYTE
5832 || (enc_dbcs != 0
5833 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5834 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5835 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5836 : (cols > 1 && ScreenLines[off_from + 1]
5837 != ScreenLines[off_to + 1])))
5838 || (enc_utf8
5839 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5840 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005841 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005842 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5843 && ScreenLines[off_from + 1]
5844 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845#endif
5846 ))
5847 return TRUE;
5848 return FALSE;
5849}
5850
5851/*
5852 * Move one "cooked" screen line to the screen, but only the characters that
5853 * have actually changed. Handle insert/delete character.
5854 * "coloff" gives the first column on the screen for this line.
5855 * "endcol" gives the columns where valid characters are.
5856 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5857 * needs to be cleared, negative otherwise.
5858 * "rlflag" is TRUE in a rightleft window:
5859 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5860 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5861 */
5862 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005863screen_line(
5864 int row,
5865 int coloff,
5866 int endcol,
5867 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005869 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005871 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872{
5873 unsigned off_from;
5874 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005875#ifdef FEAT_MBYTE
5876 unsigned max_off_from;
5877 unsigned max_off_to;
5878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005880#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 int hl;
5882#endif
5883 int force = FALSE; /* force update rest of the line */
5884 int redraw_this /* bool: does character need redraw? */
5885#ifdef FEAT_GUI
5886 = TRUE /* For GUI when while-loop empty */
5887#endif
5888 ;
5889 int redraw_next; /* redraw_this for next character */
5890#ifdef FEAT_MBYTE
5891 int clear_next = FALSE;
5892 int char_cells; /* 1: normal char */
5893 /* 2: occupies two display cells */
5894# define CHAR_CELLS char_cells
5895#else
5896# define CHAR_CELLS 1
5897#endif
5898
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005899 /* Check for illegal row and col, just in case. */
5900 if (row >= Rows)
5901 row = Rows - 1;
5902 if (endcol > Columns)
5903 endcol = Columns;
5904
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905# ifdef FEAT_CLIPBOARD
5906 clip_may_clear_selection(row, row);
5907# endif
5908
5909 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5910 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005911#ifdef FEAT_MBYTE
5912 max_off_from = off_from + screen_Columns;
5913 max_off_to = LineOffset[row] + screen_Columns;
5914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
5916#ifdef FEAT_RIGHTLEFT
5917 if (rlflag)
5918 {
5919 /* Clear rest first, because it's left of the text. */
5920 if (clear_width > 0)
5921 {
5922 while (col <= endcol && ScreenLines[off_to] == ' '
5923 && ScreenAttrs[off_to] == 0
5924# ifdef FEAT_MBYTE
5925 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5926# endif
5927 )
5928 {
5929 ++off_to;
5930 ++col;
5931 }
5932 if (col <= endcol)
5933 screen_fill(row, row + 1, col + coloff,
5934 endcol + coloff + 1, ' ', ' ', 0);
5935 }
5936 col = endcol + 1;
5937 off_to = LineOffset[row] + col + coloff;
5938 off_from += col;
5939 endcol = (clear_width > 0 ? clear_width : -clear_width);
5940 }
5941#endif /* FEAT_RIGHTLEFT */
5942
5943 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5944
5945 while (col < endcol)
5946 {
5947#ifdef FEAT_MBYTE
5948 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005949 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 else
5951 char_cells = 1;
5952#endif
5953
5954 redraw_this = redraw_next;
5955 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5956 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5957
5958#ifdef FEAT_GUI
5959 /* If the next character was bold, then redraw the current character to
5960 * remove any pixels that might have spilt over into us. This only
5961 * happens in the GUI.
5962 */
5963 if (redraw_next && gui.in_use)
5964 {
5965 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005966 if (hl > HL_ALL)
5967 hl = syn_attr2attr(hl);
5968 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 redraw_this = TRUE;
5970 }
5971#endif
5972
5973 if (redraw_this)
5974 {
5975 /*
5976 * Special handling when 'xs' termcap flag set (hpterm):
5977 * Attributes for characters are stored at the position where the
5978 * cursor is when writing the highlighting code. The
5979 * start-highlighting code must be written with the cursor on the
5980 * first highlighted character. The stop-highlighting code must
5981 * be written with the cursor just after the last highlighted
5982 * character.
5983 * Overwriting a character doesn't remove it's highlighting. Need
5984 * to clear the rest of the line, and force redrawing it
5985 * completely.
5986 */
5987 if ( p_wiv
5988 && !force
5989#ifdef FEAT_GUI
5990 && !gui.in_use
5991#endif
5992 && ScreenAttrs[off_to] != 0
5993 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5994 {
5995 /*
5996 * Need to remove highlighting attributes here.
5997 */
5998 windgoto(row, col + coloff);
5999 out_str(T_CE); /* clear rest of this screen line */
6000 screen_start(); /* don't know where cursor is now */
6001 force = TRUE; /* force redraw of rest of the line */
6002 redraw_next = TRUE; /* or else next char would miss out */
6003
6004 /*
6005 * If the previous character was highlighted, need to stop
6006 * highlighting at this character.
6007 */
6008 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6009 {
6010 screen_attr = ScreenAttrs[off_to - 1];
6011 term_windgoto(row, col + coloff);
6012 screen_stop_highlight();
6013 }
6014 else
6015 screen_attr = 0; /* highlighting has stopped */
6016 }
6017#ifdef FEAT_MBYTE
6018 if (enc_dbcs != 0)
6019 {
6020 /* Check if overwriting a double-byte with a single-byte or
6021 * the other way around requires another character to be
6022 * redrawn. For UTF-8 this isn't needed, because comparing
6023 * ScreenLinesUC[] is sufficient. */
6024 if (char_cells == 1
6025 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006026 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 {
6028 /* Writing a single-cell character over a double-cell
6029 * character: need to redraw the next cell. */
6030 ScreenLines[off_to + 1] = 0;
6031 redraw_next = TRUE;
6032 }
6033 else if (char_cells == 2
6034 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006035 && (*mb_off2cells)(off_to, max_off_to) == 1
6036 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 {
6038 /* Writing the second half of a double-cell character over
6039 * a double-cell character: need to redraw the second
6040 * cell. */
6041 ScreenLines[off_to + 2] = 0;
6042 redraw_next = TRUE;
6043 }
6044
6045 if (enc_dbcs == DBCS_JPNU)
6046 ScreenLines2[off_to] = ScreenLines2[off_from];
6047 }
6048 /* When writing a single-width character over a double-width
6049 * character and at the end of the redrawn text, need to clear out
6050 * the right halve of the old character.
6051 * Also required when writing the right halve of a double-width
6052 * char over the left halve of an existing one. */
6053 if (has_mbyte && col + char_cells == endcol
6054 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006055 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006057 && (*mb_off2cells)(off_to, max_off_to) == 1
6058 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 clear_next = TRUE;
6060#endif
6061
6062 ScreenLines[off_to] = ScreenLines[off_from];
6063#ifdef FEAT_MBYTE
6064 if (enc_utf8)
6065 {
6066 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6067 if (ScreenLinesUC[off_from] != 0)
6068 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006069 int i;
6070
6071 for (i = 0; i < Screen_mco; ++i)
6072 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 }
6074 }
6075 if (char_cells == 2)
6076 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6077#endif
6078
6079#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006080 /* The bold trick makes a single column of pixels appear in the
6081 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 * character should be redrawn too. This happens for our own GUI
6083 * and for some xterms. */
6084 if (
6085# ifdef FEAT_GUI
6086 gui.in_use
6087# endif
6088# if defined(FEAT_GUI) && defined(UNIX)
6089 ||
6090# endif
6091# ifdef UNIX
6092 term_is_xterm
6093# endif
6094 )
6095 {
6096 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006097 if (hl > HL_ALL)
6098 hl = syn_attr2attr(hl);
6099 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 redraw_next = TRUE;
6101 }
6102#endif
6103 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6104#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006105 /* For simplicity set the attributes of second half of a
6106 * double-wide character equal to the first half. */
6107 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006109
6110 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 else
6113#endif
6114 screen_char(off_to, row, col + coloff);
6115 }
6116 else if ( p_wiv
6117#ifdef FEAT_GUI
6118 && !gui.in_use
6119#endif
6120 && col + coloff > 0)
6121 {
6122 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6123 {
6124 /*
6125 * Don't output stop-highlight when moving the cursor, it will
6126 * stop the highlighting when it should continue.
6127 */
6128 screen_attr = 0;
6129 }
6130 else if (screen_attr != 0)
6131 screen_stop_highlight();
6132 }
6133
6134 off_to += CHAR_CELLS;
6135 off_from += CHAR_CELLS;
6136 col += CHAR_CELLS;
6137 }
6138
6139#ifdef FEAT_MBYTE
6140 if (clear_next)
6141 {
6142 /* Clear the second half of a double-wide character of which the left
6143 * half was overwritten with a single-wide character. */
6144 ScreenLines[off_to] = ' ';
6145 if (enc_utf8)
6146 ScreenLinesUC[off_to] = 0;
6147 screen_char(off_to, row, col + coloff);
6148 }
6149#endif
6150
6151 if (clear_width > 0
6152#ifdef FEAT_RIGHTLEFT
6153 && !rlflag
6154#endif
6155 )
6156 {
6157#ifdef FEAT_GUI
6158 int startCol = col;
6159#endif
6160
6161 /* blank out the rest of the line */
6162 while (col < clear_width && ScreenLines[off_to] == ' '
6163 && ScreenAttrs[off_to] == 0
6164#ifdef FEAT_MBYTE
6165 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6166#endif
6167 )
6168 {
6169 ++off_to;
6170 ++col;
6171 }
6172 if (col < clear_width)
6173 {
6174#ifdef FEAT_GUI
6175 /*
6176 * In the GUI, clearing the rest of the line may leave pixels
6177 * behind if the first character cleared was bold. Some bold
6178 * fonts spill over the left. In this case we redraw the previous
6179 * character too. If we didn't skip any blanks above, then we
6180 * only redraw if the character wasn't already redrawn anyway.
6181 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006182 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 {
6184 hl = ScreenAttrs[off_to];
6185 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006186 {
6187 int prev_cells = 1;
6188# ifdef FEAT_MBYTE
6189 if (enc_utf8)
6190 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6191 * that its width is 2. */
6192 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6193 else if (enc_dbcs != 0)
6194 {
6195 /* find previous character by counting from first
6196 * column and get its width. */
6197 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006198 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006199
6200 while (off < off_to)
6201 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006202 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006203 off += prev_cells;
6204 }
6205 }
6206
6207 if (enc_dbcs != 0 && prev_cells > 1)
6208 screen_char_2(off_to - prev_cells, row,
6209 col + coloff - prev_cells);
6210 else
6211# endif
6212 screen_char(off_to - prev_cells, row,
6213 col + coloff - prev_cells);
6214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 }
6216#endif
6217 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6218 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006219#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 off_to += clear_width - col;
6221 col = clear_width;
6222#endif
6223 }
6224 }
6225
6226 if (clear_width > 0)
6227 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006228#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 /* For a window that's left of another, draw the separator char. */
6230 if (col + coloff < Columns)
6231 {
6232 int c;
6233
6234 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006235 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006237 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6238 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239# endif
6240 || ScreenAttrs[off_to] != hl)
6241 {
6242 ScreenLines[off_to] = c;
6243 ScreenAttrs[off_to] = hl;
6244# ifdef FEAT_MBYTE
6245 if (enc_utf8)
6246 {
6247 if (c >= 0x80)
6248 {
6249 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006250 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 }
6252 else
6253 ScreenLinesUC[off_to] = 0;
6254 }
6255# endif
6256 screen_char(off_to, row, col + coloff);
6257 }
6258 }
6259 else
6260#endif
6261 LineWraps[row] = FALSE;
6262 }
6263}
6264
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006265#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006267 * Mirror text "str" for right-left displaying.
6268 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006270 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006271rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 char_u *p1, *p2;
6274 int t;
6275
6276 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6277 {
6278 t = *p1;
6279 *p1 = *p2;
6280 *p2 = t;
6281 }
6282}
6283#endif
6284
6285#if defined(FEAT_WINDOWS) || defined(PROTO)
6286/*
6287 * mark all status lines for redraw; used after first :cd
6288 */
6289 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006290status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 win_T *wp;
6293
Bram Moolenaar29323592016-07-24 22:04:11 +02006294 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 if (wp->w_status_height)
6296 {
6297 wp->w_redr_status = TRUE;
6298 redraw_later(VALID);
6299 }
6300}
6301
6302/*
6303 * mark all status lines of the current buffer for redraw
6304 */
6305 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006306status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
6308 win_T *wp;
6309
Bram Moolenaar29323592016-07-24 22:04:11 +02006310 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6312 {
6313 wp->w_redr_status = TRUE;
6314 redraw_later(VALID);
6315 }
6316}
6317
6318/*
6319 * Redraw all status lines that need to be redrawn.
6320 */
6321 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006322redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323{
6324 win_T *wp;
6325
Bram Moolenaar29323592016-07-24 22:04:11 +02006326 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (wp->w_redr_status)
6328 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006329 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006330 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331}
6332#endif
6333
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006334#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335/*
6336 * Redraw all status lines at the bottom of frame "frp".
6337 */
6338 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006339win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340{
6341 if (frp->fr_layout == FR_LEAF)
6342 frp->fr_win->w_redr_status = TRUE;
6343 else if (frp->fr_layout == FR_ROW)
6344 {
6345 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6346 win_redraw_last_status(frp);
6347 }
6348 else /* frp->fr_layout == FR_COL */
6349 {
6350 frp = frp->fr_child;
6351 while (frp->fr_next != NULL)
6352 frp = frp->fr_next;
6353 win_redraw_last_status(frp);
6354 }
6355}
6356#endif
6357
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006358#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359/*
6360 * Draw the verticap separator right of window "wp" starting with line "row".
6361 */
6362 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006363draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
6365 int hl;
6366 int c;
6367
6368 if (wp->w_vsep_width)
6369 {
6370 /* draw the vertical separator right of this window */
6371 c = fillchar_vsep(&hl);
6372 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6373 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6374 c, ' ', hl);
6375 }
6376}
6377#endif
6378
6379#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006380static int status_match_len(expand_T *xp, char_u *s);
6381static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382
6383/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006384 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 */
6386 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006387status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 int len = 0;
6390
6391#ifdef FEAT_MENU
6392 int emenu = (xp->xp_context == EXPAND_MENUS
6393 || xp->xp_context == EXPAND_MENUNAMES);
6394
6395 /* Check for menu separators - replace with '|'. */
6396 if (emenu && menu_is_separator(s))
6397 return 1;
6398#endif
6399
6400 while (*s != NUL)
6401 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006402 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006403 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006404 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 }
6406
6407 return len;
6408}
6409
6410/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006411 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006412 * These are backslashes used for escaping. Do show backslashes in help tags.
6413 */
6414 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006415skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006416{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006417 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006418#ifdef FEAT_MENU
6419 || ((xp->xp_context == EXPAND_MENUS
6420 || xp->xp_context == EXPAND_MENUNAMES)
6421 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6422#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006423 )
6424 {
6425#ifndef BACKSLASH_IN_FILENAME
6426 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6427 return 2;
6428#endif
6429 return 1;
6430 }
6431 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006432}
6433
6434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 * Show wildchar matches in the status line.
6436 * Show at least the "match" item.
6437 * We start at item 'first_match' in the list and show all matches that fit.
6438 *
6439 * If inversion is possible we use it. Else '=' characters are used.
6440 */
6441 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006442win_redr_status_matches(
6443 expand_T *xp,
6444 int num_matches,
6445 char_u **matches, /* list of matches */
6446 int match,
6447 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6450 int row;
6451 char_u *buf;
6452 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006453 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 int fillchar;
6455 int attr;
6456 int i;
6457 int highlight = TRUE;
6458 char_u *selstart = NULL;
6459 int selstart_col = 0;
6460 char_u *selend = NULL;
6461 static int first_match = 0;
6462 int add_left = FALSE;
6463 char_u *s;
6464#ifdef FEAT_MENU
6465 int emenu;
6466#endif
6467#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6468 int l;
6469#endif
6470
6471 if (matches == NULL) /* interrupted completion? */
6472 return;
6473
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006474#ifdef FEAT_MBYTE
6475 if (has_mbyte)
6476 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6477 else
6478#endif
6479 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 if (buf == NULL)
6481 return;
6482
6483 if (match == -1) /* don't show match but original text */
6484 {
6485 match = 0;
6486 highlight = FALSE;
6487 }
6488 /* count 1 for the ending ">" */
6489 clen = status_match_len(xp, L_MATCH(match)) + 3;
6490 if (match == 0)
6491 first_match = 0;
6492 else if (match < first_match)
6493 {
6494 /* jumping left, as far as we can go */
6495 first_match = match;
6496 add_left = TRUE;
6497 }
6498 else
6499 {
6500 /* check if match fits on the screen */
6501 for (i = first_match; i < match; ++i)
6502 clen += status_match_len(xp, L_MATCH(i)) + 2;
6503 if (first_match > 0)
6504 clen += 2;
6505 /* jumping right, put match at the left */
6506 if ((long)clen > Columns)
6507 {
6508 first_match = match;
6509 /* if showing the last match, we can add some on the left */
6510 clen = 2;
6511 for (i = match; i < num_matches; ++i)
6512 {
6513 clen += status_match_len(xp, L_MATCH(i)) + 2;
6514 if ((long)clen >= Columns)
6515 break;
6516 }
6517 if (i == num_matches)
6518 add_left = TRUE;
6519 }
6520 }
6521 if (add_left)
6522 while (first_match > 0)
6523 {
6524 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6525 if ((long)clen >= Columns)
6526 break;
6527 --first_match;
6528 }
6529
6530 fillchar = fillchar_status(&attr, TRUE);
6531
6532 if (first_match == 0)
6533 {
6534 *buf = NUL;
6535 len = 0;
6536 }
6537 else
6538 {
6539 STRCPY(buf, "< ");
6540 len = 2;
6541 }
6542 clen = len;
6543
6544 i = first_match;
6545 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6546 {
6547 if (i == match)
6548 {
6549 selstart = buf + len;
6550 selstart_col = clen;
6551 }
6552
6553 s = L_MATCH(i);
6554 /* Check for menu separators - replace with '|' */
6555#ifdef FEAT_MENU
6556 emenu = (xp->xp_context == EXPAND_MENUS
6557 || xp->xp_context == EXPAND_MENUNAMES);
6558 if (emenu && menu_is_separator(s))
6559 {
6560 STRCPY(buf + len, transchar('|'));
6561 l = (int)STRLEN(buf + len);
6562 len += l;
6563 clen += l;
6564 }
6565 else
6566#endif
6567 for ( ; *s != NUL; ++s)
6568 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006569 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 clen += ptr2cells(s);
6571#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006572 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 {
6574 STRNCPY(buf + len, s, l);
6575 s += l - 1;
6576 len += l;
6577 }
6578 else
6579#endif
6580 {
6581 STRCPY(buf + len, transchar_byte(*s));
6582 len += (int)STRLEN(buf + len);
6583 }
6584 }
6585 if (i == match)
6586 selend = buf + len;
6587
6588 *(buf + len++) = ' ';
6589 *(buf + len++) = ' ';
6590 clen += 2;
6591 if (++i == num_matches)
6592 break;
6593 }
6594
6595 if (i != num_matches)
6596 {
6597 *(buf + len++) = '>';
6598 ++clen;
6599 }
6600
6601 buf[len] = NUL;
6602
6603 row = cmdline_row - 1;
6604 if (row >= 0)
6605 {
6606 if (wild_menu_showing == 0)
6607 {
6608 if (msg_scrolled > 0)
6609 {
6610 /* Put the wildmenu just above the command line. If there is
6611 * no room, scroll the screen one line up. */
6612 if (cmdline_row == Rows - 1)
6613 {
6614 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6615 ++msg_scrolled;
6616 }
6617 else
6618 {
6619 ++cmdline_row;
6620 ++row;
6621 }
6622 wild_menu_showing = WM_SCROLLED;
6623 }
6624 else
6625 {
6626 /* Create status line if needed by setting 'laststatus' to 2.
6627 * Set 'winminheight' to zero to avoid that the window is
6628 * resized. */
6629 if (lastwin->w_status_height == 0)
6630 {
6631 save_p_ls = p_ls;
6632 save_p_wmh = p_wmh;
6633 p_ls = 2;
6634 p_wmh = 0;
6635 last_status(FALSE);
6636 }
6637 wild_menu_showing = WM_SHOWN;
6638 }
6639 }
6640
6641 screen_puts(buf, row, 0, attr);
6642 if (selstart != NULL && highlight)
6643 {
6644 *selend = NUL;
6645 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6646 }
6647
6648 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6649 }
6650
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006651#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 win_redraw_last_status(topframe);
6653#else
6654 lastwin->w_redr_status = TRUE;
6655#endif
6656 vim_free(buf);
6657}
6658#endif
6659
6660#if defined(FEAT_WINDOWS) || defined(PROTO)
6661/*
6662 * Redraw the status line of window wp.
6663 *
6664 * If inversion is possible we use it. Else '=' characters are used.
6665 */
6666 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006667win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
6669 int row;
6670 char_u *p;
6671 int len;
6672 int fillchar;
6673 int attr;
6674 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006675 static int busy = FALSE;
6676
6677 /* It's possible to get here recursively when 'statusline' (indirectly)
6678 * invokes ":redrawstatus". Simply ignore the call then. */
6679 if (busy)
6680 return;
6681 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
6683 wp->w_redr_status = FALSE;
6684 if (wp->w_status_height == 0)
6685 {
6686 /* no status line, can only be last window */
6687 redraw_cmdline = TRUE;
6688 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006689 else if (!redrawing()
6690#ifdef FEAT_INS_EXPAND
6691 /* don't update status line when popup menu is visible and may be
6692 * drawn over it */
6693 || pum_visible()
6694#endif
6695 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 {
6697 /* Don't redraw right now, do it later. */
6698 wp->w_redr_status = TRUE;
6699 }
6700#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006701 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 {
6703 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006704 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 }
6706#endif
6707 else
6708 {
6709 fillchar = fillchar_status(&attr, wp == curwin);
6710
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006711 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 p = NameBuff;
6713 len = (int)STRLEN(p);
6714
6715 if (wp->w_buffer->b_help
6716#ifdef FEAT_QUICKFIX
6717 || wp->w_p_pvw
6718#endif
6719 || bufIsChanged(wp->w_buffer)
6720 || wp->w_buffer->b_p_ro)
6721 *(p + len++) = ' ';
6722 if (wp->w_buffer->b_help)
6723 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006724 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 len += (int)STRLEN(p + len);
6726 }
6727#ifdef FEAT_QUICKFIX
6728 if (wp->w_p_pvw)
6729 {
6730 STRCPY(p + len, _("[Preview]"));
6731 len += (int)STRLEN(p + len);
6732 }
6733#endif
6734 if (bufIsChanged(wp->w_buffer))
6735 {
6736 STRCPY(p + len, "[+]");
6737 len += 3;
6738 }
6739 if (wp->w_buffer->b_p_ro)
6740 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006741 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 len += 4;
6743 }
6744
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6746 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6747 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6748 if (this_ru_col <= 1)
6749 {
6750 p = (char_u *)"<"; /* No room for file name! */
6751 len = 1;
6752 }
6753 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754#ifdef FEAT_MBYTE
6755 if (has_mbyte)
6756 {
6757 int clen = 0, i;
6758
6759 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006760 clen = mb_string2cells(p, -1);
6761
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 /* Find first character that will fit.
6763 * Going from start to end is much faster for DBCS. */
6764 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006765 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 clen -= (*mb_ptr2cells)(p + i);
6767 len = clen;
6768 if (i > 0)
6769 {
6770 p = p + i - 1;
6771 *p = '<';
6772 ++len;
6773 }
6774
6775 }
6776 else
6777#endif
6778 if (len > this_ru_col - 1)
6779 {
6780 p += len - (this_ru_col - 1);
6781 *p = '<';
6782 len = this_ru_col - 1;
6783 }
6784
6785 row = W_WINROW(wp) + wp->w_height;
6786 screen_puts(p, row, W_WINCOL(wp), attr);
6787 screen_fill(row, row + 1, len + W_WINCOL(wp),
6788 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6789
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006790 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6792 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6793 - 1 + W_WINCOL(wp)), attr);
6794
6795#ifdef FEAT_CMDL_INFO
6796 win_redr_ruler(wp, TRUE);
6797#endif
6798 }
6799
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 /*
6801 * May need to draw the character below the vertical separator.
6802 */
6803 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6804 {
6805 if (stl_connected(wp))
6806 fillchar = fillchar_status(&attr, wp == curwin);
6807 else
6808 fillchar = fillchar_vsep(&attr);
6809 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6810 attr);
6811 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006812 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813}
6814
Bram Moolenaar238a5642006-02-21 22:12:05 +00006815#ifdef FEAT_STL_OPT
6816/*
6817 * Redraw the status line according to 'statusline' and take care of any
6818 * errors encountered.
6819 */
6820 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006821redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006822{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006823 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006824 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006825
6826 /* When called recursively return. This can happen when the statusline
6827 * contains an expression that triggers a redraw. */
6828 if (entered)
6829 return;
6830 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006831
Bram Moolenaara742e082016-04-05 21:10:38 +02006832 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006833 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006834 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006835 {
6836 /* When there is an error disable the statusline, otherwise the
6837 * display is messed up with errors and a redraw triggers the problem
6838 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006839 set_string_option_direct((char_u *)"statusline", -1,
6840 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006841 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006842 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006843 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006844 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006845}
6846#endif
6847
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848/*
6849 * Return TRUE if the status line of window "wp" is connected to the status
6850 * line of the window right of it. If not, then it's a vertical separator.
6851 * Only call if (wp->w_vsep_width != 0).
6852 */
6853 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006854stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855{
6856 frame_T *fr;
6857
6858 fr = wp->w_frame;
6859 while (fr->fr_parent != NULL)
6860 {
6861 if (fr->fr_parent->fr_layout == FR_COL)
6862 {
6863 if (fr->fr_next != NULL)
6864 break;
6865 }
6866 else
6867 {
6868 if (fr->fr_next != NULL)
6869 return TRUE;
6870 }
6871 fr = fr->fr_parent;
6872 }
6873 return FALSE;
6874}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875
6876#endif /* FEAT_WINDOWS */
6877
6878#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6879/*
6880 * Get the value to show for the language mappings, active 'keymap'.
6881 */
6882 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006883get_keymap_str(
6884 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006885 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01006886 char_u *buf, /* buffer for the result */
6887 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888{
6889 char_u *p;
6890
6891 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6892 return FALSE;
6893
6894 {
6895#ifdef FEAT_EVAL
6896 buf_T *old_curbuf = curbuf;
6897 win_T *old_curwin = curwin;
6898 char_u *s;
6899
6900 curbuf = wp->w_buffer;
6901 curwin = wp;
6902 STRCPY(buf, "b:keymap_name"); /* must be writable */
6903 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006904 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 --emsg_skip;
6906 curbuf = old_curbuf;
6907 curwin = old_curwin;
6908 if (p == NULL || *p == NUL)
6909#endif
6910 {
6911#ifdef FEAT_KEYMAP
6912 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6913 p = wp->w_buffer->b_p_keymap;
6914 else
6915#endif
6916 p = (char_u *)"lang";
6917 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006918 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 buf[0] = NUL;
6920#ifdef FEAT_EVAL
6921 vim_free(s);
6922#endif
6923 }
6924 return buf[0] != NUL;
6925}
6926#endif
6927
6928#if defined(FEAT_STL_OPT) || defined(PROTO)
6929/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006930 * Redraw the status line or ruler of window "wp".
6931 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 */
6933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006934win_redr_custom(
6935 win_T *wp,
6936 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006938 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 int attr;
6940 int curattr;
6941 int row;
6942 int col = 0;
6943 int maxwidth;
6944 int width;
6945 int n;
6946 int len;
6947 int fillchar;
6948 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006949 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006951 struct stl_hlrec hltab[STL_MAX_ITEM];
6952 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006953 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006954 win_T *ewp;
6955 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956
Bram Moolenaar1d633412013-12-11 15:52:01 +01006957 /* There is a tiny chance that this gets called recursively: When
6958 * redrawing a status line triggers redrawing the ruler or tabline.
6959 * Avoid trouble by not allowing recursion. */
6960 if (entered)
6961 return;
6962 entered = TRUE;
6963
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006965 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006967 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006968 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006969 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006970 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006971 attr = hl_attr(HLF_TPF);
6972 maxwidth = Columns;
6973# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006974 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006977 else
6978 {
6979 row = W_WINROW(wp) + wp->w_height;
6980 fillchar = fillchar_status(&attr, wp == curwin);
6981 maxwidth = W_WIDTH(wp);
6982
6983 if (draw_ruler)
6984 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006985 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006986 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006987 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006988 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006989 if (*++stl == '-')
6990 stl++;
6991 if (atoi((char *)stl))
6992 while (VIM_ISDIGIT(*stl))
6993 stl++;
6994 if (*stl++ != '(')
6995 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006996 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006997#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006998 col = ru_col - (Columns - W_WIDTH(wp));
6999 if (col < (W_WIDTH(wp) + 1) / 2)
7000 col = (W_WIDTH(wp) + 1) / 2;
7001#else
7002 col = ru_col;
7003 if (col > (Columns + 1) / 2)
7004 col = (Columns + 1) / 2;
7005#endif
7006 maxwidth = W_WIDTH(wp) - col;
7007#ifdef FEAT_WINDOWS
7008 if (!wp->w_status_height)
7009#endif
7010 {
7011 row = Rows - 1;
7012 --maxwidth; /* writing in last column may cause scrolling */
7013 fillchar = ' ';
7014 attr = 0;
7015 }
7016
7017# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007018 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007019# endif
7020 }
7021 else
7022 {
7023 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007024 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007025 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007026 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007027# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007028 use_sandbox = was_set_insecurely((char_u *)"statusline",
7029 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007030# endif
7031 }
7032
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007033#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007034 col += W_WINCOL(wp);
7035#endif
7036 }
7037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007039 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
Bram Moolenaar61452852011-02-01 18:01:11 +01007041 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7042 * the cursor away and back. */
7043 ewp = wp == NULL ? curwin : wp;
7044 p_crb_save = ewp->w_p_crb;
7045 ewp->w_p_crb = FALSE;
7046
Bram Moolenaar362f3562009-11-03 16:20:34 +00007047 /* Make a copy, because the statusline may include a function call that
7048 * might change the option value and free the memory. */
7049 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007050 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007051 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007052 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007053 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007054 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007056 /* Make all characters printable. */
7057 p = transstr(buf);
7058 if (p != NULL)
7059 {
7060 vim_strncpy(buf, p, sizeof(buf) - 1);
7061 vim_free(p);
7062 }
7063
7064 /* fill up with "fillchar" */
7065 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007066 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {
7068#ifdef FEAT_MBYTE
7069 len += (*mb_char2bytes)(fillchar, buf + len);
7070#else
7071 buf[len++] = fillchar;
7072#endif
7073 ++width;
7074 }
7075 buf[len] = NUL;
7076
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007077 /*
7078 * Draw each snippet with the specified highlighting.
7079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 curattr = attr;
7081 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007082 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007084 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 screen_puts_len(p, len, row, col, curattr);
7086 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007087 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007089 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007091 else if (hltab[n].userhl < 0)
7092 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007094 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007095 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096#endif
7097 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007098 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
7100 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007101
7102 if (wp == NULL)
7103 {
7104 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7105 col = 0;
7106 len = 0;
7107 p = buf;
7108 fillchar = 0;
7109 for (n = 0; tabtab[n].start != NULL; n++)
7110 {
7111 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7112 while (col < len)
7113 TabPageIdxs[col++] = fillchar;
7114 p = tabtab[n].start;
7115 fillchar = tabtab[n].userhl;
7116 }
7117 while (col < Columns)
7118 TabPageIdxs[col++] = fillchar;
7119 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007120
7121theend:
7122 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123}
7124
7125#endif /* FEAT_STL_OPT */
7126
7127/*
7128 * Output a single character directly to the screen and update ScreenLines.
7129 */
7130 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007131screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 char_u buf[MB_MAXBYTES + 1];
7134
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007135#ifdef FEAT_MBYTE
7136 if (has_mbyte)
7137 buf[(*mb_char2bytes)(c, buf)] = NUL;
7138 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007140 {
7141 buf[0] = c;
7142 buf[1] = NUL;
7143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 screen_puts(buf, row, col, attr);
7145}
7146
7147/*
7148 * Get a single character directly from ScreenLines into "bytes[]".
7149 * Also return its attribute in *attrp;
7150 */
7151 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007152screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
7154 unsigned off;
7155
7156 /* safety check */
7157 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7158 {
7159 off = LineOffset[row] + col;
7160 *attrp = ScreenAttrs[off];
7161 bytes[0] = ScreenLines[off];
7162 bytes[1] = NUL;
7163
7164#ifdef FEAT_MBYTE
7165 if (enc_utf8 && ScreenLinesUC[off] != 0)
7166 bytes[utfc_char2bytes(off, bytes)] = NUL;
7167 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7168 {
7169 bytes[0] = ScreenLines[off];
7170 bytes[1] = ScreenLines2[off];
7171 bytes[2] = NUL;
7172 }
7173 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7174 {
7175 bytes[1] = ScreenLines[off + 1];
7176 bytes[2] = NUL;
7177 }
7178#endif
7179 }
7180}
7181
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007182#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007183static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007184
7185/*
7186 * Return TRUE if composing characters for screen posn "off" differs from
7187 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007188 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007189 */
7190 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007191screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007192{
7193 int i;
7194
7195 for (i = 0; i < Screen_mco; ++i)
7196 {
7197 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7198 return TRUE;
7199 if (u8cc[i] == 0)
7200 break;
7201 }
7202 return FALSE;
7203}
7204#endif
7205
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206/*
7207 * Put string '*text' on the screen at position 'row' and 'col', with
7208 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7209 * Note: only outputs within one row, message is truncated at screen boundary!
7210 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7211 */
7212 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007213screen_puts(
7214 char_u *text,
7215 int row,
7216 int col,
7217 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218{
7219 screen_puts_len(text, -1, row, col, attr);
7220}
7221
7222/*
7223 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7224 * a NUL.
7225 */
7226 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007227screen_puts_len(
7228 char_u *text,
7229 int textlen,
7230 int row,
7231 int col,
7232 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233{
7234 unsigned off;
7235 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007236 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 int c;
7238#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007239 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 int mbyte_blen = 1;
7241 int mbyte_cells = 1;
7242 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007243 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 int clear_next_cell = FALSE;
7245# ifdef FEAT_ARABIC
7246 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007247 int pc, nc, nc1;
7248 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249# endif
7250#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007251#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7252 int force_redraw_this;
7253 int force_redraw_next = FALSE;
7254#endif
7255 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
7257 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7258 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007259 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
Bram Moolenaarc236c162008-07-13 17:41:49 +00007261#ifdef FEAT_MBYTE
7262 /* When drawing over the right halve of a double-wide char clear out the
7263 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007264 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007265# ifdef FEAT_GUI
7266 && !gui.in_use
7267# endif
7268 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007269 {
7270 ScreenLines[off - 1] = ' ';
7271 ScreenAttrs[off - 1] = 0;
7272 if (enc_utf8)
7273 {
7274 ScreenLinesUC[off - 1] = 0;
7275 ScreenLinesC[0][off - 1] = 0;
7276 }
7277 /* redraw the previous cell, make it empty */
7278 screen_char(off - 1, row, col - 1);
7279 /* force the cell at "col" to be redrawn */
7280 force_redraw_next = TRUE;
7281 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007282#endif
7283
Bram Moolenaar367329b2007-08-30 11:53:22 +00007284#ifdef FEAT_MBYTE
7285 max_off = LineOffset[row] + screen_Columns;
7286#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007287 while (col < screen_Columns
7288 && (len < 0 || (int)(ptr - text) < len)
7289 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 {
7291 c = *ptr;
7292#ifdef FEAT_MBYTE
7293 /* check if this is the first byte of a multibyte */
7294 if (has_mbyte)
7295 {
7296 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007297 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007299 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7301 mbyte_cells = 1;
7302 else if (enc_dbcs != 0)
7303 mbyte_cells = mbyte_blen;
7304 else /* enc_utf8 */
7305 {
7306 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007307 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 (int)((text + len) - ptr));
7309 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007310 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007312# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 /* Non-BMP character: display as ? or fullwidth ?. */
7314 if (u8c >= 0x10000)
7315 {
7316 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7317 if (attr == 0)
7318 attr = hl_attr(HLF_8);
7319 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321# ifdef FEAT_ARABIC
7322 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7323 {
7324 /* Do Arabic shaping. */
7325 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7326 {
7327 /* Past end of string to be displayed. */
7328 nc = NUL;
7329 nc1 = NUL;
7330 }
7331 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007332 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007333 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7334 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007335 nc1 = pcc[0];
7336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 pc = prev_c;
7338 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007339 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 }
7341 else
7342 prev_c = u8c;
7343# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007344 if (col + mbyte_cells > screen_Columns)
7345 {
7346 /* Only 1 cell left, but character requires 2 cells:
7347 * display a '>' in the last column to avoid wrapping. */
7348 c = '>';
7349 mbyte_cells = 1;
7350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 }
7352 }
7353#endif
7354
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007355#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7356 force_redraw_this = force_redraw_next;
7357 force_redraw_next = FALSE;
7358#endif
7359
7360 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361#ifdef FEAT_MBYTE
7362 || (mbyte_cells == 2
7363 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7364 || (enc_dbcs == DBCS_JPNU
7365 && c == 0x8e
7366 && ScreenLines2[off] != ptr[1])
7367 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007368 && (ScreenLinesUC[off] !=
7369 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7370 || (ScreenLinesUC[off] != 0
7371 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372#endif
7373 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007374 || exmode_active;
7375
7376 if (need_redraw
7377#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7378 || force_redraw_this
7379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 )
7381 {
7382#if defined(FEAT_GUI) || defined(UNIX)
7383 /* The bold trick makes a single row of pixels appear in the next
7384 * character. When a bold character is removed, the next
7385 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007386 * and for some xterms. */
7387 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388# ifdef FEAT_GUI
7389 gui.in_use
7390# endif
7391# if defined(FEAT_GUI) && defined(UNIX)
7392 ||
7393# endif
7394# ifdef UNIX
7395 term_is_xterm
7396# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007397 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007399 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007401 if (n > HL_ALL)
7402 n = syn_attr2attr(n);
7403 if (n & HL_BOLD)
7404 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 }
7406#endif
7407#ifdef FEAT_MBYTE
7408 /* When at the end of the text and overwriting a two-cell
7409 * character with a one-cell character, need to clear the next
7410 * cell. Also when overwriting the left halve of a two-cell char
7411 * with the right halve of a two-cell char. Do this only once
7412 * (mb_off2cells() may return 2 on the right halve). */
7413 if (clear_next_cell)
7414 clear_next_cell = FALSE;
7415 else if (has_mbyte
7416 && (len < 0 ? ptr[mbyte_blen] == NUL
7417 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007418 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007420 && (*mb_off2cells)(off, max_off) == 1
7421 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 clear_next_cell = TRUE;
7423
7424 /* Make sure we never leave a second byte of a double-byte behind,
7425 * it confuses mb_off2cells(). */
7426 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007427 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007429 && (*mb_off2cells)(off, max_off) == 1
7430 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 ScreenLines[off + mbyte_blen] = 0;
7432#endif
7433 ScreenLines[off] = c;
7434 ScreenAttrs[off] = attr;
7435#ifdef FEAT_MBYTE
7436 if (enc_utf8)
7437 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007438 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 ScreenLinesUC[off] = 0;
7440 else
7441 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007442 int i;
7443
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007445 for (i = 0; i < Screen_mco; ++i)
7446 {
7447 ScreenLinesC[i][off] = u8cc[i];
7448 if (u8cc[i] == 0)
7449 break;
7450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 }
7452 if (mbyte_cells == 2)
7453 {
7454 ScreenLines[off + 1] = 0;
7455 ScreenAttrs[off + 1] = attr;
7456 }
7457 screen_char(off, row, col);
7458 }
7459 else if (mbyte_cells == 2)
7460 {
7461 ScreenLines[off + 1] = ptr[1];
7462 ScreenAttrs[off + 1] = attr;
7463 screen_char_2(off, row, col);
7464 }
7465 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7466 {
7467 ScreenLines2[off] = ptr[1];
7468 screen_char(off, row, col);
7469 }
7470 else
7471#endif
7472 screen_char(off, row, col);
7473 }
7474#ifdef FEAT_MBYTE
7475 if (has_mbyte)
7476 {
7477 off += mbyte_cells;
7478 col += mbyte_cells;
7479 ptr += mbyte_blen;
7480 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007481 {
7482 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007484 len = -1;
7485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 }
7487 else
7488#endif
7489 {
7490 ++off;
7491 ++col;
7492 ++ptr;
7493 }
7494 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007495
7496#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7497 /* If we detected the next character needs to be redrawn, but the text
7498 * doesn't extend up to there, update the character here. */
7499 if (force_redraw_next && col < screen_Columns)
7500 {
7501# ifdef FEAT_MBYTE
7502 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7503 screen_char_2(off, row, col);
7504 else
7505# endif
7506 screen_char(off, row, col);
7507 }
7508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509}
7510
7511#ifdef FEAT_SEARCH_EXTRA
7512/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007513 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 */
7515 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007516start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517{
7518 if (p_hls && !no_hlsearch)
7519 {
7520 last_pat_prog(&search_hl.rm);
7521 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007522# ifdef FEAT_RELTIME
7523 /* Set the time limit to 'redrawtime'. */
7524 profile_setlimit(p_rdt, &search_hl.tm);
7525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 }
7527}
7528
7529/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007530 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 */
7532 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007533end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534{
7535 if (search_hl.rm.regprog != NULL)
7536 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007537 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 search_hl.rm.regprog = NULL;
7539 }
7540}
7541
7542/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007543 * Init for calling prepare_search_hl().
7544 */
7545 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007546init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007547{
7548 matchitem_T *cur;
7549
7550 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7551 * match */
7552 cur = wp->w_match_head;
7553 while (cur != NULL)
7554 {
7555 cur->hl.rm = cur->match;
7556 if (cur->hlg_id == 0)
7557 cur->hl.attr = 0;
7558 else
7559 cur->hl.attr = syn_id2attr(cur->hlg_id);
7560 cur->hl.buf = wp->w_buffer;
7561 cur->hl.lnum = 0;
7562 cur->hl.first_lnum = 0;
7563# ifdef FEAT_RELTIME
7564 /* Set the time limit to 'redrawtime'. */
7565 profile_setlimit(p_rdt, &(cur->hl.tm));
7566# endif
7567 cur = cur->next;
7568 }
7569 search_hl.buf = wp->w_buffer;
7570 search_hl.lnum = 0;
7571 search_hl.first_lnum = 0;
7572 /* time limit is set at the toplevel, for all windows */
7573}
7574
7575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 * Advance to the match in window "wp" line "lnum" or past it.
7577 */
7578 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007579prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007581 matchitem_T *cur; /* points to the match list */
7582 match_T *shl; /* points to search_hl or a match */
7583 int shl_flag; /* flag to indicate whether search_hl
7584 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007585 int pos_inprogress; /* marks that position match search is
7586 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 int n;
7588
7589 /*
7590 * When using a multi-line pattern, start searching at the top
7591 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007592 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007594 cur = wp->w_match_head;
7595 shl_flag = FALSE;
7596 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007598 if (shl_flag == FALSE)
7599 {
7600 shl = &search_hl;
7601 shl_flag = TRUE;
7602 }
7603 else
7604 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 if (shl->rm.regprog != NULL
7606 && shl->lnum == 0
7607 && re_multiline(shl->rm.regprog))
7608 {
7609 if (shl->first_lnum == 0)
7610 {
7611# ifdef FEAT_FOLDING
7612 for (shl->first_lnum = lnum;
7613 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7614 if (hasFoldingWin(wp, shl->first_lnum - 1,
7615 NULL, NULL, TRUE, NULL))
7616 break;
7617# else
7618 shl->first_lnum = wp->w_topline;
7619# endif
7620 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007621 if (cur != NULL)
7622 cur->pos.cur = 0;
7623 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007625 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7626 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007628 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7629 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007630 pos_inprogress = cur == NULL || cur->pos.cur == 0
7631 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 if (shl->lnum != 0)
7633 {
7634 shl->first_lnum = shl->lnum
7635 + shl->rm.endpos[0].lnum
7636 - shl->rm.startpos[0].lnum;
7637 n = shl->rm.endpos[0].col;
7638 }
7639 else
7640 {
7641 ++shl->first_lnum;
7642 n = 0;
7643 }
7644 }
7645 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007646 if (shl != &search_hl && cur != NULL)
7647 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
7649}
7650
7651/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007652 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 * Uses shl->buf.
7654 * Sets shl->lnum and shl->rm contents.
7655 * Note: Assumes a previous match is always before "lnum", unless
7656 * shl->lnum is zero.
7657 * Careful: Any pointers for buffer lines will become invalid.
7658 */
7659 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007660next_search_hl(
7661 win_T *win,
7662 match_T *shl, /* points to search_hl or a match */
7663 linenr_T lnum,
7664 colnr_T mincol, /* minimal column for a match */
7665 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
7667 linenr_T l;
7668 colnr_T matchcol;
7669 long nmatched;
7670
7671 if (shl->lnum != 0)
7672 {
7673 /* Check for three situations:
7674 * 1. If the "lnum" is below a previous match, start a new search.
7675 * 2. If the previous match includes "mincol", use it.
7676 * 3. Continue after the previous match.
7677 */
7678 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7679 if (lnum > l)
7680 shl->lnum = 0;
7681 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7682 return;
7683 }
7684
7685 /*
7686 * Repeat searching for a match until one is found that includes "mincol"
7687 * or none is found in this line.
7688 */
7689 called_emsg = FALSE;
7690 for (;;)
7691 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007692#ifdef FEAT_RELTIME
7693 /* Stop searching after passing the time limit. */
7694 if (profile_passed_limit(&(shl->tm)))
7695 {
7696 shl->lnum = 0; /* no match found in time */
7697 break;
7698 }
7699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 /* Three situations:
7701 * 1. No useful previous match: search from start of line.
7702 * 2. Not Vi compatible or empty match: continue at next character.
7703 * Break the loop if this is beyond the end of the line.
7704 * 3. Vi compatible searching: continue at end of previous match.
7705 */
7706 if (shl->lnum == 0)
7707 matchcol = 0;
7708 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7709 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007710 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007712 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007713
7714 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007715 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007716 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007718 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 shl->lnum = 0;
7720 break;
7721 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007722#ifdef FEAT_MBYTE
7723 if (has_mbyte)
7724 matchcol += mb_ptr2len(ml);
7725 else
7726#endif
7727 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 }
7729 else
7730 matchcol = shl->rm.endpos[0].col;
7731
7732 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007733 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007735 /* Remember whether shl->rm is using a copy of the regprog in
7736 * cur->match. */
7737 int regprog_is_copy = (shl != &search_hl && cur != NULL
7738 && shl == &cur->hl
7739 && cur->match.regprog == cur->hl.rm.regprog);
7740
Bram Moolenaarb3414592014-06-17 17:48:32 +02007741 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7742 matchcol,
7743#ifdef FEAT_RELTIME
7744 &(shl->tm)
7745#else
7746 NULL
7747#endif
7748 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007749 /* Copy the regprog, in case it got freed and recompiled. */
7750 if (regprog_is_copy)
7751 cur->match.regprog = cur->hl.rm.regprog;
7752
Bram Moolenaarb3414592014-06-17 17:48:32 +02007753 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007754 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007755 /* Error while handling regexp: stop using this regexp. */
7756 if (shl == &search_hl)
7757 {
7758 /* don't free regprog in the match list, it's a copy */
7759 vim_regfree(shl->rm.regprog);
7760 SET_NO_HLSEARCH(TRUE);
7761 }
7762 shl->rm.regprog = NULL;
7763 shl->lnum = 0;
7764 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7765 message */
7766 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007767 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007768 }
7769 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007770 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007771 else
7772 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 if (nmatched == 0)
7774 {
7775 shl->lnum = 0; /* no match found */
7776 break;
7777 }
7778 if (shl->rm.startpos[0].lnum > 0
7779 || shl->rm.startpos[0].col >= mincol
7780 || nmatched > 1
7781 || shl->rm.endpos[0].col > mincol)
7782 {
7783 shl->lnum += shl->rm.startpos[0].lnum;
7784 break; /* useful match found */
7785 }
7786 }
7787}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788
Bram Moolenaar85077472016-10-16 14:35:48 +02007789/*
7790 * If there is a match fill "shl" and return one.
7791 * Return zero otherwise.
7792 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007793 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007794next_search_hl_pos(
7795 match_T *shl, /* points to a match */
7796 linenr_T lnum,
7797 posmatch_T *posmatch, /* match positions */
7798 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007799{
7800 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007801 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007802
Bram Moolenaarb3414592014-06-17 17:48:32 +02007803 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7804 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007805 llpos_T *pos = &posmatch->pos[i];
7806
7807 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007808 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007809 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007810 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007811 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007812 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007813 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007814 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007815 /* if this match comes before the one at "found" then swap
7816 * them */
7817 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007818 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007819 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007820
Bram Moolenaar85077472016-10-16 14:35:48 +02007821 *pos = posmatch->pos[found];
7822 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007823 }
7824 }
7825 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007826 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007827 }
7828 }
7829 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007830 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007831 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007832 colnr_T start = posmatch->pos[found].col == 0
7833 ? 0 : posmatch->pos[found].col - 1;
7834 colnr_T end = posmatch->pos[found].col == 0
7835 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007836
Bram Moolenaar85077472016-10-16 14:35:48 +02007837 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007838 shl->rm.startpos[0].lnum = 0;
7839 shl->rm.startpos[0].col = start;
7840 shl->rm.endpos[0].lnum = 0;
7841 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007842 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007843 posmatch->cur = found + 1;
7844 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007845 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007846 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007847}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007848#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007849
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007851screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
7853 attrentry_T *aep = NULL;
7854
7855 screen_attr = attr;
7856 if (full_screen
7857#ifdef WIN3264
7858 && termcap_active
7859#endif
7860 )
7861 {
7862#ifdef FEAT_GUI
7863 if (gui.in_use)
7864 {
7865 char buf[20];
7866
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007867 /* The GUI handles this internally. */
7868 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 OUT_STR(buf);
7870 }
7871 else
7872#endif
7873 {
7874 if (attr > HL_ALL) /* special HL attr. */
7875 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007876 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 aep = syn_cterm_attr2entry(attr);
7878 else
7879 aep = syn_term_attr2entry(attr);
7880 if (aep == NULL) /* did ":syntax clear" */
7881 attr = 0;
7882 else
7883 attr = aep->ae_attr;
7884 }
7885 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7886 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007887 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007888#ifdef FEAT_TERMGUICOLORS
7889 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007890 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007891#endif
7892 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007893#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007894 )
7895#endif
7896 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007897 /* If the Normal FG color has BOLD attribute and the new HL
7898 * has a FG color defined, clear BOLD. */
7899 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7901 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007902 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7903 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 out_str(T_US);
7905 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7906 out_str(T_CZH);
7907 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7908 out_str(T_MR);
7909
7910 /*
7911 * Output the color or start string after bold etc., in case the
7912 * bold etc. override the color setting.
7913 */
7914 if (aep != NULL)
7915 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007916#ifdef FEAT_TERMGUICOLORS
7917 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007919 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007920 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007921 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007922 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 }
7924 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007927 if (t_colors > 1)
7928 {
7929 if (aep->ae_u.cterm.fg_color)
7930 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7931 if (aep->ae_u.cterm.bg_color)
7932 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7933 }
7934 else
7935 {
7936 if (aep->ae_u.term.start != NULL)
7937 out_str(aep->ae_u.term.start);
7938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 }
7940 }
7941 }
7942 }
7943}
7944
7945 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007946screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947{
7948 int do_ME = FALSE; /* output T_ME code */
7949
7950 if (screen_attr != 0
7951#ifdef WIN3264
7952 && termcap_active
7953#endif
7954 )
7955 {
7956#ifdef FEAT_GUI
7957 if (gui.in_use)
7958 {
7959 char buf[20];
7960
7961 /* use internal GUI code */
7962 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7963 OUT_STR(buf);
7964 }
7965 else
7966#endif
7967 {
7968 if (screen_attr > HL_ALL) /* special HL attr. */
7969 {
7970 attrentry_T *aep;
7971
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007972 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {
7974 /*
7975 * Assume that t_me restores the original colors!
7976 */
7977 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007978 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007979#ifdef FEAT_TERMGUICOLORS
7980 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007981 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
7982 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007983#endif
7984 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007985#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007986 )
7987#endif
7988 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 do_ME = TRUE;
7990 }
7991 else
7992 {
7993 aep = syn_term_attr2entry(screen_attr);
7994 if (aep != NULL && aep->ae_u.term.stop != NULL)
7995 {
7996 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7997 do_ME = TRUE;
7998 else
7999 out_str(aep->ae_u.term.stop);
8000 }
8001 }
8002 if (aep == NULL) /* did ":syntax clear" */
8003 screen_attr = 0;
8004 else
8005 screen_attr = aep->ae_attr;
8006 }
8007
8008 /*
8009 * Often all ending-codes are equal to T_ME. Avoid outputting the
8010 * same sequence several times.
8011 */
8012 if (screen_attr & HL_STANDOUT)
8013 {
8014 if (STRCMP(T_SE, T_ME) == 0)
8015 do_ME = TRUE;
8016 else
8017 out_str(T_SE);
8018 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008019 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 {
8021 if (STRCMP(T_UE, T_ME) == 0)
8022 do_ME = TRUE;
8023 else
8024 out_str(T_UE);
8025 }
8026 if (screen_attr & HL_ITALIC)
8027 {
8028 if (STRCMP(T_CZR, T_ME) == 0)
8029 do_ME = TRUE;
8030 else
8031 out_str(T_CZR);
8032 }
8033 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8034 out_str(T_ME);
8035
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008036#ifdef FEAT_TERMGUICOLORS
8037 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008039 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008040 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008041 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008042 term_bg_rgb_color(cterm_normal_bg_gui_color);
8043 }
8044 else
8045#endif
8046 {
8047 if (t_colors > 1)
8048 {
8049 /* set Normal cterm colors */
8050 if (cterm_normal_fg_color != 0)
8051 term_fg_color(cterm_normal_fg_color - 1);
8052 if (cterm_normal_bg_color != 0)
8053 term_bg_color(cterm_normal_bg_color - 1);
8054 if (cterm_normal_fg_bold)
8055 out_str(T_MD);
8056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 }
8058 }
8059 }
8060 screen_attr = 0;
8061}
8062
8063/*
8064 * Reset the colors for a cterm. Used when leaving Vim.
8065 * The machine specific code may override this again.
8066 */
8067 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008068reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008070 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {
8072 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008073#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008074 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8075 || cterm_normal_bg_gui_color != INVALCOLOR)
8076 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008077#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {
8081 out_str(T_OP);
8082 screen_attr = -1;
8083 }
8084 if (cterm_normal_fg_bold)
8085 {
8086 out_str(T_ME);
8087 screen_attr = -1;
8088 }
8089 }
8090}
8091
8092/*
8093 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8094 * using the attributes from ScreenAttrs["off"].
8095 */
8096 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008097screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 int attr;
8100
8101 /* Check for illegal values, just in case (could happen just after
8102 * resizing). */
8103 if (row >= screen_Rows || col >= screen_Columns)
8104 return;
8105
Bram Moolenaar494838a2015-02-10 19:20:37 +01008106 /* Outputting a character in the last cell on the screen may scroll the
8107 * screen up. Only do it when the "xn" termcap property is set, otherwise
8108 * mark the character invalid (update it when scrolled up). */
8109 if (*T_XN == NUL
8110 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111#ifdef FEAT_RIGHTLEFT
8112 /* account for first command-line character in rightleft mode */
8113 && !cmdmsg_rl
8114#endif
8115 )
8116 {
8117 ScreenAttrs[off] = (sattr_T)-1;
8118 return;
8119 }
8120
8121 /*
8122 * Stop highlighting first, so it's easier to move the cursor.
8123 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008124#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 if (screen_char_attr != 0)
8126 attr = screen_char_attr;
8127 else
8128#endif
8129 attr = ScreenAttrs[off];
8130 if (screen_attr != attr)
8131 screen_stop_highlight();
8132
8133 windgoto(row, col);
8134
8135 if (screen_attr != attr)
8136 screen_start_highlight(attr);
8137
8138#ifdef FEAT_MBYTE
8139 if (enc_utf8 && ScreenLinesUC[off] != 0)
8140 {
8141 char_u buf[MB_MAXBYTES + 1];
8142
8143 /* Convert UTF-8 character to bytes and write it. */
8144
8145 buf[utfc_char2bytes(off, buf)] = NUL;
8146
8147 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008148 if (utf_ambiguous_width(ScreenLinesUC[off]))
8149 screen_cur_col = 9999;
8150 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 ++screen_cur_col;
8152 }
8153 else
8154#endif
8155 {
8156#ifdef FEAT_MBYTE
8157 out_flush_check();
8158#endif
8159 out_char(ScreenLines[off]);
8160#ifdef FEAT_MBYTE
8161 /* double-byte character in single-width cell */
8162 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8163 out_char(ScreenLines2[off]);
8164#endif
8165 }
8166
8167 screen_cur_col++;
8168}
8169
8170#ifdef FEAT_MBYTE
8171
8172/*
8173 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8174 * on the screen at position 'row' and 'col'.
8175 * The attributes of the first byte is used for all. This is required to
8176 * output the two bytes of a double-byte character with nothing in between.
8177 */
8178 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008179screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180{
8181 /* Check for illegal values (could be wrong when screen was resized). */
8182 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8183 return;
8184
8185 /* Outputting the last character on the screen may scrollup the screen.
8186 * Don't to it! Mark the character invalid (update it when scrolled up) */
8187 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8188 {
8189 ScreenAttrs[off] = (sattr_T)-1;
8190 return;
8191 }
8192
8193 /* Output the first byte normally (positions the cursor), then write the
8194 * second byte directly. */
8195 screen_char(off, row, col);
8196 out_char(ScreenLines[off + 1]);
8197 ++screen_cur_col;
8198}
8199#endif
8200
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008201#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202/*
8203 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8204 * This uses the contents of ScreenLines[] and doesn't change it.
8205 */
8206 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008207screen_draw_rectangle(
8208 int row,
8209 int col,
8210 int height,
8211 int width,
8212 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213{
8214 int r, c;
8215 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008216#ifdef FEAT_MBYTE
8217 int max_off;
8218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008220 /* Can't use ScreenLines unless initialized */
8221 if (ScreenLines == NULL)
8222 return;
8223
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 if (invert)
8225 screen_char_attr = HL_INVERSE;
8226 for (r = row; r < row + height; ++r)
8227 {
8228 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008229#ifdef FEAT_MBYTE
8230 max_off = off + screen_Columns;
8231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 for (c = col; c < col + width; ++c)
8233 {
8234#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008235 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {
8237 screen_char_2(off + c, r, c);
8238 ++c;
8239 }
8240 else
8241#endif
8242 {
8243 screen_char(off + c, r, c);
8244#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008245 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 ++c;
8247#endif
8248 }
8249 }
8250 }
8251 screen_char_attr = 0;
8252}
8253#endif
8254
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008255#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256/*
8257 * Redraw the characters for a vertically split window.
8258 */
8259 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008260redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
8262 int col;
8263 int width;
8264
8265# ifdef FEAT_CLIPBOARD
8266 clip_may_clear_selection(row, end - 1);
8267# endif
8268
8269 if (wp == NULL)
8270 {
8271 col = 0;
8272 width = Columns;
8273 }
8274 else
8275 {
8276 col = wp->w_wincol;
8277 width = wp->w_width;
8278 }
8279 screen_draw_rectangle(row, col, end - row, width, FALSE);
8280}
8281#endif
8282
8283/*
8284 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8285 * with character 'c1' in first column followed by 'c2' in the other columns.
8286 * Use attributes 'attr'.
8287 */
8288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008289screen_fill(
8290 int start_row,
8291 int end_row,
8292 int start_col,
8293 int end_col,
8294 int c1,
8295 int c2,
8296 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
8298 int row;
8299 int col;
8300 int off;
8301 int end_off;
8302 int did_delete;
8303 int c;
8304 int norm_term;
8305#if defined(FEAT_GUI) || defined(UNIX)
8306 int force_next = FALSE;
8307#endif
8308
8309 if (end_row > screen_Rows) /* safety check */
8310 end_row = screen_Rows;
8311 if (end_col > screen_Columns) /* safety check */
8312 end_col = screen_Columns;
8313 if (ScreenLines == NULL
8314 || start_row >= end_row
8315 || start_col >= end_col) /* nothing to do */
8316 return;
8317
8318 /* it's a "normal" terminal when not in a GUI or cterm */
8319 norm_term = (
8320#ifdef FEAT_GUI
8321 !gui.in_use &&
8322#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008323 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 for (row = start_row; row < end_row; ++row)
8325 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008326#ifdef FEAT_MBYTE
8327 if (has_mbyte
8328# ifdef FEAT_GUI
8329 && !gui.in_use
8330# endif
8331 )
8332 {
8333 /* When drawing over the right halve of a double-wide char clear
8334 * out the left halve. When drawing over the left halve of a
8335 * double wide-char clear out the right halve. Only needed in a
8336 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008337 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008338 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008339 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008340 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008341 }
8342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 /*
8344 * Try to use delete-line termcap code, when no attributes or in a
8345 * "normal" terminal, where a bold/italic space is just a
8346 * space.
8347 */
8348 did_delete = FALSE;
8349 if (c2 == ' '
8350 && end_col == Columns
8351 && can_clear(T_CE)
8352 && (attr == 0
8353 || (norm_term
8354 && attr <= HL_ALL
8355 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8356 {
8357 /*
8358 * check if we really need to clear something
8359 */
8360 col = start_col;
8361 if (c1 != ' ') /* don't clear first char */
8362 ++col;
8363
8364 off = LineOffset[row] + col;
8365 end_off = LineOffset[row] + end_col;
8366
8367 /* skip blanks (used often, keep it fast!) */
8368#ifdef FEAT_MBYTE
8369 if (enc_utf8)
8370 while (off < end_off && ScreenLines[off] == ' '
8371 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8372 ++off;
8373 else
8374#endif
8375 while (off < end_off && ScreenLines[off] == ' '
8376 && ScreenAttrs[off] == 0)
8377 ++off;
8378 if (off < end_off) /* something to be cleared */
8379 {
8380 col = off - LineOffset[row];
8381 screen_stop_highlight();
8382 term_windgoto(row, col);/* clear rest of this screen line */
8383 out_str(T_CE);
8384 screen_start(); /* don't know where cursor is now */
8385 col = end_col - col;
8386 while (col--) /* clear chars in ScreenLines */
8387 {
8388 ScreenLines[off] = ' ';
8389#ifdef FEAT_MBYTE
8390 if (enc_utf8)
8391 ScreenLinesUC[off] = 0;
8392#endif
8393 ScreenAttrs[off] = 0;
8394 ++off;
8395 }
8396 }
8397 did_delete = TRUE; /* the chars are cleared now */
8398 }
8399
8400 off = LineOffset[row] + start_col;
8401 c = c1;
8402 for (col = start_col; col < end_col; ++col)
8403 {
8404 if (ScreenLines[off] != c
8405#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008406 || (enc_utf8 && (int)ScreenLinesUC[off]
8407 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408#endif
8409 || ScreenAttrs[off] != attr
8410#if defined(FEAT_GUI) || defined(UNIX)
8411 || force_next
8412#endif
8413 )
8414 {
8415#if defined(FEAT_GUI) || defined(UNIX)
8416 /* The bold trick may make a single row of pixels appear in
8417 * the next character. When a bold character is removed, the
8418 * next character should be redrawn too. This happens for our
8419 * own GUI and for some xterms. */
8420 if (
8421# ifdef FEAT_GUI
8422 gui.in_use
8423# endif
8424# if defined(FEAT_GUI) && defined(UNIX)
8425 ||
8426# endif
8427# ifdef UNIX
8428 term_is_xterm
8429# endif
8430 )
8431 {
8432 if (ScreenLines[off] != ' '
8433 && (ScreenAttrs[off] > HL_ALL
8434 || ScreenAttrs[off] & HL_BOLD))
8435 force_next = TRUE;
8436 else
8437 force_next = FALSE;
8438 }
8439#endif
8440 ScreenLines[off] = c;
8441#ifdef FEAT_MBYTE
8442 if (enc_utf8)
8443 {
8444 if (c >= 0x80)
8445 {
8446 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008447 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 }
8449 else
8450 ScreenLinesUC[off] = 0;
8451 }
8452#endif
8453 ScreenAttrs[off] = attr;
8454 if (!did_delete || c != ' ')
8455 screen_char(off, row, col);
8456 }
8457 ++off;
8458 if (col == start_col)
8459 {
8460 if (did_delete)
8461 break;
8462 c = c2;
8463 }
8464 }
8465 if (end_col == Columns)
8466 LineWraps[row] = FALSE;
8467 if (row == Rows - 1) /* overwritten the command line */
8468 {
8469 redraw_cmdline = TRUE;
8470 if (c1 == ' ' && c2 == ' ')
8471 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008472 if (start_col == 0)
8473 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 }
8475 }
8476}
8477
8478/*
8479 * Check if there should be a delay. Used before clearing or redrawing the
8480 * screen or the command line.
8481 */
8482 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008483check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8486 && !did_wait_return
8487 && emsg_silent == 0)
8488 {
8489 out_flush();
8490 ui_delay(1000L, TRUE);
8491 emsg_on_display = FALSE;
8492 if (check_msg_scroll)
8493 msg_scroll = FALSE;
8494 }
8495}
8496
8497/*
8498 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008499 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 * Returns TRUE if there is a valid screen to write to.
8501 * Returns FALSE when starting up and screen not initialized yet.
8502 */
8503 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008504screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008506 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 return (ScreenLines != NULL);
8508}
8509
8510/*
8511 * Resize the shell to Rows and Columns.
8512 * Allocate ScreenLines[] and associated items.
8513 *
8514 * There may be some time between setting Rows and Columns and (re)allocating
8515 * ScreenLines[]. This happens when starting up and when (manually) changing
8516 * the shell size. Always use screen_Rows and screen_Columns to access items
8517 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8518 * final size of the shell is needed.
8519 */
8520 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008521screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 int new_row, old_row;
8524#ifdef FEAT_GUI
8525 int old_Rows;
8526#endif
8527 win_T *wp;
8528 int outofmem = FALSE;
8529 int len;
8530 schar_T *new_ScreenLines;
8531#ifdef FEAT_MBYTE
8532 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008533 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008535 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536#endif
8537 sattr_T *new_ScreenAttrs;
8538 unsigned *new_LineOffset;
8539 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008540#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008541 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008542 tabpage_T *tp;
8543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008545 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008546#ifdef FEAT_AUTOCMD
8547 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008549retry:
8550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 /*
8552 * Allocation of the screen buffers is done only when the size changes and
8553 * when Rows and Columns have been set and we have started doing full
8554 * screen stuff.
8555 */
8556 if ((ScreenLines != NULL
8557 && Rows == screen_Rows
8558 && Columns == screen_Columns
8559#ifdef FEAT_MBYTE
8560 && enc_utf8 == (ScreenLinesUC != NULL)
8561 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008562 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563#endif
8564 )
8565 || Rows == 0
8566 || Columns == 0
8567 || (!full_screen && ScreenLines == NULL))
8568 return;
8569
8570 /*
8571 * It's possible that we produce an out-of-memory message below, which
8572 * will cause this function to be called again. To break the loop, just
8573 * return here.
8574 */
8575 if (entered)
8576 return;
8577 entered = TRUE;
8578
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008579 /*
8580 * Note that the window sizes are updated before reallocating the arrays,
8581 * thus we must not redraw here!
8582 */
8583 ++RedrawingDisabled;
8584
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 win_new_shellsize(); /* fit the windows in the new sized shell */
8586
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 comp_col(); /* recompute columns for shown command and ruler */
8588
8589 /*
8590 * We're changing the size of the screen.
8591 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8592 * - Move lines from the old arrays into the new arrays, clear extra
8593 * lines (unless the screen is going to be cleared).
8594 * - Free the old arrays.
8595 *
8596 * If anything fails, make ScreenLines NULL, so we don't do anything!
8597 * Continuing with the old ScreenLines may result in a crash, because the
8598 * size is wrong.
8599 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008600 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008602#ifdef FEAT_AUTOCMD
8603 if (aucmd_win != NULL)
8604 win_free_lsize(aucmd_win);
8605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
8607 new_ScreenLines = (schar_T *)lalloc((long_u)(
8608 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8609#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008610 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 if (enc_utf8)
8612 {
8613 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8614 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008615 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008616 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8618 }
8619 if (enc_dbcs == DBCS_JPNU)
8620 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8621 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8622#endif
8623 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8624 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8625 new_LineOffset = (unsigned *)lalloc((long_u)(
8626 Rows * sizeof(unsigned)), FALSE);
8627 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008628#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008629 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008632 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 {
8634 if (win_alloc_lines(wp) == FAIL)
8635 {
8636 outofmem = TRUE;
8637#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008638 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639#endif
8640 }
8641 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008642#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008643 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8644 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008645 outofmem = TRUE;
8646#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008647#ifdef FEAT_WINDOWS
8648give_up:
8649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008651#ifdef FEAT_MBYTE
8652 for (i = 0; i < p_mco; ++i)
8653 if (new_ScreenLinesC[i] == NULL)
8654 break;
8655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 if (new_ScreenLines == NULL
8657#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008658 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8660#endif
8661 || new_ScreenAttrs == NULL
8662 || new_LineOffset == NULL
8663 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008664#ifdef FEAT_WINDOWS
8665 || new_TabPageIdxs == NULL
8666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 || outofmem)
8668 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008669 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008670 {
8671 /* guess the size */
8672 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8673
8674 /* Remember we did this to avoid getting outofmem messages over
8675 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008676 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 vim_free(new_ScreenLines);
8679 new_ScreenLines = NULL;
8680#ifdef FEAT_MBYTE
8681 vim_free(new_ScreenLinesUC);
8682 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008683 for (i = 0; i < p_mco; ++i)
8684 {
8685 vim_free(new_ScreenLinesC[i]);
8686 new_ScreenLinesC[i] = NULL;
8687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 vim_free(new_ScreenLines2);
8689 new_ScreenLines2 = NULL;
8690#endif
8691 vim_free(new_ScreenAttrs);
8692 new_ScreenAttrs = NULL;
8693 vim_free(new_LineOffset);
8694 new_LineOffset = NULL;
8695 vim_free(new_LineWraps);
8696 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008697#ifdef FEAT_WINDOWS
8698 vim_free(new_TabPageIdxs);
8699 new_TabPageIdxs = NULL;
8700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 }
8702 else
8703 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008704 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008705
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 for (new_row = 0; new_row < Rows; ++new_row)
8707 {
8708 new_LineOffset[new_row] = new_row * Columns;
8709 new_LineWraps[new_row] = FALSE;
8710
8711 /*
8712 * If the screen is not going to be cleared, copy as much as
8713 * possible from the old screen to the new one and clear the rest
8714 * (used when resizing the window at the "--more--" prompt or when
8715 * executing an external command, for the GUI).
8716 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008717 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 {
8719 (void)vim_memset(new_ScreenLines + new_row * Columns,
8720 ' ', (size_t)Columns * sizeof(schar_T));
8721#ifdef FEAT_MBYTE
8722 if (enc_utf8)
8723 {
8724 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8725 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008726 for (i = 0; i < p_mco; ++i)
8727 (void)vim_memset(new_ScreenLinesC[i]
8728 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 0, (size_t)Columns * sizeof(u8char_T));
8730 }
8731 if (enc_dbcs == DBCS_JPNU)
8732 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8733 0, (size_t)Columns * sizeof(schar_T));
8734#endif
8735 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8736 0, (size_t)Columns * sizeof(sattr_T));
8737 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008738 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 {
8740 if (screen_Columns < Columns)
8741 len = screen_Columns;
8742 else
8743 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008744#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008745 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008746 * may be invalid now. Also when p_mco changes. */
8747 if (!(enc_utf8 && ScreenLinesUC == NULL)
8748 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008749#endif
8750 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8751 ScreenLines + LineOffset[old_row],
8752 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008754 if (enc_utf8 && ScreenLinesUC != NULL
8755 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 {
8757 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8758 ScreenLinesUC + LineOffset[old_row],
8759 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008760 for (i = 0; i < p_mco; ++i)
8761 mch_memmove(new_ScreenLinesC[i]
8762 + new_LineOffset[new_row],
8763 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 (size_t)len * sizeof(u8char_T));
8765 }
8766 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8767 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8768 ScreenLines2 + LineOffset[old_row],
8769 (size_t)len * sizeof(schar_T));
8770#endif
8771 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8772 ScreenAttrs + LineOffset[old_row],
8773 (size_t)len * sizeof(sattr_T));
8774 }
8775 }
8776 }
8777 /* Use the last line of the screen for the current line. */
8778 current_ScreenLine = new_ScreenLines + Rows * Columns;
8779 }
8780
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008781 free_screenlines();
8782
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 ScreenLines = new_ScreenLines;
8784#ifdef FEAT_MBYTE
8785 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008786 for (i = 0; i < p_mco; ++i)
8787 ScreenLinesC[i] = new_ScreenLinesC[i];
8788 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 ScreenLines2 = new_ScreenLines2;
8790#endif
8791 ScreenAttrs = new_ScreenAttrs;
8792 LineOffset = new_LineOffset;
8793 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008794#ifdef FEAT_WINDOWS
8795 TabPageIdxs = new_TabPageIdxs;
8796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797
8798 /* It's important that screen_Rows and screen_Columns reflect the actual
8799 * size of ScreenLines[]. Set them before calling anything. */
8800#ifdef FEAT_GUI
8801 old_Rows = screen_Rows;
8802#endif
8803 screen_Rows = Rows;
8804 screen_Columns = Columns;
8805
8806 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008807 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 screenclear2();
8809
8810#ifdef FEAT_GUI
8811 else if (gui.in_use
8812 && !gui.starting
8813 && ScreenLines != NULL
8814 && old_Rows != Rows)
8815 {
8816 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8817 /*
8818 * Adjust the position of the cursor, for when executing an external
8819 * command.
8820 */
8821 if (msg_row >= Rows) /* Rows got smaller */
8822 msg_row = Rows - 1; /* put cursor at last row */
8823 else if (Rows > old_Rows) /* Rows got bigger */
8824 msg_row += Rows - old_Rows; /* put cursor in same place */
8825 if (msg_col >= Columns) /* Columns got smaller */
8826 msg_col = Columns - 1; /* put cursor at last column */
8827 }
8828#endif
8829
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008831 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008832
8833#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008834 /*
8835 * Do not apply autocommands more than 3 times to avoid an endless loop
8836 * in case applying autocommands always changes Rows or Columns.
8837 */
8838 if (starting == 0 && ++retry_count <= 3)
8839 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008840 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008841 /* In rare cases, autocommands may have altered Rows or Columns,
8842 * jump back to check if we need to allocate the screen again. */
8843 goto retry;
8844 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846}
8847
8848 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008849free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008850{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008851#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008852 int i;
8853
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008854 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008855 for (i = 0; i < Screen_mco; ++i)
8856 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008857 vim_free(ScreenLines2);
8858#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008859 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008860 vim_free(ScreenAttrs);
8861 vim_free(LineOffset);
8862 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008863#ifdef FEAT_WINDOWS
8864 vim_free(TabPageIdxs);
8865#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008866}
8867
8868 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008869screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870{
8871 check_for_delay(FALSE);
8872 screenalloc(FALSE); /* allocate screen buffers if size changed */
8873 screenclear2(); /* clear the screen */
8874}
8875
8876 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008877screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
8879 int i;
8880
8881 if (starting == NO_SCREEN || ScreenLines == NULL
8882#ifdef FEAT_GUI
8883 || (gui.in_use && gui.starting)
8884#endif
8885 )
8886 return;
8887
8888#ifdef FEAT_GUI
8889 if (!gui.in_use)
8890#endif
8891 screen_attr = -1; /* force setting the Normal colors */
8892 screen_stop_highlight(); /* don't want highlighting here */
8893
8894#ifdef FEAT_CLIPBOARD
8895 /* disable selection without redrawing it */
8896 clip_scroll_selection(9999);
8897#endif
8898
8899 /* blank out ScreenLines */
8900 for (i = 0; i < Rows; ++i)
8901 {
8902 lineclear(LineOffset[i], (int)Columns);
8903 LineWraps[i] = FALSE;
8904 }
8905
8906 if (can_clear(T_CL))
8907 {
8908 out_str(T_CL); /* clear the display */
8909 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008910 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 }
8912 else
8913 {
8914 /* can't clear the screen, mark all chars with invalid attributes */
8915 for (i = 0; i < Rows; ++i)
8916 lineinvalid(LineOffset[i], (int)Columns);
8917 clear_cmdline = TRUE;
8918 }
8919
8920 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8921
8922 win_rest_invalid(firstwin);
8923 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008924#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008925 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 if (must_redraw == CLEAR) /* no need to clear again */
8928 must_redraw = NOT_VALID;
8929 compute_cmdrow();
8930 msg_row = cmdline_row; /* put cursor on last line for messages */
8931 msg_col = 0;
8932 screen_start(); /* don't know where cursor is now */
8933 msg_scrolled = 0; /* can't scroll back */
8934 msg_didany = FALSE;
8935 msg_didout = FALSE;
8936}
8937
8938/*
8939 * Clear one line in ScreenLines.
8940 */
8941 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008942lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
8944 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8945#ifdef FEAT_MBYTE
8946 if (enc_utf8)
8947 (void)vim_memset(ScreenLinesUC + off, 0,
8948 (size_t)width * sizeof(u8char_T));
8949#endif
8950 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8951}
8952
8953/*
8954 * Mark one line in ScreenLines invalid by setting the attributes to an
8955 * invalid value.
8956 */
8957 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008958lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959{
8960 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8961}
8962
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008963#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964/*
8965 * Copy part of a Screenline for vertically split window "wp".
8966 */
8967 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008968linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
8970 unsigned off_to = LineOffset[to] + wp->w_wincol;
8971 unsigned off_from = LineOffset[from] + wp->w_wincol;
8972
8973 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8974 wp->w_width * sizeof(schar_T));
8975# ifdef FEAT_MBYTE
8976 if (enc_utf8)
8977 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008978 int i;
8979
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8981 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008982 for (i = 0; i < p_mco; ++i)
8983 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8984 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 }
8986 if (enc_dbcs == DBCS_JPNU)
8987 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8988 wp->w_width * sizeof(schar_T));
8989# endif
8990 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8991 wp->w_width * sizeof(sattr_T));
8992}
8993#endif
8994
8995/*
8996 * Return TRUE if clearing with term string "p" would work.
8997 * It can't work when the string is empty or it won't set the right background.
8998 */
8999 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009000can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001{
9002 return (*p != NUL && (t_colors <= 1
9003#ifdef FEAT_GUI
9004 || gui.in_use
9005#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009006#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009007 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009008 || (!p_tgc && cterm_normal_bg_color == 0)
9009#else
9010 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009011#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009012 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013}
9014
9015/*
9016 * Reset cursor position. Use whenever cursor was moved because of outputting
9017 * something directly to the screen (shell commands) or a terminal control
9018 * code.
9019 */
9020 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009021screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022{
9023 screen_cur_row = screen_cur_col = 9999;
9024}
9025
9026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 * Move the cursor to position "row","col" in the screen.
9028 * This tries to find the most efficient way to move, minimizing the number of
9029 * characters sent to the terminal.
9030 */
9031 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009032windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009034 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 int i;
9036 int plan;
9037 int cost;
9038 int wouldbe_col;
9039 int noinvcurs;
9040 char_u *bs;
9041 int goto_cost;
9042 int attr;
9043
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009044#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9046
9047#define PLAN_LE 1
9048#define PLAN_CR 2
9049#define PLAN_NL 3
9050#define PLAN_WRITE 4
9051 /* Can't use ScreenLines unless initialized */
9052 if (ScreenLines == NULL)
9053 return;
9054
9055 if (col != screen_cur_col || row != screen_cur_row)
9056 {
9057 /* Check for valid position. */
9058 if (row < 0) /* window without text lines? */
9059 row = 0;
9060 if (row >= screen_Rows)
9061 row = screen_Rows - 1;
9062 if (col >= screen_Columns)
9063 col = screen_Columns - 1;
9064
9065 /* check if no cursor movement is allowed in highlight mode */
9066 if (screen_attr && *T_MS == NUL)
9067 noinvcurs = HIGHL_COST;
9068 else
9069 noinvcurs = 0;
9070 goto_cost = GOTO_COST + noinvcurs;
9071
9072 /*
9073 * Plan how to do the positioning:
9074 * 1. Use CR to move it to column 0, same row.
9075 * 2. Use T_LE to move it a few columns to the left.
9076 * 3. Use NL to move a few lines down, column 0.
9077 * 4. Move a few columns to the right with T_ND or by writing chars.
9078 *
9079 * Don't do this if the cursor went beyond the last column, the cursor
9080 * position is unknown then (some terminals wrap, some don't )
9081 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009082 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 * characters to move the cursor to the right.
9084 */
9085 if (row >= screen_cur_row && screen_cur_col < Columns)
9086 {
9087 /*
9088 * If the cursor is in the same row, bigger col, we can use CR
9089 * or T_LE.
9090 */
9091 bs = NULL; /* init for GCC */
9092 attr = screen_attr;
9093 if (row == screen_cur_row && col < screen_cur_col)
9094 {
9095 /* "le" is preferred over "bc", because "bc" is obsolete */
9096 if (*T_LE)
9097 bs = T_LE; /* "cursor left" */
9098 else
9099 bs = T_BC; /* "backspace character (old) */
9100 if (*bs)
9101 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9102 else
9103 cost = 999;
9104 if (col + 1 < cost) /* using CR is less characters */
9105 {
9106 plan = PLAN_CR;
9107 wouldbe_col = 0;
9108 cost = 1; /* CR is just one character */
9109 }
9110 else
9111 {
9112 plan = PLAN_LE;
9113 wouldbe_col = col;
9114 }
9115 if (noinvcurs) /* will stop highlighting */
9116 {
9117 cost += noinvcurs;
9118 attr = 0;
9119 }
9120 }
9121
9122 /*
9123 * If the cursor is above where we want to be, we can use CR LF.
9124 */
9125 else if (row > screen_cur_row)
9126 {
9127 plan = PLAN_NL;
9128 wouldbe_col = 0;
9129 cost = (row - screen_cur_row) * 2; /* CR LF */
9130 if (noinvcurs) /* will stop highlighting */
9131 {
9132 cost += noinvcurs;
9133 attr = 0;
9134 }
9135 }
9136
9137 /*
9138 * If the cursor is in the same row, smaller col, just use write.
9139 */
9140 else
9141 {
9142 plan = PLAN_WRITE;
9143 wouldbe_col = screen_cur_col;
9144 cost = 0;
9145 }
9146
9147 /*
9148 * Check if any characters that need to be written have the
9149 * correct attributes. Also avoid UTF-8 characters.
9150 */
9151 i = col - wouldbe_col;
9152 if (i > 0)
9153 cost += i;
9154 if (cost < goto_cost && i > 0)
9155 {
9156 /*
9157 * Check if the attributes are correct without additionally
9158 * stopping highlighting.
9159 */
9160 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9161 while (i && *p++ == attr)
9162 --i;
9163 if (i != 0)
9164 {
9165 /*
9166 * Try if it works when highlighting is stopped here.
9167 */
9168 if (*--p == 0)
9169 {
9170 cost += noinvcurs;
9171 while (i && *p++ == 0)
9172 --i;
9173 }
9174 if (i != 0)
9175 cost = 999; /* different attributes, don't do it */
9176 }
9177#ifdef FEAT_MBYTE
9178 if (enc_utf8)
9179 {
9180 /* Don't use an UTF-8 char for positioning, it's slow. */
9181 for (i = wouldbe_col; i < col; ++i)
9182 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9183 {
9184 cost = 999;
9185 break;
9186 }
9187 }
9188#endif
9189 }
9190
9191 /*
9192 * We can do it without term_windgoto()!
9193 */
9194 if (cost < goto_cost)
9195 {
9196 if (plan == PLAN_LE)
9197 {
9198 if (noinvcurs)
9199 screen_stop_highlight();
9200 while (screen_cur_col > col)
9201 {
9202 out_str(bs);
9203 --screen_cur_col;
9204 }
9205 }
9206 else if (plan == PLAN_CR)
9207 {
9208 if (noinvcurs)
9209 screen_stop_highlight();
9210 out_char('\r');
9211 screen_cur_col = 0;
9212 }
9213 else if (plan == PLAN_NL)
9214 {
9215 if (noinvcurs)
9216 screen_stop_highlight();
9217 while (screen_cur_row < row)
9218 {
9219 out_char('\n');
9220 ++screen_cur_row;
9221 }
9222 screen_cur_col = 0;
9223 }
9224
9225 i = col - screen_cur_col;
9226 if (i > 0)
9227 {
9228 /*
9229 * Use cursor-right if it's one character only. Avoids
9230 * removing a line of pixels from the last bold char, when
9231 * using the bold trick in the GUI.
9232 */
9233 if (T_ND[0] != NUL && T_ND[1] == NUL)
9234 {
9235 while (i-- > 0)
9236 out_char(*T_ND);
9237 }
9238 else
9239 {
9240 int off;
9241
9242 off = LineOffset[row] + screen_cur_col;
9243 while (i-- > 0)
9244 {
9245 if (ScreenAttrs[off] != screen_attr)
9246 screen_stop_highlight();
9247#ifdef FEAT_MBYTE
9248 out_flush_check();
9249#endif
9250 out_char(ScreenLines[off]);
9251#ifdef FEAT_MBYTE
9252 if (enc_dbcs == DBCS_JPNU
9253 && ScreenLines[off] == 0x8e)
9254 out_char(ScreenLines2[off]);
9255#endif
9256 ++off;
9257 }
9258 }
9259 }
9260 }
9261 }
9262 else
9263 cost = 999;
9264
9265 if (cost >= goto_cost)
9266 {
9267 if (noinvcurs)
9268 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009269 if (row == screen_cur_row && (col > screen_cur_col)
9270 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 term_cursor_right(col - screen_cur_col);
9272 else
9273 term_windgoto(row, col);
9274 }
9275 screen_cur_row = row;
9276 screen_cur_col = col;
9277 }
9278}
9279
9280/*
9281 * Set cursor to its position in the current window.
9282 */
9283 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009284setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286 if (redrawing())
9287 {
9288 validate_cursor();
9289 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9290 W_WINCOL(curwin) + (
9291#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009292 /* With 'rightleft' set and the cursor on a double-wide
9293 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9295# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009296 (has_mbyte
9297 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9298 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299# endif
9300 1)) :
9301#endif
9302 curwin->w_wcol));
9303 }
9304}
9305
9306
9307/*
9308 * insert 'line_count' lines at 'row' in window 'wp'
9309 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9310 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9311 * scrolling.
9312 * Returns FAIL if the lines are not inserted, OK for success.
9313 */
9314 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009315win_ins_lines(
9316 win_T *wp,
9317 int row,
9318 int line_count,
9319 int invalid,
9320 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
9322 int did_delete;
9323 int nextrow;
9324 int lastrow;
9325 int retval;
9326
9327 if (invalid)
9328 wp->w_lines_valid = 0;
9329
9330 if (wp->w_height < 5)
9331 return FAIL;
9332
9333 if (line_count > wp->w_height - row)
9334 line_count = wp->w_height - row;
9335
9336 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9337 if (retval != MAYBE)
9338 return retval;
9339
9340 /*
9341 * If there is a next window or a status line, we first try to delete the
9342 * lines at the bottom to avoid messing what is after the window.
9343 * If this fails and there are following windows, don't do anything to avoid
9344 * messing up those windows, better just redraw.
9345 */
9346 did_delete = FALSE;
9347#ifdef FEAT_WINDOWS
9348 if (wp->w_next != NULL || wp->w_status_height)
9349 {
9350 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9351 line_count, (int)Rows, FALSE, NULL) == OK)
9352 did_delete = TRUE;
9353 else if (wp->w_next)
9354 return FAIL;
9355 }
9356#endif
9357 /*
9358 * if no lines deleted, blank the lines that will end up below the window
9359 */
9360 if (!did_delete)
9361 {
9362#ifdef FEAT_WINDOWS
9363 wp->w_redr_status = TRUE;
9364#endif
9365 redraw_cmdline = TRUE;
9366 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9367 lastrow = nextrow + line_count;
9368 if (lastrow > Rows)
9369 lastrow = Rows;
9370 screen_fill(nextrow - line_count, lastrow - line_count,
9371 W_WINCOL(wp), (int)W_ENDCOL(wp),
9372 ' ', ' ', 0);
9373 }
9374
9375 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9376 == FAIL)
9377 {
9378 /* deletion will have messed up other windows */
9379 if (did_delete)
9380 {
9381#ifdef FEAT_WINDOWS
9382 wp->w_redr_status = TRUE;
9383#endif
9384 win_rest_invalid(W_NEXT(wp));
9385 }
9386 return FAIL;
9387 }
9388
9389 return OK;
9390}
9391
9392/*
9393 * delete "line_count" window lines at "row" in window "wp"
9394 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9395 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9396 * scrolling
9397 * Return OK for success, FAIL if the lines are not deleted.
9398 */
9399 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009400win_del_lines(
9401 win_T *wp,
9402 int row,
9403 int line_count,
9404 int invalid,
9405 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406{
9407 int retval;
9408
9409 if (invalid)
9410 wp->w_lines_valid = 0;
9411
9412 if (line_count > wp->w_height - row)
9413 line_count = wp->w_height - row;
9414
9415 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9416 if (retval != MAYBE)
9417 return retval;
9418
9419 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9420 (int)Rows, FALSE, NULL) == FAIL)
9421 return FAIL;
9422
9423#ifdef FEAT_WINDOWS
9424 /*
9425 * If there are windows or status lines below, try to put them at the
9426 * correct place. If we can't do that, they have to be redrawn.
9427 */
9428 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9429 {
9430 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9431 line_count, (int)Rows, NULL) == FAIL)
9432 {
9433 wp->w_redr_status = TRUE;
9434 win_rest_invalid(wp->w_next);
9435 }
9436 }
9437 /*
9438 * If this is the last window and there is no status line, redraw the
9439 * command line later.
9440 */
9441 else
9442#endif
9443 redraw_cmdline = TRUE;
9444 return OK;
9445}
9446
9447/*
9448 * Common code for win_ins_lines() and win_del_lines().
9449 * Returns OK or FAIL when the work has been done.
9450 * Returns MAYBE when not finished yet.
9451 */
9452 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009453win_do_lines(
9454 win_T *wp,
9455 int row,
9456 int line_count,
9457 int mayclear,
9458 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459{
9460 int retval;
9461
9462 if (!redrawing() || line_count <= 0)
9463 return FAIL;
9464
9465 /* only a few lines left: redraw is faster */
9466 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009467#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 && wp->w_width == Columns
9469#endif
9470 )
9471 {
9472 screenclear(); /* will set wp->w_lines_valid to 0 */
9473 return FAIL;
9474 }
9475
9476 /*
9477 * Delete all remaining lines
9478 */
9479 if (row + line_count >= wp->w_height)
9480 {
9481 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9482 W_WINCOL(wp), (int)W_ENDCOL(wp),
9483 ' ', ' ', 0);
9484 return OK;
9485 }
9486
9487 /*
9488 * when scrolling, the message on the command line should be cleared,
9489 * otherwise it will stay there forever.
9490 */
9491 clear_cmdline = TRUE;
9492
9493 /*
9494 * If the terminal can set a scroll region, use that.
9495 * Always do this in a vertically split window. This will redraw from
9496 * ScreenLines[] when t_CV isn't defined. That's faster than using
9497 * win_line().
9498 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009499 * a character in the lower right corner of the scroll region may cause a
9500 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 */
9502 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009503#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 || W_WIDTH(wp) != Columns
9505#endif
9506 )
9507 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009508#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9510#endif
9511 scroll_region_set(wp, row);
9512 if (del)
9513 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9514 wp->w_height - row, FALSE, wp);
9515 else
9516 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9517 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009518#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9520#endif
9521 scroll_region_reset();
9522 return retval;
9523 }
9524
9525#ifdef FEAT_WINDOWS
9526 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9527 return FAIL;
9528#endif
9529
9530 return MAYBE;
9531}
9532
9533/*
9534 * window 'wp' and everything after it is messed up, mark it for redraw
9535 */
9536 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009537win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539#ifdef FEAT_WINDOWS
9540 while (wp != NULL)
9541#else
9542 if (wp != NULL)
9543#endif
9544 {
9545 redraw_win_later(wp, NOT_VALID);
9546#ifdef FEAT_WINDOWS
9547 wp->w_redr_status = TRUE;
9548 wp = wp->w_next;
9549#endif
9550 }
9551 redraw_cmdline = TRUE;
9552}
9553
9554/*
9555 * The rest of the routines in this file perform screen manipulations. The
9556 * given operation is performed physically on the screen. The corresponding
9557 * change is also made to the internal screen image. In this way, the editor
9558 * anticipates the effect of editing changes on the appearance of the screen.
9559 * That way, when we call screenupdate a complete redraw isn't usually
9560 * necessary. Another advantage is that we can keep adding code to anticipate
9561 * screen changes, and in the meantime, everything still works.
9562 */
9563
9564/*
9565 * types for inserting or deleting lines
9566 */
9567#define USE_T_CAL 1
9568#define USE_T_CDL 2
9569#define USE_T_AL 3
9570#define USE_T_CE 4
9571#define USE_T_DL 5
9572#define USE_T_SR 6
9573#define USE_NL 7
9574#define USE_T_CD 8
9575#define USE_REDRAW 9
9576
9577/*
9578 * insert lines on the screen and update ScreenLines[]
9579 * 'end' is the line after the scrolled part. Normally it is Rows.
9580 * When scrolling region used 'off' is the offset from the top for the region.
9581 * 'row' and 'end' are relative to the start of the region.
9582 *
9583 * return FAIL for failure, OK for success.
9584 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009585 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009586screen_ins_lines(
9587 int off,
9588 int row,
9589 int line_count,
9590 int end,
9591 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592{
9593 int i;
9594 int j;
9595 unsigned temp;
9596 int cursor_row;
9597 int type;
9598 int result_empty;
9599 int can_ce = can_clear(T_CE);
9600
9601 /*
9602 * FAIL if
9603 * - there is no valid screen
9604 * - the screen has to be redrawn completely
9605 * - the line count is less than one
9606 * - the line count is more than 'ttyscroll'
9607 */
9608 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9609 return FAIL;
9610
9611 /*
9612 * There are seven ways to insert lines:
9613 * 0. When in a vertically split window and t_CV isn't set, redraw the
9614 * characters from ScreenLines[].
9615 * 1. Use T_CD (clear to end of display) if it exists and the result of
9616 * the insert is just empty lines
9617 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9618 * present or line_count > 1. It looks better if we do all the inserts
9619 * at once.
9620 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9621 * insert is just empty lines and T_CE is not present or line_count >
9622 * 1.
9623 * 4. Use T_AL (insert line) if it exists.
9624 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9625 * just empty lines.
9626 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9627 * just empty lines.
9628 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9629 * the 'da' flag is not set or we have clear line capability.
9630 * 8. redraw the characters from ScreenLines[].
9631 *
9632 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9633 * the scrollbar for the window. It does have insert line, use that if it
9634 * exists.
9635 */
9636 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009637#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9639 type = USE_REDRAW;
9640 else
9641#endif
9642 if (can_clear(T_CD) && result_empty)
9643 type = USE_T_CD;
9644 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9645 type = USE_T_CAL;
9646 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9647 type = USE_T_CDL;
9648 else if (*T_AL != NUL)
9649 type = USE_T_AL;
9650 else if (can_ce && result_empty)
9651 type = USE_T_CE;
9652 else if (*T_DL != NUL && result_empty)
9653 type = USE_T_DL;
9654 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9655 type = USE_T_SR;
9656 else
9657 return FAIL;
9658
9659 /*
9660 * For clearing the lines screen_del_lines() is used. This will also take
9661 * care of t_db if necessary.
9662 */
9663 if (type == USE_T_CD || type == USE_T_CDL ||
9664 type == USE_T_CE || type == USE_T_DL)
9665 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9666
9667 /*
9668 * If text is retained below the screen, first clear or delete as many
9669 * lines at the bottom of the window as are about to be inserted so that
9670 * the deleted lines won't later surface during a screen_del_lines.
9671 */
9672 if (*T_DB)
9673 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9674
9675#ifdef FEAT_CLIPBOARD
9676 /* Remove a modeless selection when inserting lines halfway the screen
9677 * or not the full width of the screen. */
9678 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009679# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 || (wp != NULL && wp->w_width != Columns)
9681# endif
9682 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009683 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 else
9685 clip_scroll_selection(-line_count);
9686#endif
9687
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688#ifdef FEAT_GUI
9689 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9690 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009691 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692#endif
9693
9694 if (*T_CCS != NUL) /* cursor relative to region */
9695 cursor_row = row;
9696 else
9697 cursor_row = row + off;
9698
9699 /*
9700 * Shift LineOffset[] line_count down to reflect the inserted lines.
9701 * Clear the inserted lines in ScreenLines[].
9702 */
9703 row += off;
9704 end += off;
9705 for (i = 0; i < line_count; ++i)
9706 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009707#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 if (wp != NULL && wp->w_width != Columns)
9709 {
9710 /* need to copy part of a line */
9711 j = end - 1 - i;
9712 while ((j -= line_count) >= row)
9713 linecopy(j + line_count, j, wp);
9714 j += line_count;
9715 if (can_clear((char_u *)" "))
9716 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9717 else
9718 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9719 LineWraps[j] = FALSE;
9720 }
9721 else
9722#endif
9723 {
9724 j = end - 1 - i;
9725 temp = LineOffset[j];
9726 while ((j -= line_count) >= row)
9727 {
9728 LineOffset[j + line_count] = LineOffset[j];
9729 LineWraps[j + line_count] = LineWraps[j];
9730 }
9731 LineOffset[j + line_count] = temp;
9732 LineWraps[j + line_count] = FALSE;
9733 if (can_clear((char_u *)" "))
9734 lineclear(temp, (int)Columns);
9735 else
9736 lineinvalid(temp, (int)Columns);
9737 }
9738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739
9740 screen_stop_highlight();
9741 windgoto(cursor_row, 0);
9742
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009743#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 /* redraw the characters */
9745 if (type == USE_REDRAW)
9746 redraw_block(row, end, wp);
9747 else
9748#endif
9749 if (type == USE_T_CAL)
9750 {
9751 term_append_lines(line_count);
9752 screen_start(); /* don't know where cursor is now */
9753 }
9754 else
9755 {
9756 for (i = 0; i < line_count; i++)
9757 {
9758 if (type == USE_T_AL)
9759 {
9760 if (i && cursor_row != 0)
9761 windgoto(cursor_row, 0);
9762 out_str(T_AL);
9763 }
9764 else /* type == USE_T_SR */
9765 out_str(T_SR);
9766 screen_start(); /* don't know where cursor is now */
9767 }
9768 }
9769
9770 /*
9771 * With scroll-reverse and 'da' flag set we need to clear the lines that
9772 * have been scrolled down into the region.
9773 */
9774 if (type == USE_T_SR && *T_DA)
9775 {
9776 for (i = 0; i < line_count; ++i)
9777 {
9778 windgoto(off + i, 0);
9779 out_str(T_CE);
9780 screen_start(); /* don't know where cursor is now */
9781 }
9782 }
9783
9784#ifdef FEAT_GUI
9785 gui_can_update_cursor();
9786 if (gui.in_use)
9787 out_flush(); /* always flush after a scroll */
9788#endif
9789 return OK;
9790}
9791
9792/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009793 * Delete lines on the screen and update ScreenLines[].
9794 * "end" is the line after the scrolled part. Normally it is Rows.
9795 * When scrolling region used "off" is the offset from the top for the region.
9796 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 *
9798 * Return OK for success, FAIL if the lines are not deleted.
9799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009801screen_del_lines(
9802 int off,
9803 int row,
9804 int line_count,
9805 int end,
9806 int force, /* even when line_count > p_ttyscroll */
9807 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808{
9809 int j;
9810 int i;
9811 unsigned temp;
9812 int cursor_row;
9813 int cursor_end;
9814 int result_empty; /* result is empty until end of region */
9815 int can_delete; /* deleting line codes can be used */
9816 int type;
9817
9818 /*
9819 * FAIL if
9820 * - there is no valid screen
9821 * - the screen has to be redrawn completely
9822 * - the line count is less than one
9823 * - the line count is more than 'ttyscroll'
9824 */
9825 if (!screen_valid(TRUE) || line_count <= 0 ||
9826 (!force && line_count > p_ttyscroll))
9827 return FAIL;
9828
9829 /*
9830 * Check if the rest of the current region will become empty.
9831 */
9832 result_empty = row + line_count >= end;
9833
9834 /*
9835 * We can delete lines only when 'db' flag not set or when 'ce' option
9836 * available.
9837 */
9838 can_delete = (*T_DB == NUL || can_clear(T_CE));
9839
9840 /*
9841 * There are six ways to delete lines:
9842 * 0. When in a vertically split window and t_CV isn't set, redraw the
9843 * characters from ScreenLines[].
9844 * 1. Use T_CD if it exists and the result is empty.
9845 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9846 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9847 * none of the other ways work.
9848 * 4. Use T_CE (erase line) if the result is empty.
9849 * 5. Use T_DL (delete line) if it exists.
9850 * 6. redraw the characters from ScreenLines[].
9851 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009852#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9854 type = USE_REDRAW;
9855 else
9856#endif
9857 if (can_clear(T_CD) && result_empty)
9858 type = USE_T_CD;
9859#if defined(__BEOS__) && defined(BEOS_DR8)
9860 /*
9861 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9862 * its internal termcap... this works okay for tests which test *T_DB !=
9863 * NUL. It has the disadvantage that the user cannot use any :set t_*
9864 * command to get T_DB (back) to empty_option, only :set term=... will do
9865 * the trick...
9866 * Anyway, this hack will hopefully go away with the next OS release.
9867 * (Olaf Seibert)
9868 */
9869 else if (row == 0 && T_DB == empty_option
9870 && (line_count == 1 || *T_CDL == NUL))
9871#else
9872 else if (row == 0 && (
9873#ifndef AMIGA
9874 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9875 * up, so use delete-line command */
9876 line_count == 1 ||
9877#endif
9878 *T_CDL == NUL))
9879#endif
9880 type = USE_NL;
9881 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9882 type = USE_T_CDL;
9883 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009884#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 && (wp == NULL || wp->w_width == Columns)
9886#endif
9887 )
9888 type = USE_T_CE;
9889 else if (*T_DL != NUL && can_delete)
9890 type = USE_T_DL;
9891 else if (*T_CDL != NUL && can_delete)
9892 type = USE_T_CDL;
9893 else
9894 return FAIL;
9895
9896#ifdef FEAT_CLIPBOARD
9897 /* Remove a modeless selection when deleting lines halfway the screen or
9898 * not the full width of the screen. */
9899 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009900# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 || (wp != NULL && wp->w_width != Columns)
9902# endif
9903 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009904 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 else
9906 clip_scroll_selection(line_count);
9907#endif
9908
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909#ifdef FEAT_GUI
9910 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9911 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009912 gui_dont_update_cursor(gui.cursor_row >= row + off
9913 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914#endif
9915
9916 if (*T_CCS != NUL) /* cursor relative to region */
9917 {
9918 cursor_row = row;
9919 cursor_end = end;
9920 }
9921 else
9922 {
9923 cursor_row = row + off;
9924 cursor_end = end + off;
9925 }
9926
9927 /*
9928 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9929 * Clear the inserted lines in ScreenLines[].
9930 */
9931 row += off;
9932 end += off;
9933 for (i = 0; i < line_count; ++i)
9934 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009935#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 if (wp != NULL && wp->w_width != Columns)
9937 {
9938 /* need to copy part of a line */
9939 j = row + i;
9940 while ((j += line_count) <= end - 1)
9941 linecopy(j - line_count, j, wp);
9942 j -= line_count;
9943 if (can_clear((char_u *)" "))
9944 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9945 else
9946 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9947 LineWraps[j] = FALSE;
9948 }
9949 else
9950#endif
9951 {
9952 /* whole width, moving the line pointers is faster */
9953 j = row + i;
9954 temp = LineOffset[j];
9955 while ((j += line_count) <= end - 1)
9956 {
9957 LineOffset[j - line_count] = LineOffset[j];
9958 LineWraps[j - line_count] = LineWraps[j];
9959 }
9960 LineOffset[j - line_count] = temp;
9961 LineWraps[j - line_count] = FALSE;
9962 if (can_clear((char_u *)" "))
9963 lineclear(temp, (int)Columns);
9964 else
9965 lineinvalid(temp, (int)Columns);
9966 }
9967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968
9969 screen_stop_highlight();
9970
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009971#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 /* redraw the characters */
9973 if (type == USE_REDRAW)
9974 redraw_block(row, end, wp);
9975 else
9976#endif
9977 if (type == USE_T_CD) /* delete the lines */
9978 {
9979 windgoto(cursor_row, 0);
9980 out_str(T_CD);
9981 screen_start(); /* don't know where cursor is now */
9982 }
9983 else if (type == USE_T_CDL)
9984 {
9985 windgoto(cursor_row, 0);
9986 term_delete_lines(line_count);
9987 screen_start(); /* don't know where cursor is now */
9988 }
9989 /*
9990 * Deleting lines at top of the screen or scroll region: Just scroll
9991 * the whole screen (scroll region) up by outputting newlines on the
9992 * last line.
9993 */
9994 else if (type == USE_NL)
9995 {
9996 windgoto(cursor_end - 1, 0);
9997 for (i = line_count; --i >= 0; )
9998 out_char('\n'); /* cursor will remain on same line */
9999 }
10000 else
10001 {
10002 for (i = line_count; --i >= 0; )
10003 {
10004 if (type == USE_T_DL)
10005 {
10006 windgoto(cursor_row, 0);
10007 out_str(T_DL); /* delete a line */
10008 }
10009 else /* type == USE_T_CE */
10010 {
10011 windgoto(cursor_row + i, 0);
10012 out_str(T_CE); /* erase a line */
10013 }
10014 screen_start(); /* don't know where cursor is now */
10015 }
10016 }
10017
10018 /*
10019 * If the 'db' flag is set, we need to clear the lines that have been
10020 * scrolled up at the bottom of the region.
10021 */
10022 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10023 {
10024 for (i = line_count; i > 0; --i)
10025 {
10026 windgoto(cursor_end - i, 0);
10027 out_str(T_CE); /* erase a line */
10028 screen_start(); /* don't know where cursor is now */
10029 }
10030 }
10031
10032#ifdef FEAT_GUI
10033 gui_can_update_cursor();
10034 if (gui.in_use)
10035 out_flush(); /* always flush after a scroll */
10036#endif
10037
10038 return OK;
10039}
10040
10041/*
10042 * show the current mode and ruler
10043 *
10044 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10045 * If clear_cmdline is FALSE there may be a message there that needs to be
10046 * cleared only if a mode is shown.
10047 * Return the length of the message (0 if no message).
10048 */
10049 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010050showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051{
10052 int need_clear;
10053 int length = 0;
10054 int do_mode;
10055 int attr;
10056 int nwr_save;
10057#ifdef FEAT_INS_EXPAND
10058 int sub_attr;
10059#endif
10060
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010061 do_mode = ((p_smd && msg_silent == 0)
10062 && ((State & INSERT)
10063 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010064 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 if (do_mode || Recording)
10066 {
10067 /*
10068 * Don't show mode right now, when not redrawing or inside a mapping.
10069 * Call char_avail() only when we are going to show something, because
10070 * it takes a bit of time.
10071 */
10072 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10073 {
10074 redraw_cmdline = TRUE; /* show mode later */
10075 return 0;
10076 }
10077
10078 nwr_save = need_wait_return;
10079
10080 /* wait a bit before overwriting an important message */
10081 check_for_delay(FALSE);
10082
10083 /* if the cmdline is more than one line high, erase top lines */
10084 need_clear = clear_cmdline;
10085 if (clear_cmdline && cmdline_row < Rows - 1)
10086 msg_clr_cmdline(); /* will reset clear_cmdline */
10087
10088 /* Position on the last line in the window, column 0 */
10089 msg_pos_mode();
10090 cursor_off();
10091 attr = hl_attr(HLF_CM); /* Highlight mode */
10092 if (do_mode)
10093 {
10094 MSG_PUTS_ATTR("--", attr);
10095#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010096 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010097# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010098 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010099# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010100 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010101# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010102 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010103# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 MSG_PUTS_ATTR(" IM", attr);
10105# else
10106 MSG_PUTS_ATTR(" XIM", attr);
10107# endif
10108#endif
10109#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10110 if (gui.in_use)
10111 {
10112 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010113 {
10114 /* HANGUL */
10115 if (enc_utf8)
10116 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10117 else
10118 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 }
10121#endif
10122#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010123 /* CTRL-X in Insert mode */
10124 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 {
10126 /* These messages can get long, avoid a wrap in a narrow
10127 * window. Prefer showing edit_submode_extra. */
10128 length = (Rows - msg_row) * Columns - 3;
10129 if (edit_submode_extra != NULL)
10130 length -= vim_strsize(edit_submode_extra);
10131 if (length > 0)
10132 {
10133 if (edit_submode_pre != NULL)
10134 length -= vim_strsize(edit_submode_pre);
10135 if (length - vim_strsize(edit_submode) > 0)
10136 {
10137 if (edit_submode_pre != NULL)
10138 msg_puts_attr(edit_submode_pre, attr);
10139 msg_puts_attr(edit_submode, attr);
10140 }
10141 if (edit_submode_extra != NULL)
10142 {
10143 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10144 if ((int)edit_submode_highl < (int)HLF_COUNT)
10145 sub_attr = hl_attr(edit_submode_highl);
10146 else
10147 sub_attr = attr;
10148 msg_puts_attr(edit_submode_extra, sub_attr);
10149 }
10150 }
10151 length = 0;
10152 }
10153 else
10154#endif
10155 {
10156#ifdef FEAT_VREPLACE
10157 if (State & VREPLACE_FLAG)
10158 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10159 else
10160#endif
10161 if (State & REPLACE_FLAG)
10162 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10163 else if (State & INSERT)
10164 {
10165#ifdef FEAT_RIGHTLEFT
10166 if (p_ri)
10167 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10168#endif
10169 MSG_PUTS_ATTR(_(" INSERT"), attr);
10170 }
10171 else if (restart_edit == 'I')
10172 MSG_PUTS_ATTR(_(" (insert)"), attr);
10173 else if (restart_edit == 'R')
10174 MSG_PUTS_ATTR(_(" (replace)"), attr);
10175 else if (restart_edit == 'V')
10176 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10177#ifdef FEAT_RIGHTLEFT
10178 if (p_hkmap)
10179 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10180# ifdef FEAT_FKMAP
10181 if (p_fkmap)
10182 MSG_PUTS_ATTR(farsi_text_5, attr);
10183# endif
10184#endif
10185#ifdef FEAT_KEYMAP
10186 if (State & LANGMAP)
10187 {
10188# ifdef FEAT_ARABIC
10189 if (curwin->w_p_arab)
10190 MSG_PUTS_ATTR(_(" Arabic"), attr);
10191 else
10192# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010193 if (get_keymap_str(curwin, (char_u *)" (%s)",
10194 NameBuff, MAXPATHL))
10195 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 }
10197#endif
10198 if ((State & INSERT) && p_paste)
10199 MSG_PUTS_ATTR(_(" (paste)"), attr);
10200
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 if (VIsual_active)
10202 {
10203 char *p;
10204
10205 /* Don't concatenate separate words to avoid translation
10206 * problems. */
10207 switch ((VIsual_select ? 4 : 0)
10208 + (VIsual_mode == Ctrl_V) * 2
10209 + (VIsual_mode == 'V'))
10210 {
10211 case 0: p = N_(" VISUAL"); break;
10212 case 1: p = N_(" VISUAL LINE"); break;
10213 case 2: p = N_(" VISUAL BLOCK"); break;
10214 case 4: p = N_(" SELECT"); break;
10215 case 5: p = N_(" SELECT LINE"); break;
10216 default: p = N_(" SELECT BLOCK"); break;
10217 }
10218 MSG_PUTS_ATTR(_(p), attr);
10219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 MSG_PUTS_ATTR(" --", attr);
10221 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010222
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 need_clear = TRUE;
10224 }
10225 if (Recording
10226#ifdef FEAT_INS_EXPAND
10227 && edit_submode == NULL /* otherwise it gets too long */
10228#endif
10229 )
10230 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010231 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 need_clear = TRUE;
10233 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010234
10235 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 if (need_clear || clear_cmdline)
10237 msg_clr_eos();
10238 msg_didout = FALSE; /* overwrite this message */
10239 length = msg_col;
10240 msg_col = 0;
10241 need_wait_return = nwr_save; /* never ask for hit-return for this */
10242 }
10243 else if (clear_cmdline && msg_silent == 0)
10244 /* Clear the whole command line. Will reset "clear_cmdline". */
10245 msg_clr_cmdline();
10246
10247#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 /* In Visual mode the size of the selected area must be redrawn. */
10249 if (VIsual_active)
10250 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251
10252 /* If the last window has no status line, the ruler is after the mode
10253 * message and must be redrawn */
10254 if (redrawing()
10255# ifdef FEAT_WINDOWS
10256 && lastwin->w_status_height == 0
10257# endif
10258 )
10259 win_redr_ruler(lastwin, TRUE);
10260#endif
10261 redraw_cmdline = FALSE;
10262 clear_cmdline = FALSE;
10263
10264 return length;
10265}
10266
10267/*
10268 * Position for a mode message.
10269 */
10270 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010271msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272{
10273 msg_col = 0;
10274 msg_row = Rows - 1;
10275}
10276
10277/*
10278 * Delete mode message. Used when ESC is typed which is expected to end
10279 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010280 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 */
10282 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010283unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284{
10285 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010286 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 */
10288 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10289 redraw_cmdline = TRUE; /* delete mode later */
10290 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010291 clearmode();
10292}
10293
10294/*
10295 * Clear the mode message.
10296 */
10297 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010298clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010299{
10300 msg_pos_mode();
10301 if (Recording)
10302 recording_mode(hl_attr(HLF_CM));
10303 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304}
10305
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010306 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010307recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010308{
10309 MSG_PUTS_ATTR(_("recording"), attr);
10310 if (!shortmess(SHM_RECORDING))
10311 {
10312 char_u s[4];
10313 sprintf((char *)s, " @%c", Recording);
10314 MSG_PUTS_ATTR(s, attr);
10315 }
10316}
10317
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010318#if defined(FEAT_WINDOWS)
10319/*
10320 * Draw the tab pages line at the top of the Vim window.
10321 */
10322 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010323draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010324{
10325 int tabcount = 0;
10326 tabpage_T *tp;
10327 int tabwidth;
10328 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010329 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010330 int attr;
10331 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010332 win_T *cwp;
10333 int wincount;
10334 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010335 int c;
10336 int len;
10337 int attr_sel = hl_attr(HLF_TPS);
10338 int attr_nosel = hl_attr(HLF_TP);
10339 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010340 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010341 int room;
10342 int use_sep_chars = (t_colors < 8
10343#ifdef FEAT_GUI
10344 && !gui.in_use
10345#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010346#ifdef FEAT_TERMGUICOLORS
10347 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010348#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010349 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010350
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010351 if (ScreenLines == NULL)
10352 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010353 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010354
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010355#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010356 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010357 if (gui_use_tabline())
10358 {
10359 gui_update_tabline();
10360 return;
10361 }
10362#endif
10363
10364 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010365 return;
10366
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010367#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010368
10369 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10370 for (scol = 0; scol < Columns; ++scol)
10371 TabPageIdxs[scol] = 0;
10372
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010373 /* Use the 'tabline' option if it's set. */
10374 if (*p_tal != NUL)
10375 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010376 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010377
10378 /* Check for an error. If there is one we would loop in redrawing the
10379 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010380 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010381 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010382 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010383 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010384 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010385 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010386 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010387 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010388#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010389 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010390 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010391 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010392
Bram Moolenaar238a5642006-02-21 22:12:05 +000010393 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10394 if (tabwidth < 6)
10395 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010396
Bram Moolenaar238a5642006-02-21 22:12:05 +000010397 attr = attr_nosel;
10398 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010399 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010400 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10401 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010402 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010403 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010404
Bram Moolenaar238a5642006-02-21 22:12:05 +000010405 if (tp->tp_topframe == topframe)
10406 attr = attr_sel;
10407 if (use_sep_chars && col > 0)
10408 screen_putchar('|', 0, col++, attr);
10409
10410 if (tp->tp_topframe != topframe)
10411 attr = attr_nosel;
10412
10413 screen_putchar(' ', 0, col++, attr);
10414
10415 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010416 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010417 cwp = curwin;
10418 wp = firstwin;
10419 }
10420 else
10421 {
10422 cwp = tp->tp_curwin;
10423 wp = tp->tp_firstwin;
10424 }
10425
10426 modified = FALSE;
10427 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10428 if (bufIsChanged(wp->w_buffer))
10429 modified = TRUE;
10430 if (modified || wincount > 1)
10431 {
10432 if (wincount > 1)
10433 {
10434 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010435 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010436 if (col + len >= Columns - 3)
10437 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010438 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010439#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010440 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010441#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010442 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010443#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010444 );
10445 col += len;
10446 }
10447 if (modified)
10448 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10449 screen_putchar(' ', 0, col++, attr);
10450 }
10451
10452 room = scol - col + tabwidth - 1;
10453 if (room > 0)
10454 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010455 /* Get buffer name in NameBuff[] */
10456 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010457 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010458 len = vim_strsize(NameBuff);
10459 p = NameBuff;
10460#ifdef FEAT_MBYTE
10461 if (has_mbyte)
10462 while (len > room)
10463 {
10464 len -= ptr2cells(p);
10465 mb_ptr_adv(p);
10466 }
10467 else
10468#endif
10469 if (len > room)
10470 {
10471 p += len - room;
10472 len = room;
10473 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010474 if (len > Columns - col - 1)
10475 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010476
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010477 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010478 col += len;
10479 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010480 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010481
10482 /* Store the tab page number in TabPageIdxs[], so that
10483 * jump_to_mouse() knows where each one is. */
10484 ++tabcount;
10485 while (scol < col)
10486 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010487 }
10488
Bram Moolenaar238a5642006-02-21 22:12:05 +000010489 if (use_sep_chars)
10490 c = '_';
10491 else
10492 c = ' ';
10493 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010494
10495 /* Put an "X" for closing the current tab if there are several. */
10496 if (first_tabpage->tp_next != NULL)
10497 {
10498 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10499 TabPageIdxs[Columns - 1] = -999;
10500 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010501 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010502
10503 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10504 * set. */
10505 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010506}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010507
10508/*
10509 * Get buffer name for "buf" into NameBuff[].
10510 * Takes care of special buffer names and translates special characters.
10511 */
10512 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010513get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010514{
10515 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010516 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010517 else
10518 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10519 trans_characters(NameBuff, MAXPATHL);
10520}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010521#endif
10522
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10524/*
10525 * Get the character to use in a status line. Get its attributes in "*attr".
10526 */
10527 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010528fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529{
10530 int fill;
10531 if (is_curwin)
10532 {
10533 *attr = hl_attr(HLF_S);
10534 fill = fill_stl;
10535 }
10536 else
10537 {
10538 *attr = hl_attr(HLF_SNC);
10539 fill = fill_stlnc;
10540 }
10541 /* Use fill when there is highlighting, and highlighting of current
10542 * window differs, or the fillchars differ, or this is not the
10543 * current window */
10544 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010545 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 || (fill_stl != fill_stlnc)))
10547 return fill;
10548 if (is_curwin)
10549 return '^';
10550 return '=';
10551}
10552#endif
10553
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010554#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555/*
10556 * Get the character to use in a separator between vertically split windows.
10557 * Get its attributes in "*attr".
10558 */
10559 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010560fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561{
10562 *attr = hl_attr(HLF_C);
10563 if (*attr == 0 && fill_vert == ' ')
10564 return '|';
10565 else
10566 return fill_vert;
10567}
10568#endif
10569
10570/*
10571 * Return TRUE if redrawing should currently be done.
10572 */
10573 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010574redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575{
10576 return (!RedrawingDisabled
10577 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10578}
10579
10580/*
10581 * Return TRUE if printing messages should currently be done.
10582 */
10583 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010584messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585{
10586 return (!(p_lz && char_avail() && !KeyTyped));
10587}
10588
10589/*
10590 * Show current status info in ruler and various other places
10591 * If always is FALSE, only show ruler if position has changed.
10592 */
10593 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010594showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595{
10596 if (!always && !redrawing())
10597 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010598#ifdef FEAT_INS_EXPAND
10599 if (pum_visible())
10600 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010601# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010602 /* Don't redraw right now, do it later. */
10603 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010604# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010605 return;
10606 }
10607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010609 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010610 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010611 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 else
10614#endif
10615#ifdef FEAT_CMDL_INFO
10616 win_redr_ruler(curwin, always);
10617#endif
10618
10619#ifdef FEAT_TITLE
10620 if (need_maketitle
10621# ifdef FEAT_STL_OPT
10622 || (p_icon && (stl_syntax & STL_IN_ICON))
10623 || (p_title && (stl_syntax & STL_IN_TITLE))
10624# endif
10625 )
10626 maketitle();
10627#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010628#ifdef FEAT_WINDOWS
10629 /* Redraw the tab pages line if needed. */
10630 if (redraw_tabline)
10631 draw_tabline();
10632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633}
10634
10635#ifdef FEAT_CMDL_INFO
10636 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010637win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010639#define RULER_BUF_LEN 70
10640 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 int row;
10642 int fillchar;
10643 int attr;
10644 int empty_line = FALSE;
10645 colnr_T virtcol;
10646 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010647 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010649#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 int this_ru_col;
10651 int off = 0;
10652 int width = Columns;
10653# define WITH_OFF(x) x
10654# define WITH_WIDTH(x) x
10655#else
10656# define WITH_OFF(x) 0
10657# define WITH_WIDTH(x) Columns
10658# define this_ru_col ru_col
10659#endif
10660
10661 /* If 'ruler' off or redrawing disabled, don't do anything */
10662 if (!p_ru)
10663 return;
10664
10665 /*
10666 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10667 * after deleting lines, before cursor.lnum is corrected.
10668 */
10669 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10670 return;
10671
10672#ifdef FEAT_INS_EXPAND
10673 /* Don't draw the ruler while doing insert-completion, it might overwrite
10674 * the (long) mode message. */
10675# ifdef FEAT_WINDOWS
10676 if (wp == lastwin && lastwin->w_status_height == 0)
10677# endif
10678 if (edit_submode != NULL)
10679 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010680 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10681 if (pum_visible())
10682 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683#endif
10684
10685#ifdef FEAT_STL_OPT
10686 if (*p_ruf)
10687 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010688 int save_called_emsg = called_emsg;
10689
10690 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010692 if (called_emsg)
10693 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010694 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010695 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 return;
10697 }
10698#endif
10699
10700 /*
10701 * Check if not in Insert mode and the line is empty (will show "0-1").
10702 */
10703 if (!(State & INSERT)
10704 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10705 empty_line = TRUE;
10706
10707 /*
10708 * Only draw the ruler when something changed.
10709 */
10710 validate_virtcol_win(wp);
10711 if ( redraw_cmdline
10712 || always
10713 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10714 || wp->w_cursor.col != wp->w_ru_cursor.col
10715 || wp->w_virtcol != wp->w_ru_virtcol
10716#ifdef FEAT_VIRTUALEDIT
10717 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10718#endif
10719 || wp->w_topline != wp->w_ru_topline
10720 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10721#ifdef FEAT_DIFF
10722 || wp->w_topfill != wp->w_ru_topfill
10723#endif
10724 || empty_line != wp->w_ru_empty)
10725 {
10726 cursor_off();
10727#ifdef FEAT_WINDOWS
10728 if (wp->w_status_height)
10729 {
10730 row = W_WINROW(wp) + wp->w_height;
10731 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 off = W_WINCOL(wp);
10733 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 }
10735 else
10736#endif
10737 {
10738 row = Rows - 1;
10739 fillchar = ' ';
10740 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010741#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 width = Columns;
10743 off = 0;
10744#endif
10745 }
10746
10747 /* In list mode virtcol needs to be recomputed */
10748 virtcol = wp->w_virtcol;
10749 if (wp->w_p_list && lcs_tab1 == NUL)
10750 {
10751 wp->w_p_list = FALSE;
10752 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10753 wp->w_p_list = TRUE;
10754 }
10755
10756 /*
10757 * Some sprintfs return the length, some return a pointer.
10758 * To avoid portability problems we use strlen() here.
10759 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010760 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10762 ? 0L
10763 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010764 len = STRLEN(buffer);
10765 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10767 (int)virtcol + 1);
10768
10769 /*
10770 * Add a "50%" if there is room for it.
10771 * On the last line, don't print in the last column (scrolls the
10772 * screen up on some terminals).
10773 */
10774 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010775 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 o = i + vim_strsize(buffer + i + 1);
10777#ifdef FEAT_WINDOWS
10778 if (wp->w_status_height == 0) /* can't use last char of screen */
10779#endif
10780 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010781#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 this_ru_col = ru_col - (Columns - width);
10783 if (this_ru_col < 0)
10784 this_ru_col = 0;
10785#endif
10786 /* Never use more than half the window/screen width, leave the other
10787 * half for the filename. */
10788 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10789 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10790 if (this_ru_col + o < WITH_WIDTH(width))
10791 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010792 /* need at least 3 chars left for get_rel_pos() + NUL */
10793 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 {
10795#ifdef FEAT_MBYTE
10796 if (has_mbyte)
10797 i += (*mb_char2bytes)(fillchar, buffer + i);
10798 else
10799#endif
10800 buffer[i++] = fillchar;
10801 ++o;
10802 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010803 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 }
10805 /* Truncate at window boundary. */
10806#ifdef FEAT_MBYTE
10807 if (has_mbyte)
10808 {
10809 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010810 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 {
10812 o += (*mb_ptr2cells)(buffer + i);
10813 if (this_ru_col + o > WITH_WIDTH(width))
10814 {
10815 buffer[i] = NUL;
10816 break;
10817 }
10818 }
10819 }
10820 else
10821#endif
10822 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10823 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10824
10825 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10826 i = redraw_cmdline;
10827 screen_fill(row, row + 1,
10828 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10829 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10830 fillchar, fillchar, attr);
10831 /* don't redraw the cmdline because of showing the ruler */
10832 redraw_cmdline = i;
10833 wp->w_ru_cursor = wp->w_cursor;
10834 wp->w_ru_virtcol = wp->w_virtcol;
10835 wp->w_ru_empty = empty_line;
10836 wp->w_ru_topline = wp->w_topline;
10837 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10838#ifdef FEAT_DIFF
10839 wp->w_ru_topfill = wp->w_topfill;
10840#endif
10841 }
10842}
10843#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010844
10845#if defined(FEAT_LINEBREAK) || defined(PROTO)
10846/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010847 * Return the width of the 'number' and 'relativenumber' column.
10848 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010849 * Otherwise it depends on 'numberwidth' and the line count.
10850 */
10851 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010852number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010853{
10854 int n;
10855 linenr_T lnum;
10856
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010857 if (wp->w_p_rnu && !wp->w_p_nu)
10858 /* cursor line shows "0" */
10859 lnum = wp->w_height;
10860 else
10861 /* cursor line shows absolute line number */
10862 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010863
Bram Moolenaar6b314672015-03-20 15:42:10 +010010864 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010865 return wp->w_nrwidth_width;
10866 wp->w_nrwidth_line_count = lnum;
10867
10868 n = 0;
10869 do
10870 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010871 lnum /= 10;
10872 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010873 } while (lnum > 0);
10874
10875 /* 'numberwidth' gives the minimal width plus one */
10876 if (n < wp->w_p_nuw - 1)
10877 n = wp->w_p_nuw - 1;
10878
10879 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010880 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010881 return n;
10882}
10883#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010884
10885/*
10886 * Return the current cursor column. This is the actual position on the
10887 * screen. First column is 0.
10888 */
10889 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010890screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010891{
10892 return screen_cur_col;
10893}
10894
10895/*
10896 * Return the current cursor row. This is the actual position on the screen.
10897 * First row is 0.
10898 */
10899 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010900screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010901{
10902 return screen_cur_row;
10903}