blob: a392a921263f5726426eb56d6733e9234c5e1ae5 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
115/*
116 * Buffer for one screen line (characters and attributes).
117 */
118static schar_T *current_ScreenLine;
119
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static void win_update(win_T *wp);
121static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
124static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
125static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static int win_line(win_T *, linenr_T, int, int, int nochange);
128static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_RIGHTLEFT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void screen_line(int row, int coloff, int endcol, int clear_width, int rlflag);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
132#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void screen_line(int row, int coloff, int endcol, int clear_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
135#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100136#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200143# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void start_search_hl(void);
145static void end_search_hl(void);
146static void init_search_hl(win_T *wp);
147static void prepare_search_hl(win_T *wp, linenr_T lnum);
148static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
149static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_start_highlight(int attr);
152static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void screenclear2(void);
157static void lineclear(unsigned off, int width);
158static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100159#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void linecopy(int to, int from, win_T *wp);
161static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
164static void win_rest_invalid(win_T *wp);
165static void msg_pos_mode(void);
166static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_status(int *attr, int is_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100173#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100180static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif
182
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100183#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/* Ugly global: overrule attribute used by screen_char() */
185static int screen_char_attr = 0;
186#endif
187
188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
191 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 if (wp->w_redr_type < type)
205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100251redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 redraw_buf_later(curbuf, type);
254}
255
256 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100257redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 win_T *wp;
260
261 FOR_ALL_WINDOWS(wp)
262 {
263 if (wp->w_buffer == buf)
264 redraw_win_later(wp, type);
265 }
266}
267
268/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200269 * Redraw as soon as possible. When the command line is not scrolled redraw
270 * right away and restore what was on the command line.
271 * Return a code indicating what happened.
272 */
273 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100274redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200275{
276 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200277 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200278 int r;
279 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200280 schar_T *screenline; /* copy from ScreenLines[] */
281 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200282#ifdef FEAT_MBYTE
283 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200284 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200285 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200286 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200287#endif
288
289 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200290 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200291 return ret;
292
293 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200296 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200298 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299 if (screenline == NULL || screenattr == NULL)
300 ret = 2;
301#ifdef FEAT_MBYTE
302 if (enc_utf8)
303 {
304 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200305 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306 if (screenlineUC == NULL)
307 ret = 2;
308 for (i = 0; i < p_mco; ++i)
309 {
310 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 if (screenlineC[i] == NULL)
313 ret = 2;
314 }
315 }
316 if (enc_dbcs == DBCS_JPNU)
317 {
318 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 if (screenline2 == NULL)
321 ret = 2;
322 }
323#endif
324
325 if (ret != 2)
326 {
327 /* Save the text displayed in the command line area. */
328 for (r = 0; r < rows; ++r)
329 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200331 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200332 (size_t)cols * sizeof(schar_T));
333 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200334 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336#ifdef FEAT_MBYTE
337 if (enc_utf8)
338 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200339 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200343 mch_memmove(screenlineC[i] + r * cols,
344 ScreenLinesC[i] + LineOffset[cmdline_row + r],
345 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200346 }
347 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200348 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200349 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351#endif
352 }
353
354 update_screen(0);
355 ret = 3;
356
357 if (must_redraw == 0)
358 {
359 int off = (int)(current_ScreenLine - ScreenLines);
360
361 /* Restore the text displayed in the command line area. */
362 for (r = 0; r < rows; ++r)
363 {
364 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 screenline + r * cols,
366 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 screenattr + r * cols,
369 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370#ifdef FEAT_MBYTE
371 if (enc_utf8)
372 {
373 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 screenlineUC + r * cols,
375 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 for (i = 0; i < p_mco; ++i)
377 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 screenlineC[i] + r * cols,
379 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 }
381 if (enc_dbcs == DBCS_JPNU)
382 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200383 screenline2 + r * cols,
384 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200385#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200386 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387 }
388 ret = 4;
389 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 }
391
392 vim_free(screenline);
393 vim_free(screenattr);
394#ifdef FEAT_MBYTE
395 if (enc_utf8)
396 {
397 vim_free(screenlineUC);
398 for (i = 0; i < p_mco; ++i)
399 vim_free(screenlineC[i]);
400 }
401 if (enc_dbcs == DBCS_JPNU)
402 vim_free(screenline2);
403#endif
404
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200405 /* Show the intro message when appropriate. */
406 maybe_intro_message();
407
408 setcursor();
409
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 return ret;
411}
412
413/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100414 * Invoked after an asynchronous callback is called.
415 * If an echo command was used the cursor needs to be put back where
416 * it belongs. If highlighting was changed a redraw is needed.
417 */
418 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200419redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100420{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100421 if (State == HITRETURN || State == ASKMORE)
422 ; /* do nothing */
423 else if (State & CMDLINE)
424 redrawcmdline();
Bram Moolenaar144445d2016-07-08 21:41:54 +0200425 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100426 {
427 update_screen(0);
428 setcursor();
429 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100430 cursor_on();
431 out_flush();
432#ifdef FEAT_GUI
433 if (gui.in_use)
434 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200435 /* Don't update the cursor when it is blinking and off to avoid
436 * flicker. */
437 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200438 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439 gui_mch_flush();
440 }
441#endif
442}
443
444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 * Changed something in the current window, at buffer line "lnum", that
446 * requires that line and possibly other lines to be redrawn.
447 * Used when entering/leaving Insert mode with the cursor on a folded line.
448 * Used to remove the "$" from a change command.
449 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
450 * may become invalid and the whole window will have to be redrawn.
451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100453redrawWinline(
454 linenr_T lnum,
455 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457#ifdef FEAT_FOLDING
458 int i;
459#endif
460
461 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
462 curwin->w_redraw_top = lnum;
463 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
464 curwin->w_redraw_bot = lnum;
465 redraw_later(VALID);
466
467#ifdef FEAT_FOLDING
468 if (invalid)
469 {
470 /* A w_lines[] entry for this lnum has become invalid. */
471 i = find_wl_entry(curwin, lnum);
472 if (i >= 0)
473 curwin->w_lines[i].wl_valid = FALSE;
474 }
475#endif
476}
477
478/*
479 * update all windows that are editing the current buffer
480 */
481 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100482update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
484 redraw_curbuf_later(type);
485 update_screen(type);
486}
487
488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 * Based on the current value of curwin->w_topline, transfer a screenfull
490 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
491 */
492 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100493update_screen(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494{
495 win_T *wp;
496 static int did_intro = FALSE;
497#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
498 int did_one;
499#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200500#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200501 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200502 int gui_cursor_col;
503 int gui_cursor_row;
504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000506 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 if (!screen_valid(TRUE))
508 return;
509
510 if (must_redraw)
511 {
512 if (type < must_redraw) /* use maximal type */
513 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000514
515 /* must_redraw is reset here, so that when we run into some weird
516 * reason to redraw while busy redrawing (e.g., asynchronous
517 * scrolling), or update_topline() in win_update() will cause a
518 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 must_redraw = 0;
520 }
521
522 /* Need to update w_lines[]. */
523 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
524 type = NOT_VALID;
525
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000526 /* Postpone the redrawing when it's not needed and when being called
527 * recursively. */
528 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
530 redraw_later(type); /* remember type for next time */
531 must_redraw = type;
532 if (type > INVERTED_ALL)
533 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
534 return;
535 }
536
537 updating_screen = TRUE;
538#ifdef FEAT_SYN_HL
539 ++display_tick; /* let syntax code know we're in a next round of
540 * display updating */
541#endif
542
543 /*
544 * if the screen was scrolled up when displaying a message, scroll it down
545 */
546 if (msg_scrolled)
547 {
548 clear_cmdline = TRUE;
549 if (msg_scrolled > Rows - 5) /* clearing is faster */
550 type = CLEAR;
551 else if (type != CLEAR)
552 {
553 check_for_delay(FALSE);
554 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
555 type = CLEAR;
556 FOR_ALL_WINDOWS(wp)
557 {
558 if (W_WINROW(wp) < msg_scrolled)
559 {
560 if (W_WINROW(wp) + wp->w_height > msg_scrolled
561 && wp->w_redr_type < REDRAW_TOP
562 && wp->w_lines_valid > 0
563 && wp->w_topline == wp->w_lines[0].wl_lnum)
564 {
565 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
566 wp->w_redr_type = REDRAW_TOP;
567 }
568 else
569 {
570 wp->w_redr_type = NOT_VALID;
571#ifdef FEAT_WINDOWS
572 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
573 <= msg_scrolled)
574 wp->w_redr_status = TRUE;
575#endif
576 }
577 }
578 }
579 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000580#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000581 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 }
584 msg_scrolled = 0;
585 need_wait_return = FALSE;
586 }
587
588 /* reset cmdline_row now (may have been changed temporarily) */
589 compute_cmdrow();
590
591 /* Check for changed highlighting */
592 if (need_highlight_changed)
593 highlight_changed();
594
595 if (type == CLEAR) /* first clear screen */
596 {
597 screenclear(); /* will reset clear_cmdline */
598 type = NOT_VALID;
599 }
600
601 if (clear_cmdline) /* going to clear cmdline (done below) */
602 check_for_delay(FALSE);
603
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000604#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200605 /* Force redraw when width of 'number' or 'relativenumber' column
606 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000607 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200608 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
609 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000610 curwin->w_redr_type = NOT_VALID;
611#endif
612
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 /*
614 * Only start redrawing if there is really something to do.
615 */
616 if (type == INVERTED)
617 update_curswant();
618 if (curwin->w_redr_type < type
619 && !((type == VALID
620 && curwin->w_lines[0].wl_valid
621#ifdef FEAT_DIFF
622 && curwin->w_topfill == curwin->w_old_topfill
623 && curwin->w_botfill == curwin->w_old_botfill
624#endif
625 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000627 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
629 && curwin->w_old_visual_mode == VIsual_mode
630 && (curwin->w_valid & VALID_VIRTCOL)
631 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 ))
633 curwin->w_redr_type = type;
634
Bram Moolenaar5a305422006-04-28 22:38:25 +0000635#ifdef FEAT_WINDOWS
636 /* Redraw the tab pages line if needed. */
637 if (redraw_tabline || type >= NOT_VALID)
638 draw_tabline();
639#endif
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#ifdef FEAT_SYN_HL
642 /*
643 * Correct stored syntax highlighting info for changes in each displayed
644 * buffer. Each buffer must only be done once.
645 */
646 FOR_ALL_WINDOWS(wp)
647 {
648 if (wp->w_buffer->b_mod_set)
649 {
650# ifdef FEAT_WINDOWS
651 win_T *wwp;
652
653 /* Check if we already did this buffer. */
654 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
655 if (wwp->w_buffer == wp->w_buffer)
656 break;
657# endif
658 if (
659# ifdef FEAT_WINDOWS
660 wwp == wp &&
661# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200662 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 syn_stack_apply_changes(wp->w_buffer);
664 }
665 }
666#endif
667
668 /*
669 * Go from top to bottom through the windows, redrawing the ones that need
670 * it.
671 */
672#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
673 did_one = FALSE;
674#endif
675#ifdef FEAT_SEARCH_EXTRA
676 search_hl.rm.regprog = NULL;
677#endif
678 FOR_ALL_WINDOWS(wp)
679 {
680 if (wp->w_redr_type != 0)
681 {
682 cursor_off();
683#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
684 if (!did_one)
685 {
686 did_one = TRUE;
687# ifdef FEAT_SEARCH_EXTRA
688 start_search_hl();
689# endif
690# ifdef FEAT_CLIPBOARD
691 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200692 if (clip_star.available && clip_isautosel_star())
693 clip_update_selection(&clip_star);
694 if (clip_plus.available && clip_isautosel_plus())
695 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696# endif
697#ifdef FEAT_GUI
698 /* Remove the cursor before starting to do anything, because
699 * scrolling may make it difficult to redraw the text under
700 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200701 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200702 {
703 gui_cursor_col = gui.cursor_col;
704 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200706 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708#endif
709 }
710#endif
711 win_update(wp);
712 }
713
714#ifdef FEAT_WINDOWS
715 /* redraw status line after the window to minimize cursor movement */
716 if (wp->w_redr_status)
717 {
718 cursor_off();
719 win_redr_status(wp);
720 }
721#endif
722 }
723#if defined(FEAT_SEARCH_EXTRA)
724 end_search_hl();
725#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100726#ifdef FEAT_INS_EXPAND
727 /* May need to redraw the popup menu. */
728 if (pum_visible())
729 pum_redraw();
730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
732#ifdef FEAT_WINDOWS
733 /* Reset b_mod_set flags. Going through all windows is probably faster
734 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200735 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 wp->w_buffer->b_mod_set = FALSE;
737#else
738 curbuf->b_mod_set = FALSE;
739#endif
740
741 updating_screen = FALSE;
742#ifdef FEAT_GUI
743 gui_may_resize_shell();
744#endif
745
746 /* Clear or redraw the command line. Done last, because scrolling may
747 * mess up the command line. */
748 if (clear_cmdline || redraw_cmdline)
749 showmode();
750
751 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200752 if (!did_intro)
753 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 did_intro = TRUE;
755
756#ifdef FEAT_GUI
757 /* Redraw the cursor and update the scrollbars when all screen updating is
758 * done. */
759 if (gui.in_use)
760 {
761 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200762 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200763 {
764 /* Put the GUI position where the cursor was, gui_update_cursor()
765 * uses that. */
766 gui.col = gui_cursor_col;
767 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200768# ifdef FEAT_MBYTE
769 gui.col = mb_fix_col(gui.col, gui.row);
770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200772 screen_cur_col = gui.col;
773 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 gui_update_scrollbars(FALSE);
776 }
777#endif
778}
779
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100780#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
781/*
782 * Prepare for updating one or more windows.
783 * Caller must check for "updating_screen" already set to avoid recursiveness.
784 */
785 static void
786update_prepare(void)
787{
788 cursor_off();
789 updating_screen = TRUE;
790#ifdef FEAT_GUI
791 /* Remove the cursor before starting to do anything, because scrolling may
792 * make it difficult to redraw the text under it. */
793 if (gui.in_use)
794 gui_undraw_cursor();
795#endif
796#ifdef FEAT_SEARCH_EXTRA
797 start_search_hl();
798#endif
799}
800
801/*
802 * Finish updating one or more windows.
803 */
804 static void
805update_finish(void)
806{
807 if (redraw_cmdline)
808 showmode();
809
810# ifdef FEAT_SEARCH_EXTRA
811 end_search_hl();
812# endif
813
814 updating_screen = FALSE;
815
816# ifdef FEAT_GUI
817 gui_may_resize_shell();
818
819 /* Redraw the cursor and update the scrollbars when all screen updating is
820 * done. */
821 if (gui.in_use)
822 {
823 out_flush(); /* required before updating the cursor */
824 gui_update_cursor(FALSE, FALSE);
825 gui_update_scrollbars(FALSE);
826 }
827# endif
828}
829#endif
830
Bram Moolenaar860cae12010-06-05 23:22:07 +0200831#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200832/*
833 * Return TRUE if the cursor line in window "wp" may be concealed, according
834 * to the 'concealcursor' option.
835 */
836 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100837conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200838{
839 int c;
840
841 if (*wp->w_p_cocu == NUL)
842 return FALSE;
843 if (get_real_state() & VISUAL)
844 c = 'v';
845 else if (State & INSERT)
846 c = 'i';
847 else if (State & NORMAL)
848 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200849 else if (State & CMDLINE)
850 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200851 else
852 return FALSE;
853 return vim_strchr(wp->w_p_cocu, c) != NULL;
854}
855
856/*
857 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
858 */
859 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100860conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200861{
862 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
863 {
864 need_cursor_line_redraw = TRUE;
865 /* Need to recompute cursor column, e.g., when starting Visual mode
866 * without concealing. */
867 curs_columns(TRUE);
868 }
869}
870
Bram Moolenaar860cae12010-06-05 23:22:07 +0200871 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100872update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200873{
874 int row;
875 int j;
876
Bram Moolenaar908be432016-05-24 10:51:30 +0200877 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100878 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200879 return;
880
Bram Moolenaar860cae12010-06-05 23:22:07 +0200881 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200882 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200883 {
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100884 update_prepare();
885
Bram Moolenaar860cae12010-06-05 23:22:07 +0200886 row = 0;
887 for (j = 0; j < wp->w_lines_valid; ++j)
888 {
889 if (lnum == wp->w_lines[j].wl_lnum)
890 {
891 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200892# ifdef FEAT_SEARCH_EXTRA
893 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200894 start_search_hl();
895 prepare_search_hl(wp, lnum);
896# endif
897 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
898# if defined(FEAT_SEARCH_EXTRA)
899 end_search_hl();
900# endif
901 break;
902 }
903 row += wp->w_lines[j].wl_size;
904 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100905
906 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200907 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200908 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909}
910#endif
911
912#if defined(FEAT_SIGNS) || defined(PROTO)
913 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100914update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915{
916 win_T *wp;
917 int doit = FALSE;
918
919# ifdef FEAT_FOLDING
920 win_foldinfo.fi_level = 0;
921# endif
922
923 /* update/delete a specific mark */
924 FOR_ALL_WINDOWS(wp)
925 {
926 if (buf != NULL && lnum > 0)
927 {
928 if (wp->w_buffer == buf && lnum >= wp->w_topline
929 && lnum < wp->w_botline)
930 {
931 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
932 wp->w_redraw_top = lnum;
933 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
934 wp->w_redraw_bot = lnum;
935 redraw_win_later(wp, VALID);
936 }
937 }
938 else
939 redraw_win_later(wp, VALID);
940 if (wp->w_redr_type != 0)
941 doit = TRUE;
942 }
943
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100944 /* Return when there is nothing to do, screen updating is already
945 * happening (recursive call) or still starting up. */
946 if (!doit || updating_screen
947#ifdef FEAT_GUI
948 || gui.starting
949#endif
950 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 return;
952
953 /* update all windows that need updating */
954 update_prepare();
955
956# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +0200957 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 {
959 if (wp->w_redr_type != 0)
960 win_update(wp);
961 if (wp->w_redr_status)
962 win_redr_status(wp);
963 }
964# else
965 if (curwin->w_redr_type != 0)
966 win_update(curwin);
967# endif
968
969 update_finish();
970}
971#endif
972
973
974#if defined(FEAT_GUI) || defined(PROTO)
975/*
976 * Update a single window, its status line and maybe the command line msg.
977 * Used for the GUI scrollbar.
978 */
979 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100980updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000982 /* return if already busy updating */
983 if (updating_screen)
984 return;
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 update_prepare();
987
988#ifdef FEAT_CLIPBOARD
989 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200990 if (clip_star.available && clip_isautosel_star())
991 clip_update_selection(&clip_star);
992 if (clip_plus.available && clip_isautosel_plus())
993 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000999 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001000 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001001 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (wp->w_redr_status
1004# ifdef FEAT_CMDL_INFO
1005 || p_ru
1006# endif
1007# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001008 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009# endif
1010 )
1011 win_redr_status(wp);
1012#endif
1013
1014 update_finish();
1015}
1016#endif
1017
1018/*
1019 * Update a single window.
1020 *
1021 * This may cause the windows below it also to be redrawn (when clearing the
1022 * screen or scrolling lines).
1023 *
1024 * How the window is redrawn depends on wp->w_redr_type. Each type also
1025 * implies the one below it.
1026 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001027 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1029 * INVERTED redraw the changed part of the Visual area
1030 * INVERTED_ALL redraw the whole Visual area
1031 * VALID 1. scroll up/down to adjust for a changed w_topline
1032 * 2. update lines at the top when scrolled down
1033 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001034 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 * b_mod_top and b_mod_bot.
1036 * - if wp->w_redraw_top non-zero, redraw lines between
1037 * wp->w_redraw_top and wp->w_redr_bot.
1038 * - continue redrawing when syntax status is invalid.
1039 * 4. if scrolled up, update lines at the bottom.
1040 * This results in three areas that may need updating:
1041 * top: from first row to top_end (when scrolled down)
1042 * mid: from mid_start to mid_end (update inversion or changed text)
1043 * bot: from bot_start to last row (when scrolled up)
1044 */
1045 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001046win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
1048 buf_T *buf = wp->w_buffer;
1049 int type;
1050 int top_end = 0; /* Below last row of the top area that needs
1051 updating. 0 when no top area updating. */
1052 int mid_start = 999;/* first row of the mid area that needs
1053 updating. 999 when no mid area updating. */
1054 int mid_end = 0; /* Below last row of the mid area that needs
1055 updating. 0 when no mid area updating. */
1056 int bot_start = 999;/* first row of the bot area that needs
1057 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 int scrolled_down = FALSE; /* TRUE when scrolled down when
1059 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001061 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 int top_to_mod = FALSE; /* redraw above mod_top */
1063#endif
1064
1065 int row; /* current window row to display */
1066 linenr_T lnum; /* current buffer lnum to display */
1067 int idx; /* current index in w_lines[] */
1068 int srow; /* starting row of the current line */
1069
1070 int eof = FALSE; /* if TRUE, we hit the end of the file */
1071 int didline = FALSE; /* if TRUE, we finished the last line */
1072 int i;
1073 long j;
1074 static int recursive = FALSE; /* being called recursively */
1075 int old_botline = wp->w_botline;
1076#ifdef FEAT_FOLDING
1077 long fold_count;
1078#endif
1079#ifdef FEAT_SYN_HL
1080 /* remember what happened to the previous line, to know if
1081 * check_visual_highlight() can be used */
1082#define DID_NONE 1 /* didn't update a line */
1083#define DID_LINE 2 /* updated a normal line */
1084#define DID_FOLD 3 /* updated a folded line */
1085 int did_update = DID_NONE;
1086 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1087#endif
1088 linenr_T mod_top = 0;
1089 linenr_T mod_bot = 0;
1090#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1091 int save_got_int;
1092#endif
1093
1094 type = wp->w_redr_type;
1095
1096 if (type == NOT_VALID)
1097 {
1098#ifdef FEAT_WINDOWS
1099 wp->w_redr_status = TRUE;
1100#endif
1101 wp->w_lines_valid = 0;
1102 }
1103
1104 /* Window is zero-height: nothing to draw. */
1105 if (wp->w_height == 0)
1106 {
1107 wp->w_redr_type = 0;
1108 return;
1109 }
1110
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001111#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 /* Window is zero-width: Only need to draw the separator. */
1113 if (wp->w_width == 0)
1114 {
1115 /* draw the vertical separator right of this window */
1116 draw_vsep_win(wp, 0);
1117 wp->w_redr_type = 0;
1118 return;
1119 }
1120#endif
1121
1122#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001123 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124#endif
1125
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001126#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001127 /* Force redraw when width of 'number' or 'relativenumber' column
1128 * changes. */
1129 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001130 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001131 {
1132 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001133 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001134 }
1135 else
1136#endif
1137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1139 {
1140 /*
1141 * When there are both inserted/deleted lines and specific lines to be
1142 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1143 * everything (only happens when redrawing is off for while).
1144 */
1145 type = NOT_VALID;
1146 }
1147 else
1148 {
1149 /*
1150 * Set mod_top to the first line that needs displaying because of
1151 * changes. Set mod_bot to the first line after the changes.
1152 */
1153 mod_top = wp->w_redraw_top;
1154 if (wp->w_redraw_bot != 0)
1155 mod_bot = wp->w_redraw_bot + 1;
1156 else
1157 mod_bot = 0;
1158 wp->w_redraw_top = 0; /* reset for next time */
1159 wp->w_redraw_bot = 0;
1160 if (buf->b_mod_set)
1161 {
1162 if (mod_top == 0 || mod_top > buf->b_mod_top)
1163 {
1164 mod_top = buf->b_mod_top;
1165#ifdef FEAT_SYN_HL
1166 /* Need to redraw lines above the change that may be included
1167 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001168 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001170 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (mod_top < 1)
1172 mod_top = 1;
1173 }
1174#endif
1175 }
1176 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1177 mod_bot = buf->b_mod_bot;
1178
1179#ifdef FEAT_SEARCH_EXTRA
1180 /* When 'hlsearch' is on and using a multi-line search pattern, a
1181 * change in one line may make the Search highlighting in a
1182 * previous line invalid. Simple solution: redraw all visible
1183 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001184 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001186 if (search_hl.rm.regprog != NULL
1187 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001189 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001190 {
1191 cur = wp->w_match_head;
1192 while (cur != NULL)
1193 {
1194 if (cur->match.regprog != NULL
1195 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001196 {
1197 top_to_mod = TRUE;
1198 break;
1199 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001200 cur = cur->next;
1201 }
1202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#endif
1204 }
1205#ifdef FEAT_FOLDING
1206 if (mod_top != 0 && hasAnyFolding(wp))
1207 {
1208 linenr_T lnumt, lnumb;
1209
1210 /*
1211 * A change in a line can cause lines above it to become folded or
1212 * unfolded. Find the top most buffer line that may be affected.
1213 * If the line was previously folded and displayed, get the first
1214 * line of that fold. If the line is folded now, get the first
1215 * folded line. Use the minimum of these two.
1216 */
1217
1218 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1219 * the line below it. If there is no valid entry, use w_topline.
1220 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1221 * to this line. If there is no valid entry, use MAXLNUM. */
1222 lnumt = wp->w_topline;
1223 lnumb = MAXLNUM;
1224 for (i = 0; i < wp->w_lines_valid; ++i)
1225 if (wp->w_lines[i].wl_valid)
1226 {
1227 if (wp->w_lines[i].wl_lastlnum < mod_top)
1228 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1229 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1230 {
1231 lnumb = wp->w_lines[i].wl_lnum;
1232 /* When there is a fold column it might need updating
1233 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001234 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 ++lnumb;
1236 }
1237 }
1238
1239 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1240 if (mod_top > lnumt)
1241 mod_top = lnumt;
1242
1243 /* Now do the same for the bottom line (one above mod_bot). */
1244 --mod_bot;
1245 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1246 ++mod_bot;
1247 if (mod_bot < lnumb)
1248 mod_bot = lnumb;
1249 }
1250#endif
1251
1252 /* When a change starts above w_topline and the end is below
1253 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001254 * If the end of the change is above w_topline: do like no change was
1255 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (mod_top != 0 && mod_top < wp->w_topline)
1257 {
1258 if (mod_bot > wp->w_topline)
1259 mod_top = wp->w_topline;
1260#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001261 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 top_end = 1;
1263#endif
1264 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001265
1266 /* When line numbers are displayed need to redraw all lines below
1267 * inserted/deleted lines. */
1268 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1269 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271
1272 /*
1273 * When only displaying the lines at the top, set top_end. Used when
1274 * window has scrolled down for msg_scrolled.
1275 */
1276 if (type == REDRAW_TOP)
1277 {
1278 j = 0;
1279 for (i = 0; i < wp->w_lines_valid; ++i)
1280 {
1281 j += wp->w_lines[i].wl_size;
1282 if (j >= wp->w_upd_rows)
1283 {
1284 top_end = j;
1285 break;
1286 }
1287 }
1288 if (top_end == 0)
1289 /* not found (cannot happen?): redraw everything */
1290 type = NOT_VALID;
1291 else
1292 /* top area defined, the rest is VALID */
1293 type = VALID;
1294 }
1295
Bram Moolenaar367329b2007-08-30 11:53:22 +00001296 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001297 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1298 * non-zero and thus not FALSE) will indicate that screenclear() was not
1299 * called. */
1300 if (screen_cleared)
1301 screen_cleared = MAYBE;
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 /*
1304 * If there are no changes on the screen that require a complete redraw,
1305 * handle three cases:
1306 * 1: we are off the top of the screen by a few lines: scroll down
1307 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1308 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1309 * w_lines[] that needs updating.
1310 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001311 if ((type == VALID || type == SOME_VALID
1312 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313#ifdef FEAT_DIFF
1314 && !wp->w_botfill && !wp->w_old_botfill
1315#endif
1316 )
1317 {
1318 if (mod_top != 0 && wp->w_topline == mod_top)
1319 {
1320 /*
1321 * w_topline is the first changed line, the scrolling will be done
1322 * further down.
1323 */
1324 }
1325 else if (wp->w_lines[0].wl_valid
1326 && (wp->w_topline < wp->w_lines[0].wl_lnum
1327#ifdef FEAT_DIFF
1328 || (wp->w_topline == wp->w_lines[0].wl_lnum
1329 && wp->w_topfill > wp->w_old_topfill)
1330#endif
1331 ))
1332 {
1333 /*
1334 * New topline is above old topline: May scroll down.
1335 */
1336#ifdef FEAT_FOLDING
1337 if (hasAnyFolding(wp))
1338 {
1339 linenr_T ln;
1340
1341 /* count the number of lines we are off, counting a sequence
1342 * of folded lines as one */
1343 j = 0;
1344 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1345 {
1346 ++j;
1347 if (j >= wp->w_height - 2)
1348 break;
1349 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1350 }
1351 }
1352 else
1353#endif
1354 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1355 if (j < wp->w_height - 2) /* not too far off */
1356 {
1357 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1358#ifdef FEAT_DIFF
1359 /* insert extra lines for previously invisible filler lines */
1360 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1361 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1362 - wp->w_old_topfill;
1363#endif
1364 if (i < wp->w_height - 2) /* less than a screen off */
1365 {
1366 /*
1367 * Try to insert the correct number of lines.
1368 * If not the last window, delete the lines at the bottom.
1369 * win_ins_lines may fail when the terminal can't do it.
1370 */
1371 if (i > 0)
1372 check_for_delay(FALSE);
1373 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1374 {
1375 if (wp->w_lines_valid != 0)
1376 {
1377 /* Need to update rows that are new, stop at the
1378 * first one that scrolled down. */
1379 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382 /* Move the entries that were scrolled, disable
1383 * the entries for the lines to be redrawn. */
1384 if ((wp->w_lines_valid += j) > wp->w_height)
1385 wp->w_lines_valid = wp->w_height;
1386 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1387 wp->w_lines[idx] = wp->w_lines[idx - j];
1388 while (idx >= 0)
1389 wp->w_lines[idx--].wl_valid = FALSE;
1390 }
1391 }
1392 else
1393 mid_start = 0; /* redraw all lines */
1394 }
1395 else
1396 mid_start = 0; /* redraw all lines */
1397 }
1398 else
1399 mid_start = 0; /* redraw all lines */
1400 }
1401 else
1402 {
1403 /*
1404 * New topline is at or below old topline: May scroll up.
1405 * When topline didn't change, find first entry in w_lines[] that
1406 * needs updating.
1407 */
1408
1409 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1410 j = -1;
1411 row = 0;
1412 for (i = 0; i < wp->w_lines_valid; i++)
1413 {
1414 if (wp->w_lines[i].wl_valid
1415 && wp->w_lines[i].wl_lnum == wp->w_topline)
1416 {
1417 j = i;
1418 break;
1419 }
1420 row += wp->w_lines[i].wl_size;
1421 }
1422 if (j == -1)
1423 {
1424 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1425 * lines */
1426 mid_start = 0;
1427 }
1428 else
1429 {
1430 /*
1431 * Try to delete the correct number of lines.
1432 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1433 */
1434#ifdef FEAT_DIFF
1435 /* If the topline didn't change, delete old filler lines,
1436 * otherwise delete filler lines of the new topline... */
1437 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1438 row += wp->w_old_topfill;
1439 else
1440 row += diff_check_fill(wp, wp->w_topline);
1441 /* ... but don't delete new filler lines. */
1442 row -= wp->w_topfill;
1443#endif
1444 if (row > 0)
1445 {
1446 check_for_delay(FALSE);
1447 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1448 bot_start = wp->w_height - row;
1449 else
1450 mid_start = 0; /* redraw all lines */
1451 }
1452 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1453 {
1454 /*
1455 * Skip the lines (below the deleted lines) that are still
1456 * valid and don't need redrawing. Copy their info
1457 * upwards, to compensate for the deleted lines. Set
1458 * bot_start to the first row that needs redrawing.
1459 */
1460 bot_start = 0;
1461 idx = 0;
1462 for (;;)
1463 {
1464 wp->w_lines[idx] = wp->w_lines[j];
1465 /* stop at line that didn't fit, unless it is still
1466 * valid (no lines deleted) */
1467 if (row > 0 && bot_start + row
1468 + (int)wp->w_lines[j].wl_size > wp->w_height)
1469 {
1470 wp->w_lines_valid = idx + 1;
1471 break;
1472 }
1473 bot_start += wp->w_lines[idx++].wl_size;
1474
1475 /* stop at the last valid entry in w_lines[].wl_size */
1476 if (++j >= wp->w_lines_valid)
1477 {
1478 wp->w_lines_valid = idx;
1479 break;
1480 }
1481 }
1482#ifdef FEAT_DIFF
1483 /* Correct the first entry for filler lines at the top
1484 * when it won't get updated below. */
1485 if (wp->w_p_diff && bot_start > 0)
1486 wp->w_lines[0].wl_size =
1487 plines_win_nofill(wp, wp->w_topline, TRUE)
1488 + wp->w_topfill;
1489#endif
1490 }
1491 }
1492 }
1493
1494 /* When starting redraw in the first line, redraw all lines. When
1495 * there is only one window it's probably faster to clear the screen
1496 * first. */
1497 if (mid_start == 0)
1498 {
1499 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001500 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001501 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001502 /* Clear the screen when it was not done by win_del_lines() or
1503 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1504 * then. */
1505 if (screen_cleared != TRUE)
1506 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001507#ifdef FEAT_WINDOWS
1508 /* The screen was cleared, redraw the tab pages line. */
1509 if (redraw_tabline)
1510 draw_tabline();
1511#endif
1512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001514
1515 /* When win_del_lines() or win_ins_lines() caused the screen to be
1516 * cleared (only happens for the first window) or when screenclear()
1517 * was called directly above, "must_redraw" will have been set to
1518 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1519 if (screen_cleared == TRUE)
1520 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 }
1522 else
1523 {
1524 /* Not VALID or INVERTED: redraw all lines. */
1525 mid_start = 0;
1526 mid_end = wp->w_height;
1527 }
1528
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001529 if (type == SOME_VALID)
1530 {
1531 /* SOME_VALID: redraw all lines. */
1532 mid_start = 0;
1533 mid_end = wp->w_height;
1534 type = NOT_VALID;
1535 }
1536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 /* check if we are updating or removing the inverted part */
1538 if ((VIsual_active && buf == curwin->w_buffer)
1539 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1540 {
1541 linenr_T from, to;
1542
1543 if (VIsual_active)
1544 {
1545 if (VIsual_active
1546 && (VIsual_mode != wp->w_old_visual_mode
1547 || type == INVERTED_ALL))
1548 {
1549 /*
1550 * If the type of Visual selection changed, redraw the whole
1551 * selection. Also when the ownership of the X selection is
1552 * gained or lost.
1553 */
1554 if (curwin->w_cursor.lnum < VIsual.lnum)
1555 {
1556 from = curwin->w_cursor.lnum;
1557 to = VIsual.lnum;
1558 }
1559 else
1560 {
1561 from = VIsual.lnum;
1562 to = curwin->w_cursor.lnum;
1563 }
1564 /* redraw more when the cursor moved as well */
1565 if (wp->w_old_cursor_lnum < from)
1566 from = wp->w_old_cursor_lnum;
1567 if (wp->w_old_cursor_lnum > to)
1568 to = wp->w_old_cursor_lnum;
1569 if (wp->w_old_visual_lnum < from)
1570 from = wp->w_old_visual_lnum;
1571 if (wp->w_old_visual_lnum > to)
1572 to = wp->w_old_visual_lnum;
1573 }
1574 else
1575 {
1576 /*
1577 * Find the line numbers that need to be updated: The lines
1578 * between the old cursor position and the current cursor
1579 * position. Also check if the Visual position changed.
1580 */
1581 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1582 {
1583 from = curwin->w_cursor.lnum;
1584 to = wp->w_old_cursor_lnum;
1585 }
1586 else
1587 {
1588 from = wp->w_old_cursor_lnum;
1589 to = curwin->w_cursor.lnum;
1590 if (from == 0) /* Visual mode just started */
1591 from = to;
1592 }
1593
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001594 if (VIsual.lnum != wp->w_old_visual_lnum
1595 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597 if (wp->w_old_visual_lnum < from
1598 && wp->w_old_visual_lnum != 0)
1599 from = wp->w_old_visual_lnum;
1600 if (wp->w_old_visual_lnum > to)
1601 to = wp->w_old_visual_lnum;
1602 if (VIsual.lnum < from)
1603 from = VIsual.lnum;
1604 if (VIsual.lnum > to)
1605 to = VIsual.lnum;
1606 }
1607 }
1608
1609 /*
1610 * If in block mode and changed column or curwin->w_curswant:
1611 * update all lines.
1612 * First compute the actual start and end column.
1613 */
1614 if (VIsual_mode == Ctrl_V)
1615 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001616 colnr_T fromc, toc;
1617#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1618 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaar404406a2014-10-09 13:24:43 +02001620 if (curwin->w_p_lbr)
1621 ve_flags = VE_ALL;
1622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001624#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1625 ve_flags = save_ve_flags;
1626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 ++toc;
1628 if (curwin->w_curswant == MAXCOL)
1629 toc = MAXCOL;
1630
1631 if (fromc != wp->w_old_cursor_fcol
1632 || toc != wp->w_old_cursor_lcol)
1633 {
1634 if (from > VIsual.lnum)
1635 from = VIsual.lnum;
1636 if (to < VIsual.lnum)
1637 to = VIsual.lnum;
1638 }
1639 wp->w_old_cursor_fcol = fromc;
1640 wp->w_old_cursor_lcol = toc;
1641 }
1642 }
1643 else
1644 {
1645 /* Use the line numbers of the old Visual area. */
1646 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1647 {
1648 from = wp->w_old_cursor_lnum;
1649 to = wp->w_old_visual_lnum;
1650 }
1651 else
1652 {
1653 from = wp->w_old_visual_lnum;
1654 to = wp->w_old_cursor_lnum;
1655 }
1656 }
1657
1658 /*
1659 * There is no need to update lines above the top of the window.
1660 */
1661 if (from < wp->w_topline)
1662 from = wp->w_topline;
1663
1664 /*
1665 * If we know the value of w_botline, use it to restrict the update to
1666 * the lines that are visible in the window.
1667 */
1668 if (wp->w_valid & VALID_BOTLINE)
1669 {
1670 if (from >= wp->w_botline)
1671 from = wp->w_botline - 1;
1672 if (to >= wp->w_botline)
1673 to = wp->w_botline - 1;
1674 }
1675
1676 /*
1677 * Find the minimal part to be updated.
1678 * Watch out for scrolling that made entries in w_lines[] invalid.
1679 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1680 * top_end; need to redraw from top_end to the "to" line.
1681 * A middle mouse click with a Visual selection may change the text
1682 * above the Visual area and reset wl_valid, do count these for
1683 * mid_end (in srow).
1684 */
1685 if (mid_start > 0)
1686 {
1687 lnum = wp->w_topline;
1688 idx = 0;
1689 srow = 0;
1690 if (scrolled_down)
1691 mid_start = top_end;
1692 else
1693 mid_start = 0;
1694 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1695 {
1696 if (wp->w_lines[idx].wl_valid)
1697 mid_start += wp->w_lines[idx].wl_size;
1698 else if (!scrolled_down)
1699 srow += wp->w_lines[idx].wl_size;
1700 ++idx;
1701# ifdef FEAT_FOLDING
1702 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1703 lnum = wp->w_lines[idx].wl_lnum;
1704 else
1705# endif
1706 ++lnum;
1707 }
1708 srow += mid_start;
1709 mid_end = wp->w_height;
1710 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1711 {
1712 if (wp->w_lines[idx].wl_valid
1713 && wp->w_lines[idx].wl_lnum >= to + 1)
1714 {
1715 /* Only update until first row of this line */
1716 mid_end = srow;
1717 break;
1718 }
1719 srow += wp->w_lines[idx].wl_size;
1720 }
1721 }
1722 }
1723
1724 if (VIsual_active && buf == curwin->w_buffer)
1725 {
1726 wp->w_old_visual_mode = VIsual_mode;
1727 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1728 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001729 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 wp->w_old_curswant = curwin->w_curswant;
1731 }
1732 else
1733 {
1734 wp->w_old_visual_mode = 0;
1735 wp->w_old_cursor_lnum = 0;
1736 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001737 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1741 /* reset got_int, otherwise regexp won't work */
1742 save_got_int = got_int;
1743 got_int = 0;
1744#endif
1745#ifdef FEAT_FOLDING
1746 win_foldinfo.fi_level = 0;
1747#endif
1748
1749 /*
1750 * Update all the window rows.
1751 */
1752 idx = 0; /* first entry in w_lines[].wl_size */
1753 row = 0;
1754 srow = 0;
1755 lnum = wp->w_topline; /* first line shown in window */
1756 for (;;)
1757 {
1758 /* stop updating when reached the end of the window (check for _past_
1759 * the end of the window is at the end of the loop) */
1760 if (row == wp->w_height)
1761 {
1762 didline = TRUE;
1763 break;
1764 }
1765
1766 /* stop updating when hit the end of the file */
1767 if (lnum > buf->b_ml.ml_line_count)
1768 {
1769 eof = TRUE;
1770 break;
1771 }
1772
1773 /* Remember the starting row of the line that is going to be dealt
1774 * with. It is used further down when the line doesn't fit. */
1775 srow = row;
1776
1777 /*
1778 * Update a line when it is in an area that needs updating, when it
1779 * has changes or w_lines[idx] is invalid.
1780 * bot_start may be halfway a wrapped line after using
1781 * win_del_lines(), check if the current line includes it.
1782 * When syntax folding is being used, the saved syntax states will
1783 * already have been updated, we can't see where the syntax state is
1784 * the same again, just update until the end of the window.
1785 */
1786 if (row < top_end
1787 || (row >= mid_start && row < mid_end)
1788#ifdef FEAT_SEARCH_EXTRA
1789 || top_to_mod
1790#endif
1791 || idx >= wp->w_lines_valid
1792 || (row + wp->w_lines[idx].wl_size > bot_start)
1793 || (mod_top != 0
1794 && (lnum == mod_top
1795 || (lnum >= mod_top
1796 && (lnum < mod_bot
1797#ifdef FEAT_SYN_HL
1798 || did_update == DID_FOLD
1799 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001800 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 && (
1802# ifdef FEAT_FOLDING
1803 (foldmethodIsSyntax(wp)
1804 && hasAnyFolding(wp)) ||
1805# endif
1806 syntax_check_changed(lnum)))
1807#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001808#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001809 /* match in fixed position might need redraw
1810 * if lines were inserted or deleted */
1811 || (wp->w_match_head != NULL
1812 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 )))))
1815 {
1816#ifdef FEAT_SEARCH_EXTRA
1817 if (lnum == mod_top)
1818 top_to_mod = FALSE;
1819#endif
1820
1821 /*
1822 * When at start of changed lines: May scroll following lines
1823 * up or down to minimize redrawing.
1824 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001825 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 */
1827 if (lnum == mod_top
1828 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001829 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 {
1831 int old_rows = 0;
1832 int new_rows = 0;
1833 int xtra_rows;
1834 linenr_T l;
1835
1836 /* Count the old number of window rows, using w_lines[], which
1837 * should still contain the sizes for the lines as they are
1838 * currently displayed. */
1839 for (i = idx; i < wp->w_lines_valid; ++i)
1840 {
1841 /* Only valid lines have a meaningful wl_lnum. Invalid
1842 * lines are part of the changed area. */
1843 if (wp->w_lines[i].wl_valid
1844 && wp->w_lines[i].wl_lnum == mod_bot)
1845 break;
1846 old_rows += wp->w_lines[i].wl_size;
1847#ifdef FEAT_FOLDING
1848 if (wp->w_lines[i].wl_valid
1849 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1850 {
1851 /* Must have found the last valid entry above mod_bot.
1852 * Add following invalid entries. */
1853 ++i;
1854 while (i < wp->w_lines_valid
1855 && !wp->w_lines[i].wl_valid)
1856 old_rows += wp->w_lines[i++].wl_size;
1857 break;
1858 }
1859#endif
1860 }
1861
1862 if (i >= wp->w_lines_valid)
1863 {
1864 /* We can't find a valid line below the changed lines,
1865 * need to redraw until the end of the window.
1866 * Inserting/deleting lines has no use. */
1867 bot_start = 0;
1868 }
1869 else
1870 {
1871 /* Able to count old number of rows: Count new window
1872 * rows, and may insert/delete lines */
1873 j = idx;
1874 for (l = lnum; l < mod_bot; ++l)
1875 {
1876#ifdef FEAT_FOLDING
1877 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1878 ++new_rows;
1879 else
1880#endif
1881#ifdef FEAT_DIFF
1882 if (l == wp->w_topline)
1883 new_rows += plines_win_nofill(wp, l, TRUE)
1884 + wp->w_topfill;
1885 else
1886#endif
1887 new_rows += plines_win(wp, l, TRUE);
1888 ++j;
1889 if (new_rows > wp->w_height - row - 2)
1890 {
1891 /* it's getting too much, must redraw the rest */
1892 new_rows = 9999;
1893 break;
1894 }
1895 }
1896 xtra_rows = new_rows - old_rows;
1897 if (xtra_rows < 0)
1898 {
1899 /* May scroll text up. If there is not enough
1900 * remaining text or scrolling fails, must redraw the
1901 * rest. If scrolling works, must redraw the text
1902 * below the scrolled text. */
1903 if (row - xtra_rows >= wp->w_height - 2)
1904 mod_bot = MAXLNUM;
1905 else
1906 {
1907 check_for_delay(FALSE);
1908 if (win_del_lines(wp, row,
1909 -xtra_rows, FALSE, FALSE) == FAIL)
1910 mod_bot = MAXLNUM;
1911 else
1912 bot_start = wp->w_height + xtra_rows;
1913 }
1914 }
1915 else if (xtra_rows > 0)
1916 {
1917 /* May scroll text down. If there is not enough
1918 * remaining text of scrolling fails, must redraw the
1919 * rest. */
1920 if (row + xtra_rows >= wp->w_height - 2)
1921 mod_bot = MAXLNUM;
1922 else
1923 {
1924 check_for_delay(FALSE);
1925 if (win_ins_lines(wp, row + old_rows,
1926 xtra_rows, FALSE, FALSE) == FAIL)
1927 mod_bot = MAXLNUM;
1928 else if (top_end > row + old_rows)
1929 /* Scrolled the part at the top that requires
1930 * updating down. */
1931 top_end += xtra_rows;
1932 }
1933 }
1934
1935 /* When not updating the rest, may need to move w_lines[]
1936 * entries. */
1937 if (mod_bot != MAXLNUM && i != j)
1938 {
1939 if (j < i)
1940 {
1941 int x = row + new_rows;
1942
1943 /* move entries in w_lines[] upwards */
1944 for (;;)
1945 {
1946 /* stop at last valid entry in w_lines[] */
1947 if (i >= wp->w_lines_valid)
1948 {
1949 wp->w_lines_valid = j;
1950 break;
1951 }
1952 wp->w_lines[j] = wp->w_lines[i];
1953 /* stop at a line that won't fit */
1954 if (x + (int)wp->w_lines[j].wl_size
1955 > wp->w_height)
1956 {
1957 wp->w_lines_valid = j + 1;
1958 break;
1959 }
1960 x += wp->w_lines[j++].wl_size;
1961 ++i;
1962 }
1963 if (bot_start > x)
1964 bot_start = x;
1965 }
1966 else /* j > i */
1967 {
1968 /* move entries in w_lines[] downwards */
1969 j -= i;
1970 wp->w_lines_valid += j;
1971 if (wp->w_lines_valid > wp->w_height)
1972 wp->w_lines_valid = wp->w_height;
1973 for (i = wp->w_lines_valid; i - j >= idx; --i)
1974 wp->w_lines[i] = wp->w_lines[i - j];
1975
1976 /* The w_lines[] entries for inserted lines are
1977 * now invalid, but wl_size may be used above.
1978 * Reset to zero. */
1979 while (i >= idx)
1980 {
1981 wp->w_lines[i].wl_size = 0;
1982 wp->w_lines[i--].wl_valid = FALSE;
1983 }
1984 }
1985 }
1986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 }
1988
1989#ifdef FEAT_FOLDING
1990 /*
1991 * When lines are folded, display one line for all of them.
1992 * Otherwise, display normally (can be several display lines when
1993 * 'wrap' is on).
1994 */
1995 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1996 if (fold_count != 0)
1997 {
1998 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1999 ++row;
2000 --fold_count;
2001 wp->w_lines[idx].wl_folded = TRUE;
2002 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2003# ifdef FEAT_SYN_HL
2004 did_update = DID_FOLD;
2005# endif
2006 }
2007 else
2008#endif
2009 if (idx < wp->w_lines_valid
2010 && wp->w_lines[idx].wl_valid
2011 && wp->w_lines[idx].wl_lnum == lnum
2012 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002013 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 && srow + wp->w_lines[idx].wl_size > wp->w_height
2015#ifdef FEAT_DIFF
2016 && diff_check_fill(wp, lnum) == 0
2017#endif
2018 )
2019 {
2020 /* This line is not going to fit. Don't draw anything here,
2021 * will draw "@ " lines below. */
2022 row = wp->w_height + 1;
2023 }
2024 else
2025 {
2026#ifdef FEAT_SEARCH_EXTRA
2027 prepare_search_hl(wp, lnum);
2028#endif
2029#ifdef FEAT_SYN_HL
2030 /* Let the syntax stuff know we skipped a few lines. */
2031 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002032 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 syntax_end_parsing(syntax_last_parsed + 1);
2034#endif
2035
2036 /*
2037 * Display one line.
2038 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002039 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040
2041#ifdef FEAT_FOLDING
2042 wp->w_lines[idx].wl_folded = FALSE;
2043 wp->w_lines[idx].wl_lastlnum = lnum;
2044#endif
2045#ifdef FEAT_SYN_HL
2046 did_update = DID_LINE;
2047 syntax_last_parsed = lnum;
2048#endif
2049 }
2050
2051 wp->w_lines[idx].wl_lnum = lnum;
2052 wp->w_lines[idx].wl_valid = TRUE;
2053 if (row > wp->w_height) /* past end of screen */
2054 {
2055 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002056 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2058 ++idx;
2059 break;
2060 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002061 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 wp->w_lines[idx].wl_size = row - srow;
2063 ++idx;
2064#ifdef FEAT_FOLDING
2065 lnum += fold_count + 1;
2066#else
2067 ++lnum;
2068#endif
2069 }
2070 else
2071 {
2072 /* This line does not need updating, advance to the next one */
2073 row += wp->w_lines[idx++].wl_size;
2074 if (row > wp->w_height) /* past end of screen */
2075 break;
2076#ifdef FEAT_FOLDING
2077 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2078#else
2079 ++lnum;
2080#endif
2081#ifdef FEAT_SYN_HL
2082 did_update = DID_NONE;
2083#endif
2084 }
2085
2086 if (lnum > buf->b_ml.ml_line_count)
2087 {
2088 eof = TRUE;
2089 break;
2090 }
2091 }
2092 /*
2093 * End of loop over all window lines.
2094 */
2095
2096
2097 if (idx > wp->w_lines_valid)
2098 wp->w_lines_valid = idx;
2099
2100#ifdef FEAT_SYN_HL
2101 /*
2102 * Let the syntax stuff know we stop parsing here.
2103 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002104 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 syntax_end_parsing(syntax_last_parsed + 1);
2106#endif
2107
2108 /*
2109 * If we didn't hit the end of the file, and we didn't finish the last
2110 * line we were working on, then the line didn't fit.
2111 */
2112 wp->w_empty_rows = 0;
2113#ifdef FEAT_DIFF
2114 wp->w_filler_rows = 0;
2115#endif
2116 if (!eof && !didline)
2117 {
2118 if (lnum == wp->w_topline)
2119 {
2120 /*
2121 * Single line that does not fit!
2122 * Don't overwrite it, it can be edited.
2123 */
2124 wp->w_botline = lnum + 1;
2125 }
2126#ifdef FEAT_DIFF
2127 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2128 {
2129 /* Window ends in filler lines. */
2130 wp->w_botline = lnum;
2131 wp->w_filler_rows = wp->w_height - srow;
2132 }
2133#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002134 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2135 {
2136 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2137
2138 /*
2139 * Last line isn't finished: Display "@@@" in the last screen line.
2140 */
2141 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
2142 hl_attr(HLF_AT));
2143 screen_fill(scr_row, scr_row + 1,
2144 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
2145 '@', ' ', hl_attr(HLF_AT));
2146 set_empty_rows(wp, srow);
2147 wp->w_botline = lnum;
2148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2150 {
2151 /*
2152 * Last line isn't finished: Display "@@@" at the end.
2153 */
2154 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2155 W_WINROW(wp) + wp->w_height,
2156 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2157 '@', '@', hl_attr(HLF_AT));
2158 set_empty_rows(wp, srow);
2159 wp->w_botline = lnum;
2160 }
2161 else
2162 {
2163 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2164 wp->w_botline = lnum;
2165 }
2166 }
2167 else
2168 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002169#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 draw_vsep_win(wp, row);
2171#endif
2172 if (eof) /* we hit the end of the file */
2173 {
2174 wp->w_botline = buf->b_ml.ml_line_count + 1;
2175#ifdef FEAT_DIFF
2176 j = diff_check_fill(wp, wp->w_botline);
2177 if (j > 0 && !wp->w_botfill)
2178 {
2179 /*
2180 * Display filler lines at the end of the file
2181 */
2182 if (char2cells(fill_diff) > 1)
2183 i = '-';
2184 else
2185 i = fill_diff;
2186 if (row + j > wp->w_height)
2187 j = wp->w_height - row;
2188 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2189 row += j;
2190 }
2191#endif
2192 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002193 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 wp->w_botline = lnum;
2195
2196 /* make sure the rest of the screen is blank */
2197 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002198 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 }
2200
2201 /* Reset the type of redrawing required, the window has been updated. */
2202 wp->w_redr_type = 0;
2203#ifdef FEAT_DIFF
2204 wp->w_old_topfill = wp->w_topfill;
2205 wp->w_old_botfill = wp->w_botfill;
2206#endif
2207
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002208 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
2210 /*
2211 * There is a trick with w_botline. If we invalidate it on each
2212 * change that might modify it, this will cause a lot of expensive
2213 * calls to plines() in update_topline() each time. Therefore the
2214 * value of w_botline is often approximated, and this value is used to
2215 * compute the value of w_topline. If the value of w_botline was
2216 * wrong, check that the value of w_topline is correct (cursor is on
2217 * the visible part of the text). If it's not, we need to redraw
2218 * again. Mostly this just means scrolling up a few lines, so it
2219 * doesn't look too bad. Only do this for the current window (where
2220 * changes are relevant).
2221 */
2222 wp->w_valid |= VALID_BOTLINE;
2223 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2224 {
2225 recursive = TRUE;
2226 curwin->w_valid &= ~VALID_TOPLINE;
2227 update_topline(); /* may invalidate w_botline again */
2228 if (must_redraw != 0)
2229 {
2230 /* Don't update for changes in buffer again. */
2231 i = curbuf->b_mod_set;
2232 curbuf->b_mod_set = FALSE;
2233 win_update(curwin);
2234 must_redraw = 0;
2235 curbuf->b_mod_set = i;
2236 }
2237 recursive = FALSE;
2238 }
2239 }
2240
2241#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2242 /* restore got_int, unless CTRL-C was hit while redrawing */
2243 if (!got_int)
2244 got_int = save_got_int;
2245#endif
2246}
2247
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248/*
2249 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2250 * as the filler character.
2251 */
2252 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002253win_draw_end(
2254 win_T *wp,
2255 int c1,
2256 int c2,
2257 int row,
2258 int endrow,
2259 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260{
2261#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2262 int n = 0;
2263# define FDC_OFF n
2264#else
2265# define FDC_OFF 0
2266#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002267#ifdef FEAT_FOLDING
2268 int fdc = compute_foldcolumn(wp, 0);
2269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270
2271#ifdef FEAT_RIGHTLEFT
2272 if (wp->w_p_rl)
2273 {
2274 /* No check for cmdline window: should never be right-left. */
2275# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002276 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278 if (n > 0)
2279 {
2280 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002281 if (n > W_WIDTH(wp))
2282 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2284 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2285 ' ', ' ', hl_attr(HLF_FC));
2286 }
2287# endif
2288# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002289 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 int nn = n + 2;
2292
2293 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002294 if (nn > W_WIDTH(wp))
2295 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2297 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2298 ' ', ' ', hl_attr(HLF_SC));
2299 n = nn;
2300 }
2301# endif
2302 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2303 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2304 c2, c2, hl_attr(hl));
2305 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2306 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2307 c1, c2, hl_attr(hl));
2308 }
2309 else
2310#endif
2311 {
2312#ifdef FEAT_CMDWIN
2313 if (cmdwin_type != 0 && wp == curwin)
2314 {
2315 /* draw the cmdline character in the leftmost column */
2316 n = 1;
2317 if (n > wp->w_width)
2318 n = wp->w_width;
2319 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2320 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2321 cmdwin_type, ' ', hl_attr(HLF_AT));
2322 }
2323#endif
2324#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002325 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002327 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
2329 /* draw the fold column at the left */
2330 if (nn > W_WIDTH(wp))
2331 nn = W_WIDTH(wp);
2332 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2333 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2334 ' ', ' ', hl_attr(HLF_FC));
2335 n = nn;
2336 }
2337#endif
2338#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002339 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
2341 int nn = n + 2;
2342
2343 /* draw the sign column after the fold column */
2344 if (nn > W_WIDTH(wp))
2345 nn = W_WIDTH(wp);
2346 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2347 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2348 ' ', ' ', hl_attr(HLF_SC));
2349 n = nn;
2350 }
2351#endif
2352 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2353 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2354 c1, c2, hl_attr(hl));
2355 }
2356 set_empty_rows(wp, row);
2357}
2358
Bram Moolenaar1a384422010-07-14 19:53:30 +02002359#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002360static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002361
2362/*
2363 * Advance **color_cols and return TRUE when there are columns to draw.
2364 */
2365 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002366advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002367{
2368 while (**color_cols >= 0 && vcol > **color_cols)
2369 ++*color_cols;
2370 return (**color_cols >= 0);
2371}
2372#endif
2373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374#ifdef FEAT_FOLDING
2375/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002376 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2377 * space is available for window "wp", minus "col".
2378 */
2379 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002380compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002381{
2382 int fdc = wp->w_p_fdc;
2383 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2384 int wwidth = W_WIDTH(wp);
2385
2386 if (fdc > wwidth - (col + wmw))
2387 fdc = wwidth - (col + wmw);
2388 return fdc;
2389}
2390
2391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 * Display one folded line.
2393 */
2394 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002395fold_line(
2396 win_T *wp,
2397 long fold_count,
2398 foldinfo_T *foldinfo,
2399 linenr_T lnum,
2400 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002402 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 pos_T *top, *bot;
2404 linenr_T lnume = lnum + fold_count - 1;
2405 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002406 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 int col;
2409 int txtcol;
2410 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002411 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
2413 /* Build the fold line:
2414 * 1. Add the cmdwin_type for the command-line window
2415 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002416 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 * 4. Compose the text
2418 * 5. Add the text
2419 * 6. set highlighting for the Visual area an other text
2420 */
2421 col = 0;
2422
2423 /*
2424 * 1. Add the cmdwin_type for the command-line window
2425 * Ignores 'rightleft', this window is never right-left.
2426 */
2427#ifdef FEAT_CMDWIN
2428 if (cmdwin_type != 0 && wp == curwin)
2429 {
2430 ScreenLines[off] = cmdwin_type;
2431 ScreenAttrs[off] = hl_attr(HLF_AT);
2432#ifdef FEAT_MBYTE
2433 if (enc_utf8)
2434 ScreenLinesUC[off] = 0;
2435#endif
2436 ++col;
2437 }
2438#endif
2439
2440 /*
2441 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002442 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002444 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 if (fdc > 0)
2446 {
2447 fill_foldcolumn(buf, wp, TRUE, lnum);
2448#ifdef FEAT_RIGHTLEFT
2449 if (wp->w_p_rl)
2450 {
2451 int i;
2452
2453 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2454 hl_attr(HLF_FC));
2455 /* reverse the fold column */
2456 for (i = 0; i < fdc; ++i)
2457 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2458 }
2459 else
2460#endif
2461 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2462 col += fdc;
2463 }
2464
2465#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002466# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2467 for (ri = 0; ri < l; ++ri) \
2468 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2469 else \
2470 for (ri = 0; ri < l; ++ri) \
2471 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002473# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2474 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475#endif
2476
Bram Moolenaar64486672010-05-16 15:46:46 +02002477 /* Set all attributes of the 'number' or 'relativenumber' column and the
2478 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002479 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481#ifdef FEAT_SIGNS
2482 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002483 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
2485 len = W_WIDTH(wp) - col;
2486 if (len > 0)
2487 {
2488 if (len > 2)
2489 len = 2;
2490# ifdef FEAT_RIGHTLEFT
2491 if (wp->w_p_rl)
2492 /* the line number isn't reversed */
2493 copy_text_attr(off + W_WIDTH(wp) - len - col,
2494 (char_u *)" ", len, hl_attr(HLF_FL));
2495 else
2496# endif
2497 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2498 col += len;
2499 }
2500 }
2501#endif
2502
2503 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002504 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002506 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508 len = W_WIDTH(wp) - col;
2509 if (len > 0)
2510 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002511 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002512 long num;
2513 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002514
2515 if (len > w + 1)
2516 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002517
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002518 if (wp->w_p_nu && !wp->w_p_rnu)
2519 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002520 num = (long)lnum;
2521 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002522 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002523 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002524 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002525 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002526 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002527 /* 'number' + 'relativenumber': cursor line shows absolute
2528 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002529 num = lnum;
2530 fmt = "%-*ld ";
2531 }
2532 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002533
Bram Moolenaar700e7342013-01-30 12:31:36 +01002534 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535#ifdef FEAT_RIGHTLEFT
2536 if (wp->w_p_rl)
2537 /* the line number isn't reversed */
2538 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2539 hl_attr(HLF_FL));
2540 else
2541#endif
2542 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2543 col += len;
2544 }
2545 }
2546
2547 /*
2548 * 4. Compose the folded-line string with 'foldtext', if set.
2549 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002550 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552 txtcol = col; /* remember where text starts */
2553
2554 /*
2555 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2556 * Right-left text is put in columns 0 - number-col, normal text is put
2557 * in columns number-col - window-width.
2558 */
2559#ifdef FEAT_MBYTE
2560 if (has_mbyte)
2561 {
2562 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002563 int u8c, u8cc[MAX_MCO];
2564 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 int idx;
2566 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002567 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568# ifdef FEAT_ARABIC
2569 int prev_c = 0; /* previous Arabic character */
2570 int prev_c1 = 0; /* first composing char for prev_c */
2571# endif
2572
2573# ifdef FEAT_RIGHTLEFT
2574 if (wp->w_p_rl)
2575 idx = off;
2576 else
2577# endif
2578 idx = off + col;
2579
2580 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2581 for (p = text; *p != NUL; )
2582 {
2583 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002584 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (col + cells > W_WIDTH(wp)
2586# ifdef FEAT_RIGHTLEFT
2587 - (wp->w_p_rl ? col : 0)
2588# endif
2589 )
2590 break;
2591 ScreenLines[idx] = *p;
2592 if (enc_utf8)
2593 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002594 u8c = utfc_ptr2char(p, u8cc);
2595 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
2597 ScreenLinesUC[idx] = 0;
2598#ifdef FEAT_ARABIC
2599 prev_c = u8c;
2600#endif
2601 }
2602 else
2603 {
2604#ifdef FEAT_ARABIC
2605 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2606 {
2607 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002608 int pc, pc1, nc;
2609 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 int firstbyte = *p;
2611
2612 /* The idea of what is the previous and next
2613 * character depends on 'rightleft'. */
2614 if (wp->w_p_rl)
2615 {
2616 pc = prev_c;
2617 pc1 = prev_c1;
2618 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002619 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 }
2621 else
2622 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002623 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002625 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
2627 prev_c = u8c;
2628
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002629 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 pc, pc1, nc);
2631 ScreenLines[idx] = firstbyte;
2632 }
2633 else
2634 prev_c = u8c;
2635#endif
2636 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002637#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (u8c >= 0x10000)
2639 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2640 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002643 for (i = 0; i < Screen_mco; ++i)
2644 {
2645 ScreenLinesC[i][idx] = u8cc[i];
2646 if (u8cc[i] == 0)
2647 break;
2648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650 if (cells > 1)
2651 ScreenLines[idx + 1] = 0;
2652 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002653 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2654 /* double-byte single width character */
2655 ScreenLines2[idx] = p[1];
2656 else if (cells > 1)
2657 /* double-width character */
2658 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 col += cells;
2660 idx += cells;
2661 p += c_len;
2662 }
2663 }
2664 else
2665#endif
2666 {
2667 len = (int)STRLEN(text);
2668 if (len > W_WIDTH(wp) - col)
2669 len = W_WIDTH(wp) - col;
2670 if (len > 0)
2671 {
2672#ifdef FEAT_RIGHTLEFT
2673 if (wp->w_p_rl)
2674 STRNCPY(current_ScreenLine, text, len);
2675 else
2676#endif
2677 STRNCPY(current_ScreenLine + col, text, len);
2678 col += len;
2679 }
2680 }
2681
2682 /* Fill the rest of the line with the fold filler */
2683#ifdef FEAT_RIGHTLEFT
2684 if (wp->w_p_rl)
2685 col -= txtcol;
2686#endif
2687 while (col < W_WIDTH(wp)
2688#ifdef FEAT_RIGHTLEFT
2689 - (wp->w_p_rl ? txtcol : 0)
2690#endif
2691 )
2692 {
2693#ifdef FEAT_MBYTE
2694 if (enc_utf8)
2695 {
2696 if (fill_fold >= 0x80)
2697 {
2698 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002699 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701 else
2702 ScreenLinesUC[off + col] = 0;
2703 }
2704#endif
2705 ScreenLines[off + col++] = fill_fold;
2706 }
2707
2708 if (text != buf)
2709 vim_free(text);
2710
2711 /*
2712 * 6. set highlighting for the Visual area an other text.
2713 * If all folded lines are in the Visual area, highlight the line.
2714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2716 {
2717 if (ltoreq(curwin->w_cursor, VIsual))
2718 {
2719 /* Visual is after curwin->w_cursor */
2720 top = &curwin->w_cursor;
2721 bot = &VIsual;
2722 }
2723 else
2724 {
2725 /* Visual is before curwin->w_cursor */
2726 top = &VIsual;
2727 bot = &curwin->w_cursor;
2728 }
2729 if (lnum >= top->lnum
2730 && lnume <= bot->lnum
2731 && (VIsual_mode != 'v'
2732 || ((lnum > top->lnum
2733 || (lnum == top->lnum
2734 && top->col == 0))
2735 && (lnume < bot->lnum
2736 || (lnume == bot->lnum
2737 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002738 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
2740 if (VIsual_mode == Ctrl_V)
2741 {
2742 /* Visual block mode: highlight the chars part of the block */
2743 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2744 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002745 if (wp->w_old_cursor_lcol != MAXCOL
2746 && wp->w_old_cursor_lcol + txtcol
2747 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 len = wp->w_old_cursor_lcol;
2749 else
2750 len = W_WIDTH(wp) - txtcol;
2751 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002752 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754 }
2755 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002758 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002763#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002764 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2765 if (wp->w_p_cc_cols)
2766 {
2767 int i = 0;
2768 int j = wp->w_p_cc_cols[i];
2769 int old_txtcol = txtcol;
2770
2771 while (j > -1)
2772 {
2773 txtcol += j;
2774 if (wp->w_p_wrap)
2775 txtcol -= wp->w_skipcol;
2776 else
2777 txtcol -= wp->w_leftcol;
2778 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2779 ScreenAttrs[off + txtcol] = hl_combine_attr(
2780 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2781 txtcol = old_txtcol;
2782 j = wp->w_p_cc_cols[++i];
2783 }
2784 }
2785
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002786 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002787 if (wp->w_p_cuc)
2788 {
2789 txtcol += wp->w_virtcol;
2790 if (wp->w_p_wrap)
2791 txtcol -= wp->w_skipcol;
2792 else
2793 txtcol -= wp->w_leftcol;
2794 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2795 ScreenAttrs[off + txtcol] = hl_combine_attr(
2796 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2797 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2801 (int)W_WIDTH(wp), FALSE);
2802
2803 /*
2804 * Update w_cline_height and w_cline_folded if the cursor line was
2805 * updated (saves a call to plines() later).
2806 */
2807 if (wp == curwin
2808 && lnum <= curwin->w_cursor.lnum
2809 && lnume >= curwin->w_cursor.lnum)
2810 {
2811 curwin->w_cline_row = row;
2812 curwin->w_cline_height = 1;
2813 curwin->w_cline_folded = TRUE;
2814 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2815 }
2816}
2817
2818/*
2819 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2820 */
2821 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002822copy_text_attr(
2823 int off,
2824 char_u *buf,
2825 int len,
2826 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002828 int i;
2829
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 mch_memmove(ScreenLines + off, buf, (size_t)len);
2831# ifdef FEAT_MBYTE
2832 if (enc_utf8)
2833 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2834# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002835 for (i = 0; i < len; ++i)
2836 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837}
2838
2839/*
2840 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002841 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 */
2843 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002844fill_foldcolumn(
2845 char_u *p,
2846 win_T *wp,
2847 int closed, /* TRUE of FALSE */
2848 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
2850 int i = 0;
2851 int level;
2852 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002853 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002854 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
2856 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002857 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 level = win_foldinfo.fi_level;
2860 if (level > 0)
2861 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002862 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002863 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002864
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 /* If the column is too narrow, we start at the lowest level that
2866 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002867 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 if (first_level < 1)
2869 first_level = 1;
2870
Bram Moolenaar1c934292015-01-27 16:39:29 +01002871 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 {
2873 if (win_foldinfo.fi_lnum == lnum
2874 && first_level + i >= win_foldinfo.fi_low_level)
2875 p[i] = '-';
2876 else if (first_level == 1)
2877 p[i] = '|';
2878 else if (first_level + i <= 9)
2879 p[i] = '0' + first_level + i;
2880 else
2881 p[i] = '>';
2882 if (first_level + i == level)
2883 break;
2884 }
2885 }
2886 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002887 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888}
2889#endif /* FEAT_FOLDING */
2890
2891/*
2892 * Display line "lnum" of window 'wp' on the screen.
2893 * Start at row "startrow", stop when "endrow" is reached.
2894 * wp->w_virtcol needs to be valid.
2895 *
2896 * Return the number of last row the line occupies.
2897 */
2898 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002899win_line(
2900 win_T *wp,
2901 linenr_T lnum,
2902 int startrow,
2903 int endrow,
2904 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 int col; /* visual column on screen */
2907 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2908 int c = 0; /* init for GCC */
2909 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002910#ifdef FEAT_LINEBREAK
2911 long vcol_sbr = -1; /* virtual column after showbreak */
2912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 long vcol_prev = -1; /* "vcol" of previous character */
2914 char_u *line; /* current line */
2915 char_u *ptr; /* current position in "line" */
2916 int row; /* row in the window, excl w_winrow */
2917 int screen_row; /* row on the screen, incl w_winrow */
2918
2919 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2920 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002921 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002922 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 int c_extra = NUL; /* extra chars, all the same */
2924 int extra_attr = 0; /* attributes when n_extra != 0 */
2925 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2926 displaying lcs_eol at end-of-line */
2927 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2928 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2929
2930 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2931 int saved_n_extra = 0;
2932 char_u *saved_p_extra = NULL;
2933 int saved_c_extra = 0;
2934 int saved_char_attr = 0;
2935
2936 int n_attr = 0; /* chars with special attr */
2937 int saved_attr2 = 0; /* char_attr saved for n_attr */
2938 int n_attr3 = 0; /* chars with overruling special attr */
2939 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2940
2941 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2942
2943 int fromcol, tocol; /* start/end of inverting */
2944 int fromcol_prev = -2; /* start of inverting after cursor */
2945 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002947 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 pos_T pos;
2949 long v;
2950
2951 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002952 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2954 in this line */
2955 int attr = 0; /* attributes for area highlighting */
2956 int area_attr = 0; /* attributes desired by highlighting */
2957 int search_attr = 0; /* attributes desired by 'hlsearch' */
2958#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002959 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 int syntax_attr = 0; /* attributes desired by syntax */
2961 int has_syntax = FALSE; /* this buffer has syntax highl. */
2962 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002963 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002964 int draw_color_col = FALSE; /* highlight colorcolumn */
2965 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002966#endif
2967#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002968 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002969# define SPWORDLEN 150
2970 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002971 int nextlinecol = 0; /* column where nextline[] starts */
2972 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002973 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002974 int spell_attr = 0; /* attributes desired by spelling */
2975 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002976 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2977 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002978 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002979 static int cap_col = -1; /* column to check for Cap word */
2980 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002981 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982#endif
2983 int extra_check; /* has syntax or linebreak */
2984#ifdef FEAT_MBYTE
2985 int multi_attr = 0; /* attributes desired by multibyte */
2986 int mb_l = 1; /* multi-byte byte length */
2987 int mb_c = 0; /* decoded multi-byte character */
2988 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002989 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990#endif
2991#ifdef FEAT_DIFF
2992 int filler_lines; /* nr of filler lines to be drawn */
2993 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002994 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 int change_start = MAXCOL; /* first col of changed area */
2996 int change_end = -1; /* last col of changed area */
2997#endif
2998 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2999#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003000 int need_showbreak = FALSE; /* overlong line, skipping first x
3001 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003003#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3004 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003006 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007#endif
3008#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003009 matchitem_T *cur; /* points to the match list */
3010 match_T *shl; /* points to search_hl or a match */
3011 int shl_flag; /* flag to indicate whether search_hl
3012 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003013 int pos_inprogress; /* marks that position match search is
3014 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003015 int prevcol_hl_flag; /* flag to indicate whether prevcol
3016 equals startcol of search_hl or one
3017 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018#endif
3019#ifdef FEAT_ARABIC
3020 int prev_c = 0; /* previous Arabic character */
3021 int prev_c1 = 0; /* first composing char for prev_c */
3022#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003023#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003024 int did_line_attr = 0;
3025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026
3027 /* draw_state: items that are drawn in sequence: */
3028#define WL_START 0 /* nothing done yet */
3029#ifdef FEAT_CMDWIN
3030# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3031#else
3032# define WL_CMDLINE WL_START
3033#endif
3034#ifdef FEAT_FOLDING
3035# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3036#else
3037# define WL_FOLD WL_CMDLINE
3038#endif
3039#ifdef FEAT_SIGNS
3040# define WL_SIGN WL_FOLD + 1 /* column for signs */
3041#else
3042# define WL_SIGN WL_FOLD /* column for signs */
3043#endif
3044#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003045#ifdef FEAT_LINEBREAK
3046# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003048# define WL_BRI WL_NR
3049#endif
3050#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3051# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3052#else
3053# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054#endif
3055#define WL_LINE WL_SBR + 1 /* text in the line */
3056 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003057#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 int feedback_col = 0;
3059 int feedback_old_attr = -1;
3060#endif
3061
Bram Moolenaar860cae12010-06-05 23:22:07 +02003062#ifdef FEAT_CONCEAL
3063 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003064 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003065 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003066 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003067 int is_concealing = FALSE;
3068 int boguscols = 0; /* nonexistent columns added to force
3069 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003070 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003071 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003072 int match_conc = 0; /* cchar for match functions */
3073 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003074 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003075# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003076# define FIX_FOR_BOGUSCOLS \
3077 { \
3078 n_extra += vcol_off; \
3079 vcol -= vcol_off; \
3080 vcol_off = 0; \
3081 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003082 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003083 boguscols = 0; \
3084 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003085#else
3086# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
3089 if (startrow > endrow) /* past the end already! */
3090 return startrow;
3091
3092 row = startrow;
3093 screen_row = row + W_WINROW(wp);
3094
3095 /*
3096 * To speed up the loop below, set extra_check when there is linebreak,
3097 * trailing white space and/or syntax processing to be done.
3098 */
3099#ifdef FEAT_LINEBREAK
3100 extra_check = wp->w_p_lbr;
3101#else
3102 extra_check = 0;
3103#endif
3104#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003105 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 {
3107 /* Prepare for syntax highlighting in this line. When there is an
3108 * error, stop syntax highlighting. */
3109 save_did_emsg = did_emsg;
3110 did_emsg = FALSE;
3111 syntax_start(wp, lnum);
3112 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003113 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 else
3115 {
3116 did_emsg = save_did_emsg;
3117 has_syntax = TRUE;
3118 extra_check = TRUE;
3119 }
3120 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003121
3122 /* Check for columns to display for 'colorcolumn'. */
3123 color_cols = wp->w_p_cc_cols;
3124 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003125 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003126#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003127
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003128#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003129 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003130 && *wp->w_s->b_p_spl != NUL
3131 && wp->w_s->b_langp.ga_len > 0
3132 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003133 {
3134 /* Prepare for spell checking. */
3135 has_spell = TRUE;
3136 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003137
3138 /* Get the start of the next line, so that words that wrap to the next
3139 * line are found too: "et<line-break>al.".
3140 * Trick: skip a few chars for C/shell/Vim comments */
3141 nextline[SPWORDLEN] = NUL;
3142 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3143 {
3144 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3145 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3146 }
3147
3148 /* When a word wrapped from the previous line the start of the current
3149 * line is valid. */
3150 if (lnum == checked_lnum)
3151 cur_checked_col = checked_col;
3152 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003153
3154 /* When there was a sentence end in the previous line may require a
3155 * word starting with capital in this line. In line 1 always check
3156 * the first word. */
3157 if (lnum != capcol_lnum)
3158 cap_col = -1;
3159 if (lnum == 1)
3160 cap_col = 0;
3161 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#endif
3164
3165 /*
3166 * handle visual active in this window
3167 */
3168 fromcol = -10;
3169 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3171 {
3172 /* Visual is after curwin->w_cursor */
3173 if (ltoreq(curwin->w_cursor, VIsual))
3174 {
3175 top = &curwin->w_cursor;
3176 bot = &VIsual;
3177 }
3178 else /* Visual is before curwin->w_cursor */
3179 {
3180 top = &VIsual;
3181 bot = &curwin->w_cursor;
3182 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003183 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (VIsual_mode == Ctrl_V) /* block mode */
3185 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003186 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 fromcol = wp->w_old_cursor_fcol;
3189 tocol = wp->w_old_cursor_lcol;
3190 }
3191 }
3192 else /* non-block mode */
3193 {
3194 if (lnum > top->lnum && lnum <= bot->lnum)
3195 fromcol = 0;
3196 else if (lnum == top->lnum)
3197 {
3198 if (VIsual_mode == 'V') /* linewise */
3199 fromcol = 0;
3200 else
3201 {
3202 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3203 if (gchar_pos(top) == NUL)
3204 tocol = fromcol + 1;
3205 }
3206 }
3207 if (VIsual_mode != 'V' && lnum == bot->lnum)
3208 {
3209 if (*p_sel == 'e' && bot->col == 0
3210#ifdef FEAT_VIRTUALEDIT
3211 && bot->coladd == 0
3212#endif
3213 )
3214 {
3215 fromcol = -10;
3216 tocol = MAXCOL;
3217 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003218 else if (bot->col == MAXCOL)
3219 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 else
3221 {
3222 pos = *bot;
3223 if (*p_sel == 'e')
3224 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3225 else
3226 {
3227 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3228 ++tocol;
3229 }
3230 }
3231 }
3232 }
3233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 /* Check if the character under the cursor should not be inverted */
3235 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003236#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 )
3240 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
3242 /* if inverting in this line set area_highlighting */
3243 if (fromcol >= 0)
3244 {
3245 area_highlighting = TRUE;
3246 attr = hl_attr(HLF_V);
3247#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003248 if ((clip_star.available && !clip_star.owned
3249 && clip_isautosel_star())
3250 || (clip_plus.available && !clip_plus.owned
3251 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 attr = hl_attr(HLF_VNC);
3253#endif
3254 }
3255 }
3256
3257 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003258 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003260 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 && wp == curwin
3262 && lnum >= curwin->w_cursor.lnum
3263 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3264 {
3265 if (lnum == curwin->w_cursor.lnum)
3266 getvcol(curwin, &(curwin->w_cursor),
3267 (colnr_T *)&fromcol, NULL, NULL);
3268 else
3269 fromcol = 0;
3270 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3271 {
3272 pos.lnum = lnum;
3273 pos.col = search_match_endcol;
3274 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3275 }
3276 else
3277 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003278 /* do at least one character; happens when past end of line */
3279 if (fromcol == tocol)
3280 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 area_highlighting = TRUE;
3282 attr = hl_attr(HLF_I);
3283 }
3284
3285#ifdef FEAT_DIFF
3286 filler_lines = diff_check(wp, lnum);
3287 if (filler_lines < 0)
3288 {
3289 if (filler_lines == -1)
3290 {
3291 if (diff_find_change(wp, lnum, &change_start, &change_end))
3292 diff_hlf = HLF_ADD; /* added line */
3293 else if (change_start == 0)
3294 diff_hlf = HLF_TXD; /* changed text */
3295 else
3296 diff_hlf = HLF_CHD; /* changed line */
3297 }
3298 else
3299 diff_hlf = HLF_ADD; /* added line */
3300 filler_lines = 0;
3301 area_highlighting = TRUE;
3302 }
3303 if (lnum == wp->w_topline)
3304 filler_lines = wp->w_topfill;
3305 filler_todo = filler_lines;
3306#endif
3307
3308#ifdef LINE_ATTR
3309# ifdef FEAT_SIGNS
3310 /* If this line has a sign with line highlighting set line_attr. */
3311 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3312 if (v != 0)
3313 line_attr = sign_get_attr((int)v, TRUE);
3314# endif
3315# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3316 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003317 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 line_attr = hl_attr(HLF_L);
3319# endif
3320 if (line_attr != 0)
3321 area_highlighting = TRUE;
3322#endif
3323
3324 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3325 ptr = line;
3326
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003327#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003328 if (has_spell)
3329 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003330 /* For checking first word with a capital skip white space. */
3331 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003332 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003333
Bram Moolenaar30abd282005-06-22 22:35:10 +00003334 /* To be able to spell-check over line boundaries copy the end of the
3335 * current line into nextline[]. Above the start of the next line was
3336 * copied to nextline[SPWORDLEN]. */
3337 if (nextline[SPWORDLEN] == NUL)
3338 {
3339 /* No next line or it is empty. */
3340 nextlinecol = MAXCOL;
3341 nextline_idx = 0;
3342 }
3343 else
3344 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003345 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003346 if (v < SPWORDLEN)
3347 {
3348 /* Short line, use it completely and append the start of the
3349 * next line. */
3350 nextlinecol = 0;
3351 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003352 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003353 nextline_idx = v + 1;
3354 }
3355 else
3356 {
3357 /* Long line, use only the last SPWORDLEN bytes. */
3358 nextlinecol = v - SPWORDLEN;
3359 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3360 nextline_idx = SPWORDLEN + 1;
3361 }
3362 }
3363 }
3364#endif
3365
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003366 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003368 if (lcs_space || lcs_trail)
3369 extra_check = TRUE;
3370 /* find start of trailing whitespace */
3371 if (lcs_trail)
3372 {
3373 trailcol = (colnr_T)STRLEN(ptr);
3374 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3375 --trailcol;
3376 trailcol += (colnr_T) (ptr - line);
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379
3380 /*
3381 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3382 * first character to be displayed.
3383 */
3384 if (wp->w_p_wrap)
3385 v = wp->w_skipcol;
3386 else
3387 v = wp->w_leftcol;
3388 if (v > 0)
3389 {
3390#ifdef FEAT_MBYTE
3391 char_u *prev_ptr = ptr;
3392#endif
3393 while (vcol < v && *ptr != NUL)
3394 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003395 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 vcol += c;
3397#ifdef FEAT_MBYTE
3398 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003400 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003403 /* When:
3404 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003405 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003406 * - 'virtualedit' is set, or
3407 * - the visual mode is active,
3408 * the end of the line may be before the start of the displayed part.
3409 */
3410 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003411#ifdef FEAT_SYN_HL
3412 wp->w_p_cuc || draw_color_col ||
3413#endif
3414#ifdef FEAT_VIRTUALEDIT
3415 virtual_active() ||
3416#endif
3417 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003418 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 /* Handle a character that's not completely on the screen: Put ptr at
3423 * that character but skip the first few screen characters. */
3424 if (vcol > v)
3425 {
3426 vcol -= c;
3427#ifdef FEAT_MBYTE
3428 ptr = prev_ptr;
3429#else
3430 --ptr;
3431#endif
3432 n_skip = v - vcol;
3433 }
3434
3435 /*
3436 * Adjust for when the inverted text is before the screen,
3437 * and when the start of the inverted text is before the screen.
3438 */
3439 if (tocol <= vcol)
3440 fromcol = 0;
3441 else if (fromcol >= 0 && fromcol < vcol)
3442 fromcol = vcol;
3443
3444#ifdef FEAT_LINEBREAK
3445 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3446 if (wp->w_p_wrap)
3447 need_showbreak = TRUE;
3448#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003449#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003450 /* When spell checking a word we need to figure out the start of the
3451 * word and if it's badly spelled or not. */
3452 if (has_spell)
3453 {
3454 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003455 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003456 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003457
3458 pos = wp->w_cursor;
3459 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003460 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003461 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003462
3463 /* spell_move_to() may call ml_get() and make "line" invalid */
3464 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3465 ptr = line + linecol;
3466
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003467 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003468 {
3469 /* no bad word found at line start, don't check until end of a
3470 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003471 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003472 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003473 }
3474 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003475 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003476 /* bad word found, use attributes until end of word */
3477 word_end = wp->w_cursor.col + len + 1;
3478
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003479 /* Turn index into actual attributes. */
3480 if (spell_hlf != HLF_COUNT)
3481 spell_attr = highlight_attr[spell_hlf];
3482 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003483 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003484
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003485# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003486 /* Need to restart syntax highlighting for this line. */
3487 if (has_syntax)
3488 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003489# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003490 }
3491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493
3494 /*
3495 * Correct highlighting for cursor that can't be disabled.
3496 * Avoids having to check this for each character.
3497 */
3498 if (fromcol >= 0)
3499 {
3500 if (noinvcur)
3501 {
3502 if ((colnr_T)fromcol == wp->w_virtcol)
3503 {
3504 /* highlighting starts at cursor, let it start just after the
3505 * cursor */
3506 fromcol_prev = fromcol;
3507 fromcol = -1;
3508 }
3509 else if ((colnr_T)fromcol < wp->w_virtcol)
3510 /* restart highlighting after the cursor */
3511 fromcol_prev = wp->w_virtcol;
3512 }
3513 if (fromcol >= tocol)
3514 fromcol = -1;
3515 }
3516
3517#ifdef FEAT_SEARCH_EXTRA
3518 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003519 * Handle highlighting the last used search pattern and matches.
3520 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003522 cur = wp->w_match_head;
3523 shl_flag = FALSE;
3524 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003526 if (shl_flag == FALSE)
3527 {
3528 shl = &search_hl;
3529 shl_flag = TRUE;
3530 }
3531 else
3532 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003533 shl->startcol = MAXCOL;
3534 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003536 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003537 v = (long)(ptr - line);
3538 if (cur != NULL)
3539 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003540 next_search_hl(wp, shl, lnum, (colnr_T)v,
3541 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003542
3543 /* Need to get the line again, a multi-line regexp may have made it
3544 * invalid. */
3545 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3546 ptr = line + v;
3547
3548 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003550 if (shl->lnum == lnum)
3551 shl->startcol = shl->rm.startpos[0].col;
3552 else
3553 shl->startcol = 0;
3554 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3555 - shl->rm.startpos[0].lnum)
3556 shl->endcol = shl->rm.endpos[0].col;
3557 else
3558 shl->endcol = MAXCOL;
3559 /* Highlight one character for an empty match. */
3560 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003563 if (has_mbyte && line[shl->endcol] != NUL)
3564 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3565 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003567 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003569 if ((long)shl->startcol < v) /* match at leftcol */
3570 {
3571 shl->attr_cur = shl->attr;
3572 search_attr = shl->attr;
3573 }
3574 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003576 if (shl != &search_hl && cur != NULL)
3577 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579#endif
3580
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003581#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003582 /* Cursor line highlighting for 'cursorline' in the current window. Not
3583 * when Visual mode is active, because it's not clear what is selected
3584 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003585 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003586 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003587 {
3588 line_attr = hl_attr(HLF_CUL);
3589 area_highlighting = TRUE;
3590 }
3591#endif
3592
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003593 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 col = 0;
3595#ifdef FEAT_RIGHTLEFT
3596 if (wp->w_p_rl)
3597 {
3598 /* Rightleft window: process the text in the normal direction, but put
3599 * it in current_ScreenLine[] from right to left. Start at the
3600 * rightmost column of the window. */
3601 col = W_WIDTH(wp) - 1;
3602 off += col;
3603 }
3604#endif
3605
3606 /*
3607 * Repeat for the whole displayed line.
3608 */
3609 for (;;)
3610 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003611#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003612 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 /* Skip this quickly when working on the text. */
3615 if (draw_state != WL_LINE)
3616 {
3617#ifdef FEAT_CMDWIN
3618 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3619 {
3620 draw_state = WL_CMDLINE;
3621 if (cmdwin_type != 0 && wp == curwin)
3622 {
3623 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003625 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 char_attr = hl_attr(HLF_AT);
3627 }
3628 }
3629#endif
3630
3631#ifdef FEAT_FOLDING
3632 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3633 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003634 int fdc = compute_foldcolumn(wp, 0);
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003637 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003639 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003640 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003641 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003642 p_extra_free = alloc(12 + 1);
3643
3644 if (p_extra_free != NULL)
3645 {
3646 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3647 n_extra = fdc;
3648 p_extra_free[n_extra] = NUL;
3649 p_extra = p_extra_free;
3650 c_extra = NUL;
3651 char_attr = hl_attr(HLF_FC);
3652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
3654 }
3655#endif
3656
3657#ifdef FEAT_SIGNS
3658 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3659 {
3660 draw_state = WL_SIGN;
3661 /* Show the sign column when there are any signs in this
3662 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003663 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003665 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003667 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668# endif
3669
3670 /* Draw two cells with the sign value or blank. */
3671 c_extra = ' ';
3672 char_attr = hl_attr(HLF_SC);
3673 n_extra = 2;
3674
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003675 if (row == startrow
3676#ifdef FEAT_DIFF
3677 + filler_lines && filler_todo <= 0
3678#endif
3679 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
3681 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3682 SIGN_TEXT);
3683# ifdef FEAT_SIGN_ICONS
3684 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3685 SIGN_ICON);
3686 if (gui.in_use && icon_sign != 0)
3687 {
3688 /* Use the image in this position. */
3689 c_extra = SIGN_BYTE;
3690# ifdef FEAT_NETBEANS_INTG
3691 if (buf_signcount(wp->w_buffer, lnum) > 1)
3692 c_extra = MULTISIGN_BYTE;
3693# endif
3694 char_attr = icon_sign;
3695 }
3696 else
3697# endif
3698 if (text_sign != 0)
3699 {
3700 p_extra = sign_get_text(text_sign);
3701 if (p_extra != NULL)
3702 {
3703 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003704 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
3706 char_attr = sign_get_attr(text_sign, FALSE);
3707 }
3708 }
3709 }
3710 }
3711#endif
3712
3713 if (draw_state == WL_NR - 1 && n_extra == 0)
3714 {
3715 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003716 /* Display the absolute or relative line number. After the
3717 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3718 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 && (row == startrow
3720#ifdef FEAT_DIFF
3721 + filler_lines
3722#endif
3723 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3724 {
3725 /* Draw the line number (empty space after wrapping). */
3726 if (row == startrow
3727#ifdef FEAT_DIFF
3728 + filler_lines
3729#endif
3730 )
3731 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003732 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003733 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003734
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003735 if (wp->w_p_nu && !wp->w_p_rnu)
3736 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003737 num = (long)lnum;
3738 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003739 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003740 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003741 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003742 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003743 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003744 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003745 num = lnum;
3746 fmt = "%-*ld ";
3747 }
3748 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003749
Bram Moolenaar700e7342013-01-30 12:31:36 +01003750 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003751 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 if (wp->w_skipcol > 0)
3753 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3754 *p_extra = '-';
3755#ifdef FEAT_RIGHTLEFT
3756 if (wp->w_p_rl) /* reverse line numbers */
3757 rl_mirror(extra);
3758#endif
3759 p_extra = extra;
3760 c_extra = NUL;
3761 }
3762 else
3763 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003764 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003766#ifdef FEAT_SYN_HL
3767 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003768 * the current line differently.
3769 * TODO: Can we use CursorLine instead of CursorLineNr
3770 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003771 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003772 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003773 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
3776 }
3777
Bram Moolenaar597a4222014-06-25 14:39:50 +02003778#ifdef FEAT_LINEBREAK
3779 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3780 && n_extra == 0 && *p_sbr != NUL)
3781 /* draw indent after showbreak value */
3782 draw_state = WL_BRI;
3783 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3784 /* After the showbreak, draw the breakindent */
3785 draw_state = WL_BRI - 1;
3786
3787 /* draw 'breakindent': indent wrapped text accordingly */
3788 if (draw_state == WL_BRI - 1 && n_extra == 0)
3789 {
3790 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003791 /* if need_showbreak is set, breakindent also applies */
3792 if (wp->w_p_bri && n_extra == 0
3793 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003794# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003795 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003796# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003797 )
3798 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003799 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003800# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003801 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003802 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003803 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003804# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003805 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3806 char_attr = hl_combine_attr(char_attr,
3807 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003808# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003809 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003810# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003811 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003812 c_extra = ' ';
3813 n_extra = get_breakindent_win(wp,
3814 ml_get_buf(wp->w_buffer, lnum, FALSE));
3815 /* Correct end of highlighted area for 'breakindent',
3816 * required when 'linebreak' is also set. */
3817 if (tocol == vcol)
3818 tocol += n_extra;
3819 }
3820 }
3821#endif
3822
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3824 if (draw_state == WL_SBR - 1 && n_extra == 0)
3825 {
3826 draw_state = WL_SBR;
3827# ifdef FEAT_DIFF
3828 if (filler_todo > 0)
3829 {
3830 /* Draw "deleted" diff line(s). */
3831 if (char2cells(fill_diff) > 1)
3832 c_extra = '-';
3833 else
3834 c_extra = fill_diff;
3835# ifdef FEAT_RIGHTLEFT
3836 if (wp->w_p_rl)
3837 n_extra = col + 1;
3838 else
3839# endif
3840 n_extra = W_WIDTH(wp) - col;
3841 char_attr = hl_attr(HLF_DED);
3842 }
3843# endif
3844# ifdef FEAT_LINEBREAK
3845 if (*p_sbr != NUL && need_showbreak)
3846 {
3847 /* Draw 'showbreak' at the start of each broken line. */
3848 p_extra = p_sbr;
3849 c_extra = NUL;
3850 n_extra = (int)STRLEN(p_sbr);
3851 char_attr = hl_attr(HLF_AT);
3852 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003853 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 /* Correct end of highlighted area for 'showbreak',
3855 * required when 'linebreak' is also set. */
3856 if (tocol == vcol)
3857 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003858#ifdef FEAT_SYN_HL
3859 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003860 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003861 char_attr = hl_combine_attr(char_attr,
3862 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
3865# endif
3866 }
3867#endif
3868
3869 if (draw_state == WL_LINE - 1 && n_extra == 0)
3870 {
3871 draw_state = WL_LINE;
3872 if (saved_n_extra)
3873 {
3874 /* Continue item from end of wrapped line. */
3875 n_extra = saved_n_extra;
3876 c_extra = saved_c_extra;
3877 p_extra = saved_p_extra;
3878 char_attr = saved_char_attr;
3879 }
3880 else
3881 char_attr = 0;
3882 }
3883 }
3884
3885 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003886 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003887 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888#ifdef FEAT_DIFF
3889 && filler_todo <= 0
3890#endif
3891 )
3892 {
3893 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3894 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003895 /* Pretend we have finished updating the window. Except when
3896 * 'cursorcolumn' is set. */
3897#ifdef FEAT_SYN_HL
3898 if (wp->w_p_cuc)
3899 row = wp->w_cline_row + wp->w_cline_height;
3900 else
3901#endif
3902 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 break;
3904 }
3905
3906 if (draw_state == WL_LINE && area_highlighting)
3907 {
3908 /* handle Visual or match highlighting in this line */
3909 if (vcol == fromcol
3910#ifdef FEAT_MBYTE
3911 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3912 && (*mb_ptr2cells)(ptr) > 1)
3913#endif
3914 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003915 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 && vcol < tocol))
3917 area_attr = attr; /* start highlighting */
3918 else if (area_attr != 0
3919 && (vcol == tocol
3920 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923#ifdef FEAT_SEARCH_EXTRA
3924 if (!n_extra)
3925 {
3926 /*
3927 * Check for start/end of search pattern match.
3928 * After end, check for start/end of next match.
3929 * When another match, have to check for start again.
3930 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003931 * Do this for 'search_hl' and the match list (ordered by
3932 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003934 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003935 cur = wp->w_match_head;
3936 shl_flag = FALSE;
3937 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003939 if (shl_flag == FALSE
3940 && ((cur != NULL
3941 && cur->priority > SEARCH_HL_PRIORITY)
3942 || cur == NULL))
3943 {
3944 shl = &search_hl;
3945 shl_flag = TRUE;
3946 }
3947 else
3948 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003949 if (cur != NULL)
3950 cur->pos.cur = 0;
3951 pos_inprogress = TRUE;
3952 while (shl->rm.regprog != NULL
3953 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003955 if (shl->startcol != MAXCOL
3956 && v >= (long)shl->startcol
3957 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003959#ifdef FEAT_MBYTE
3960 int tmp_col = v + MB_PTR2LEN(ptr);
3961
3962 if (shl->endcol < tmp_col)
3963 shl->endcol = tmp_col;
3964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003966#ifdef FEAT_CONCEAL
3967 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3968 == cur->hlg_id)
3969 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02003970 has_match_conc =
3971 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003972 match_conc = cur->conceal_char;
3973 }
3974 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02003975 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003978 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 {
3980 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003981 next_search_hl(wp, shl, lnum, (colnr_T)v,
3982 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003983 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003984 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
3986 /* Need to get the line again, a multi-line regexp
3987 * may have made it invalid. */
3988 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3989 ptr = line + v;
3990
3991 if (shl->lnum == lnum)
3992 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003993 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003995 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003997 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003999 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
4001 /* highlight empty match, try again after
4002 * it */
4003#ifdef FEAT_MBYTE
4004 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004005 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004006 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 else
4008#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004009 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
4011
4012 /* Loop to check if the match starts at the
4013 * current position */
4014 continue;
4015 }
4016 }
4017 break;
4018 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004019 if (shl != &search_hl && cur != NULL)
4020 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004022
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004023 /* Use attributes from match with highest priority among
4024 * 'search_hl' and the match list. */
4025 search_attr = search_hl.attr_cur;
4026 cur = wp->w_match_head;
4027 shl_flag = FALSE;
4028 while (cur != NULL || shl_flag == FALSE)
4029 {
4030 if (shl_flag == FALSE
4031 && ((cur != NULL
4032 && cur->priority > SEARCH_HL_PRIORITY)
4033 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004034 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004035 shl = &search_hl;
4036 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004037 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004038 else
4039 shl = &cur->hl;
4040 if (shl->attr_cur != 0)
4041 search_attr = shl->attr_cur;
4042 if (shl != &search_hl && cur != NULL)
4043 cur = cur->next;
4044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046#endif
4047
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004049 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004051 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4052 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004054 if (diff_hlf == HLF_TXD && ptr - line > change_end
4055 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004057 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004058 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4059 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004062
4063 /* Decide which of the highlight attributes to use. */
4064 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004065#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004066 if (area_attr != 0)
4067 char_attr = hl_combine_attr(line_attr, area_attr);
4068 else if (search_attr != 0)
4069 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004070 /* Use line_attr when not in the Visual or 'incsearch' area
4071 * (area_attr may be 0 when "noinvcur" is set). */
4072 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004073 || vcol < fromcol || vcol_prev < fromcol_prev
4074 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004075 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004076#else
4077 if (area_attr != 0)
4078 char_attr = area_attr;
4079 else if (search_attr != 0)
4080 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004081#endif
4082 else
4083 {
4084 attr_pri = FALSE;
4085#ifdef FEAT_SYN_HL
4086 if (has_syntax)
4087 char_attr = syntax_attr;
4088 else
4089#endif
4090 char_attr = 0;
4091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093
4094 /*
4095 * Get the next character to put on the screen.
4096 */
4097 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004098 * The "p_extra" points to the extra stuff that is inserted to
4099 * represent special characters (non-printable stuff) and other
4100 * things. When all characters are the same, c_extra is used.
4101 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4102 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4104 */
4105 if (n_extra > 0)
4106 {
4107 if (c_extra != NUL)
4108 {
4109 c = c_extra;
4110#ifdef FEAT_MBYTE
4111 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4112 if (enc_utf8 && (*mb_char2len)(c) > 1)
4113 {
4114 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004115 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004116 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118 else
4119 mb_utf8 = FALSE;
4120#endif
4121 }
4122 else
4123 {
4124 c = *p_extra;
4125#ifdef FEAT_MBYTE
4126 if (has_mbyte)
4127 {
4128 mb_c = c;
4129 if (enc_utf8)
4130 {
4131 /* If the UTF-8 character is more than one byte:
4132 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004133 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 mb_utf8 = FALSE;
4135 if (mb_l > n_extra)
4136 mb_l = 1;
4137 else if (mb_l > 1)
4138 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004139 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004141 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 }
4144 else
4145 {
4146 /* if this is a DBCS character, put it in "mb_c" */
4147 mb_l = MB_BYTE2LEN(c);
4148 if (mb_l >= n_extra)
4149 mb_l = 1;
4150 else if (mb_l > 1)
4151 mb_c = (c << 8) + p_extra[1];
4152 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004153 if (mb_l == 0) /* at the NUL at end-of-line */
4154 mb_l = 1;
4155
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 /* If a double-width char doesn't fit display a '>' in the
4157 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004158 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159# ifdef FEAT_RIGHTLEFT
4160 wp->w_p_rl ? (col <= 0) :
4161# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004162 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 && (*mb_char2cells)(mb_c) == 2)
4164 {
4165 c = '>';
4166 mb_c = c;
4167 mb_l = 1;
4168 mb_utf8 = FALSE;
4169 multi_attr = hl_attr(HLF_AT);
4170 /* put the pointer back to output the double-width
4171 * character at the start of the next line. */
4172 ++n_extra;
4173 --p_extra;
4174 }
4175 else
4176 {
4177 n_extra -= mb_l - 1;
4178 p_extra += mb_l - 1;
4179 }
4180 }
4181#endif
4182 ++p_extra;
4183 }
4184 --n_extra;
4185 }
4186 else
4187 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004188 if (p_extra_free != NULL)
4189 {
4190 vim_free(p_extra_free);
4191 p_extra_free = NULL;
4192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 /*
4194 * Get a character from the line itself.
4195 */
4196 c = *ptr;
4197#ifdef FEAT_MBYTE
4198 if (has_mbyte)
4199 {
4200 mb_c = c;
4201 if (enc_utf8)
4202 {
4203 /* If the UTF-8 character is more than one byte: Decode it
4204 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004205 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 mb_utf8 = FALSE;
4207 if (mb_l > 1)
4208 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004209 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 /* Overlong encoded ASCII or ASCII with composing char
4211 * is displayed normally, except a NUL. */
4212 if (mb_c < 0x80)
4213 c = mb_c;
4214 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004215
4216 /* At start of the line we can have a composing char.
4217 * Draw it as a space with a composing char. */
4218 if (utf_iscomposing(mb_c))
4219 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004220 int i;
4221
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004222 for (i = Screen_mco - 1; i > 0; --i)
4223 u8cc[i] = u8cc[i - 1];
4224 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004225 mb_c = ' ';
4226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228
4229 if ((mb_l == 1 && c >= 0x80)
4230 || (mb_l >= 1 && mb_c == 0)
4231 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004232# ifdef UNICODE16
4233 || mb_c >= 0x10000
4234# endif
4235 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 {
4237 /*
4238 * Illegal UTF-8 byte: display as <xx>.
4239 * Non-BMP character : display as ? or fullwidth ?.
4240 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004241# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
4245 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004246# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 if (wp->w_p_rl) /* reverse */
4248 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004251# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 else if (utf_char2cells(mb_c) != 2)
4253 STRCPY(extra, "?");
4254 else
4255 /* 0xff1f in UTF-8: full-width '?' */
4256 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 p_extra = extra;
4260 c = *p_extra;
4261 mb_c = mb_ptr2char_adv(&p_extra);
4262 mb_utf8 = (c >= 0x80);
4263 n_extra = (int)STRLEN(p_extra);
4264 c_extra = NUL;
4265 if (area_attr == 0 && search_attr == 0)
4266 {
4267 n_attr = n_extra + 1;
4268 extra_attr = hl_attr(HLF_8);
4269 saved_attr2 = char_attr; /* save current attr */
4270 }
4271 }
4272 else if (mb_l == 0) /* at the NUL at end-of-line */
4273 mb_l = 1;
4274#ifdef FEAT_ARABIC
4275 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4276 {
4277 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004278 int pc, pc1, nc;
4279 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
4281 /* The idea of what is the previous and next
4282 * character depends on 'rightleft'. */
4283 if (wp->w_p_rl)
4284 {
4285 pc = prev_c;
4286 pc1 = prev_c1;
4287 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004288 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 else
4291 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004292 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004294 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 prev_c = mb_c;
4297
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004298 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300 else
4301 prev_c = mb_c;
4302#endif
4303 }
4304 else /* enc_dbcs */
4305 {
4306 mb_l = MB_BYTE2LEN(c);
4307 if (mb_l == 0) /* at the NUL at end-of-line */
4308 mb_l = 1;
4309 else if (mb_l > 1)
4310 {
4311 /* We assume a second byte below 32 is illegal.
4312 * Hopefully this is OK for all double-byte encodings!
4313 */
4314 if (ptr[1] >= 32)
4315 mb_c = (c << 8) + ptr[1];
4316 else
4317 {
4318 if (ptr[1] == NUL)
4319 {
4320 /* head byte at end of line */
4321 mb_l = 1;
4322 transchar_nonprint(extra, c);
4323 }
4324 else
4325 {
4326 /* illegal tail byte */
4327 mb_l = 2;
4328 STRCPY(extra, "XX");
4329 }
4330 p_extra = extra;
4331 n_extra = (int)STRLEN(extra) - 1;
4332 c_extra = NUL;
4333 c = *p_extra++;
4334 if (area_attr == 0 && search_attr == 0)
4335 {
4336 n_attr = n_extra + 1;
4337 extra_attr = hl_attr(HLF_8);
4338 saved_attr2 = char_attr; /* save current attr */
4339 }
4340 mb_c = c;
4341 }
4342 }
4343 }
4344 /* If a double-width char doesn't fit display a '>' in the
4345 * last column; the character is displayed at the start of the
4346 * next line. */
4347 if ((
4348# ifdef FEAT_RIGHTLEFT
4349 wp->w_p_rl ? (col <= 0) :
4350# endif
4351 (col >= W_WIDTH(wp) - 1))
4352 && (*mb_char2cells)(mb_c) == 2)
4353 {
4354 c = '>';
4355 mb_c = c;
4356 mb_utf8 = FALSE;
4357 mb_l = 1;
4358 multi_attr = hl_attr(HLF_AT);
4359 /* Put pointer back so that the character will be
4360 * displayed at the start of the next line. */
4361 --ptr;
4362 }
4363 else if (*ptr != NUL)
4364 ptr += mb_l - 1;
4365
4366 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004367 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004368 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004369 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004372 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 c = ' ';
4374 if (area_attr == 0 && search_attr == 0)
4375 {
4376 n_attr = n_extra + 1;
4377 extra_attr = hl_attr(HLF_AT);
4378 saved_attr2 = char_attr; /* save current attr */
4379 }
4380 mb_c = c;
4381 mb_utf8 = FALSE;
4382 mb_l = 1;
4383 }
4384
4385 }
4386#endif
4387 ++ptr;
4388
4389 if (extra_check)
4390 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004391#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004392 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004393#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004394
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004395#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 /* Get syntax attribute, unless still at the start of the line
4397 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004398 v = (long)(ptr - line);
4399 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 {
4401 /* Get the syntax attribute for the character. If there
4402 * is an error, disable syntax highlighting. */
4403 save_did_emsg = did_emsg;
4404 did_emsg = FALSE;
4405
Bram Moolenaar217ad922005-03-20 22:37:15 +00004406 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004407# ifdef FEAT_SPELL
4408 has_spell ? &can_spell :
4409# endif
4410 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
4412 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004413 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004414 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004415 has_syntax = FALSE;
4416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 else
4418 did_emsg = save_did_emsg;
4419
4420 /* Need to get the line again, a multi-line regexp may
4421 * have made it invalid. */
4422 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4423 ptr = line + v;
4424
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004425 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004427 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004428 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004429# ifdef FEAT_CONCEAL
4430 /* no concealing past the end of the line, it interferes
4431 * with line highlighting */
4432 if (c == NUL)
4433 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004434 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004435 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004438#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004439
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004440#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004441 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004442 * Only do this when there is no syntax highlighting, the
4443 * @Spell cluster is not used or the current syntax item
4444 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004445 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004446 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004447 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004448# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004449 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004450 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004451# endif
4452 if (c != 0 && (
4453# ifdef FEAT_SYN_HL
4454 !has_syntax ||
4455# endif
4456 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004457 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004458 char_u *prev_ptr, *p;
4459 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004460 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004461# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004462 if (has_mbyte)
4463 {
4464 prev_ptr = ptr - mb_l;
4465 v -= mb_l - 1;
4466 }
4467 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004468# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004469 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004470
4471 /* Use nextline[] if possible, it has the start of the
4472 * next line concatenated. */
4473 if ((prev_ptr - line) - nextlinecol >= 0)
4474 p = nextline + (prev_ptr - line) - nextlinecol;
4475 else
4476 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004477 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004478 len = spell_check(wp, p, &spell_hlf, &cap_col,
4479 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004480 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004481
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004482 /* In Insert mode only highlight a word that
4483 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004484 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004485 && (State & INSERT) != 0
4486 && wp->w_cursor.lnum == lnum
4487 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004488 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004489 && wp->w_cursor.col < (colnr_T)word_end)
4490 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004491 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004492 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004493 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004494
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004495 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004496 && (p - nextline) + len > nextline_idx)
4497 {
4498 /* Remember that the good word continues at the
4499 * start of the next line. */
4500 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004501 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004502 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004503
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004504 /* Turn index into actual attributes. */
4505 if (spell_hlf != HLF_COUNT)
4506 spell_attr = highlight_attr[spell_hlf];
4507
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004508 if (cap_col > 0)
4509 {
4510 if (p != prev_ptr
4511 && (p - nextline) + cap_col >= nextline_idx)
4512 {
4513 /* Remember that the word in the next line
4514 * must start with a capital. */
4515 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004516 cap_col = (int)((p - nextline) + cap_col
4517 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004518 }
4519 else
4520 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004521 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004522 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004523 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004524 }
4525 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004526 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004527 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004528 char_attr = hl_combine_attr(char_attr, spell_attr);
4529 else
4530 char_attr = hl_combine_attr(spell_attr, char_attr);
4531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532#endif
4533#ifdef FEAT_LINEBREAK
4534 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004535 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004537 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004539# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004540 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004541# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004542 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004544 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004546 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004547
Bram Moolenaar597a4222014-06-25 14:39:50 +02004548 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004549 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004550 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004551 if (c == TAB && n_extra + col > W_WIDTH(wp))
4552 n_extra = (int)wp->w_buffer->b_p_ts
4553 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4554
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004555# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004556 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004557# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004561 {
4562#ifdef FEAT_CONCEAL
4563 if (c == TAB)
4564 /* See "Tab alignment" below. */
4565 FIX_FOR_BOGUSCOLS;
4566#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004567 if (!wp->w_p_list)
4568 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 }
4571#endif
4572
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004573 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4574 */
4575 if (wp->w_p_list
4576 && (((c == 160
4577#ifdef FEAT_MBYTE
4578 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4579#endif
4580 ) && lcs_nbsp)
4581 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4582 {
4583 c = (c == ' ') ? lcs_space : lcs_nbsp;
4584 if (area_attr == 0 && search_attr == 0)
4585 {
4586 n_attr = 1;
4587 extra_attr = hl_attr(HLF_8);
4588 saved_attr2 = char_attr; /* save current attr */
4589 }
4590#ifdef FEAT_MBYTE
4591 mb_c = c;
4592 if (enc_utf8 && (*mb_char2len)(c) > 1)
4593 {
4594 mb_utf8 = TRUE;
4595 u8cc[0] = 0;
4596 c = 0xc0;
4597 }
4598 else
4599 mb_utf8 = FALSE;
4600#endif
4601 }
4602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4604 {
4605 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004606 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608 n_attr = 1;
4609 extra_attr = hl_attr(HLF_8);
4610 saved_attr2 = char_attr; /* save current attr */
4611 }
4612#ifdef FEAT_MBYTE
4613 mb_c = c;
4614 if (enc_utf8 && (*mb_char2len)(c) > 1)
4615 {
4616 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004617 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004618 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 }
4620 else
4621 mb_utf8 = FALSE;
4622#endif
4623 }
4624 }
4625
4626 /*
4627 * Handling of non-printable characters.
4628 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004629 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
4631 /*
4632 * when getting a character from the file, we may have to
4633 * turn it into something else on the way to putting it
4634 * into "ScreenLines".
4635 */
4636 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4637 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004638 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004639 long vcol_adjusted = vcol; /* removed showbreak length */
4640#ifdef FEAT_LINEBREAK
4641 /* only adjust the tab_len, when at the first column
4642 * after the showbreak value was drawn */
4643 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4644 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004647 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004648 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4649
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004650#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004651 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004652#endif
4653 /* tab amount depends on current column */
4654 n_extra = tab_len;
4655#ifdef FEAT_LINEBREAK
4656 else
4657 {
4658 char_u *p;
4659 int len = n_extra;
4660 int i;
4661 int saved_nextra = n_extra;
4662
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004663#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004664 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004665 /* there are characters to conceal */
4666 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004667 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4668 */
4669 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4670 && n_extra > tab_len)
4671 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004672#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004673
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004674 /* if n_extra > 0, it gives the number of chars, to
4675 * use for a tab, else we need to calculate the width
4676 * for a tab */
4677#ifdef FEAT_MBYTE
4678 len = (tab_len * mb_char2len(lcs_tab2));
4679 if (n_extra > 0)
4680 len += n_extra - tab_len;
4681#endif
4682 c = lcs_tab1;
4683 p = alloc((unsigned)(len + 1));
4684 vim_memset(p, ' ', len);
4685 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004686 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004687 p_extra_free = p;
4688 for (i = 0; i < tab_len; i++)
4689 {
4690#ifdef FEAT_MBYTE
4691 mb_char2bytes(lcs_tab2, p);
4692 p += mb_char2len(lcs_tab2);
4693 n_extra += mb_char2len(lcs_tab2)
4694 - (saved_nextra > 0 ? 1 : 0);
4695#else
4696 p[i] = lcs_tab2;
4697#endif
4698 }
4699 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004700#ifdef FEAT_CONCEAL
4701 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4702 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004703 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004704 n_extra -= vcol_off;
4705#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004706 }
4707#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004708#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004709 {
4710 int vc_saved = vcol_off;
4711
4712 /* Tab alignment should be identical regardless of
4713 * 'conceallevel' value. So tab compensates of all
4714 * previous concealed characters, and thus resets
4715 * vcol_off and boguscols accumulated so far in the
4716 * line. Note that the tab can be longer than
4717 * 'tabstop' when there are concealed characters. */
4718 FIX_FOR_BOGUSCOLS;
4719
4720 /* Make sure, the highlighting for the tab char will be
4721 * correctly set further below (effectively reverts the
4722 * FIX_FOR_BOGSUCOLS macro */
4723 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004724 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004725 tab_len += vc_saved;
4726 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728#ifdef FEAT_MBYTE
4729 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4730#endif
4731 if (wp->w_p_list)
4732 {
4733 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004734#ifdef FEAT_LINEBREAK
4735 if (wp->w_p_lbr)
4736 c_extra = NUL; /* using p_extra from above */
4737 else
4738#endif
4739 c_extra = lcs_tab2;
4740 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 extra_attr = hl_attr(HLF_8);
4742 saved_attr2 = char_attr; /* save current attr */
4743#ifdef FEAT_MBYTE
4744 mb_c = c;
4745 if (enc_utf8 && (*mb_char2len)(c) > 1)
4746 {
4747 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004748 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004749 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
4751#endif
4752 }
4753 else
4754 {
4755 c_extra = ' ';
4756 c = ' ';
4757 }
4758 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004759 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004760 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004761 || ((fromcol >= 0 || fromcol_prev >= 0)
4762 && tocol > vcol
4763 && VIsual_mode != Ctrl_V
4764 && (
4765# ifdef FEAT_RIGHTLEFT
4766 wp->w_p_rl ? (col >= 0) :
4767# endif
4768 (col < W_WIDTH(wp)))
4769 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004770 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004771 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004772 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004774 /* Display a '$' after the line or highlight an extra
4775 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4777 /* For a diff line the highlighting continues after the
4778 * "$". */
4779 if (
4780# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004781 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782# ifdef LINE_ATTR
4783 &&
4784# endif
4785# endif
4786# ifdef LINE_ATTR
4787 line_attr == 0
4788# endif
4789 )
4790#endif
4791 {
4792#ifdef FEAT_VIRTUALEDIT
4793 /* In virtualedit, visual selections may extend
4794 * beyond end of line. */
4795 if (area_highlighting && virtual_active()
4796 && tocol != MAXCOL && vcol < tocol)
4797 n_extra = 0;
4798 else
4799#endif
4800 {
4801 p_extra = at_end_str;
4802 n_extra = 1;
4803 c_extra = NUL;
4804 }
4805 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004806 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004807 c = lcs_eol;
4808 else
4809 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 lcs_eol_one = -1;
4811 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004812 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
4814 extra_attr = hl_attr(HLF_AT);
4815 n_attr = 1;
4816 }
4817#ifdef FEAT_MBYTE
4818 mb_c = c;
4819 if (enc_utf8 && (*mb_char2len)(c) > 1)
4820 {
4821 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004822 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004823 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
4825 else
4826 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4827#endif
4828 }
4829 else if (c != NUL)
4830 {
4831 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004832 if (n_extra == 0)
4833 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834#ifdef FEAT_RIGHTLEFT
4835 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4836 rl_mirror(p_extra); /* reverse "<12>" */
4837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004839#ifdef FEAT_LINEBREAK
4840 if (wp->w_p_lbr)
4841 {
4842 char_u *p;
4843
4844 c = *p_extra;
4845 p = alloc((unsigned)n_extra + 1);
4846 vim_memset(p, ' ', n_extra);
4847 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4848 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004849 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004850 p_extra_free = p_extra = p;
4851 }
4852 else
4853#endif
4854 {
4855 n_extra = byte2cells(c) - 1;
4856 c = *p_extra++;
4857 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004858 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
4860 n_attr = n_extra + 1;
4861 extra_attr = hl_attr(HLF_8);
4862 saved_attr2 = char_attr; /* save current attr */
4863 }
4864#ifdef FEAT_MBYTE
4865 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4866#endif
4867 }
4868#ifdef FEAT_VIRTUALEDIT
4869 else if (VIsual_active
4870 && (VIsual_mode == Ctrl_V
4871 || VIsual_mode == 'v')
4872 && virtual_active()
4873 && tocol != MAXCOL
4874 && vcol < tocol
4875 && (
4876# ifdef FEAT_RIGHTLEFT
4877 wp->w_p_rl ? (col >= 0) :
4878# endif
4879 (col < W_WIDTH(wp))))
4880 {
4881 c = ' ';
4882 --ptr; /* put it back at the NUL */
4883 }
4884#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004885#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 else if ((
4887# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004888 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 ) && (
4892# ifdef FEAT_RIGHTLEFT
4893 wp->w_p_rl ? (col >= 0) :
4894# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004895 (col
4896# ifdef FEAT_CONCEAL
4897 - boguscols
4898# endif
4899 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
4901 /* Highlight until the right side of the window */
4902 c = ' ';
4903 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004904
4905 /* Remember we do the char for line highlighting. */
4906 ++did_line_attr;
4907
4908 /* don't do search HL for the rest of the line */
4909 if (line_attr != 0 && char_attr == search_attr && col > 0)
4910 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911# ifdef FEAT_DIFF
4912 if (diff_hlf == HLF_TXD)
4913 {
4914 diff_hlf = HLF_CHD;
4915 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004918 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4919 char_attr = hl_combine_attr(char_attr,
4920 hl_attr(HLF_CUL));
4921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923# endif
4924 }
4925#endif
4926 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004927
4928#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004929 if ( wp->w_p_cole > 0
4930 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004931 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02004932 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004933 && !(lnum_in_visual_area
4934 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004935 {
4936 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02004937 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004938 && (syn_get_sub_char() != NUL || match_conc
4939 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004940 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004941 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004942 /* First time at this concealed item: display one
4943 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004944 if (match_conc)
4945 c = match_conc;
4946 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004947 c = syn_get_sub_char();
4948 else if (lcs_conceal != NUL)
4949 c = lcs_conceal;
4950 else
4951 c = ' ';
4952
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004953 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004954
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004955 if (n_extra > 0)
4956 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004957 vcol += n_extra;
4958 if (wp->w_p_wrap && n_extra > 0)
4959 {
4960# ifdef FEAT_RIGHTLEFT
4961 if (wp->w_p_rl)
4962 {
4963 col -= n_extra;
4964 boguscols -= n_extra;
4965 }
4966 else
4967# endif
4968 {
4969 boguscols += n_extra;
4970 col += n_extra;
4971 }
4972 }
4973 n_extra = 0;
4974 n_attr = 0;
4975 }
4976 else if (n_skip == 0)
4977 {
4978 is_concealing = TRUE;
4979 n_skip = 1;
4980 }
4981# ifdef FEAT_MBYTE
4982 mb_c = c;
4983 if (enc_utf8 && (*mb_char2len)(c) > 1)
4984 {
4985 mb_utf8 = TRUE;
4986 u8cc[0] = 0;
4987 c = 0xc0;
4988 }
4989 else
4990 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4991# endif
4992 }
4993 else
4994 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004995 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004996 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004997 }
4998#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 }
5000
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005001#ifdef FEAT_CONCEAL
5002 /* In the cursor line and we may be concealing characters: correct
5003 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005004 if (!did_wcol && draw_state == WL_LINE
5005 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005006 && conceal_cursor_line(wp)
5007 && (int)wp->w_virtcol <= vcol + n_skip)
5008 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005009# ifdef FEAT_RIGHTLEFT
5010 if (wp->w_p_rl)
5011 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5012 else
5013# endif
5014 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005015 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005016 did_wcol = TRUE;
5017 }
5018#endif
5019
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 /* Don't override visual selection highlighting. */
5021 if (n_attr > 0
5022 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005023 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 char_attr = extra_attr;
5025
Bram Moolenaar81695252004-12-29 20:58:21 +00005026#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 /* XIM don't send preedit_start and preedit_end, but they send
5028 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5029 * im_is_preediting() here. */
5030 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005031 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 && (State & INSERT)
5033 && !p_imdisable
5034 && im_is_preediting()
5035 && draw_state == WL_LINE)
5036 {
5037 colnr_T tcol;
5038
5039 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005040 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 else
5042 tcol = preedit_end_col;
5043 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5044 {
5045 if (feedback_old_attr < 0)
5046 {
5047 feedback_col = 0;
5048 feedback_old_attr = char_attr;
5049 }
5050 char_attr = im_get_feedback_attr(feedback_col);
5051 if (char_attr < 0)
5052 char_attr = feedback_old_attr;
5053 feedback_col++;
5054 }
5055 else if (feedback_old_attr >= 0)
5056 {
5057 char_attr = feedback_old_attr;
5058 feedback_old_attr = -1;
5059 feedback_col = 0;
5060 }
5061 }
5062#endif
5063 /*
5064 * Handle the case where we are in column 0 but not on the first
5065 * character of the line and the user wants us to show us a
5066 * special character (via 'listchars' option "precedes:<char>".
5067 */
5068 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005069 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5071#ifdef FEAT_DIFF
5072 && filler_todo <= 0
5073#endif
5074 && draw_state > WL_NR
5075 && c != NUL)
5076 {
5077 c = lcs_prec;
5078 lcs_prec_todo = NUL;
5079#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005080 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5081 {
5082 /* Double-width character being overwritten by the "precedes"
5083 * character, need to fill up half the character. */
5084 c_extra = MB_FILLER_CHAR;
5085 n_extra = 1;
5086 n_attr = 2;
5087 extra_attr = hl_attr(HLF_AT);
5088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 mb_c = c;
5090 if (enc_utf8 && (*mb_char2len)(c) > 1)
5091 {
5092 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005093 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005094 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096 else
5097 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5098#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005099 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
5101 saved_attr3 = char_attr; /* save current attr */
5102 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5103 n_attr3 = 1;
5104 }
5105 }
5106
5107 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005108 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005110 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005111#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005112 || did_line_attr == 1
5113#endif
5114 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005116#ifdef FEAT_SEARCH_EXTRA
5117 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005118
5119 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005120 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005121 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005122#endif
5123
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005124 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 * highlight match at end of line. If it's beyond the last
5126 * char on the screen, just overwrite that one (tricky!) Not
5127 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005128#ifdef FEAT_SEARCH_EXTRA
5129 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005130 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005131 prevcol_hl_flag = TRUE;
5132 else
5133 {
5134 cur = wp->w_match_head;
5135 while (cur != NULL)
5136 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005137 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005138 {
5139 prevcol_hl_flag = TRUE;
5140 break;
5141 }
5142 cur = cur->next;
5143 }
5144 }
5145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005147 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005148 && (VIsual_mode != Ctrl_V
5149 || lnum == VIsual.lnum
5150 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005151 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152#ifdef FEAT_SEARCH_EXTRA
5153 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005154 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005155# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005156 && did_line_attr <= 1
5157# endif
5158 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159#endif
5160 ))
5161 {
5162 int n = 0;
5163
5164#ifdef FEAT_RIGHTLEFT
5165 if (wp->w_p_rl)
5166 {
5167 if (col < 0)
5168 n = 1;
5169 }
5170 else
5171#endif
5172 {
5173 if (col >= W_WIDTH(wp))
5174 n = -1;
5175 }
5176 if (n != 0)
5177 {
5178 /* At the window boundary, highlight the last character
5179 * instead (better than nothing). */
5180 off += n;
5181 col += n;
5182 }
5183 else
5184 {
5185 /* Add a blank character to highlight. */
5186 ScreenLines[off] = ' ';
5187#ifdef FEAT_MBYTE
5188 if (enc_utf8)
5189 ScreenLinesUC[off] = 0;
5190#endif
5191 }
5192#ifdef FEAT_SEARCH_EXTRA
5193 if (area_attr == 0)
5194 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005195 /* Use attributes from match with highest priority among
5196 * 'search_hl' and the match list. */
5197 char_attr = search_hl.attr;
5198 cur = wp->w_match_head;
5199 shl_flag = FALSE;
5200 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005201 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005202 if (shl_flag == FALSE
5203 && ((cur != NULL
5204 && cur->priority > SEARCH_HL_PRIORITY)
5205 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005206 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005207 shl = &search_hl;
5208 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005209 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005210 else
5211 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005212 if ((ptr - line) - 1 == (long)shl->startcol
5213 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005214 char_attr = shl->attr;
5215 if (shl != &search_hl && cur != NULL)
5216 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219#endif
5220 ScreenAttrs[off] = char_attr;
5221#ifdef FEAT_RIGHTLEFT
5222 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005225 --off;
5226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 else
5228#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005231 ++off;
5232 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005233 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005234#ifdef FEAT_SYN_HL
5235 eol_hl_off = 1;
5236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
Bram Moolenaar91170f82006-05-05 21:15:17 +00005240 /*
5241 * At end of the text line.
5242 */
5243 if (c == NUL)
5244 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005245#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005246 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5247 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005248 {
5249 /* highlight last char after line */
5250 --col;
5251 --off;
5252 --vcol;
5253 }
5254
Bram Moolenaar1a384422010-07-14 19:53:30 +02005255 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005256 if (wp->w_p_wrap)
5257 v = wp->w_skipcol;
5258 else
5259 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005260
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005261 /* check if line ends before left margin */
5262 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005263 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005264#ifdef FEAT_CONCEAL
5265 /* Get rid of the boguscols now, we want to draw until the right
5266 * edge for 'cursorcolumn'. */
5267 col -= boguscols;
5268 boguscols = 0;
5269#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005270
5271 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005272 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005273
5274 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005275 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5276 && (int)wp->w_virtcol <
5277 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005278 && lnum != wp->w_cursor.lnum)
5279 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005280# ifdef FEAT_RIGHTLEFT
5281 && !wp->w_p_rl
5282# endif
5283 )
5284 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005285 int rightmost_vcol = 0;
5286 int i;
5287
5288 if (wp->w_p_cuc)
5289 rightmost_vcol = wp->w_virtcol;
5290 if (draw_color_col)
5291 /* determine rightmost colorcolumn to possibly draw */
5292 for (i = 0; color_cols[i] >= 0; ++i)
5293 if (rightmost_vcol < color_cols[i])
5294 rightmost_vcol = color_cols[i];
5295
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005296 while (col < W_WIDTH(wp))
5297 {
5298 ScreenLines[off] = ' ';
5299#ifdef FEAT_MBYTE
5300 if (enc_utf8)
5301 ScreenLinesUC[off] = 0;
5302#endif
5303 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005304 if (draw_color_col)
5305 draw_color_col = advance_color_col(VCOL_HLC,
5306 &color_cols);
5307
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005308 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005309 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005310 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005311 ScreenAttrs[off++] = hl_attr(HLF_MC);
5312 else
5313 ScreenAttrs[off++] = 0;
5314
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005315 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005316 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005317
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005318 ++vcol;
5319 }
5320 }
5321#endif
5322
Bram Moolenaar860cae12010-06-05 23:22:07 +02005323 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5324 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 row++;
5326
5327 /*
5328 * Update w_cline_height and w_cline_folded if the cursor line was
5329 * updated (saves a call to plines() later).
5330 */
5331 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5332 {
5333 curwin->w_cline_row = startrow;
5334 curwin->w_cline_height = row - startrow;
5335#ifdef FEAT_FOLDING
5336 curwin->w_cline_folded = FALSE;
5337#endif
5338 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5339 }
5340
5341 break;
5342 }
5343
5344 /* line continues beyond line end */
5345 if (lcs_ext
5346 && !wp->w_p_wrap
5347#ifdef FEAT_DIFF
5348 && filler_todo <= 0
5349#endif
5350 && (
5351#ifdef FEAT_RIGHTLEFT
5352 wp->w_p_rl ? col == 0 :
5353#endif
5354 col == W_WIDTH(wp) - 1)
5355 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005356 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5358 {
5359 c = lcs_ext;
5360 char_attr = hl_attr(HLF_AT);
5361#ifdef FEAT_MBYTE
5362 mb_c = c;
5363 if (enc_utf8 && (*mb_char2len)(c) > 1)
5364 {
5365 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005366 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005367 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369 else
5370 mb_utf8 = FALSE;
5371#endif
5372 }
5373
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005374#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005375 /* advance to the next 'colorcolumn' */
5376 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005377 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005378
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005379 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005380 * highlight the cursor position itself.
5381 * Also highlight the 'colorcolumn' if it is different than
5382 * 'cursorcolumn' */
5383 vcol_save_attr = -1;
5384 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005385 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005386 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005387 && lnum != wp->w_cursor.lnum)
5388 {
5389 vcol_save_attr = char_attr;
5390 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5391 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005392 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005393 {
5394 vcol_save_attr = char_attr;
5395 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5396 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005397 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005398#endif
5399
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 /*
5401 * Store character to be displayed.
5402 * Skip characters that are left of the screen for 'nowrap'.
5403 */
5404 vcol_prev = vcol;
5405 if (draw_state < WL_LINE || n_skip <= 0)
5406 {
5407 /*
5408 * Store the character.
5409 */
5410#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5411 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5412 {
5413 /* A double-wide character is: put first halve in left cell. */
5414 --off;
5415 --col;
5416 }
5417#endif
5418 ScreenLines[off] = c;
5419#ifdef FEAT_MBYTE
5420 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005421 {
5422 if ((mb_c & 0xff00) == 0x8e00)
5423 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 else if (enc_utf8)
5427 {
5428 if (mb_utf8)
5429 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005430 int i;
5431
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005433 if ((c & 0xff) == 0)
5434 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005435 for (i = 0; i < Screen_mco; ++i)
5436 {
5437 ScreenLinesC[i][off] = u8cc[i];
5438 if (u8cc[i] == 0)
5439 break;
5440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 }
5442 else
5443 ScreenLinesUC[off] = 0;
5444 }
5445 if (multi_attr)
5446 {
5447 ScreenAttrs[off] = multi_attr;
5448 multi_attr = 0;
5449 }
5450 else
5451#endif
5452 ScreenAttrs[off] = char_attr;
5453
5454#ifdef FEAT_MBYTE
5455 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5456 {
5457 /* Need to fill two screen columns. */
5458 ++off;
5459 ++col;
5460 if (enc_utf8)
5461 /* UTF-8: Put a 0 in the second screen char. */
5462 ScreenLines[off] = 0;
5463 else
5464 /* DBCS: Put second byte in the second screen char. */
5465 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005466 if (draw_state > WL_NR
5467#ifdef FEAT_DIFF
5468 && filler_todo <= 0
5469#endif
5470 )
5471 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 /* When "tocol" is halfway a character, set it to the end of
5473 * the character, otherwise highlighting won't stop. */
5474 if (tocol == vcol)
5475 ++tocol;
5476#ifdef FEAT_RIGHTLEFT
5477 if (wp->w_p_rl)
5478 {
5479 /* now it's time to backup one cell */
5480 --off;
5481 --col;
5482 }
5483#endif
5484 }
5485#endif
5486#ifdef FEAT_RIGHTLEFT
5487 if (wp->w_p_rl)
5488 {
5489 --off;
5490 --col;
5491 }
5492 else
5493#endif
5494 {
5495 ++off;
5496 ++col;
5497 }
5498 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005499#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005500 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005501 {
5502 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005503 ++vcol_off;
5504 if (n_extra > 0)
5505 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005506 if (wp->w_p_wrap)
5507 {
5508 /*
5509 * Special voodoo required if 'wrap' is on.
5510 *
5511 * Advance the column indicator to force the line
5512 * drawing to wrap early. This will make the line
5513 * take up the same screen space when parts are concealed,
5514 * so that cursor line computations aren't messed up.
5515 *
5516 * To avoid the fictitious advance of 'col' causing
5517 * trailing junk to be written out of the screen line
5518 * we are building, 'boguscols' keeps track of the number
5519 * of bad columns we have advanced.
5520 */
5521 if (n_extra > 0)
5522 {
5523 vcol += n_extra;
5524# ifdef FEAT_RIGHTLEFT
5525 if (wp->w_p_rl)
5526 {
5527 col -= n_extra;
5528 boguscols -= n_extra;
5529 }
5530 else
5531# endif
5532 {
5533 col += n_extra;
5534 boguscols += n_extra;
5535 }
5536 n_extra = 0;
5537 n_attr = 0;
5538 }
5539
5540
5541# ifdef FEAT_MBYTE
5542 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5543 {
5544 /* Need to fill two screen columns. */
5545# ifdef FEAT_RIGHTLEFT
5546 if (wp->w_p_rl)
5547 {
5548 --boguscols;
5549 --col;
5550 }
5551 else
5552# endif
5553 {
5554 ++boguscols;
5555 ++col;
5556 }
5557 }
5558# endif
5559
5560# ifdef FEAT_RIGHTLEFT
5561 if (wp->w_p_rl)
5562 {
5563 --boguscols;
5564 --col;
5565 }
5566 else
5567# endif
5568 {
5569 ++boguscols;
5570 ++col;
5571 }
5572 }
5573 else
5574 {
5575 if (n_extra > 0)
5576 {
5577 vcol += n_extra;
5578 n_extra = 0;
5579 n_attr = 0;
5580 }
5581 }
5582
5583 }
5584#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 else
5586 --n_skip;
5587
Bram Moolenaar64486672010-05-16 15:46:46 +02005588 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5589 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005590 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591#ifdef FEAT_DIFF
5592 && filler_todo <= 0
5593#endif
5594 )
5595 ++vcol;
5596
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005597#ifdef FEAT_SYN_HL
5598 if (vcol_save_attr >= 0)
5599 char_attr = vcol_save_attr;
5600#endif
5601
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 /* restore attributes after "predeces" in 'listchars' */
5603 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5604 char_attr = saved_attr3;
5605
5606 /* restore attributes after last 'listchars' or 'number' char */
5607 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5608 char_attr = saved_attr2;
5609
5610 /*
5611 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005612 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 */
5614 if ((
5615#ifdef FEAT_RIGHTLEFT
5616 wp->w_p_rl ? (col < 0) :
5617#endif
5618 (col >= W_WIDTH(wp)))
5619 && (*ptr != NUL
5620#ifdef FEAT_DIFF
5621 || filler_todo > 0
5622#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005623 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5625 )
5626 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005627#ifdef FEAT_CONCEAL
5628 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5629 (int)W_WIDTH(wp), wp->w_p_rl);
5630 boguscols = 0;
5631#else
5632 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5633 (int)W_WIDTH(wp), wp->w_p_rl);
5634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 ++row;
5636 ++screen_row;
5637
5638 /* When not wrapping and finished diff lines, or when displayed
5639 * '$' and highlighting until last column, break here. */
5640 if ((!wp->w_p_wrap
5641#ifdef FEAT_DIFF
5642 && filler_todo <= 0
5643#endif
5644 ) || lcs_eol_one == -1)
5645 break;
5646
5647 /* When the window is too narrow draw all "@" lines. */
5648 if (draw_state != WL_LINE
5649#ifdef FEAT_DIFF
5650 && filler_todo <= 0
5651#endif
5652 )
5653 {
5654 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005655#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 draw_vsep_win(wp, row);
5657#endif
5658 row = endrow;
5659 }
5660
5661 /* When line got too long for screen break here. */
5662 if (row == endrow)
5663 {
5664 ++row;
5665 break;
5666 }
5667
5668 if (screen_cur_row == screen_row - 1
5669#ifdef FEAT_DIFF
5670 && filler_todo <= 0
5671#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005672#ifdef FEAT_WINDOWS
5673 && W_WIDTH(wp) == Columns
5674#endif
5675 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
5677 /* Remember that the line wraps, used for modeless copy. */
5678 LineWraps[screen_row - 1] = TRUE;
5679
5680 /*
5681 * Special trick to make copy/paste of wrapped lines work with
5682 * xterm/screen: write an extra character beyond the end of
5683 * the line. This will work with all terminal types
5684 * (regardless of the xn,am settings).
5685 * Only do this on a fast tty.
5686 * Only do this if the cursor is on the current line
5687 * (something has been written in it).
5688 * Don't do this for the GUI.
5689 * Don't do this for double-width characters.
5690 * Don't do this for a window not at the right screen border.
5691 */
5692 if (p_tf
5693#ifdef FEAT_GUI
5694 && !gui.in_use
5695#endif
5696#ifdef FEAT_MBYTE
5697 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005698 && ((*mb_off2cells)(LineOffset[screen_row],
5699 LineOffset[screen_row] + screen_Columns)
5700 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005702 + (int)Columns - 2,
5703 LineOffset[screen_row] + screen_Columns)
5704 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705#endif
5706 )
5707 {
5708 /* First make sure we are at the end of the screen line,
5709 * then output the same character again to let the
5710 * terminal know about the wrap. If the terminal doesn't
5711 * auto-wrap, we overwrite the character. */
5712 if (screen_cur_col != W_WIDTH(wp))
5713 screen_char(LineOffset[screen_row - 1]
5714 + (unsigned)Columns - 1,
5715 screen_row - 1, (int)(Columns - 1));
5716
5717#ifdef FEAT_MBYTE
5718 /* When there is a multi-byte character, just output a
5719 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005720 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5721 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 out_char(' ');
5723 else
5724#endif
5725 out_char(ScreenLines[LineOffset[screen_row - 1]
5726 + (Columns - 1)]);
5727 /* force a redraw of the first char on the next line */
5728 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5729 screen_start(); /* don't know where cursor is now */
5730 }
5731 }
5732
5733 col = 0;
5734 off = (unsigned)(current_ScreenLine - ScreenLines);
5735#ifdef FEAT_RIGHTLEFT
5736 if (wp->w_p_rl)
5737 {
5738 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5739 off += col;
5740 }
5741#endif
5742
5743 /* reset the drawing state for the start of a wrapped line */
5744 draw_state = WL_START;
5745 saved_n_extra = n_extra;
5746 saved_p_extra = p_extra;
5747 saved_c_extra = c_extra;
5748 saved_char_attr = char_attr;
5749 n_extra = 0;
5750 lcs_prec_todo = lcs_prec;
5751#ifdef FEAT_LINEBREAK
5752# ifdef FEAT_DIFF
5753 if (filler_todo <= 0)
5754# endif
5755 need_showbreak = TRUE;
5756#endif
5757#ifdef FEAT_DIFF
5758 --filler_todo;
5759 /* When the filler lines are actually below the last line of the
5760 * file, don't draw the line itself, break here. */
5761 if (filler_todo == 0 && wp->w_botfill)
5762 break;
5763#endif
5764 }
5765
5766 } /* for every character in the line */
5767
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005768#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005769 /* After an empty line check first word for capital. */
5770 if (*skipwhite(line) == NUL)
5771 {
5772 capcol_lnum = lnum + 1;
5773 cap_col = 0;
5774 }
5775#endif
5776
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005777 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 return row;
5779}
5780
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005781#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005782static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005783
5784/*
5785 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005786 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005787 */
5788 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005789comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005790{
5791 int i;
5792
5793 for (i = 0; i < Screen_mco; ++i)
5794 {
5795 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5796 return TRUE;
5797 if (ScreenLinesC[i][off_from] == 0)
5798 break;
5799 }
5800 return FALSE;
5801}
5802#endif
5803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804/*
5805 * Check whether the given character needs redrawing:
5806 * - the (first byte of the) character is different
5807 * - the attributes are different
5808 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005809 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 */
5811 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005812char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813{
5814 if (cols > 0
5815 && ((ScreenLines[off_from] != ScreenLines[off_to]
5816 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5817
5818#ifdef FEAT_MBYTE
5819 || (enc_dbcs != 0
5820 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5821 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5822 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5823 : (cols > 1 && ScreenLines[off_from + 1]
5824 != ScreenLines[off_to + 1])))
5825 || (enc_utf8
5826 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5827 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005828 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005829 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5830 && ScreenLines[off_from + 1]
5831 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832#endif
5833 ))
5834 return TRUE;
5835 return FALSE;
5836}
5837
5838/*
5839 * Move one "cooked" screen line to the screen, but only the characters that
5840 * have actually changed. Handle insert/delete character.
5841 * "coloff" gives the first column on the screen for this line.
5842 * "endcol" gives the columns where valid characters are.
5843 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5844 * needs to be cleared, negative otherwise.
5845 * "rlflag" is TRUE in a rightleft window:
5846 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5847 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5848 */
5849 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005850screen_line(
5851 int row,
5852 int coloff,
5853 int endcol,
5854 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005856 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005858 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859{
5860 unsigned off_from;
5861 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005862#ifdef FEAT_MBYTE
5863 unsigned max_off_from;
5864 unsigned max_off_to;
5865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005867#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 int hl;
5869#endif
5870 int force = FALSE; /* force update rest of the line */
5871 int redraw_this /* bool: does character need redraw? */
5872#ifdef FEAT_GUI
5873 = TRUE /* For GUI when while-loop empty */
5874#endif
5875 ;
5876 int redraw_next; /* redraw_this for next character */
5877#ifdef FEAT_MBYTE
5878 int clear_next = FALSE;
5879 int char_cells; /* 1: normal char */
5880 /* 2: occupies two display cells */
5881# define CHAR_CELLS char_cells
5882#else
5883# define CHAR_CELLS 1
5884#endif
5885
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005886 /* Check for illegal row and col, just in case. */
5887 if (row >= Rows)
5888 row = Rows - 1;
5889 if (endcol > Columns)
5890 endcol = Columns;
5891
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892# ifdef FEAT_CLIPBOARD
5893 clip_may_clear_selection(row, row);
5894# endif
5895
5896 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5897 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005898#ifdef FEAT_MBYTE
5899 max_off_from = off_from + screen_Columns;
5900 max_off_to = LineOffset[row] + screen_Columns;
5901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
5903#ifdef FEAT_RIGHTLEFT
5904 if (rlflag)
5905 {
5906 /* Clear rest first, because it's left of the text. */
5907 if (clear_width > 0)
5908 {
5909 while (col <= endcol && ScreenLines[off_to] == ' '
5910 && ScreenAttrs[off_to] == 0
5911# ifdef FEAT_MBYTE
5912 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5913# endif
5914 )
5915 {
5916 ++off_to;
5917 ++col;
5918 }
5919 if (col <= endcol)
5920 screen_fill(row, row + 1, col + coloff,
5921 endcol + coloff + 1, ' ', ' ', 0);
5922 }
5923 col = endcol + 1;
5924 off_to = LineOffset[row] + col + coloff;
5925 off_from += col;
5926 endcol = (clear_width > 0 ? clear_width : -clear_width);
5927 }
5928#endif /* FEAT_RIGHTLEFT */
5929
5930 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5931
5932 while (col < endcol)
5933 {
5934#ifdef FEAT_MBYTE
5935 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005936 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 else
5938 char_cells = 1;
5939#endif
5940
5941 redraw_this = redraw_next;
5942 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5943 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5944
5945#ifdef FEAT_GUI
5946 /* If the next character was bold, then redraw the current character to
5947 * remove any pixels that might have spilt over into us. This only
5948 * happens in the GUI.
5949 */
5950 if (redraw_next && gui.in_use)
5951 {
5952 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005953 if (hl > HL_ALL)
5954 hl = syn_attr2attr(hl);
5955 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 redraw_this = TRUE;
5957 }
5958#endif
5959
5960 if (redraw_this)
5961 {
5962 /*
5963 * Special handling when 'xs' termcap flag set (hpterm):
5964 * Attributes for characters are stored at the position where the
5965 * cursor is when writing the highlighting code. The
5966 * start-highlighting code must be written with the cursor on the
5967 * first highlighted character. The stop-highlighting code must
5968 * be written with the cursor just after the last highlighted
5969 * character.
5970 * Overwriting a character doesn't remove it's highlighting. Need
5971 * to clear the rest of the line, and force redrawing it
5972 * completely.
5973 */
5974 if ( p_wiv
5975 && !force
5976#ifdef FEAT_GUI
5977 && !gui.in_use
5978#endif
5979 && ScreenAttrs[off_to] != 0
5980 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5981 {
5982 /*
5983 * Need to remove highlighting attributes here.
5984 */
5985 windgoto(row, col + coloff);
5986 out_str(T_CE); /* clear rest of this screen line */
5987 screen_start(); /* don't know where cursor is now */
5988 force = TRUE; /* force redraw of rest of the line */
5989 redraw_next = TRUE; /* or else next char would miss out */
5990
5991 /*
5992 * If the previous character was highlighted, need to stop
5993 * highlighting at this character.
5994 */
5995 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5996 {
5997 screen_attr = ScreenAttrs[off_to - 1];
5998 term_windgoto(row, col + coloff);
5999 screen_stop_highlight();
6000 }
6001 else
6002 screen_attr = 0; /* highlighting has stopped */
6003 }
6004#ifdef FEAT_MBYTE
6005 if (enc_dbcs != 0)
6006 {
6007 /* Check if overwriting a double-byte with a single-byte or
6008 * the other way around requires another character to be
6009 * redrawn. For UTF-8 this isn't needed, because comparing
6010 * ScreenLinesUC[] is sufficient. */
6011 if (char_cells == 1
6012 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006013 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 {
6015 /* Writing a single-cell character over a double-cell
6016 * character: need to redraw the next cell. */
6017 ScreenLines[off_to + 1] = 0;
6018 redraw_next = TRUE;
6019 }
6020 else if (char_cells == 2
6021 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006022 && (*mb_off2cells)(off_to, max_off_to) == 1
6023 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 {
6025 /* Writing the second half of a double-cell character over
6026 * a double-cell character: need to redraw the second
6027 * cell. */
6028 ScreenLines[off_to + 2] = 0;
6029 redraw_next = TRUE;
6030 }
6031
6032 if (enc_dbcs == DBCS_JPNU)
6033 ScreenLines2[off_to] = ScreenLines2[off_from];
6034 }
6035 /* When writing a single-width character over a double-width
6036 * character and at the end of the redrawn text, need to clear out
6037 * the right halve of the old character.
6038 * Also required when writing the right halve of a double-width
6039 * char over the left halve of an existing one. */
6040 if (has_mbyte && col + char_cells == endcol
6041 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006042 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006044 && (*mb_off2cells)(off_to, max_off_to) == 1
6045 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 clear_next = TRUE;
6047#endif
6048
6049 ScreenLines[off_to] = ScreenLines[off_from];
6050#ifdef FEAT_MBYTE
6051 if (enc_utf8)
6052 {
6053 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6054 if (ScreenLinesUC[off_from] != 0)
6055 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006056 int i;
6057
6058 for (i = 0; i < Screen_mco; ++i)
6059 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 }
6061 }
6062 if (char_cells == 2)
6063 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6064#endif
6065
6066#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006067 /* The bold trick makes a single column of pixels appear in the
6068 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 * character should be redrawn too. This happens for our own GUI
6070 * and for some xterms. */
6071 if (
6072# ifdef FEAT_GUI
6073 gui.in_use
6074# endif
6075# if defined(FEAT_GUI) && defined(UNIX)
6076 ||
6077# endif
6078# ifdef UNIX
6079 term_is_xterm
6080# endif
6081 )
6082 {
6083 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006084 if (hl > HL_ALL)
6085 hl = syn_attr2attr(hl);
6086 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 redraw_next = TRUE;
6088 }
6089#endif
6090 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6091#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006092 /* For simplicity set the attributes of second half of a
6093 * double-wide character equal to the first half. */
6094 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006096
6097 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 else
6100#endif
6101 screen_char(off_to, row, col + coloff);
6102 }
6103 else if ( p_wiv
6104#ifdef FEAT_GUI
6105 && !gui.in_use
6106#endif
6107 && col + coloff > 0)
6108 {
6109 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6110 {
6111 /*
6112 * Don't output stop-highlight when moving the cursor, it will
6113 * stop the highlighting when it should continue.
6114 */
6115 screen_attr = 0;
6116 }
6117 else if (screen_attr != 0)
6118 screen_stop_highlight();
6119 }
6120
6121 off_to += CHAR_CELLS;
6122 off_from += CHAR_CELLS;
6123 col += CHAR_CELLS;
6124 }
6125
6126#ifdef FEAT_MBYTE
6127 if (clear_next)
6128 {
6129 /* Clear the second half of a double-wide character of which the left
6130 * half was overwritten with a single-wide character. */
6131 ScreenLines[off_to] = ' ';
6132 if (enc_utf8)
6133 ScreenLinesUC[off_to] = 0;
6134 screen_char(off_to, row, col + coloff);
6135 }
6136#endif
6137
6138 if (clear_width > 0
6139#ifdef FEAT_RIGHTLEFT
6140 && !rlflag
6141#endif
6142 )
6143 {
6144#ifdef FEAT_GUI
6145 int startCol = col;
6146#endif
6147
6148 /* blank out the rest of the line */
6149 while (col < clear_width && ScreenLines[off_to] == ' '
6150 && ScreenAttrs[off_to] == 0
6151#ifdef FEAT_MBYTE
6152 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6153#endif
6154 )
6155 {
6156 ++off_to;
6157 ++col;
6158 }
6159 if (col < clear_width)
6160 {
6161#ifdef FEAT_GUI
6162 /*
6163 * In the GUI, clearing the rest of the line may leave pixels
6164 * behind if the first character cleared was bold. Some bold
6165 * fonts spill over the left. In this case we redraw the previous
6166 * character too. If we didn't skip any blanks above, then we
6167 * only redraw if the character wasn't already redrawn anyway.
6168 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006169 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 {
6171 hl = ScreenAttrs[off_to];
6172 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006173 {
6174 int prev_cells = 1;
6175# ifdef FEAT_MBYTE
6176 if (enc_utf8)
6177 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6178 * that its width is 2. */
6179 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6180 else if (enc_dbcs != 0)
6181 {
6182 /* find previous character by counting from first
6183 * column and get its width. */
6184 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006185 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006186
6187 while (off < off_to)
6188 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006189 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006190 off += prev_cells;
6191 }
6192 }
6193
6194 if (enc_dbcs != 0 && prev_cells > 1)
6195 screen_char_2(off_to - prev_cells, row,
6196 col + coloff - prev_cells);
6197 else
6198# endif
6199 screen_char(off_to - prev_cells, row,
6200 col + coloff - prev_cells);
6201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 }
6203#endif
6204 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6205 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006206#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 off_to += clear_width - col;
6208 col = clear_width;
6209#endif
6210 }
6211 }
6212
6213 if (clear_width > 0)
6214 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006215#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 /* For a window that's left of another, draw the separator char. */
6217 if (col + coloff < Columns)
6218 {
6219 int c;
6220
6221 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006222 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006224 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6225 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226# endif
6227 || ScreenAttrs[off_to] != hl)
6228 {
6229 ScreenLines[off_to] = c;
6230 ScreenAttrs[off_to] = hl;
6231# ifdef FEAT_MBYTE
6232 if (enc_utf8)
6233 {
6234 if (c >= 0x80)
6235 {
6236 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006237 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 }
6239 else
6240 ScreenLinesUC[off_to] = 0;
6241 }
6242# endif
6243 screen_char(off_to, row, col + coloff);
6244 }
6245 }
6246 else
6247#endif
6248 LineWraps[row] = FALSE;
6249 }
6250}
6251
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006252#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006254 * Mirror text "str" for right-left displaying.
6255 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006257 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006258rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 char_u *p1, *p2;
6261 int t;
6262
6263 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6264 {
6265 t = *p1;
6266 *p1 = *p2;
6267 *p2 = t;
6268 }
6269}
6270#endif
6271
6272#if defined(FEAT_WINDOWS) || defined(PROTO)
6273/*
6274 * mark all status lines for redraw; used after first :cd
6275 */
6276 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006277status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
6279 win_T *wp;
6280
Bram Moolenaar29323592016-07-24 22:04:11 +02006281 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 if (wp->w_status_height)
6283 {
6284 wp->w_redr_status = TRUE;
6285 redraw_later(VALID);
6286 }
6287}
6288
6289/*
6290 * mark all status lines of the current buffer for redraw
6291 */
6292 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006293status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
6295 win_T *wp;
6296
Bram Moolenaar29323592016-07-24 22:04:11 +02006297 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6299 {
6300 wp->w_redr_status = TRUE;
6301 redraw_later(VALID);
6302 }
6303}
6304
6305/*
6306 * Redraw all status lines that need to be redrawn.
6307 */
6308 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006309redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 win_T *wp;
6312
Bram Moolenaar29323592016-07-24 22:04:11 +02006313 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 if (wp->w_redr_status)
6315 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006316 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006317 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318}
6319#endif
6320
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006321#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322/*
6323 * Redraw all status lines at the bottom of frame "frp".
6324 */
6325 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006326win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
6328 if (frp->fr_layout == FR_LEAF)
6329 frp->fr_win->w_redr_status = TRUE;
6330 else if (frp->fr_layout == FR_ROW)
6331 {
6332 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6333 win_redraw_last_status(frp);
6334 }
6335 else /* frp->fr_layout == FR_COL */
6336 {
6337 frp = frp->fr_child;
6338 while (frp->fr_next != NULL)
6339 frp = frp->fr_next;
6340 win_redraw_last_status(frp);
6341 }
6342}
6343#endif
6344
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006345#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346/*
6347 * Draw the verticap separator right of window "wp" starting with line "row".
6348 */
6349 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006350draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
6352 int hl;
6353 int c;
6354
6355 if (wp->w_vsep_width)
6356 {
6357 /* draw the vertical separator right of this window */
6358 c = fillchar_vsep(&hl);
6359 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6360 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6361 c, ' ', hl);
6362 }
6363}
6364#endif
6365
6366#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006367static int status_match_len(expand_T *xp, char_u *s);
6368static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369
6370/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006371 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 */
6373 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006374status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375{
6376 int len = 0;
6377
6378#ifdef FEAT_MENU
6379 int emenu = (xp->xp_context == EXPAND_MENUS
6380 || xp->xp_context == EXPAND_MENUNAMES);
6381
6382 /* Check for menu separators - replace with '|'. */
6383 if (emenu && menu_is_separator(s))
6384 return 1;
6385#endif
6386
6387 while (*s != NUL)
6388 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006389 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006390 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006391 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 }
6393
6394 return len;
6395}
6396
6397/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006398 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006399 * These are backslashes used for escaping. Do show backslashes in help tags.
6400 */
6401 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006402skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006403{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006404 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006405#ifdef FEAT_MENU
6406 || ((xp->xp_context == EXPAND_MENUS
6407 || xp->xp_context == EXPAND_MENUNAMES)
6408 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6409#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006410 )
6411 {
6412#ifndef BACKSLASH_IN_FILENAME
6413 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6414 return 2;
6415#endif
6416 return 1;
6417 }
6418 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006419}
6420
6421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 * Show wildchar matches in the status line.
6423 * Show at least the "match" item.
6424 * We start at item 'first_match' in the list and show all matches that fit.
6425 *
6426 * If inversion is possible we use it. Else '=' characters are used.
6427 */
6428 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006429win_redr_status_matches(
6430 expand_T *xp,
6431 int num_matches,
6432 char_u **matches, /* list of matches */
6433 int match,
6434 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435{
6436#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6437 int row;
6438 char_u *buf;
6439 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006440 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 int fillchar;
6442 int attr;
6443 int i;
6444 int highlight = TRUE;
6445 char_u *selstart = NULL;
6446 int selstart_col = 0;
6447 char_u *selend = NULL;
6448 static int first_match = 0;
6449 int add_left = FALSE;
6450 char_u *s;
6451#ifdef FEAT_MENU
6452 int emenu;
6453#endif
6454#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6455 int l;
6456#endif
6457
6458 if (matches == NULL) /* interrupted completion? */
6459 return;
6460
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006461#ifdef FEAT_MBYTE
6462 if (has_mbyte)
6463 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6464 else
6465#endif
6466 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (buf == NULL)
6468 return;
6469
6470 if (match == -1) /* don't show match but original text */
6471 {
6472 match = 0;
6473 highlight = FALSE;
6474 }
6475 /* count 1 for the ending ">" */
6476 clen = status_match_len(xp, L_MATCH(match)) + 3;
6477 if (match == 0)
6478 first_match = 0;
6479 else if (match < first_match)
6480 {
6481 /* jumping left, as far as we can go */
6482 first_match = match;
6483 add_left = TRUE;
6484 }
6485 else
6486 {
6487 /* check if match fits on the screen */
6488 for (i = first_match; i < match; ++i)
6489 clen += status_match_len(xp, L_MATCH(i)) + 2;
6490 if (first_match > 0)
6491 clen += 2;
6492 /* jumping right, put match at the left */
6493 if ((long)clen > Columns)
6494 {
6495 first_match = match;
6496 /* if showing the last match, we can add some on the left */
6497 clen = 2;
6498 for (i = match; i < num_matches; ++i)
6499 {
6500 clen += status_match_len(xp, L_MATCH(i)) + 2;
6501 if ((long)clen >= Columns)
6502 break;
6503 }
6504 if (i == num_matches)
6505 add_left = TRUE;
6506 }
6507 }
6508 if (add_left)
6509 while (first_match > 0)
6510 {
6511 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6512 if ((long)clen >= Columns)
6513 break;
6514 --first_match;
6515 }
6516
6517 fillchar = fillchar_status(&attr, TRUE);
6518
6519 if (first_match == 0)
6520 {
6521 *buf = NUL;
6522 len = 0;
6523 }
6524 else
6525 {
6526 STRCPY(buf, "< ");
6527 len = 2;
6528 }
6529 clen = len;
6530
6531 i = first_match;
6532 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6533 {
6534 if (i == match)
6535 {
6536 selstart = buf + len;
6537 selstart_col = clen;
6538 }
6539
6540 s = L_MATCH(i);
6541 /* Check for menu separators - replace with '|' */
6542#ifdef FEAT_MENU
6543 emenu = (xp->xp_context == EXPAND_MENUS
6544 || xp->xp_context == EXPAND_MENUNAMES);
6545 if (emenu && menu_is_separator(s))
6546 {
6547 STRCPY(buf + len, transchar('|'));
6548 l = (int)STRLEN(buf + len);
6549 len += l;
6550 clen += l;
6551 }
6552 else
6553#endif
6554 for ( ; *s != NUL; ++s)
6555 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006556 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 clen += ptr2cells(s);
6558#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006559 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
6561 STRNCPY(buf + len, s, l);
6562 s += l - 1;
6563 len += l;
6564 }
6565 else
6566#endif
6567 {
6568 STRCPY(buf + len, transchar_byte(*s));
6569 len += (int)STRLEN(buf + len);
6570 }
6571 }
6572 if (i == match)
6573 selend = buf + len;
6574
6575 *(buf + len++) = ' ';
6576 *(buf + len++) = ' ';
6577 clen += 2;
6578 if (++i == num_matches)
6579 break;
6580 }
6581
6582 if (i != num_matches)
6583 {
6584 *(buf + len++) = '>';
6585 ++clen;
6586 }
6587
6588 buf[len] = NUL;
6589
6590 row = cmdline_row - 1;
6591 if (row >= 0)
6592 {
6593 if (wild_menu_showing == 0)
6594 {
6595 if (msg_scrolled > 0)
6596 {
6597 /* Put the wildmenu just above the command line. If there is
6598 * no room, scroll the screen one line up. */
6599 if (cmdline_row == Rows - 1)
6600 {
6601 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6602 ++msg_scrolled;
6603 }
6604 else
6605 {
6606 ++cmdline_row;
6607 ++row;
6608 }
6609 wild_menu_showing = WM_SCROLLED;
6610 }
6611 else
6612 {
6613 /* Create status line if needed by setting 'laststatus' to 2.
6614 * Set 'winminheight' to zero to avoid that the window is
6615 * resized. */
6616 if (lastwin->w_status_height == 0)
6617 {
6618 save_p_ls = p_ls;
6619 save_p_wmh = p_wmh;
6620 p_ls = 2;
6621 p_wmh = 0;
6622 last_status(FALSE);
6623 }
6624 wild_menu_showing = WM_SHOWN;
6625 }
6626 }
6627
6628 screen_puts(buf, row, 0, attr);
6629 if (selstart != NULL && highlight)
6630 {
6631 *selend = NUL;
6632 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6633 }
6634
6635 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6636 }
6637
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006638#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 win_redraw_last_status(topframe);
6640#else
6641 lastwin->w_redr_status = TRUE;
6642#endif
6643 vim_free(buf);
6644}
6645#endif
6646
6647#if defined(FEAT_WINDOWS) || defined(PROTO)
6648/*
6649 * Redraw the status line of window wp.
6650 *
6651 * If inversion is possible we use it. Else '=' characters are used.
6652 */
6653 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006654win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
6656 int row;
6657 char_u *p;
6658 int len;
6659 int fillchar;
6660 int attr;
6661 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006662 static int busy = FALSE;
6663
6664 /* It's possible to get here recursively when 'statusline' (indirectly)
6665 * invokes ":redrawstatus". Simply ignore the call then. */
6666 if (busy)
6667 return;
6668 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669
6670 wp->w_redr_status = FALSE;
6671 if (wp->w_status_height == 0)
6672 {
6673 /* no status line, can only be last window */
6674 redraw_cmdline = TRUE;
6675 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006676 else if (!redrawing()
6677#ifdef FEAT_INS_EXPAND
6678 /* don't update status line when popup menu is visible and may be
6679 * drawn over it */
6680 || pum_visible()
6681#endif
6682 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 {
6684 /* Don't redraw right now, do it later. */
6685 wp->w_redr_status = TRUE;
6686 }
6687#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006688 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 {
6690 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006691 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 }
6693#endif
6694 else
6695 {
6696 fillchar = fillchar_status(&attr, wp == curwin);
6697
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006698 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 p = NameBuff;
6700 len = (int)STRLEN(p);
6701
6702 if (wp->w_buffer->b_help
6703#ifdef FEAT_QUICKFIX
6704 || wp->w_p_pvw
6705#endif
6706 || bufIsChanged(wp->w_buffer)
6707 || wp->w_buffer->b_p_ro)
6708 *(p + len++) = ' ';
6709 if (wp->w_buffer->b_help)
6710 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006711 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 len += (int)STRLEN(p + len);
6713 }
6714#ifdef FEAT_QUICKFIX
6715 if (wp->w_p_pvw)
6716 {
6717 STRCPY(p + len, _("[Preview]"));
6718 len += (int)STRLEN(p + len);
6719 }
6720#endif
6721 if (bufIsChanged(wp->w_buffer))
6722 {
6723 STRCPY(p + len, "[+]");
6724 len += 3;
6725 }
6726 if (wp->w_buffer->b_p_ro)
6727 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006728 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 len += 4;
6730 }
6731
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6733 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6734 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6735 if (this_ru_col <= 1)
6736 {
6737 p = (char_u *)"<"; /* No room for file name! */
6738 len = 1;
6739 }
6740 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741#ifdef FEAT_MBYTE
6742 if (has_mbyte)
6743 {
6744 int clen = 0, i;
6745
6746 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006747 clen = mb_string2cells(p, -1);
6748
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 /* Find first character that will fit.
6750 * Going from start to end is much faster for DBCS. */
6751 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006752 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 clen -= (*mb_ptr2cells)(p + i);
6754 len = clen;
6755 if (i > 0)
6756 {
6757 p = p + i - 1;
6758 *p = '<';
6759 ++len;
6760 }
6761
6762 }
6763 else
6764#endif
6765 if (len > this_ru_col - 1)
6766 {
6767 p += len - (this_ru_col - 1);
6768 *p = '<';
6769 len = this_ru_col - 1;
6770 }
6771
6772 row = W_WINROW(wp) + wp->w_height;
6773 screen_puts(p, row, W_WINCOL(wp), attr);
6774 screen_fill(row, row + 1, len + W_WINCOL(wp),
6775 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6776
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006777 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6779 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6780 - 1 + W_WINCOL(wp)), attr);
6781
6782#ifdef FEAT_CMDL_INFO
6783 win_redr_ruler(wp, TRUE);
6784#endif
6785 }
6786
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 /*
6788 * May need to draw the character below the vertical separator.
6789 */
6790 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6791 {
6792 if (stl_connected(wp))
6793 fillchar = fillchar_status(&attr, wp == curwin);
6794 else
6795 fillchar = fillchar_vsep(&attr);
6796 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6797 attr);
6798 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006799 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800}
6801
Bram Moolenaar238a5642006-02-21 22:12:05 +00006802#ifdef FEAT_STL_OPT
6803/*
6804 * Redraw the status line according to 'statusline' and take care of any
6805 * errors encountered.
6806 */
6807 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006808redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006809{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006810 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006811 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006812
6813 /* When called recursively return. This can happen when the statusline
6814 * contains an expression that triggers a redraw. */
6815 if (entered)
6816 return;
6817 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006818
Bram Moolenaara742e082016-04-05 21:10:38 +02006819 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006820 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006821 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006822 {
6823 /* When there is an error disable the statusline, otherwise the
6824 * display is messed up with errors and a redraw triggers the problem
6825 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006826 set_string_option_direct((char_u *)"statusline", -1,
6827 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006828 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006829 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006830 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006831 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006832}
6833#endif
6834
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835/*
6836 * Return TRUE if the status line of window "wp" is connected to the status
6837 * line of the window right of it. If not, then it's a vertical separator.
6838 * Only call if (wp->w_vsep_width != 0).
6839 */
6840 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006841stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
6843 frame_T *fr;
6844
6845 fr = wp->w_frame;
6846 while (fr->fr_parent != NULL)
6847 {
6848 if (fr->fr_parent->fr_layout == FR_COL)
6849 {
6850 if (fr->fr_next != NULL)
6851 break;
6852 }
6853 else
6854 {
6855 if (fr->fr_next != NULL)
6856 return TRUE;
6857 }
6858 fr = fr->fr_parent;
6859 }
6860 return FALSE;
6861}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862
6863#endif /* FEAT_WINDOWS */
6864
6865#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6866/*
6867 * Get the value to show for the language mappings, active 'keymap'.
6868 */
6869 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006870get_keymap_str(
6871 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006872 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01006873 char_u *buf, /* buffer for the result */
6874 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875{
6876 char_u *p;
6877
6878 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6879 return FALSE;
6880
6881 {
6882#ifdef FEAT_EVAL
6883 buf_T *old_curbuf = curbuf;
6884 win_T *old_curwin = curwin;
6885 char_u *s;
6886
6887 curbuf = wp->w_buffer;
6888 curwin = wp;
6889 STRCPY(buf, "b:keymap_name"); /* must be writable */
6890 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006891 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 --emsg_skip;
6893 curbuf = old_curbuf;
6894 curwin = old_curwin;
6895 if (p == NULL || *p == NUL)
6896#endif
6897 {
6898#ifdef FEAT_KEYMAP
6899 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6900 p = wp->w_buffer->b_p_keymap;
6901 else
6902#endif
6903 p = (char_u *)"lang";
6904 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006905 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 buf[0] = NUL;
6907#ifdef FEAT_EVAL
6908 vim_free(s);
6909#endif
6910 }
6911 return buf[0] != NUL;
6912}
6913#endif
6914
6915#if defined(FEAT_STL_OPT) || defined(PROTO)
6916/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006917 * Redraw the status line or ruler of window "wp".
6918 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 */
6920 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006921win_redr_custom(
6922 win_T *wp,
6923 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006925 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 int attr;
6927 int curattr;
6928 int row;
6929 int col = 0;
6930 int maxwidth;
6931 int width;
6932 int n;
6933 int len;
6934 int fillchar;
6935 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006936 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006938 struct stl_hlrec hltab[STL_MAX_ITEM];
6939 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006940 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006941 win_T *ewp;
6942 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
Bram Moolenaar1d633412013-12-11 15:52:01 +01006944 /* There is a tiny chance that this gets called recursively: When
6945 * redrawing a status line triggers redrawing the ruler or tabline.
6946 * Avoid trouble by not allowing recursion. */
6947 if (entered)
6948 return;
6949 entered = TRUE;
6950
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006952 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006954 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006955 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006956 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006957 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006958 attr = hl_attr(HLF_TPF);
6959 maxwidth = Columns;
6960# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006961 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006962# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006964 else
6965 {
6966 row = W_WINROW(wp) + wp->w_height;
6967 fillchar = fillchar_status(&attr, wp == curwin);
6968 maxwidth = W_WIDTH(wp);
6969
6970 if (draw_ruler)
6971 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006972 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006973 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006974 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006975 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006976 if (*++stl == '-')
6977 stl++;
6978 if (atoi((char *)stl))
6979 while (VIM_ISDIGIT(*stl))
6980 stl++;
6981 if (*stl++ != '(')
6982 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006983 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006984#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006985 col = ru_col - (Columns - W_WIDTH(wp));
6986 if (col < (W_WIDTH(wp) + 1) / 2)
6987 col = (W_WIDTH(wp) + 1) / 2;
6988#else
6989 col = ru_col;
6990 if (col > (Columns + 1) / 2)
6991 col = (Columns + 1) / 2;
6992#endif
6993 maxwidth = W_WIDTH(wp) - col;
6994#ifdef FEAT_WINDOWS
6995 if (!wp->w_status_height)
6996#endif
6997 {
6998 row = Rows - 1;
6999 --maxwidth; /* writing in last column may cause scrolling */
7000 fillchar = ' ';
7001 attr = 0;
7002 }
7003
7004# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007005 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007006# endif
7007 }
7008 else
7009 {
7010 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007011 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007012 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007013 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007014# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007015 use_sandbox = was_set_insecurely((char_u *)"statusline",
7016 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007017# endif
7018 }
7019
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007020#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007021 col += W_WINCOL(wp);
7022#endif
7023 }
7024
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007026 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
Bram Moolenaar61452852011-02-01 18:01:11 +01007028 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7029 * the cursor away and back. */
7030 ewp = wp == NULL ? curwin : wp;
7031 p_crb_save = ewp->w_p_crb;
7032 ewp->w_p_crb = FALSE;
7033
Bram Moolenaar362f3562009-11-03 16:20:34 +00007034 /* Make a copy, because the statusline may include a function call that
7035 * might change the option value and free the memory. */
7036 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007037 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007038 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007039 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007040 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007041 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007043 /* Make all characters printable. */
7044 p = transstr(buf);
7045 if (p != NULL)
7046 {
7047 vim_strncpy(buf, p, sizeof(buf) - 1);
7048 vim_free(p);
7049 }
7050
7051 /* fill up with "fillchar" */
7052 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007053 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 {
7055#ifdef FEAT_MBYTE
7056 len += (*mb_char2bytes)(fillchar, buf + len);
7057#else
7058 buf[len++] = fillchar;
7059#endif
7060 ++width;
7061 }
7062 buf[len] = NUL;
7063
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007064 /*
7065 * Draw each snippet with the specified highlighting.
7066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 curattr = attr;
7068 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007069 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007071 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 screen_puts_len(p, len, row, col, curattr);
7073 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007074 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007076 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007078 else if (hltab[n].userhl < 0)
7079 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007081 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007082 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083#endif
7084 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007085 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 }
7087 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007088
7089 if (wp == NULL)
7090 {
7091 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7092 col = 0;
7093 len = 0;
7094 p = buf;
7095 fillchar = 0;
7096 for (n = 0; tabtab[n].start != NULL; n++)
7097 {
7098 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7099 while (col < len)
7100 TabPageIdxs[col++] = fillchar;
7101 p = tabtab[n].start;
7102 fillchar = tabtab[n].userhl;
7103 }
7104 while (col < Columns)
7105 TabPageIdxs[col++] = fillchar;
7106 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007107
7108theend:
7109 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110}
7111
7112#endif /* FEAT_STL_OPT */
7113
7114/*
7115 * Output a single character directly to the screen and update ScreenLines.
7116 */
7117 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007118screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 char_u buf[MB_MAXBYTES + 1];
7121
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007122#ifdef FEAT_MBYTE
7123 if (has_mbyte)
7124 buf[(*mb_char2bytes)(c, buf)] = NUL;
7125 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007127 {
7128 buf[0] = c;
7129 buf[1] = NUL;
7130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 screen_puts(buf, row, col, attr);
7132}
7133
7134/*
7135 * Get a single character directly from ScreenLines into "bytes[]".
7136 * Also return its attribute in *attrp;
7137 */
7138 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007139screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
7141 unsigned off;
7142
7143 /* safety check */
7144 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7145 {
7146 off = LineOffset[row] + col;
7147 *attrp = ScreenAttrs[off];
7148 bytes[0] = ScreenLines[off];
7149 bytes[1] = NUL;
7150
7151#ifdef FEAT_MBYTE
7152 if (enc_utf8 && ScreenLinesUC[off] != 0)
7153 bytes[utfc_char2bytes(off, bytes)] = NUL;
7154 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7155 {
7156 bytes[0] = ScreenLines[off];
7157 bytes[1] = ScreenLines2[off];
7158 bytes[2] = NUL;
7159 }
7160 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7161 {
7162 bytes[1] = ScreenLines[off + 1];
7163 bytes[2] = NUL;
7164 }
7165#endif
7166 }
7167}
7168
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007169#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007170static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007171
7172/*
7173 * Return TRUE if composing characters for screen posn "off" differs from
7174 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007175 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007176 */
7177 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007178screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007179{
7180 int i;
7181
7182 for (i = 0; i < Screen_mco; ++i)
7183 {
7184 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7185 return TRUE;
7186 if (u8cc[i] == 0)
7187 break;
7188 }
7189 return FALSE;
7190}
7191#endif
7192
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193/*
7194 * Put string '*text' on the screen at position 'row' and 'col', with
7195 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7196 * Note: only outputs within one row, message is truncated at screen boundary!
7197 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7198 */
7199 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007200screen_puts(
7201 char_u *text,
7202 int row,
7203 int col,
7204 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205{
7206 screen_puts_len(text, -1, row, col, attr);
7207}
7208
7209/*
7210 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7211 * a NUL.
7212 */
7213 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007214screen_puts_len(
7215 char_u *text,
7216 int textlen,
7217 int row,
7218 int col,
7219 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
7221 unsigned off;
7222 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007223 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 int c;
7225#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007226 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 int mbyte_blen = 1;
7228 int mbyte_cells = 1;
7229 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007230 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 int clear_next_cell = FALSE;
7232# ifdef FEAT_ARABIC
7233 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007234 int pc, nc, nc1;
7235 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236# endif
7237#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007238#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7239 int force_redraw_this;
7240 int force_redraw_next = FALSE;
7241#endif
7242 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
7244 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7245 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007246 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
Bram Moolenaarc236c162008-07-13 17:41:49 +00007248#ifdef FEAT_MBYTE
7249 /* When drawing over the right halve of a double-wide char clear out the
7250 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007251 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007252# ifdef FEAT_GUI
7253 && !gui.in_use
7254# endif
7255 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007256 {
7257 ScreenLines[off - 1] = ' ';
7258 ScreenAttrs[off - 1] = 0;
7259 if (enc_utf8)
7260 {
7261 ScreenLinesUC[off - 1] = 0;
7262 ScreenLinesC[0][off - 1] = 0;
7263 }
7264 /* redraw the previous cell, make it empty */
7265 screen_char(off - 1, row, col - 1);
7266 /* force the cell at "col" to be redrawn */
7267 force_redraw_next = TRUE;
7268 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007269#endif
7270
Bram Moolenaar367329b2007-08-30 11:53:22 +00007271#ifdef FEAT_MBYTE
7272 max_off = LineOffset[row] + screen_Columns;
7273#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007274 while (col < screen_Columns
7275 && (len < 0 || (int)(ptr - text) < len)
7276 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {
7278 c = *ptr;
7279#ifdef FEAT_MBYTE
7280 /* check if this is the first byte of a multibyte */
7281 if (has_mbyte)
7282 {
7283 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007284 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007286 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7288 mbyte_cells = 1;
7289 else if (enc_dbcs != 0)
7290 mbyte_cells = mbyte_blen;
7291 else /* enc_utf8 */
7292 {
7293 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007294 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 (int)((text + len) - ptr));
7296 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007297 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007299# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 /* Non-BMP character: display as ? or fullwidth ?. */
7301 if (u8c >= 0x10000)
7302 {
7303 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7304 if (attr == 0)
7305 attr = hl_attr(HLF_8);
7306 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308# ifdef FEAT_ARABIC
7309 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7310 {
7311 /* Do Arabic shaping. */
7312 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7313 {
7314 /* Past end of string to be displayed. */
7315 nc = NUL;
7316 nc1 = NUL;
7317 }
7318 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007319 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007320 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7321 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007322 nc1 = pcc[0];
7323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 pc = prev_c;
7325 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007326 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 }
7328 else
7329 prev_c = u8c;
7330# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007331 if (col + mbyte_cells > screen_Columns)
7332 {
7333 /* Only 1 cell left, but character requires 2 cells:
7334 * display a '>' in the last column to avoid wrapping. */
7335 c = '>';
7336 mbyte_cells = 1;
7337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 }
7339 }
7340#endif
7341
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007342#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7343 force_redraw_this = force_redraw_next;
7344 force_redraw_next = FALSE;
7345#endif
7346
7347 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348#ifdef FEAT_MBYTE
7349 || (mbyte_cells == 2
7350 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7351 || (enc_dbcs == DBCS_JPNU
7352 && c == 0x8e
7353 && ScreenLines2[off] != ptr[1])
7354 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007355 && (ScreenLinesUC[off] !=
7356 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7357 || (ScreenLinesUC[off] != 0
7358 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359#endif
7360 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007361 || exmode_active;
7362
7363 if (need_redraw
7364#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7365 || force_redraw_this
7366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 )
7368 {
7369#if defined(FEAT_GUI) || defined(UNIX)
7370 /* The bold trick makes a single row of pixels appear in the next
7371 * character. When a bold character is removed, the next
7372 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007373 * and for some xterms. */
7374 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375# ifdef FEAT_GUI
7376 gui.in_use
7377# endif
7378# if defined(FEAT_GUI) && defined(UNIX)
7379 ||
7380# endif
7381# ifdef UNIX
7382 term_is_xterm
7383# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007384 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007386 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007388 if (n > HL_ALL)
7389 n = syn_attr2attr(n);
7390 if (n & HL_BOLD)
7391 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 }
7393#endif
7394#ifdef FEAT_MBYTE
7395 /* When at the end of the text and overwriting a two-cell
7396 * character with a one-cell character, need to clear the next
7397 * cell. Also when overwriting the left halve of a two-cell char
7398 * with the right halve of a two-cell char. Do this only once
7399 * (mb_off2cells() may return 2 on the right halve). */
7400 if (clear_next_cell)
7401 clear_next_cell = FALSE;
7402 else if (has_mbyte
7403 && (len < 0 ? ptr[mbyte_blen] == NUL
7404 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007405 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007407 && (*mb_off2cells)(off, max_off) == 1
7408 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 clear_next_cell = TRUE;
7410
7411 /* Make sure we never leave a second byte of a double-byte behind,
7412 * it confuses mb_off2cells(). */
7413 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007414 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007416 && (*mb_off2cells)(off, max_off) == 1
7417 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 ScreenLines[off + mbyte_blen] = 0;
7419#endif
7420 ScreenLines[off] = c;
7421 ScreenAttrs[off] = attr;
7422#ifdef FEAT_MBYTE
7423 if (enc_utf8)
7424 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007425 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 ScreenLinesUC[off] = 0;
7427 else
7428 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007429 int i;
7430
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007432 for (i = 0; i < Screen_mco; ++i)
7433 {
7434 ScreenLinesC[i][off] = u8cc[i];
7435 if (u8cc[i] == 0)
7436 break;
7437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 }
7439 if (mbyte_cells == 2)
7440 {
7441 ScreenLines[off + 1] = 0;
7442 ScreenAttrs[off + 1] = attr;
7443 }
7444 screen_char(off, row, col);
7445 }
7446 else if (mbyte_cells == 2)
7447 {
7448 ScreenLines[off + 1] = ptr[1];
7449 ScreenAttrs[off + 1] = attr;
7450 screen_char_2(off, row, col);
7451 }
7452 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7453 {
7454 ScreenLines2[off] = ptr[1];
7455 screen_char(off, row, col);
7456 }
7457 else
7458#endif
7459 screen_char(off, row, col);
7460 }
7461#ifdef FEAT_MBYTE
7462 if (has_mbyte)
7463 {
7464 off += mbyte_cells;
7465 col += mbyte_cells;
7466 ptr += mbyte_blen;
7467 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007468 {
7469 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007471 len = -1;
7472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 }
7474 else
7475#endif
7476 {
7477 ++off;
7478 ++col;
7479 ++ptr;
7480 }
7481 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007482
7483#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7484 /* If we detected the next character needs to be redrawn, but the text
7485 * doesn't extend up to there, update the character here. */
7486 if (force_redraw_next && col < screen_Columns)
7487 {
7488# ifdef FEAT_MBYTE
7489 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7490 screen_char_2(off, row, col);
7491 else
7492# endif
7493 screen_char(off, row, col);
7494 }
7495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496}
7497
7498#ifdef FEAT_SEARCH_EXTRA
7499/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007500 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 */
7502 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007503start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504{
7505 if (p_hls && !no_hlsearch)
7506 {
7507 last_pat_prog(&search_hl.rm);
7508 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007509# ifdef FEAT_RELTIME
7510 /* Set the time limit to 'redrawtime'. */
7511 profile_setlimit(p_rdt, &search_hl.tm);
7512# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 }
7514}
7515
7516/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007517 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 */
7519 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007520end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
7522 if (search_hl.rm.regprog != NULL)
7523 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007524 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 search_hl.rm.regprog = NULL;
7526 }
7527}
7528
7529/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007530 * Init for calling prepare_search_hl().
7531 */
7532 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007533init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007534{
7535 matchitem_T *cur;
7536
7537 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7538 * match */
7539 cur = wp->w_match_head;
7540 while (cur != NULL)
7541 {
7542 cur->hl.rm = cur->match;
7543 if (cur->hlg_id == 0)
7544 cur->hl.attr = 0;
7545 else
7546 cur->hl.attr = syn_id2attr(cur->hlg_id);
7547 cur->hl.buf = wp->w_buffer;
7548 cur->hl.lnum = 0;
7549 cur->hl.first_lnum = 0;
7550# ifdef FEAT_RELTIME
7551 /* Set the time limit to 'redrawtime'. */
7552 profile_setlimit(p_rdt, &(cur->hl.tm));
7553# endif
7554 cur = cur->next;
7555 }
7556 search_hl.buf = wp->w_buffer;
7557 search_hl.lnum = 0;
7558 search_hl.first_lnum = 0;
7559 /* time limit is set at the toplevel, for all windows */
7560}
7561
7562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 * Advance to the match in window "wp" line "lnum" or past it.
7564 */
7565 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007566prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007568 matchitem_T *cur; /* points to the match list */
7569 match_T *shl; /* points to search_hl or a match */
7570 int shl_flag; /* flag to indicate whether search_hl
7571 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007572 int pos_inprogress; /* marks that position match search is
7573 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 int n;
7575
7576 /*
7577 * When using a multi-line pattern, start searching at the top
7578 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007579 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007581 cur = wp->w_match_head;
7582 shl_flag = FALSE;
7583 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007585 if (shl_flag == FALSE)
7586 {
7587 shl = &search_hl;
7588 shl_flag = TRUE;
7589 }
7590 else
7591 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 if (shl->rm.regprog != NULL
7593 && shl->lnum == 0
7594 && re_multiline(shl->rm.regprog))
7595 {
7596 if (shl->first_lnum == 0)
7597 {
7598# ifdef FEAT_FOLDING
7599 for (shl->first_lnum = lnum;
7600 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7601 if (hasFoldingWin(wp, shl->first_lnum - 1,
7602 NULL, NULL, TRUE, NULL))
7603 break;
7604# else
7605 shl->first_lnum = wp->w_topline;
7606# endif
7607 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007608 if (cur != NULL)
7609 cur->pos.cur = 0;
7610 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007612 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7613 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007615 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7616 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007617 pos_inprogress = cur == NULL || cur->pos.cur == 0
7618 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 if (shl->lnum != 0)
7620 {
7621 shl->first_lnum = shl->lnum
7622 + shl->rm.endpos[0].lnum
7623 - shl->rm.startpos[0].lnum;
7624 n = shl->rm.endpos[0].col;
7625 }
7626 else
7627 {
7628 ++shl->first_lnum;
7629 n = 0;
7630 }
7631 }
7632 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007633 if (shl != &search_hl && cur != NULL)
7634 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 }
7636}
7637
7638/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007639 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 * Uses shl->buf.
7641 * Sets shl->lnum and shl->rm contents.
7642 * Note: Assumes a previous match is always before "lnum", unless
7643 * shl->lnum is zero.
7644 * Careful: Any pointers for buffer lines will become invalid.
7645 */
7646 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007647next_search_hl(
7648 win_T *win,
7649 match_T *shl, /* points to search_hl or a match */
7650 linenr_T lnum,
7651 colnr_T mincol, /* minimal column for a match */
7652 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653{
7654 linenr_T l;
7655 colnr_T matchcol;
7656 long nmatched;
7657
7658 if (shl->lnum != 0)
7659 {
7660 /* Check for three situations:
7661 * 1. If the "lnum" is below a previous match, start a new search.
7662 * 2. If the previous match includes "mincol", use it.
7663 * 3. Continue after the previous match.
7664 */
7665 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7666 if (lnum > l)
7667 shl->lnum = 0;
7668 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7669 return;
7670 }
7671
7672 /*
7673 * Repeat searching for a match until one is found that includes "mincol"
7674 * or none is found in this line.
7675 */
7676 called_emsg = FALSE;
7677 for (;;)
7678 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007679#ifdef FEAT_RELTIME
7680 /* Stop searching after passing the time limit. */
7681 if (profile_passed_limit(&(shl->tm)))
7682 {
7683 shl->lnum = 0; /* no match found in time */
7684 break;
7685 }
7686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 /* Three situations:
7688 * 1. No useful previous match: search from start of line.
7689 * 2. Not Vi compatible or empty match: continue at next character.
7690 * Break the loop if this is beyond the end of the line.
7691 * 3. Vi compatible searching: continue at end of previous match.
7692 */
7693 if (shl->lnum == 0)
7694 matchcol = 0;
7695 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7696 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007697 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007699 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007700
7701 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007702 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007703 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007705 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 shl->lnum = 0;
7707 break;
7708 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007709#ifdef FEAT_MBYTE
7710 if (has_mbyte)
7711 matchcol += mb_ptr2len(ml);
7712 else
7713#endif
7714 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 }
7716 else
7717 matchcol = shl->rm.endpos[0].col;
7718
7719 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007720 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007722 /* Remember whether shl->rm is using a copy of the regprog in
7723 * cur->match. */
7724 int regprog_is_copy = (shl != &search_hl && cur != NULL
7725 && shl == &cur->hl
7726 && cur->match.regprog == cur->hl.rm.regprog);
7727
Bram Moolenaarb3414592014-06-17 17:48:32 +02007728 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7729 matchcol,
7730#ifdef FEAT_RELTIME
7731 &(shl->tm)
7732#else
7733 NULL
7734#endif
7735 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007736 /* Copy the regprog, in case it got freed and recompiled. */
7737 if (regprog_is_copy)
7738 cur->match.regprog = cur->hl.rm.regprog;
7739
Bram Moolenaarb3414592014-06-17 17:48:32 +02007740 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007741 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007742 /* Error while handling regexp: stop using this regexp. */
7743 if (shl == &search_hl)
7744 {
7745 /* don't free regprog in the match list, it's a copy */
7746 vim_regfree(shl->rm.regprog);
7747 SET_NO_HLSEARCH(TRUE);
7748 }
7749 shl->rm.regprog = NULL;
7750 shl->lnum = 0;
7751 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7752 message */
7753 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007754 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007755 }
7756 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007757 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007758 else
7759 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 if (nmatched == 0)
7761 {
7762 shl->lnum = 0; /* no match found */
7763 break;
7764 }
7765 if (shl->rm.startpos[0].lnum > 0
7766 || shl->rm.startpos[0].col >= mincol
7767 || nmatched > 1
7768 || shl->rm.endpos[0].col > mincol)
7769 {
7770 shl->lnum += shl->rm.startpos[0].lnum;
7771 break; /* useful match found */
7772 }
7773 }
7774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775
Bram Moolenaar85077472016-10-16 14:35:48 +02007776/*
7777 * If there is a match fill "shl" and return one.
7778 * Return zero otherwise.
7779 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007780 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007781next_search_hl_pos(
7782 match_T *shl, /* points to a match */
7783 linenr_T lnum,
7784 posmatch_T *posmatch, /* match positions */
7785 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007786{
7787 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007788 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007789
Bram Moolenaarb3414592014-06-17 17:48:32 +02007790 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7791 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007792 llpos_T *pos = &posmatch->pos[i];
7793
7794 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007795 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007796 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007797 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007798 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007799 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007800 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007801 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007802 /* if this match comes before the one at "found" then swap
7803 * them */
7804 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007805 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007806 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007807
Bram Moolenaar85077472016-10-16 14:35:48 +02007808 *pos = posmatch->pos[found];
7809 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007810 }
7811 }
7812 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007813 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007814 }
7815 }
7816 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007817 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007818 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007819 colnr_T start = posmatch->pos[found].col == 0
7820 ? 0 : posmatch->pos[found].col - 1;
7821 colnr_T end = posmatch->pos[found].col == 0
7822 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007823
Bram Moolenaar85077472016-10-16 14:35:48 +02007824 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007825 shl->rm.startpos[0].lnum = 0;
7826 shl->rm.startpos[0].col = start;
7827 shl->rm.endpos[0].lnum = 0;
7828 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007829 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007830 posmatch->cur = found + 1;
7831 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007832 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007833 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007834}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007835#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007836
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007838screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
7840 attrentry_T *aep = NULL;
7841
7842 screen_attr = attr;
7843 if (full_screen
7844#ifdef WIN3264
7845 && termcap_active
7846#endif
7847 )
7848 {
7849#ifdef FEAT_GUI
7850 if (gui.in_use)
7851 {
7852 char buf[20];
7853
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007854 /* The GUI handles this internally. */
7855 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 OUT_STR(buf);
7857 }
7858 else
7859#endif
7860 {
7861 if (attr > HL_ALL) /* special HL attr. */
7862 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007863 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 aep = syn_cterm_attr2entry(attr);
7865 else
7866 aep = syn_term_attr2entry(attr);
7867 if (aep == NULL) /* did ":syntax clear" */
7868 attr = 0;
7869 else
7870 attr = aep->ae_attr;
7871 }
7872 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7873 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007874 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007875#ifdef FEAT_TERMGUICOLORS
7876 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007877 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007878#endif
7879 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007880#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007881 )
7882#endif
7883 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007884 /* If the Normal FG color has BOLD attribute and the new HL
7885 * has a FG color defined, clear BOLD. */
7886 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7888 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007889 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7890 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 out_str(T_US);
7892 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7893 out_str(T_CZH);
7894 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7895 out_str(T_MR);
7896
7897 /*
7898 * Output the color or start string after bold etc., in case the
7899 * bold etc. override the color setting.
7900 */
7901 if (aep != NULL)
7902 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007903#ifdef FEAT_TERMGUICOLORS
7904 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007906 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007907 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007908 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007909 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 }
7911 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007914 if (t_colors > 1)
7915 {
7916 if (aep->ae_u.cterm.fg_color)
7917 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7918 if (aep->ae_u.cterm.bg_color)
7919 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7920 }
7921 else
7922 {
7923 if (aep->ae_u.term.start != NULL)
7924 out_str(aep->ae_u.term.start);
7925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 }
7927 }
7928 }
7929 }
7930}
7931
7932 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007933screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
7935 int do_ME = FALSE; /* output T_ME code */
7936
7937 if (screen_attr != 0
7938#ifdef WIN3264
7939 && termcap_active
7940#endif
7941 )
7942 {
7943#ifdef FEAT_GUI
7944 if (gui.in_use)
7945 {
7946 char buf[20];
7947
7948 /* use internal GUI code */
7949 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7950 OUT_STR(buf);
7951 }
7952 else
7953#endif
7954 {
7955 if (screen_attr > HL_ALL) /* special HL attr. */
7956 {
7957 attrentry_T *aep;
7958
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007959 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 {
7961 /*
7962 * Assume that t_me restores the original colors!
7963 */
7964 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007965 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007966#ifdef FEAT_TERMGUICOLORS
7967 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007968 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
7969 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007970#endif
7971 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007972#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007973 )
7974#endif
7975 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 do_ME = TRUE;
7977 }
7978 else
7979 {
7980 aep = syn_term_attr2entry(screen_attr);
7981 if (aep != NULL && aep->ae_u.term.stop != NULL)
7982 {
7983 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7984 do_ME = TRUE;
7985 else
7986 out_str(aep->ae_u.term.stop);
7987 }
7988 }
7989 if (aep == NULL) /* did ":syntax clear" */
7990 screen_attr = 0;
7991 else
7992 screen_attr = aep->ae_attr;
7993 }
7994
7995 /*
7996 * Often all ending-codes are equal to T_ME. Avoid outputting the
7997 * same sequence several times.
7998 */
7999 if (screen_attr & HL_STANDOUT)
8000 {
8001 if (STRCMP(T_SE, T_ME) == 0)
8002 do_ME = TRUE;
8003 else
8004 out_str(T_SE);
8005 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008006 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 {
8008 if (STRCMP(T_UE, T_ME) == 0)
8009 do_ME = TRUE;
8010 else
8011 out_str(T_UE);
8012 }
8013 if (screen_attr & HL_ITALIC)
8014 {
8015 if (STRCMP(T_CZR, T_ME) == 0)
8016 do_ME = TRUE;
8017 else
8018 out_str(T_CZR);
8019 }
8020 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8021 out_str(T_ME);
8022
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008023#ifdef FEAT_TERMGUICOLORS
8024 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008026 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008027 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008028 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008029 term_bg_rgb_color(cterm_normal_bg_gui_color);
8030 }
8031 else
8032#endif
8033 {
8034 if (t_colors > 1)
8035 {
8036 /* set Normal cterm colors */
8037 if (cterm_normal_fg_color != 0)
8038 term_fg_color(cterm_normal_fg_color - 1);
8039 if (cterm_normal_bg_color != 0)
8040 term_bg_color(cterm_normal_bg_color - 1);
8041 if (cterm_normal_fg_bold)
8042 out_str(T_MD);
8043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 }
8045 }
8046 }
8047 screen_attr = 0;
8048}
8049
8050/*
8051 * Reset the colors for a cterm. Used when leaving Vim.
8052 * The machine specific code may override this again.
8053 */
8054 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008055reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008057 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {
8059 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008060#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008061 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8062 || cterm_normal_bg_gui_color != INVALCOLOR)
8063 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008064#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {
8068 out_str(T_OP);
8069 screen_attr = -1;
8070 }
8071 if (cterm_normal_fg_bold)
8072 {
8073 out_str(T_ME);
8074 screen_attr = -1;
8075 }
8076 }
8077}
8078
8079/*
8080 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8081 * using the attributes from ScreenAttrs["off"].
8082 */
8083 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008084screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
8086 int attr;
8087
8088 /* Check for illegal values, just in case (could happen just after
8089 * resizing). */
8090 if (row >= screen_Rows || col >= screen_Columns)
8091 return;
8092
Bram Moolenaar494838a2015-02-10 19:20:37 +01008093 /* Outputting a character in the last cell on the screen may scroll the
8094 * screen up. Only do it when the "xn" termcap property is set, otherwise
8095 * mark the character invalid (update it when scrolled up). */
8096 if (*T_XN == NUL
8097 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098#ifdef FEAT_RIGHTLEFT
8099 /* account for first command-line character in rightleft mode */
8100 && !cmdmsg_rl
8101#endif
8102 )
8103 {
8104 ScreenAttrs[off] = (sattr_T)-1;
8105 return;
8106 }
8107
8108 /*
8109 * Stop highlighting first, so it's easier to move the cursor.
8110 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008111#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 if (screen_char_attr != 0)
8113 attr = screen_char_attr;
8114 else
8115#endif
8116 attr = ScreenAttrs[off];
8117 if (screen_attr != attr)
8118 screen_stop_highlight();
8119
8120 windgoto(row, col);
8121
8122 if (screen_attr != attr)
8123 screen_start_highlight(attr);
8124
8125#ifdef FEAT_MBYTE
8126 if (enc_utf8 && ScreenLinesUC[off] != 0)
8127 {
8128 char_u buf[MB_MAXBYTES + 1];
8129
8130 /* Convert UTF-8 character to bytes and write it. */
8131
8132 buf[utfc_char2bytes(off, buf)] = NUL;
8133
8134 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008135 if (utf_ambiguous_width(ScreenLinesUC[off]))
8136 screen_cur_col = 9999;
8137 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 ++screen_cur_col;
8139 }
8140 else
8141#endif
8142 {
8143#ifdef FEAT_MBYTE
8144 out_flush_check();
8145#endif
8146 out_char(ScreenLines[off]);
8147#ifdef FEAT_MBYTE
8148 /* double-byte character in single-width cell */
8149 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8150 out_char(ScreenLines2[off]);
8151#endif
8152 }
8153
8154 screen_cur_col++;
8155}
8156
8157#ifdef FEAT_MBYTE
8158
8159/*
8160 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8161 * on the screen at position 'row' and 'col'.
8162 * The attributes of the first byte is used for all. This is required to
8163 * output the two bytes of a double-byte character with nothing in between.
8164 */
8165 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008166screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167{
8168 /* Check for illegal values (could be wrong when screen was resized). */
8169 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8170 return;
8171
8172 /* Outputting the last character on the screen may scrollup the screen.
8173 * Don't to it! Mark the character invalid (update it when scrolled up) */
8174 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8175 {
8176 ScreenAttrs[off] = (sattr_T)-1;
8177 return;
8178 }
8179
8180 /* Output the first byte normally (positions the cursor), then write the
8181 * second byte directly. */
8182 screen_char(off, row, col);
8183 out_char(ScreenLines[off + 1]);
8184 ++screen_cur_col;
8185}
8186#endif
8187
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008188#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189/*
8190 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8191 * This uses the contents of ScreenLines[] and doesn't change it.
8192 */
8193 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008194screen_draw_rectangle(
8195 int row,
8196 int col,
8197 int height,
8198 int width,
8199 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200{
8201 int r, c;
8202 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008203#ifdef FEAT_MBYTE
8204 int max_off;
8205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008207 /* Can't use ScreenLines unless initialized */
8208 if (ScreenLines == NULL)
8209 return;
8210
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 if (invert)
8212 screen_char_attr = HL_INVERSE;
8213 for (r = row; r < row + height; ++r)
8214 {
8215 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008216#ifdef FEAT_MBYTE
8217 max_off = off + screen_Columns;
8218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 for (c = col; c < col + width; ++c)
8220 {
8221#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008222 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {
8224 screen_char_2(off + c, r, c);
8225 ++c;
8226 }
8227 else
8228#endif
8229 {
8230 screen_char(off + c, r, c);
8231#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008232 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 ++c;
8234#endif
8235 }
8236 }
8237 }
8238 screen_char_attr = 0;
8239}
8240#endif
8241
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008242#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243/*
8244 * Redraw the characters for a vertically split window.
8245 */
8246 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008247redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248{
8249 int col;
8250 int width;
8251
8252# ifdef FEAT_CLIPBOARD
8253 clip_may_clear_selection(row, end - 1);
8254# endif
8255
8256 if (wp == NULL)
8257 {
8258 col = 0;
8259 width = Columns;
8260 }
8261 else
8262 {
8263 col = wp->w_wincol;
8264 width = wp->w_width;
8265 }
8266 screen_draw_rectangle(row, col, end - row, width, FALSE);
8267}
8268#endif
8269
8270/*
8271 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8272 * with character 'c1' in first column followed by 'c2' in the other columns.
8273 * Use attributes 'attr'.
8274 */
8275 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008276screen_fill(
8277 int start_row,
8278 int end_row,
8279 int start_col,
8280 int end_col,
8281 int c1,
8282 int c2,
8283 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284{
8285 int row;
8286 int col;
8287 int off;
8288 int end_off;
8289 int did_delete;
8290 int c;
8291 int norm_term;
8292#if defined(FEAT_GUI) || defined(UNIX)
8293 int force_next = FALSE;
8294#endif
8295
8296 if (end_row > screen_Rows) /* safety check */
8297 end_row = screen_Rows;
8298 if (end_col > screen_Columns) /* safety check */
8299 end_col = screen_Columns;
8300 if (ScreenLines == NULL
8301 || start_row >= end_row
8302 || start_col >= end_col) /* nothing to do */
8303 return;
8304
8305 /* it's a "normal" terminal when not in a GUI or cterm */
8306 norm_term = (
8307#ifdef FEAT_GUI
8308 !gui.in_use &&
8309#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008310 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 for (row = start_row; row < end_row; ++row)
8312 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008313#ifdef FEAT_MBYTE
8314 if (has_mbyte
8315# ifdef FEAT_GUI
8316 && !gui.in_use
8317# endif
8318 )
8319 {
8320 /* When drawing over the right halve of a double-wide char clear
8321 * out the left halve. When drawing over the left halve of a
8322 * double wide-char clear out the right halve. Only needed in a
8323 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008324 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008325 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008326 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008327 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008328 }
8329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 /*
8331 * Try to use delete-line termcap code, when no attributes or in a
8332 * "normal" terminal, where a bold/italic space is just a
8333 * space.
8334 */
8335 did_delete = FALSE;
8336 if (c2 == ' '
8337 && end_col == Columns
8338 && can_clear(T_CE)
8339 && (attr == 0
8340 || (norm_term
8341 && attr <= HL_ALL
8342 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8343 {
8344 /*
8345 * check if we really need to clear something
8346 */
8347 col = start_col;
8348 if (c1 != ' ') /* don't clear first char */
8349 ++col;
8350
8351 off = LineOffset[row] + col;
8352 end_off = LineOffset[row] + end_col;
8353
8354 /* skip blanks (used often, keep it fast!) */
8355#ifdef FEAT_MBYTE
8356 if (enc_utf8)
8357 while (off < end_off && ScreenLines[off] == ' '
8358 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8359 ++off;
8360 else
8361#endif
8362 while (off < end_off && ScreenLines[off] == ' '
8363 && ScreenAttrs[off] == 0)
8364 ++off;
8365 if (off < end_off) /* something to be cleared */
8366 {
8367 col = off - LineOffset[row];
8368 screen_stop_highlight();
8369 term_windgoto(row, col);/* clear rest of this screen line */
8370 out_str(T_CE);
8371 screen_start(); /* don't know where cursor is now */
8372 col = end_col - col;
8373 while (col--) /* clear chars in ScreenLines */
8374 {
8375 ScreenLines[off] = ' ';
8376#ifdef FEAT_MBYTE
8377 if (enc_utf8)
8378 ScreenLinesUC[off] = 0;
8379#endif
8380 ScreenAttrs[off] = 0;
8381 ++off;
8382 }
8383 }
8384 did_delete = TRUE; /* the chars are cleared now */
8385 }
8386
8387 off = LineOffset[row] + start_col;
8388 c = c1;
8389 for (col = start_col; col < end_col; ++col)
8390 {
8391 if (ScreenLines[off] != c
8392#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008393 || (enc_utf8 && (int)ScreenLinesUC[off]
8394 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395#endif
8396 || ScreenAttrs[off] != attr
8397#if defined(FEAT_GUI) || defined(UNIX)
8398 || force_next
8399#endif
8400 )
8401 {
8402#if defined(FEAT_GUI) || defined(UNIX)
8403 /* The bold trick may make a single row of pixels appear in
8404 * the next character. When a bold character is removed, the
8405 * next character should be redrawn too. This happens for our
8406 * own GUI and for some xterms. */
8407 if (
8408# ifdef FEAT_GUI
8409 gui.in_use
8410# endif
8411# if defined(FEAT_GUI) && defined(UNIX)
8412 ||
8413# endif
8414# ifdef UNIX
8415 term_is_xterm
8416# endif
8417 )
8418 {
8419 if (ScreenLines[off] != ' '
8420 && (ScreenAttrs[off] > HL_ALL
8421 || ScreenAttrs[off] & HL_BOLD))
8422 force_next = TRUE;
8423 else
8424 force_next = FALSE;
8425 }
8426#endif
8427 ScreenLines[off] = c;
8428#ifdef FEAT_MBYTE
8429 if (enc_utf8)
8430 {
8431 if (c >= 0x80)
8432 {
8433 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008434 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 }
8436 else
8437 ScreenLinesUC[off] = 0;
8438 }
8439#endif
8440 ScreenAttrs[off] = attr;
8441 if (!did_delete || c != ' ')
8442 screen_char(off, row, col);
8443 }
8444 ++off;
8445 if (col == start_col)
8446 {
8447 if (did_delete)
8448 break;
8449 c = c2;
8450 }
8451 }
8452 if (end_col == Columns)
8453 LineWraps[row] = FALSE;
8454 if (row == Rows - 1) /* overwritten the command line */
8455 {
8456 redraw_cmdline = TRUE;
8457 if (c1 == ' ' && c2 == ' ')
8458 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008459 if (start_col == 0)
8460 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 }
8462 }
8463}
8464
8465/*
8466 * Check if there should be a delay. Used before clearing or redrawing the
8467 * screen or the command line.
8468 */
8469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008470check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471{
8472 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8473 && !did_wait_return
8474 && emsg_silent == 0)
8475 {
8476 out_flush();
8477 ui_delay(1000L, TRUE);
8478 emsg_on_display = FALSE;
8479 if (check_msg_scroll)
8480 msg_scroll = FALSE;
8481 }
8482}
8483
8484/*
8485 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008486 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 * Returns TRUE if there is a valid screen to write to.
8488 * Returns FALSE when starting up and screen not initialized yet.
8489 */
8490 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008491screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008493 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 return (ScreenLines != NULL);
8495}
8496
8497/*
8498 * Resize the shell to Rows and Columns.
8499 * Allocate ScreenLines[] and associated items.
8500 *
8501 * There may be some time between setting Rows and Columns and (re)allocating
8502 * ScreenLines[]. This happens when starting up and when (manually) changing
8503 * the shell size. Always use screen_Rows and screen_Columns to access items
8504 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8505 * final size of the shell is needed.
8506 */
8507 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008508screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509{
8510 int new_row, old_row;
8511#ifdef FEAT_GUI
8512 int old_Rows;
8513#endif
8514 win_T *wp;
8515 int outofmem = FALSE;
8516 int len;
8517 schar_T *new_ScreenLines;
8518#ifdef FEAT_MBYTE
8519 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008520 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008522 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523#endif
8524 sattr_T *new_ScreenAttrs;
8525 unsigned *new_LineOffset;
8526 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008527#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008528 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008529 tabpage_T *tp;
8530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008532 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008533#ifdef FEAT_AUTOCMD
8534 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008536retry:
8537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 /*
8539 * Allocation of the screen buffers is done only when the size changes and
8540 * when Rows and Columns have been set and we have started doing full
8541 * screen stuff.
8542 */
8543 if ((ScreenLines != NULL
8544 && Rows == screen_Rows
8545 && Columns == screen_Columns
8546#ifdef FEAT_MBYTE
8547 && enc_utf8 == (ScreenLinesUC != NULL)
8548 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008549 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550#endif
8551 )
8552 || Rows == 0
8553 || Columns == 0
8554 || (!full_screen && ScreenLines == NULL))
8555 return;
8556
8557 /*
8558 * It's possible that we produce an out-of-memory message below, which
8559 * will cause this function to be called again. To break the loop, just
8560 * return here.
8561 */
8562 if (entered)
8563 return;
8564 entered = TRUE;
8565
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008566 /*
8567 * Note that the window sizes are updated before reallocating the arrays,
8568 * thus we must not redraw here!
8569 */
8570 ++RedrawingDisabled;
8571
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 win_new_shellsize(); /* fit the windows in the new sized shell */
8573
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 comp_col(); /* recompute columns for shown command and ruler */
8575
8576 /*
8577 * We're changing the size of the screen.
8578 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8579 * - Move lines from the old arrays into the new arrays, clear extra
8580 * lines (unless the screen is going to be cleared).
8581 * - Free the old arrays.
8582 *
8583 * If anything fails, make ScreenLines NULL, so we don't do anything!
8584 * Continuing with the old ScreenLines may result in a crash, because the
8585 * size is wrong.
8586 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008587 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008589#ifdef FEAT_AUTOCMD
8590 if (aucmd_win != NULL)
8591 win_free_lsize(aucmd_win);
8592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593
8594 new_ScreenLines = (schar_T *)lalloc((long_u)(
8595 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8596#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008597 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 if (enc_utf8)
8599 {
8600 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8601 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008602 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008603 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8605 }
8606 if (enc_dbcs == DBCS_JPNU)
8607 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8608 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8609#endif
8610 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8611 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8612 new_LineOffset = (unsigned *)lalloc((long_u)(
8613 Rows * sizeof(unsigned)), FALSE);
8614 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008615#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008616 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008619 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 {
8621 if (win_alloc_lines(wp) == FAIL)
8622 {
8623 outofmem = TRUE;
8624#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008625 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626#endif
8627 }
8628 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008629#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008630 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8631 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008632 outofmem = TRUE;
8633#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008634#ifdef FEAT_WINDOWS
8635give_up:
8636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008638#ifdef FEAT_MBYTE
8639 for (i = 0; i < p_mco; ++i)
8640 if (new_ScreenLinesC[i] == NULL)
8641 break;
8642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 if (new_ScreenLines == NULL
8644#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008645 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8647#endif
8648 || new_ScreenAttrs == NULL
8649 || new_LineOffset == NULL
8650 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008651#ifdef FEAT_WINDOWS
8652 || new_TabPageIdxs == NULL
8653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 || outofmem)
8655 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008656 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008657 {
8658 /* guess the size */
8659 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8660
8661 /* Remember we did this to avoid getting outofmem messages over
8662 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008663 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 vim_free(new_ScreenLines);
8666 new_ScreenLines = NULL;
8667#ifdef FEAT_MBYTE
8668 vim_free(new_ScreenLinesUC);
8669 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008670 for (i = 0; i < p_mco; ++i)
8671 {
8672 vim_free(new_ScreenLinesC[i]);
8673 new_ScreenLinesC[i] = NULL;
8674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 vim_free(new_ScreenLines2);
8676 new_ScreenLines2 = NULL;
8677#endif
8678 vim_free(new_ScreenAttrs);
8679 new_ScreenAttrs = NULL;
8680 vim_free(new_LineOffset);
8681 new_LineOffset = NULL;
8682 vim_free(new_LineWraps);
8683 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008684#ifdef FEAT_WINDOWS
8685 vim_free(new_TabPageIdxs);
8686 new_TabPageIdxs = NULL;
8687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 }
8689 else
8690 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008691 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008692
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 for (new_row = 0; new_row < Rows; ++new_row)
8694 {
8695 new_LineOffset[new_row] = new_row * Columns;
8696 new_LineWraps[new_row] = FALSE;
8697
8698 /*
8699 * If the screen is not going to be cleared, copy as much as
8700 * possible from the old screen to the new one and clear the rest
8701 * (used when resizing the window at the "--more--" prompt or when
8702 * executing an external command, for the GUI).
8703 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008704 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 {
8706 (void)vim_memset(new_ScreenLines + new_row * Columns,
8707 ' ', (size_t)Columns * sizeof(schar_T));
8708#ifdef FEAT_MBYTE
8709 if (enc_utf8)
8710 {
8711 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8712 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008713 for (i = 0; i < p_mco; ++i)
8714 (void)vim_memset(new_ScreenLinesC[i]
8715 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 0, (size_t)Columns * sizeof(u8char_T));
8717 }
8718 if (enc_dbcs == DBCS_JPNU)
8719 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8720 0, (size_t)Columns * sizeof(schar_T));
8721#endif
8722 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8723 0, (size_t)Columns * sizeof(sattr_T));
8724 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008725 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 {
8727 if (screen_Columns < Columns)
8728 len = screen_Columns;
8729 else
8730 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008731#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008732 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008733 * may be invalid now. Also when p_mco changes. */
8734 if (!(enc_utf8 && ScreenLinesUC == NULL)
8735 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008736#endif
8737 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8738 ScreenLines + LineOffset[old_row],
8739 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008741 if (enc_utf8 && ScreenLinesUC != NULL
8742 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {
8744 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8745 ScreenLinesUC + LineOffset[old_row],
8746 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008747 for (i = 0; i < p_mco; ++i)
8748 mch_memmove(new_ScreenLinesC[i]
8749 + new_LineOffset[new_row],
8750 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 (size_t)len * sizeof(u8char_T));
8752 }
8753 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8754 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8755 ScreenLines2 + LineOffset[old_row],
8756 (size_t)len * sizeof(schar_T));
8757#endif
8758 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8759 ScreenAttrs + LineOffset[old_row],
8760 (size_t)len * sizeof(sattr_T));
8761 }
8762 }
8763 }
8764 /* Use the last line of the screen for the current line. */
8765 current_ScreenLine = new_ScreenLines + Rows * Columns;
8766 }
8767
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008768 free_screenlines();
8769
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 ScreenLines = new_ScreenLines;
8771#ifdef FEAT_MBYTE
8772 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008773 for (i = 0; i < p_mco; ++i)
8774 ScreenLinesC[i] = new_ScreenLinesC[i];
8775 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 ScreenLines2 = new_ScreenLines2;
8777#endif
8778 ScreenAttrs = new_ScreenAttrs;
8779 LineOffset = new_LineOffset;
8780 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008781#ifdef FEAT_WINDOWS
8782 TabPageIdxs = new_TabPageIdxs;
8783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784
8785 /* It's important that screen_Rows and screen_Columns reflect the actual
8786 * size of ScreenLines[]. Set them before calling anything. */
8787#ifdef FEAT_GUI
8788 old_Rows = screen_Rows;
8789#endif
8790 screen_Rows = Rows;
8791 screen_Columns = Columns;
8792
8793 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008794 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 screenclear2();
8796
8797#ifdef FEAT_GUI
8798 else if (gui.in_use
8799 && !gui.starting
8800 && ScreenLines != NULL
8801 && old_Rows != Rows)
8802 {
8803 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8804 /*
8805 * Adjust the position of the cursor, for when executing an external
8806 * command.
8807 */
8808 if (msg_row >= Rows) /* Rows got smaller */
8809 msg_row = Rows - 1; /* put cursor at last row */
8810 else if (Rows > old_Rows) /* Rows got bigger */
8811 msg_row += Rows - old_Rows; /* put cursor in same place */
8812 if (msg_col >= Columns) /* Columns got smaller */
8813 msg_col = Columns - 1; /* put cursor at last column */
8814 }
8815#endif
8816
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008818 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008819
8820#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008821 /*
8822 * Do not apply autocommands more than 3 times to avoid an endless loop
8823 * in case applying autocommands always changes Rows or Columns.
8824 */
8825 if (starting == 0 && ++retry_count <= 3)
8826 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008827 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008828 /* In rare cases, autocommands may have altered Rows or Columns,
8829 * jump back to check if we need to allocate the screen again. */
8830 goto retry;
8831 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833}
8834
8835 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008836free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008837{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008838#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008839 int i;
8840
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008841 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008842 for (i = 0; i < Screen_mco; ++i)
8843 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008844 vim_free(ScreenLines2);
8845#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008846 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008847 vim_free(ScreenAttrs);
8848 vim_free(LineOffset);
8849 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008850#ifdef FEAT_WINDOWS
8851 vim_free(TabPageIdxs);
8852#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008853}
8854
8855 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008856screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857{
8858 check_for_delay(FALSE);
8859 screenalloc(FALSE); /* allocate screen buffers if size changed */
8860 screenclear2(); /* clear the screen */
8861}
8862
8863 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008864screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
8866 int i;
8867
8868 if (starting == NO_SCREEN || ScreenLines == NULL
8869#ifdef FEAT_GUI
8870 || (gui.in_use && gui.starting)
8871#endif
8872 )
8873 return;
8874
8875#ifdef FEAT_GUI
8876 if (!gui.in_use)
8877#endif
8878 screen_attr = -1; /* force setting the Normal colors */
8879 screen_stop_highlight(); /* don't want highlighting here */
8880
8881#ifdef FEAT_CLIPBOARD
8882 /* disable selection without redrawing it */
8883 clip_scroll_selection(9999);
8884#endif
8885
8886 /* blank out ScreenLines */
8887 for (i = 0; i < Rows; ++i)
8888 {
8889 lineclear(LineOffset[i], (int)Columns);
8890 LineWraps[i] = FALSE;
8891 }
8892
8893 if (can_clear(T_CL))
8894 {
8895 out_str(T_CL); /* clear the display */
8896 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008897 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 }
8899 else
8900 {
8901 /* can't clear the screen, mark all chars with invalid attributes */
8902 for (i = 0; i < Rows; ++i)
8903 lineinvalid(LineOffset[i], (int)Columns);
8904 clear_cmdline = TRUE;
8905 }
8906
8907 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8908
8909 win_rest_invalid(firstwin);
8910 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008911#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008912 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 if (must_redraw == CLEAR) /* no need to clear again */
8915 must_redraw = NOT_VALID;
8916 compute_cmdrow();
8917 msg_row = cmdline_row; /* put cursor on last line for messages */
8918 msg_col = 0;
8919 screen_start(); /* don't know where cursor is now */
8920 msg_scrolled = 0; /* can't scroll back */
8921 msg_didany = FALSE;
8922 msg_didout = FALSE;
8923}
8924
8925/*
8926 * Clear one line in ScreenLines.
8927 */
8928 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008929lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930{
8931 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8932#ifdef FEAT_MBYTE
8933 if (enc_utf8)
8934 (void)vim_memset(ScreenLinesUC + off, 0,
8935 (size_t)width * sizeof(u8char_T));
8936#endif
8937 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8938}
8939
8940/*
8941 * Mark one line in ScreenLines invalid by setting the attributes to an
8942 * invalid value.
8943 */
8944 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008945lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
8947 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8948}
8949
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008950#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951/*
8952 * Copy part of a Screenline for vertically split window "wp".
8953 */
8954 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008955linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956{
8957 unsigned off_to = LineOffset[to] + wp->w_wincol;
8958 unsigned off_from = LineOffset[from] + wp->w_wincol;
8959
8960 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8961 wp->w_width * sizeof(schar_T));
8962# ifdef FEAT_MBYTE
8963 if (enc_utf8)
8964 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008965 int i;
8966
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8968 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008969 for (i = 0; i < p_mco; ++i)
8970 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8971 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 }
8973 if (enc_dbcs == DBCS_JPNU)
8974 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8975 wp->w_width * sizeof(schar_T));
8976# endif
8977 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8978 wp->w_width * sizeof(sattr_T));
8979}
8980#endif
8981
8982/*
8983 * Return TRUE if clearing with term string "p" would work.
8984 * It can't work when the string is empty or it won't set the right background.
8985 */
8986 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008987can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
8989 return (*p != NUL && (t_colors <= 1
8990#ifdef FEAT_GUI
8991 || gui.in_use
8992#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008993#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008994 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008995 || (!p_tgc && cterm_normal_bg_color == 0)
8996#else
8997 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008998#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02008999 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000}
9001
9002/*
9003 * Reset cursor position. Use whenever cursor was moved because of outputting
9004 * something directly to the screen (shell commands) or a terminal control
9005 * code.
9006 */
9007 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009008screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010 screen_cur_row = screen_cur_col = 9999;
9011}
9012
9013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 * Move the cursor to position "row","col" in the screen.
9015 * This tries to find the most efficient way to move, minimizing the number of
9016 * characters sent to the terminal.
9017 */
9018 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009019windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009021 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 int i;
9023 int plan;
9024 int cost;
9025 int wouldbe_col;
9026 int noinvcurs;
9027 char_u *bs;
9028 int goto_cost;
9029 int attr;
9030
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009031#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9033
9034#define PLAN_LE 1
9035#define PLAN_CR 2
9036#define PLAN_NL 3
9037#define PLAN_WRITE 4
9038 /* Can't use ScreenLines unless initialized */
9039 if (ScreenLines == NULL)
9040 return;
9041
9042 if (col != screen_cur_col || row != screen_cur_row)
9043 {
9044 /* Check for valid position. */
9045 if (row < 0) /* window without text lines? */
9046 row = 0;
9047 if (row >= screen_Rows)
9048 row = screen_Rows - 1;
9049 if (col >= screen_Columns)
9050 col = screen_Columns - 1;
9051
9052 /* check if no cursor movement is allowed in highlight mode */
9053 if (screen_attr && *T_MS == NUL)
9054 noinvcurs = HIGHL_COST;
9055 else
9056 noinvcurs = 0;
9057 goto_cost = GOTO_COST + noinvcurs;
9058
9059 /*
9060 * Plan how to do the positioning:
9061 * 1. Use CR to move it to column 0, same row.
9062 * 2. Use T_LE to move it a few columns to the left.
9063 * 3. Use NL to move a few lines down, column 0.
9064 * 4. Move a few columns to the right with T_ND or by writing chars.
9065 *
9066 * Don't do this if the cursor went beyond the last column, the cursor
9067 * position is unknown then (some terminals wrap, some don't )
9068 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009069 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 * characters to move the cursor to the right.
9071 */
9072 if (row >= screen_cur_row && screen_cur_col < Columns)
9073 {
9074 /*
9075 * If the cursor is in the same row, bigger col, we can use CR
9076 * or T_LE.
9077 */
9078 bs = NULL; /* init for GCC */
9079 attr = screen_attr;
9080 if (row == screen_cur_row && col < screen_cur_col)
9081 {
9082 /* "le" is preferred over "bc", because "bc" is obsolete */
9083 if (*T_LE)
9084 bs = T_LE; /* "cursor left" */
9085 else
9086 bs = T_BC; /* "backspace character (old) */
9087 if (*bs)
9088 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9089 else
9090 cost = 999;
9091 if (col + 1 < cost) /* using CR is less characters */
9092 {
9093 plan = PLAN_CR;
9094 wouldbe_col = 0;
9095 cost = 1; /* CR is just one character */
9096 }
9097 else
9098 {
9099 plan = PLAN_LE;
9100 wouldbe_col = col;
9101 }
9102 if (noinvcurs) /* will stop highlighting */
9103 {
9104 cost += noinvcurs;
9105 attr = 0;
9106 }
9107 }
9108
9109 /*
9110 * If the cursor is above where we want to be, we can use CR LF.
9111 */
9112 else if (row > screen_cur_row)
9113 {
9114 plan = PLAN_NL;
9115 wouldbe_col = 0;
9116 cost = (row - screen_cur_row) * 2; /* CR LF */
9117 if (noinvcurs) /* will stop highlighting */
9118 {
9119 cost += noinvcurs;
9120 attr = 0;
9121 }
9122 }
9123
9124 /*
9125 * If the cursor is in the same row, smaller col, just use write.
9126 */
9127 else
9128 {
9129 plan = PLAN_WRITE;
9130 wouldbe_col = screen_cur_col;
9131 cost = 0;
9132 }
9133
9134 /*
9135 * Check if any characters that need to be written have the
9136 * correct attributes. Also avoid UTF-8 characters.
9137 */
9138 i = col - wouldbe_col;
9139 if (i > 0)
9140 cost += i;
9141 if (cost < goto_cost && i > 0)
9142 {
9143 /*
9144 * Check if the attributes are correct without additionally
9145 * stopping highlighting.
9146 */
9147 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9148 while (i && *p++ == attr)
9149 --i;
9150 if (i != 0)
9151 {
9152 /*
9153 * Try if it works when highlighting is stopped here.
9154 */
9155 if (*--p == 0)
9156 {
9157 cost += noinvcurs;
9158 while (i && *p++ == 0)
9159 --i;
9160 }
9161 if (i != 0)
9162 cost = 999; /* different attributes, don't do it */
9163 }
9164#ifdef FEAT_MBYTE
9165 if (enc_utf8)
9166 {
9167 /* Don't use an UTF-8 char for positioning, it's slow. */
9168 for (i = wouldbe_col; i < col; ++i)
9169 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9170 {
9171 cost = 999;
9172 break;
9173 }
9174 }
9175#endif
9176 }
9177
9178 /*
9179 * We can do it without term_windgoto()!
9180 */
9181 if (cost < goto_cost)
9182 {
9183 if (plan == PLAN_LE)
9184 {
9185 if (noinvcurs)
9186 screen_stop_highlight();
9187 while (screen_cur_col > col)
9188 {
9189 out_str(bs);
9190 --screen_cur_col;
9191 }
9192 }
9193 else if (plan == PLAN_CR)
9194 {
9195 if (noinvcurs)
9196 screen_stop_highlight();
9197 out_char('\r');
9198 screen_cur_col = 0;
9199 }
9200 else if (plan == PLAN_NL)
9201 {
9202 if (noinvcurs)
9203 screen_stop_highlight();
9204 while (screen_cur_row < row)
9205 {
9206 out_char('\n');
9207 ++screen_cur_row;
9208 }
9209 screen_cur_col = 0;
9210 }
9211
9212 i = col - screen_cur_col;
9213 if (i > 0)
9214 {
9215 /*
9216 * Use cursor-right if it's one character only. Avoids
9217 * removing a line of pixels from the last bold char, when
9218 * using the bold trick in the GUI.
9219 */
9220 if (T_ND[0] != NUL && T_ND[1] == NUL)
9221 {
9222 while (i-- > 0)
9223 out_char(*T_ND);
9224 }
9225 else
9226 {
9227 int off;
9228
9229 off = LineOffset[row] + screen_cur_col;
9230 while (i-- > 0)
9231 {
9232 if (ScreenAttrs[off] != screen_attr)
9233 screen_stop_highlight();
9234#ifdef FEAT_MBYTE
9235 out_flush_check();
9236#endif
9237 out_char(ScreenLines[off]);
9238#ifdef FEAT_MBYTE
9239 if (enc_dbcs == DBCS_JPNU
9240 && ScreenLines[off] == 0x8e)
9241 out_char(ScreenLines2[off]);
9242#endif
9243 ++off;
9244 }
9245 }
9246 }
9247 }
9248 }
9249 else
9250 cost = 999;
9251
9252 if (cost >= goto_cost)
9253 {
9254 if (noinvcurs)
9255 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009256 if (row == screen_cur_row && (col > screen_cur_col)
9257 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 term_cursor_right(col - screen_cur_col);
9259 else
9260 term_windgoto(row, col);
9261 }
9262 screen_cur_row = row;
9263 screen_cur_col = col;
9264 }
9265}
9266
9267/*
9268 * Set cursor to its position in the current window.
9269 */
9270 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009271setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272{
9273 if (redrawing())
9274 {
9275 validate_cursor();
9276 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9277 W_WINCOL(curwin) + (
9278#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009279 /* With 'rightleft' set and the cursor on a double-wide
9280 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9282# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009283 (has_mbyte
9284 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9285 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286# endif
9287 1)) :
9288#endif
9289 curwin->w_wcol));
9290 }
9291}
9292
9293
9294/*
9295 * insert 'line_count' lines at 'row' in window 'wp'
9296 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9297 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9298 * scrolling.
9299 * Returns FAIL if the lines are not inserted, OK for success.
9300 */
9301 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009302win_ins_lines(
9303 win_T *wp,
9304 int row,
9305 int line_count,
9306 int invalid,
9307 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308{
9309 int did_delete;
9310 int nextrow;
9311 int lastrow;
9312 int retval;
9313
9314 if (invalid)
9315 wp->w_lines_valid = 0;
9316
9317 if (wp->w_height < 5)
9318 return FAIL;
9319
9320 if (line_count > wp->w_height - row)
9321 line_count = wp->w_height - row;
9322
9323 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9324 if (retval != MAYBE)
9325 return retval;
9326
9327 /*
9328 * If there is a next window or a status line, we first try to delete the
9329 * lines at the bottom to avoid messing what is after the window.
9330 * If this fails and there are following windows, don't do anything to avoid
9331 * messing up those windows, better just redraw.
9332 */
9333 did_delete = FALSE;
9334#ifdef FEAT_WINDOWS
9335 if (wp->w_next != NULL || wp->w_status_height)
9336 {
9337 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9338 line_count, (int)Rows, FALSE, NULL) == OK)
9339 did_delete = TRUE;
9340 else if (wp->w_next)
9341 return FAIL;
9342 }
9343#endif
9344 /*
9345 * if no lines deleted, blank the lines that will end up below the window
9346 */
9347 if (!did_delete)
9348 {
9349#ifdef FEAT_WINDOWS
9350 wp->w_redr_status = TRUE;
9351#endif
9352 redraw_cmdline = TRUE;
9353 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9354 lastrow = nextrow + line_count;
9355 if (lastrow > Rows)
9356 lastrow = Rows;
9357 screen_fill(nextrow - line_count, lastrow - line_count,
9358 W_WINCOL(wp), (int)W_ENDCOL(wp),
9359 ' ', ' ', 0);
9360 }
9361
9362 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9363 == FAIL)
9364 {
9365 /* deletion will have messed up other windows */
9366 if (did_delete)
9367 {
9368#ifdef FEAT_WINDOWS
9369 wp->w_redr_status = TRUE;
9370#endif
9371 win_rest_invalid(W_NEXT(wp));
9372 }
9373 return FAIL;
9374 }
9375
9376 return OK;
9377}
9378
9379/*
9380 * delete "line_count" window lines at "row" in window "wp"
9381 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9382 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9383 * scrolling
9384 * Return OK for success, FAIL if the lines are not deleted.
9385 */
9386 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009387win_del_lines(
9388 win_T *wp,
9389 int row,
9390 int line_count,
9391 int invalid,
9392 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
9394 int retval;
9395
9396 if (invalid)
9397 wp->w_lines_valid = 0;
9398
9399 if (line_count > wp->w_height - row)
9400 line_count = wp->w_height - row;
9401
9402 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9403 if (retval != MAYBE)
9404 return retval;
9405
9406 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9407 (int)Rows, FALSE, NULL) == FAIL)
9408 return FAIL;
9409
9410#ifdef FEAT_WINDOWS
9411 /*
9412 * If there are windows or status lines below, try to put them at the
9413 * correct place. If we can't do that, they have to be redrawn.
9414 */
9415 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9416 {
9417 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9418 line_count, (int)Rows, NULL) == FAIL)
9419 {
9420 wp->w_redr_status = TRUE;
9421 win_rest_invalid(wp->w_next);
9422 }
9423 }
9424 /*
9425 * If this is the last window and there is no status line, redraw the
9426 * command line later.
9427 */
9428 else
9429#endif
9430 redraw_cmdline = TRUE;
9431 return OK;
9432}
9433
9434/*
9435 * Common code for win_ins_lines() and win_del_lines().
9436 * Returns OK or FAIL when the work has been done.
9437 * Returns MAYBE when not finished yet.
9438 */
9439 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009440win_do_lines(
9441 win_T *wp,
9442 int row,
9443 int line_count,
9444 int mayclear,
9445 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
9447 int retval;
9448
9449 if (!redrawing() || line_count <= 0)
9450 return FAIL;
9451
9452 /* only a few lines left: redraw is faster */
9453 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009454#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 && wp->w_width == Columns
9456#endif
9457 )
9458 {
9459 screenclear(); /* will set wp->w_lines_valid to 0 */
9460 return FAIL;
9461 }
9462
9463 /*
9464 * Delete all remaining lines
9465 */
9466 if (row + line_count >= wp->w_height)
9467 {
9468 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9469 W_WINCOL(wp), (int)W_ENDCOL(wp),
9470 ' ', ' ', 0);
9471 return OK;
9472 }
9473
9474 /*
9475 * when scrolling, the message on the command line should be cleared,
9476 * otherwise it will stay there forever.
9477 */
9478 clear_cmdline = TRUE;
9479
9480 /*
9481 * If the terminal can set a scroll region, use that.
9482 * Always do this in a vertically split window. This will redraw from
9483 * ScreenLines[] when t_CV isn't defined. That's faster than using
9484 * win_line().
9485 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009486 * a character in the lower right corner of the scroll region may cause a
9487 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 */
9489 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009490#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 || W_WIDTH(wp) != Columns
9492#endif
9493 )
9494 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009495#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9497#endif
9498 scroll_region_set(wp, row);
9499 if (del)
9500 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9501 wp->w_height - row, FALSE, wp);
9502 else
9503 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9504 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009505#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9507#endif
9508 scroll_region_reset();
9509 return retval;
9510 }
9511
9512#ifdef FEAT_WINDOWS
9513 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9514 return FAIL;
9515#endif
9516
9517 return MAYBE;
9518}
9519
9520/*
9521 * window 'wp' and everything after it is messed up, mark it for redraw
9522 */
9523 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009524win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525{
9526#ifdef FEAT_WINDOWS
9527 while (wp != NULL)
9528#else
9529 if (wp != NULL)
9530#endif
9531 {
9532 redraw_win_later(wp, NOT_VALID);
9533#ifdef FEAT_WINDOWS
9534 wp->w_redr_status = TRUE;
9535 wp = wp->w_next;
9536#endif
9537 }
9538 redraw_cmdline = TRUE;
9539}
9540
9541/*
9542 * The rest of the routines in this file perform screen manipulations. The
9543 * given operation is performed physically on the screen. The corresponding
9544 * change is also made to the internal screen image. In this way, the editor
9545 * anticipates the effect of editing changes on the appearance of the screen.
9546 * That way, when we call screenupdate a complete redraw isn't usually
9547 * necessary. Another advantage is that we can keep adding code to anticipate
9548 * screen changes, and in the meantime, everything still works.
9549 */
9550
9551/*
9552 * types for inserting or deleting lines
9553 */
9554#define USE_T_CAL 1
9555#define USE_T_CDL 2
9556#define USE_T_AL 3
9557#define USE_T_CE 4
9558#define USE_T_DL 5
9559#define USE_T_SR 6
9560#define USE_NL 7
9561#define USE_T_CD 8
9562#define USE_REDRAW 9
9563
9564/*
9565 * insert lines on the screen and update ScreenLines[]
9566 * 'end' is the line after the scrolled part. Normally it is Rows.
9567 * When scrolling region used 'off' is the offset from the top for the region.
9568 * 'row' and 'end' are relative to the start of the region.
9569 *
9570 * return FAIL for failure, OK for success.
9571 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009572 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009573screen_ins_lines(
9574 int off,
9575 int row,
9576 int line_count,
9577 int end,
9578 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
9580 int i;
9581 int j;
9582 unsigned temp;
9583 int cursor_row;
9584 int type;
9585 int result_empty;
9586 int can_ce = can_clear(T_CE);
9587
9588 /*
9589 * FAIL if
9590 * - there is no valid screen
9591 * - the screen has to be redrawn completely
9592 * - the line count is less than one
9593 * - the line count is more than 'ttyscroll'
9594 */
9595 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9596 return FAIL;
9597
9598 /*
9599 * There are seven ways to insert lines:
9600 * 0. When in a vertically split window and t_CV isn't set, redraw the
9601 * characters from ScreenLines[].
9602 * 1. Use T_CD (clear to end of display) if it exists and the result of
9603 * the insert is just empty lines
9604 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9605 * present or line_count > 1. It looks better if we do all the inserts
9606 * at once.
9607 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9608 * insert is just empty lines and T_CE is not present or line_count >
9609 * 1.
9610 * 4. Use T_AL (insert line) if it exists.
9611 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9612 * just empty lines.
9613 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9614 * just empty lines.
9615 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9616 * the 'da' flag is not set or we have clear line capability.
9617 * 8. redraw the characters from ScreenLines[].
9618 *
9619 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9620 * the scrollbar for the window. It does have insert line, use that if it
9621 * exists.
9622 */
9623 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009624#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9626 type = USE_REDRAW;
9627 else
9628#endif
9629 if (can_clear(T_CD) && result_empty)
9630 type = USE_T_CD;
9631 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9632 type = USE_T_CAL;
9633 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9634 type = USE_T_CDL;
9635 else if (*T_AL != NUL)
9636 type = USE_T_AL;
9637 else if (can_ce && result_empty)
9638 type = USE_T_CE;
9639 else if (*T_DL != NUL && result_empty)
9640 type = USE_T_DL;
9641 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9642 type = USE_T_SR;
9643 else
9644 return FAIL;
9645
9646 /*
9647 * For clearing the lines screen_del_lines() is used. This will also take
9648 * care of t_db if necessary.
9649 */
9650 if (type == USE_T_CD || type == USE_T_CDL ||
9651 type == USE_T_CE || type == USE_T_DL)
9652 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9653
9654 /*
9655 * If text is retained below the screen, first clear or delete as many
9656 * lines at the bottom of the window as are about to be inserted so that
9657 * the deleted lines won't later surface during a screen_del_lines.
9658 */
9659 if (*T_DB)
9660 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9661
9662#ifdef FEAT_CLIPBOARD
9663 /* Remove a modeless selection when inserting lines halfway the screen
9664 * or not the full width of the screen. */
9665 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009666# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 || (wp != NULL && wp->w_width != Columns)
9668# endif
9669 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009670 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 else
9672 clip_scroll_selection(-line_count);
9673#endif
9674
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675#ifdef FEAT_GUI
9676 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9677 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009678 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679#endif
9680
9681 if (*T_CCS != NUL) /* cursor relative to region */
9682 cursor_row = row;
9683 else
9684 cursor_row = row + off;
9685
9686 /*
9687 * Shift LineOffset[] line_count down to reflect the inserted lines.
9688 * Clear the inserted lines in ScreenLines[].
9689 */
9690 row += off;
9691 end += off;
9692 for (i = 0; i < line_count; ++i)
9693 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009694#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 if (wp != NULL && wp->w_width != Columns)
9696 {
9697 /* need to copy part of a line */
9698 j = end - 1 - i;
9699 while ((j -= line_count) >= row)
9700 linecopy(j + line_count, j, wp);
9701 j += line_count;
9702 if (can_clear((char_u *)" "))
9703 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9704 else
9705 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9706 LineWraps[j] = FALSE;
9707 }
9708 else
9709#endif
9710 {
9711 j = end - 1 - i;
9712 temp = LineOffset[j];
9713 while ((j -= line_count) >= row)
9714 {
9715 LineOffset[j + line_count] = LineOffset[j];
9716 LineWraps[j + line_count] = LineWraps[j];
9717 }
9718 LineOffset[j + line_count] = temp;
9719 LineWraps[j + line_count] = FALSE;
9720 if (can_clear((char_u *)" "))
9721 lineclear(temp, (int)Columns);
9722 else
9723 lineinvalid(temp, (int)Columns);
9724 }
9725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726
9727 screen_stop_highlight();
9728 windgoto(cursor_row, 0);
9729
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009730#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 /* redraw the characters */
9732 if (type == USE_REDRAW)
9733 redraw_block(row, end, wp);
9734 else
9735#endif
9736 if (type == USE_T_CAL)
9737 {
9738 term_append_lines(line_count);
9739 screen_start(); /* don't know where cursor is now */
9740 }
9741 else
9742 {
9743 for (i = 0; i < line_count; i++)
9744 {
9745 if (type == USE_T_AL)
9746 {
9747 if (i && cursor_row != 0)
9748 windgoto(cursor_row, 0);
9749 out_str(T_AL);
9750 }
9751 else /* type == USE_T_SR */
9752 out_str(T_SR);
9753 screen_start(); /* don't know where cursor is now */
9754 }
9755 }
9756
9757 /*
9758 * With scroll-reverse and 'da' flag set we need to clear the lines that
9759 * have been scrolled down into the region.
9760 */
9761 if (type == USE_T_SR && *T_DA)
9762 {
9763 for (i = 0; i < line_count; ++i)
9764 {
9765 windgoto(off + i, 0);
9766 out_str(T_CE);
9767 screen_start(); /* don't know where cursor is now */
9768 }
9769 }
9770
9771#ifdef FEAT_GUI
9772 gui_can_update_cursor();
9773 if (gui.in_use)
9774 out_flush(); /* always flush after a scroll */
9775#endif
9776 return OK;
9777}
9778
9779/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009780 * Delete lines on the screen and update ScreenLines[].
9781 * "end" is the line after the scrolled part. Normally it is Rows.
9782 * When scrolling region used "off" is the offset from the top for the region.
9783 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 *
9785 * Return OK for success, FAIL if the lines are not deleted.
9786 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009788screen_del_lines(
9789 int off,
9790 int row,
9791 int line_count,
9792 int end,
9793 int force, /* even when line_count > p_ttyscroll */
9794 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795{
9796 int j;
9797 int i;
9798 unsigned temp;
9799 int cursor_row;
9800 int cursor_end;
9801 int result_empty; /* result is empty until end of region */
9802 int can_delete; /* deleting line codes can be used */
9803 int type;
9804
9805 /*
9806 * FAIL if
9807 * - there is no valid screen
9808 * - the screen has to be redrawn completely
9809 * - the line count is less than one
9810 * - the line count is more than 'ttyscroll'
9811 */
9812 if (!screen_valid(TRUE) || line_count <= 0 ||
9813 (!force && line_count > p_ttyscroll))
9814 return FAIL;
9815
9816 /*
9817 * Check if the rest of the current region will become empty.
9818 */
9819 result_empty = row + line_count >= end;
9820
9821 /*
9822 * We can delete lines only when 'db' flag not set or when 'ce' option
9823 * available.
9824 */
9825 can_delete = (*T_DB == NUL || can_clear(T_CE));
9826
9827 /*
9828 * There are six ways to delete lines:
9829 * 0. When in a vertically split window and t_CV isn't set, redraw the
9830 * characters from ScreenLines[].
9831 * 1. Use T_CD if it exists and the result is empty.
9832 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9833 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9834 * none of the other ways work.
9835 * 4. Use T_CE (erase line) if the result is empty.
9836 * 5. Use T_DL (delete line) if it exists.
9837 * 6. redraw the characters from ScreenLines[].
9838 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009839#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9841 type = USE_REDRAW;
9842 else
9843#endif
9844 if (can_clear(T_CD) && result_empty)
9845 type = USE_T_CD;
9846#if defined(__BEOS__) && defined(BEOS_DR8)
9847 /*
9848 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9849 * its internal termcap... this works okay for tests which test *T_DB !=
9850 * NUL. It has the disadvantage that the user cannot use any :set t_*
9851 * command to get T_DB (back) to empty_option, only :set term=... will do
9852 * the trick...
9853 * Anyway, this hack will hopefully go away with the next OS release.
9854 * (Olaf Seibert)
9855 */
9856 else if (row == 0 && T_DB == empty_option
9857 && (line_count == 1 || *T_CDL == NUL))
9858#else
9859 else if (row == 0 && (
9860#ifndef AMIGA
9861 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9862 * up, so use delete-line command */
9863 line_count == 1 ||
9864#endif
9865 *T_CDL == NUL))
9866#endif
9867 type = USE_NL;
9868 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9869 type = USE_T_CDL;
9870 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009871#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 && (wp == NULL || wp->w_width == Columns)
9873#endif
9874 )
9875 type = USE_T_CE;
9876 else if (*T_DL != NUL && can_delete)
9877 type = USE_T_DL;
9878 else if (*T_CDL != NUL && can_delete)
9879 type = USE_T_CDL;
9880 else
9881 return FAIL;
9882
9883#ifdef FEAT_CLIPBOARD
9884 /* Remove a modeless selection when deleting lines halfway the screen or
9885 * not the full width of the screen. */
9886 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009887# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 || (wp != NULL && wp->w_width != Columns)
9889# endif
9890 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009891 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 else
9893 clip_scroll_selection(line_count);
9894#endif
9895
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896#ifdef FEAT_GUI
9897 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9898 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009899 gui_dont_update_cursor(gui.cursor_row >= row + off
9900 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901#endif
9902
9903 if (*T_CCS != NUL) /* cursor relative to region */
9904 {
9905 cursor_row = row;
9906 cursor_end = end;
9907 }
9908 else
9909 {
9910 cursor_row = row + off;
9911 cursor_end = end + off;
9912 }
9913
9914 /*
9915 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9916 * Clear the inserted lines in ScreenLines[].
9917 */
9918 row += off;
9919 end += off;
9920 for (i = 0; i < line_count; ++i)
9921 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009922#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 if (wp != NULL && wp->w_width != Columns)
9924 {
9925 /* need to copy part of a line */
9926 j = row + i;
9927 while ((j += line_count) <= end - 1)
9928 linecopy(j - line_count, j, wp);
9929 j -= line_count;
9930 if (can_clear((char_u *)" "))
9931 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9932 else
9933 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9934 LineWraps[j] = FALSE;
9935 }
9936 else
9937#endif
9938 {
9939 /* whole width, moving the line pointers is faster */
9940 j = row + i;
9941 temp = LineOffset[j];
9942 while ((j += line_count) <= end - 1)
9943 {
9944 LineOffset[j - line_count] = LineOffset[j];
9945 LineWraps[j - line_count] = LineWraps[j];
9946 }
9947 LineOffset[j - line_count] = temp;
9948 LineWraps[j - line_count] = FALSE;
9949 if (can_clear((char_u *)" "))
9950 lineclear(temp, (int)Columns);
9951 else
9952 lineinvalid(temp, (int)Columns);
9953 }
9954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955
9956 screen_stop_highlight();
9957
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009958#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 /* redraw the characters */
9960 if (type == USE_REDRAW)
9961 redraw_block(row, end, wp);
9962 else
9963#endif
9964 if (type == USE_T_CD) /* delete the lines */
9965 {
9966 windgoto(cursor_row, 0);
9967 out_str(T_CD);
9968 screen_start(); /* don't know where cursor is now */
9969 }
9970 else if (type == USE_T_CDL)
9971 {
9972 windgoto(cursor_row, 0);
9973 term_delete_lines(line_count);
9974 screen_start(); /* don't know where cursor is now */
9975 }
9976 /*
9977 * Deleting lines at top of the screen or scroll region: Just scroll
9978 * the whole screen (scroll region) up by outputting newlines on the
9979 * last line.
9980 */
9981 else if (type == USE_NL)
9982 {
9983 windgoto(cursor_end - 1, 0);
9984 for (i = line_count; --i >= 0; )
9985 out_char('\n'); /* cursor will remain on same line */
9986 }
9987 else
9988 {
9989 for (i = line_count; --i >= 0; )
9990 {
9991 if (type == USE_T_DL)
9992 {
9993 windgoto(cursor_row, 0);
9994 out_str(T_DL); /* delete a line */
9995 }
9996 else /* type == USE_T_CE */
9997 {
9998 windgoto(cursor_row + i, 0);
9999 out_str(T_CE); /* erase a line */
10000 }
10001 screen_start(); /* don't know where cursor is now */
10002 }
10003 }
10004
10005 /*
10006 * If the 'db' flag is set, we need to clear the lines that have been
10007 * scrolled up at the bottom of the region.
10008 */
10009 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10010 {
10011 for (i = line_count; i > 0; --i)
10012 {
10013 windgoto(cursor_end - i, 0);
10014 out_str(T_CE); /* erase a line */
10015 screen_start(); /* don't know where cursor is now */
10016 }
10017 }
10018
10019#ifdef FEAT_GUI
10020 gui_can_update_cursor();
10021 if (gui.in_use)
10022 out_flush(); /* always flush after a scroll */
10023#endif
10024
10025 return OK;
10026}
10027
10028/*
10029 * show the current mode and ruler
10030 *
10031 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10032 * If clear_cmdline is FALSE there may be a message there that needs to be
10033 * cleared only if a mode is shown.
10034 * Return the length of the message (0 if no message).
10035 */
10036 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010037showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038{
10039 int need_clear;
10040 int length = 0;
10041 int do_mode;
10042 int attr;
10043 int nwr_save;
10044#ifdef FEAT_INS_EXPAND
10045 int sub_attr;
10046#endif
10047
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010048 do_mode = ((p_smd && msg_silent == 0)
10049 && ((State & INSERT)
10050 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010051 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 if (do_mode || Recording)
10053 {
10054 /*
10055 * Don't show mode right now, when not redrawing or inside a mapping.
10056 * Call char_avail() only when we are going to show something, because
10057 * it takes a bit of time.
10058 */
10059 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10060 {
10061 redraw_cmdline = TRUE; /* show mode later */
10062 return 0;
10063 }
10064
10065 nwr_save = need_wait_return;
10066
10067 /* wait a bit before overwriting an important message */
10068 check_for_delay(FALSE);
10069
10070 /* if the cmdline is more than one line high, erase top lines */
10071 need_clear = clear_cmdline;
10072 if (clear_cmdline && cmdline_row < Rows - 1)
10073 msg_clr_cmdline(); /* will reset clear_cmdline */
10074
10075 /* Position on the last line in the window, column 0 */
10076 msg_pos_mode();
10077 cursor_off();
10078 attr = hl_attr(HLF_CM); /* Highlight mode */
10079 if (do_mode)
10080 {
10081 MSG_PUTS_ATTR("--", attr);
10082#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010083 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010084# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010085 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010086# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010087 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010088# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010089 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010090# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 MSG_PUTS_ATTR(" IM", attr);
10092# else
10093 MSG_PUTS_ATTR(" XIM", attr);
10094# endif
10095#endif
10096#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10097 if (gui.in_use)
10098 {
10099 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010100 {
10101 /* HANGUL */
10102 if (enc_utf8)
10103 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10104 else
10105 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 }
10108#endif
10109#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010110 /* CTRL-X in Insert mode */
10111 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 {
10113 /* These messages can get long, avoid a wrap in a narrow
10114 * window. Prefer showing edit_submode_extra. */
10115 length = (Rows - msg_row) * Columns - 3;
10116 if (edit_submode_extra != NULL)
10117 length -= vim_strsize(edit_submode_extra);
10118 if (length > 0)
10119 {
10120 if (edit_submode_pre != NULL)
10121 length -= vim_strsize(edit_submode_pre);
10122 if (length - vim_strsize(edit_submode) > 0)
10123 {
10124 if (edit_submode_pre != NULL)
10125 msg_puts_attr(edit_submode_pre, attr);
10126 msg_puts_attr(edit_submode, attr);
10127 }
10128 if (edit_submode_extra != NULL)
10129 {
10130 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10131 if ((int)edit_submode_highl < (int)HLF_COUNT)
10132 sub_attr = hl_attr(edit_submode_highl);
10133 else
10134 sub_attr = attr;
10135 msg_puts_attr(edit_submode_extra, sub_attr);
10136 }
10137 }
10138 length = 0;
10139 }
10140 else
10141#endif
10142 {
10143#ifdef FEAT_VREPLACE
10144 if (State & VREPLACE_FLAG)
10145 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10146 else
10147#endif
10148 if (State & REPLACE_FLAG)
10149 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10150 else if (State & INSERT)
10151 {
10152#ifdef FEAT_RIGHTLEFT
10153 if (p_ri)
10154 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10155#endif
10156 MSG_PUTS_ATTR(_(" INSERT"), attr);
10157 }
10158 else if (restart_edit == 'I')
10159 MSG_PUTS_ATTR(_(" (insert)"), attr);
10160 else if (restart_edit == 'R')
10161 MSG_PUTS_ATTR(_(" (replace)"), attr);
10162 else if (restart_edit == 'V')
10163 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10164#ifdef FEAT_RIGHTLEFT
10165 if (p_hkmap)
10166 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10167# ifdef FEAT_FKMAP
10168 if (p_fkmap)
10169 MSG_PUTS_ATTR(farsi_text_5, attr);
10170# endif
10171#endif
10172#ifdef FEAT_KEYMAP
10173 if (State & LANGMAP)
10174 {
10175# ifdef FEAT_ARABIC
10176 if (curwin->w_p_arab)
10177 MSG_PUTS_ATTR(_(" Arabic"), attr);
10178 else
10179# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010180 if (get_keymap_str(curwin, (char_u *)" (%s)",
10181 NameBuff, MAXPATHL))
10182 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 }
10184#endif
10185 if ((State & INSERT) && p_paste)
10186 MSG_PUTS_ATTR(_(" (paste)"), attr);
10187
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 if (VIsual_active)
10189 {
10190 char *p;
10191
10192 /* Don't concatenate separate words to avoid translation
10193 * problems. */
10194 switch ((VIsual_select ? 4 : 0)
10195 + (VIsual_mode == Ctrl_V) * 2
10196 + (VIsual_mode == 'V'))
10197 {
10198 case 0: p = N_(" VISUAL"); break;
10199 case 1: p = N_(" VISUAL LINE"); break;
10200 case 2: p = N_(" VISUAL BLOCK"); break;
10201 case 4: p = N_(" SELECT"); break;
10202 case 5: p = N_(" SELECT LINE"); break;
10203 default: p = N_(" SELECT BLOCK"); break;
10204 }
10205 MSG_PUTS_ATTR(_(p), attr);
10206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 MSG_PUTS_ATTR(" --", attr);
10208 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010209
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 need_clear = TRUE;
10211 }
10212 if (Recording
10213#ifdef FEAT_INS_EXPAND
10214 && edit_submode == NULL /* otherwise it gets too long */
10215#endif
10216 )
10217 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010218 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 need_clear = TRUE;
10220 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010221
10222 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 if (need_clear || clear_cmdline)
10224 msg_clr_eos();
10225 msg_didout = FALSE; /* overwrite this message */
10226 length = msg_col;
10227 msg_col = 0;
10228 need_wait_return = nwr_save; /* never ask for hit-return for this */
10229 }
10230 else if (clear_cmdline && msg_silent == 0)
10231 /* Clear the whole command line. Will reset "clear_cmdline". */
10232 msg_clr_cmdline();
10233
10234#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 /* In Visual mode the size of the selected area must be redrawn. */
10236 if (VIsual_active)
10237 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238
10239 /* If the last window has no status line, the ruler is after the mode
10240 * message and must be redrawn */
10241 if (redrawing()
10242# ifdef FEAT_WINDOWS
10243 && lastwin->w_status_height == 0
10244# endif
10245 )
10246 win_redr_ruler(lastwin, TRUE);
10247#endif
10248 redraw_cmdline = FALSE;
10249 clear_cmdline = FALSE;
10250
10251 return length;
10252}
10253
10254/*
10255 * Position for a mode message.
10256 */
10257 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010258msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259{
10260 msg_col = 0;
10261 msg_row = Rows - 1;
10262}
10263
10264/*
10265 * Delete mode message. Used when ESC is typed which is expected to end
10266 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010267 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 */
10269 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010270unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271{
10272 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010273 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 */
10275 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10276 redraw_cmdline = TRUE; /* delete mode later */
10277 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010278 clearmode();
10279}
10280
10281/*
10282 * Clear the mode message.
10283 */
10284 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010285clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010286{
10287 msg_pos_mode();
10288 if (Recording)
10289 recording_mode(hl_attr(HLF_CM));
10290 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291}
10292
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010293 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010294recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010295{
10296 MSG_PUTS_ATTR(_("recording"), attr);
10297 if (!shortmess(SHM_RECORDING))
10298 {
10299 char_u s[4];
10300 sprintf((char *)s, " @%c", Recording);
10301 MSG_PUTS_ATTR(s, attr);
10302 }
10303}
10304
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010305#if defined(FEAT_WINDOWS)
10306/*
10307 * Draw the tab pages line at the top of the Vim window.
10308 */
10309 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010310draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010311{
10312 int tabcount = 0;
10313 tabpage_T *tp;
10314 int tabwidth;
10315 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010316 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010317 int attr;
10318 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010319 win_T *cwp;
10320 int wincount;
10321 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010322 int c;
10323 int len;
10324 int attr_sel = hl_attr(HLF_TPS);
10325 int attr_nosel = hl_attr(HLF_TP);
10326 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010327 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010328 int room;
10329 int use_sep_chars = (t_colors < 8
10330#ifdef FEAT_GUI
10331 && !gui.in_use
10332#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010333#ifdef FEAT_TERMGUICOLORS
10334 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010335#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010336 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010337
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010338 if (ScreenLines == NULL)
10339 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010340 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010341
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010342#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010343 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010344 if (gui_use_tabline())
10345 {
10346 gui_update_tabline();
10347 return;
10348 }
10349#endif
10350
10351 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010352 return;
10353
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010354#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010355
10356 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10357 for (scol = 0; scol < Columns; ++scol)
10358 TabPageIdxs[scol] = 0;
10359
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010360 /* Use the 'tabline' option if it's set. */
10361 if (*p_tal != NUL)
10362 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010363 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010364
10365 /* Check for an error. If there is one we would loop in redrawing the
10366 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010367 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010368 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010369 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010370 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010371 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010372 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010373 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010374 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010375#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010376 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010377 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010378 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010379
Bram Moolenaar238a5642006-02-21 22:12:05 +000010380 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10381 if (tabwidth < 6)
10382 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010383
Bram Moolenaar238a5642006-02-21 22:12:05 +000010384 attr = attr_nosel;
10385 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010386 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010387 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10388 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010389 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010390 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010391
Bram Moolenaar238a5642006-02-21 22:12:05 +000010392 if (tp->tp_topframe == topframe)
10393 attr = attr_sel;
10394 if (use_sep_chars && col > 0)
10395 screen_putchar('|', 0, col++, attr);
10396
10397 if (tp->tp_topframe != topframe)
10398 attr = attr_nosel;
10399
10400 screen_putchar(' ', 0, col++, attr);
10401
10402 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010403 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010404 cwp = curwin;
10405 wp = firstwin;
10406 }
10407 else
10408 {
10409 cwp = tp->tp_curwin;
10410 wp = tp->tp_firstwin;
10411 }
10412
10413 modified = FALSE;
10414 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10415 if (bufIsChanged(wp->w_buffer))
10416 modified = TRUE;
10417 if (modified || wincount > 1)
10418 {
10419 if (wincount > 1)
10420 {
10421 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010422 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010423 if (col + len >= Columns - 3)
10424 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010425 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010426#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010427 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010428#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010429 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010430#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010431 );
10432 col += len;
10433 }
10434 if (modified)
10435 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10436 screen_putchar(' ', 0, col++, attr);
10437 }
10438
10439 room = scol - col + tabwidth - 1;
10440 if (room > 0)
10441 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010442 /* Get buffer name in NameBuff[] */
10443 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010444 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010445 len = vim_strsize(NameBuff);
10446 p = NameBuff;
10447#ifdef FEAT_MBYTE
10448 if (has_mbyte)
10449 while (len > room)
10450 {
10451 len -= ptr2cells(p);
10452 mb_ptr_adv(p);
10453 }
10454 else
10455#endif
10456 if (len > room)
10457 {
10458 p += len - room;
10459 len = room;
10460 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010461 if (len > Columns - col - 1)
10462 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010463
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010464 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010465 col += len;
10466 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010467 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010468
10469 /* Store the tab page number in TabPageIdxs[], so that
10470 * jump_to_mouse() knows where each one is. */
10471 ++tabcount;
10472 while (scol < col)
10473 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010474 }
10475
Bram Moolenaar238a5642006-02-21 22:12:05 +000010476 if (use_sep_chars)
10477 c = '_';
10478 else
10479 c = ' ';
10480 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010481
10482 /* Put an "X" for closing the current tab if there are several. */
10483 if (first_tabpage->tp_next != NULL)
10484 {
10485 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10486 TabPageIdxs[Columns - 1] = -999;
10487 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010488 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010489
10490 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10491 * set. */
10492 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010493}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010494
10495/*
10496 * Get buffer name for "buf" into NameBuff[].
10497 * Takes care of special buffer names and translates special characters.
10498 */
10499 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010500get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010501{
10502 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010503 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010504 else
10505 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10506 trans_characters(NameBuff, MAXPATHL);
10507}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010508#endif
10509
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10511/*
10512 * Get the character to use in a status line. Get its attributes in "*attr".
10513 */
10514 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010515fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516{
10517 int fill;
10518 if (is_curwin)
10519 {
10520 *attr = hl_attr(HLF_S);
10521 fill = fill_stl;
10522 }
10523 else
10524 {
10525 *attr = hl_attr(HLF_SNC);
10526 fill = fill_stlnc;
10527 }
10528 /* Use fill when there is highlighting, and highlighting of current
10529 * window differs, or the fillchars differ, or this is not the
10530 * current window */
10531 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010532 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 || (fill_stl != fill_stlnc)))
10534 return fill;
10535 if (is_curwin)
10536 return '^';
10537 return '=';
10538}
10539#endif
10540
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010541#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542/*
10543 * Get the character to use in a separator between vertically split windows.
10544 * Get its attributes in "*attr".
10545 */
10546 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010547fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548{
10549 *attr = hl_attr(HLF_C);
10550 if (*attr == 0 && fill_vert == ' ')
10551 return '|';
10552 else
10553 return fill_vert;
10554}
10555#endif
10556
10557/*
10558 * Return TRUE if redrawing should currently be done.
10559 */
10560 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010561redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562{
10563 return (!RedrawingDisabled
10564 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10565}
10566
10567/*
10568 * Return TRUE if printing messages should currently be done.
10569 */
10570 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010571messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572{
10573 return (!(p_lz && char_avail() && !KeyTyped));
10574}
10575
10576/*
10577 * Show current status info in ruler and various other places
10578 * If always is FALSE, only show ruler if position has changed.
10579 */
10580 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010581showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582{
10583 if (!always && !redrawing())
10584 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010585#ifdef FEAT_INS_EXPAND
10586 if (pum_visible())
10587 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010588# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010589 /* Don't redraw right now, do it later. */
10590 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010591# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010592 return;
10593 }
10594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010596 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010597 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010598 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 else
10601#endif
10602#ifdef FEAT_CMDL_INFO
10603 win_redr_ruler(curwin, always);
10604#endif
10605
10606#ifdef FEAT_TITLE
10607 if (need_maketitle
10608# ifdef FEAT_STL_OPT
10609 || (p_icon && (stl_syntax & STL_IN_ICON))
10610 || (p_title && (stl_syntax & STL_IN_TITLE))
10611# endif
10612 )
10613 maketitle();
10614#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010615#ifdef FEAT_WINDOWS
10616 /* Redraw the tab pages line if needed. */
10617 if (redraw_tabline)
10618 draw_tabline();
10619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620}
10621
10622#ifdef FEAT_CMDL_INFO
10623 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010624win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010626#define RULER_BUF_LEN 70
10627 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 int row;
10629 int fillchar;
10630 int attr;
10631 int empty_line = FALSE;
10632 colnr_T virtcol;
10633 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010634 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010636#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 int this_ru_col;
10638 int off = 0;
10639 int width = Columns;
10640# define WITH_OFF(x) x
10641# define WITH_WIDTH(x) x
10642#else
10643# define WITH_OFF(x) 0
10644# define WITH_WIDTH(x) Columns
10645# define this_ru_col ru_col
10646#endif
10647
10648 /* If 'ruler' off or redrawing disabled, don't do anything */
10649 if (!p_ru)
10650 return;
10651
10652 /*
10653 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10654 * after deleting lines, before cursor.lnum is corrected.
10655 */
10656 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10657 return;
10658
10659#ifdef FEAT_INS_EXPAND
10660 /* Don't draw the ruler while doing insert-completion, it might overwrite
10661 * the (long) mode message. */
10662# ifdef FEAT_WINDOWS
10663 if (wp == lastwin && lastwin->w_status_height == 0)
10664# endif
10665 if (edit_submode != NULL)
10666 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010667 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10668 if (pum_visible())
10669 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670#endif
10671
10672#ifdef FEAT_STL_OPT
10673 if (*p_ruf)
10674 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010675 int save_called_emsg = called_emsg;
10676
10677 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010679 if (called_emsg)
10680 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010681 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010682 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 return;
10684 }
10685#endif
10686
10687 /*
10688 * Check if not in Insert mode and the line is empty (will show "0-1").
10689 */
10690 if (!(State & INSERT)
10691 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10692 empty_line = TRUE;
10693
10694 /*
10695 * Only draw the ruler when something changed.
10696 */
10697 validate_virtcol_win(wp);
10698 if ( redraw_cmdline
10699 || always
10700 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10701 || wp->w_cursor.col != wp->w_ru_cursor.col
10702 || wp->w_virtcol != wp->w_ru_virtcol
10703#ifdef FEAT_VIRTUALEDIT
10704 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10705#endif
10706 || wp->w_topline != wp->w_ru_topline
10707 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10708#ifdef FEAT_DIFF
10709 || wp->w_topfill != wp->w_ru_topfill
10710#endif
10711 || empty_line != wp->w_ru_empty)
10712 {
10713 cursor_off();
10714#ifdef FEAT_WINDOWS
10715 if (wp->w_status_height)
10716 {
10717 row = W_WINROW(wp) + wp->w_height;
10718 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 off = W_WINCOL(wp);
10720 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 }
10722 else
10723#endif
10724 {
10725 row = Rows - 1;
10726 fillchar = ' ';
10727 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010728#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 width = Columns;
10730 off = 0;
10731#endif
10732 }
10733
10734 /* In list mode virtcol needs to be recomputed */
10735 virtcol = wp->w_virtcol;
10736 if (wp->w_p_list && lcs_tab1 == NUL)
10737 {
10738 wp->w_p_list = FALSE;
10739 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10740 wp->w_p_list = TRUE;
10741 }
10742
10743 /*
10744 * Some sprintfs return the length, some return a pointer.
10745 * To avoid portability problems we use strlen() here.
10746 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010747 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10749 ? 0L
10750 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010751 len = STRLEN(buffer);
10752 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10754 (int)virtcol + 1);
10755
10756 /*
10757 * Add a "50%" if there is room for it.
10758 * On the last line, don't print in the last column (scrolls the
10759 * screen up on some terminals).
10760 */
10761 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010762 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 o = i + vim_strsize(buffer + i + 1);
10764#ifdef FEAT_WINDOWS
10765 if (wp->w_status_height == 0) /* can't use last char of screen */
10766#endif
10767 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010768#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 this_ru_col = ru_col - (Columns - width);
10770 if (this_ru_col < 0)
10771 this_ru_col = 0;
10772#endif
10773 /* Never use more than half the window/screen width, leave the other
10774 * half for the filename. */
10775 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10776 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10777 if (this_ru_col + o < WITH_WIDTH(width))
10778 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010779 /* need at least 3 chars left for get_rel_pos() + NUL */
10780 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 {
10782#ifdef FEAT_MBYTE
10783 if (has_mbyte)
10784 i += (*mb_char2bytes)(fillchar, buffer + i);
10785 else
10786#endif
10787 buffer[i++] = fillchar;
10788 ++o;
10789 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010790 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 }
10792 /* Truncate at window boundary. */
10793#ifdef FEAT_MBYTE
10794 if (has_mbyte)
10795 {
10796 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010797 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 {
10799 o += (*mb_ptr2cells)(buffer + i);
10800 if (this_ru_col + o > WITH_WIDTH(width))
10801 {
10802 buffer[i] = NUL;
10803 break;
10804 }
10805 }
10806 }
10807 else
10808#endif
10809 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10810 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10811
10812 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10813 i = redraw_cmdline;
10814 screen_fill(row, row + 1,
10815 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10816 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10817 fillchar, fillchar, attr);
10818 /* don't redraw the cmdline because of showing the ruler */
10819 redraw_cmdline = i;
10820 wp->w_ru_cursor = wp->w_cursor;
10821 wp->w_ru_virtcol = wp->w_virtcol;
10822 wp->w_ru_empty = empty_line;
10823 wp->w_ru_topline = wp->w_topline;
10824 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10825#ifdef FEAT_DIFF
10826 wp->w_ru_topfill = wp->w_topfill;
10827#endif
10828 }
10829}
10830#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010831
10832#if defined(FEAT_LINEBREAK) || defined(PROTO)
10833/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010834 * Return the width of the 'number' and 'relativenumber' column.
10835 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010836 * Otherwise it depends on 'numberwidth' and the line count.
10837 */
10838 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010839number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010840{
10841 int n;
10842 linenr_T lnum;
10843
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010844 if (wp->w_p_rnu && !wp->w_p_nu)
10845 /* cursor line shows "0" */
10846 lnum = wp->w_height;
10847 else
10848 /* cursor line shows absolute line number */
10849 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010850
Bram Moolenaar6b314672015-03-20 15:42:10 +010010851 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010852 return wp->w_nrwidth_width;
10853 wp->w_nrwidth_line_count = lnum;
10854
10855 n = 0;
10856 do
10857 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010858 lnum /= 10;
10859 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010860 } while (lnum > 0);
10861
10862 /* 'numberwidth' gives the minimal width plus one */
10863 if (n < wp->w_p_nuw - 1)
10864 n = wp->w_p_nuw - 1;
10865
10866 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010867 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010868 return n;
10869}
10870#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010871
10872/*
10873 * Return the current cursor column. This is the actual position on the
10874 * screen. First column is 0.
10875 */
10876 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010877screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010878{
10879 return screen_cur_col;
10880}
10881
10882/*
10883 * Return the current cursor row. This is the actual position on the screen.
10884 * First row is 0.
10885 */
10886 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010887screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010888{
10889 return screen_cur_row;
10890}