blob: 0889db91d61cbbae6355d99d48c936582f55f59d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
115/*
116 * Buffer for one screen line (characters and attributes).
117 */
118static schar_T *current_ScreenLine;
119
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static void win_update(win_T *wp);
121static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
124static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
125static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static int win_line(win_T *, linenr_T, int, int, int nochange);
128static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_RIGHTLEFT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void screen_line(int row, int coloff, int endcol, int clear_width, int rlflag);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
132#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void screen_line(int row, int coloff, int endcol, int clear_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
135#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100136#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200143# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void start_search_hl(void);
145static void end_search_hl(void);
146static void init_search_hl(win_T *wp);
147static void prepare_search_hl(win_T *wp, linenr_T lnum);
148static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
149static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_start_highlight(int attr);
152static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void screenclear2(void);
157static void lineclear(unsigned off, int width);
158static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100159#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void linecopy(int to, int from, win_T *wp);
161static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
164static void win_rest_invalid(win_T *wp);
165static void msg_pos_mode(void);
166static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_status(int *attr, int is_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100173#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100180static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif
182
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100183#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/* Ugly global: overrule attribute used by screen_char() */
185static int screen_char_attr = 0;
186#endif
187
188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
191 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 if (wp->w_redr_type < type)
205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100251redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 redraw_buf_later(curbuf, type);
254}
255
256 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100257redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 win_T *wp;
260
261 FOR_ALL_WINDOWS(wp)
262 {
263 if (wp->w_buffer == buf)
264 redraw_win_later(wp, type);
265 }
266}
267
268/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200269 * Redraw as soon as possible. When the command line is not scrolled redraw
270 * right away and restore what was on the command line.
271 * Return a code indicating what happened.
272 */
273 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100274redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200275{
276 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200277 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200278 int r;
279 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200280 schar_T *screenline; /* copy from ScreenLines[] */
281 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200282#ifdef FEAT_MBYTE
283 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200284 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200285 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200286 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200287#endif
288
289 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200290 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200291 return ret;
292
293 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200296 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200298 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299 if (screenline == NULL || screenattr == NULL)
300 ret = 2;
301#ifdef FEAT_MBYTE
302 if (enc_utf8)
303 {
304 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200305 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306 if (screenlineUC == NULL)
307 ret = 2;
308 for (i = 0; i < p_mco; ++i)
309 {
310 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 if (screenlineC[i] == NULL)
313 ret = 2;
314 }
315 }
316 if (enc_dbcs == DBCS_JPNU)
317 {
318 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 if (screenline2 == NULL)
321 ret = 2;
322 }
323#endif
324
325 if (ret != 2)
326 {
327 /* Save the text displayed in the command line area. */
328 for (r = 0; r < rows; ++r)
329 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200331 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200332 (size_t)cols * sizeof(schar_T));
333 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200334 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336#ifdef FEAT_MBYTE
337 if (enc_utf8)
338 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200339 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200343 mch_memmove(screenlineC[i] + r * cols,
344 ScreenLinesC[i] + LineOffset[cmdline_row + r],
345 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200346 }
347 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200348 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200349 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351#endif
352 }
353
354 update_screen(0);
355 ret = 3;
356
357 if (must_redraw == 0)
358 {
359 int off = (int)(current_ScreenLine - ScreenLines);
360
361 /* Restore the text displayed in the command line area. */
362 for (r = 0; r < rows; ++r)
363 {
364 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 screenline + r * cols,
366 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 screenattr + r * cols,
369 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370#ifdef FEAT_MBYTE
371 if (enc_utf8)
372 {
373 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 screenlineUC + r * cols,
375 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 for (i = 0; i < p_mco; ++i)
377 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 screenlineC[i] + r * cols,
379 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 }
381 if (enc_dbcs == DBCS_JPNU)
382 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200383 screenline2 + r * cols,
384 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200385#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200386 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387 }
388 ret = 4;
389 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 }
391
392 vim_free(screenline);
393 vim_free(screenattr);
394#ifdef FEAT_MBYTE
395 if (enc_utf8)
396 {
397 vim_free(screenlineUC);
398 for (i = 0; i < p_mco; ++i)
399 vim_free(screenlineC[i]);
400 }
401 if (enc_dbcs == DBCS_JPNU)
402 vim_free(screenline2);
403#endif
404
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200405 /* Show the intro message when appropriate. */
406 maybe_intro_message();
407
408 setcursor();
409
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 return ret;
411}
412
413/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100414 * Invoked after an asynchronous callback is called.
415 * If an echo command was used the cursor needs to be put back where
416 * it belongs. If highlighting was changed a redraw is needed.
417 */
418 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200419redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100420{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100421 if (State == HITRETURN || State == ASKMORE)
422 ; /* do nothing */
423 else if (State & CMDLINE)
424 redrawcmdline();
Bram Moolenaar144445d2016-07-08 21:41:54 +0200425 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100426 {
427 update_screen(0);
428 setcursor();
429 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100430 cursor_on();
431 out_flush();
432#ifdef FEAT_GUI
433 if (gui.in_use)
434 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200435 /* Don't update the cursor when it is blinking and off to avoid
436 * flicker. */
437 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200438 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439 gui_mch_flush();
440 }
441#endif
442}
443
444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 * Changed something in the current window, at buffer line "lnum", that
446 * requires that line and possibly other lines to be redrawn.
447 * Used when entering/leaving Insert mode with the cursor on a folded line.
448 * Used to remove the "$" from a change command.
449 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
450 * may become invalid and the whole window will have to be redrawn.
451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100453redrawWinline(
454 linenr_T lnum,
455 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457#ifdef FEAT_FOLDING
458 int i;
459#endif
460
461 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
462 curwin->w_redraw_top = lnum;
463 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
464 curwin->w_redraw_bot = lnum;
465 redraw_later(VALID);
466
467#ifdef FEAT_FOLDING
468 if (invalid)
469 {
470 /* A w_lines[] entry for this lnum has become invalid. */
471 i = find_wl_entry(curwin, lnum);
472 if (i >= 0)
473 curwin->w_lines[i].wl_valid = FALSE;
474 }
475#endif
476}
477
478/*
479 * update all windows that are editing the current buffer
480 */
481 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100482update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
484 redraw_curbuf_later(type);
485 update_screen(type);
486}
487
488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 * Based on the current value of curwin->w_topline, transfer a screenfull
490 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
491 */
492 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100493update_screen(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494{
495 win_T *wp;
496 static int did_intro = FALSE;
497#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
498 int did_one;
499#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200500#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200501 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200502 int gui_cursor_col;
503 int gui_cursor_row;
504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000506 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 if (!screen_valid(TRUE))
508 return;
509
510 if (must_redraw)
511 {
512 if (type < must_redraw) /* use maximal type */
513 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000514
515 /* must_redraw is reset here, so that when we run into some weird
516 * reason to redraw while busy redrawing (e.g., asynchronous
517 * scrolling), or update_topline() in win_update() will cause a
518 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 must_redraw = 0;
520 }
521
522 /* Need to update w_lines[]. */
523 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
524 type = NOT_VALID;
525
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000526 /* Postpone the redrawing when it's not needed and when being called
527 * recursively. */
528 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
530 redraw_later(type); /* remember type for next time */
531 must_redraw = type;
532 if (type > INVERTED_ALL)
533 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
534 return;
535 }
536
537 updating_screen = TRUE;
538#ifdef FEAT_SYN_HL
539 ++display_tick; /* let syntax code know we're in a next round of
540 * display updating */
541#endif
542
543 /*
544 * if the screen was scrolled up when displaying a message, scroll it down
545 */
546 if (msg_scrolled)
547 {
548 clear_cmdline = TRUE;
549 if (msg_scrolled > Rows - 5) /* clearing is faster */
550 type = CLEAR;
551 else if (type != CLEAR)
552 {
553 check_for_delay(FALSE);
554 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
555 type = CLEAR;
556 FOR_ALL_WINDOWS(wp)
557 {
558 if (W_WINROW(wp) < msg_scrolled)
559 {
560 if (W_WINROW(wp) + wp->w_height > msg_scrolled
561 && wp->w_redr_type < REDRAW_TOP
562 && wp->w_lines_valid > 0
563 && wp->w_topline == wp->w_lines[0].wl_lnum)
564 {
565 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
566 wp->w_redr_type = REDRAW_TOP;
567 }
568 else
569 {
570 wp->w_redr_type = NOT_VALID;
571#ifdef FEAT_WINDOWS
572 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
573 <= msg_scrolled)
574 wp->w_redr_status = TRUE;
575#endif
576 }
577 }
578 }
579 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000580#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000581 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 }
584 msg_scrolled = 0;
585 need_wait_return = FALSE;
586 }
587
588 /* reset cmdline_row now (may have been changed temporarily) */
589 compute_cmdrow();
590
591 /* Check for changed highlighting */
592 if (need_highlight_changed)
593 highlight_changed();
594
595 if (type == CLEAR) /* first clear screen */
596 {
597 screenclear(); /* will reset clear_cmdline */
598 type = NOT_VALID;
599 }
600
601 if (clear_cmdline) /* going to clear cmdline (done below) */
602 check_for_delay(FALSE);
603
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000604#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200605 /* Force redraw when width of 'number' or 'relativenumber' column
606 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000607 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200608 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
609 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000610 curwin->w_redr_type = NOT_VALID;
611#endif
612
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 /*
614 * Only start redrawing if there is really something to do.
615 */
616 if (type == INVERTED)
617 update_curswant();
618 if (curwin->w_redr_type < type
619 && !((type == VALID
620 && curwin->w_lines[0].wl_valid
621#ifdef FEAT_DIFF
622 && curwin->w_topfill == curwin->w_old_topfill
623 && curwin->w_botfill == curwin->w_old_botfill
624#endif
625 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000627 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
629 && curwin->w_old_visual_mode == VIsual_mode
630 && (curwin->w_valid & VALID_VIRTCOL)
631 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 ))
633 curwin->w_redr_type = type;
634
Bram Moolenaar5a305422006-04-28 22:38:25 +0000635#ifdef FEAT_WINDOWS
636 /* Redraw the tab pages line if needed. */
637 if (redraw_tabline || type >= NOT_VALID)
638 draw_tabline();
639#endif
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#ifdef FEAT_SYN_HL
642 /*
643 * Correct stored syntax highlighting info for changes in each displayed
644 * buffer. Each buffer must only be done once.
645 */
646 FOR_ALL_WINDOWS(wp)
647 {
648 if (wp->w_buffer->b_mod_set)
649 {
650# ifdef FEAT_WINDOWS
651 win_T *wwp;
652
653 /* Check if we already did this buffer. */
654 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
655 if (wwp->w_buffer == wp->w_buffer)
656 break;
657# endif
658 if (
659# ifdef FEAT_WINDOWS
660 wwp == wp &&
661# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200662 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 syn_stack_apply_changes(wp->w_buffer);
664 }
665 }
666#endif
667
668 /*
669 * Go from top to bottom through the windows, redrawing the ones that need
670 * it.
671 */
672#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
673 did_one = FALSE;
674#endif
675#ifdef FEAT_SEARCH_EXTRA
676 search_hl.rm.regprog = NULL;
677#endif
678 FOR_ALL_WINDOWS(wp)
679 {
680 if (wp->w_redr_type != 0)
681 {
682 cursor_off();
683#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
684 if (!did_one)
685 {
686 did_one = TRUE;
687# ifdef FEAT_SEARCH_EXTRA
688 start_search_hl();
689# endif
690# ifdef FEAT_CLIPBOARD
691 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200692 if (clip_star.available && clip_isautosel_star())
693 clip_update_selection(&clip_star);
694 if (clip_plus.available && clip_isautosel_plus())
695 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696# endif
697#ifdef FEAT_GUI
698 /* Remove the cursor before starting to do anything, because
699 * scrolling may make it difficult to redraw the text under
700 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200701 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200702 {
703 gui_cursor_col = gui.cursor_col;
704 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200706 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708#endif
709 }
710#endif
711 win_update(wp);
712 }
713
714#ifdef FEAT_WINDOWS
715 /* redraw status line after the window to minimize cursor movement */
716 if (wp->w_redr_status)
717 {
718 cursor_off();
719 win_redr_status(wp);
720 }
721#endif
722 }
723#if defined(FEAT_SEARCH_EXTRA)
724 end_search_hl();
725#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100726#ifdef FEAT_INS_EXPAND
727 /* May need to redraw the popup menu. */
728 if (pum_visible())
729 pum_redraw();
730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
732#ifdef FEAT_WINDOWS
733 /* Reset b_mod_set flags. Going through all windows is probably faster
734 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200735 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 wp->w_buffer->b_mod_set = FALSE;
737#else
738 curbuf->b_mod_set = FALSE;
739#endif
740
741 updating_screen = FALSE;
742#ifdef FEAT_GUI
743 gui_may_resize_shell();
744#endif
745
746 /* Clear or redraw the command line. Done last, because scrolling may
747 * mess up the command line. */
748 if (clear_cmdline || redraw_cmdline)
749 showmode();
750
751 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200752 if (!did_intro)
753 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 did_intro = TRUE;
755
756#ifdef FEAT_GUI
757 /* Redraw the cursor and update the scrollbars when all screen updating is
758 * done. */
759 if (gui.in_use)
760 {
761 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200762 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200763 {
764 /* Put the GUI position where the cursor was, gui_update_cursor()
765 * uses that. */
766 gui.col = gui_cursor_col;
767 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200768# ifdef FEAT_MBYTE
769 gui.col = mb_fix_col(gui.col, gui.row);
770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200772 screen_cur_col = gui.col;
773 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 gui_update_scrollbars(FALSE);
776 }
777#endif
778}
779
Bram Moolenaar860cae12010-06-05 23:22:07 +0200780#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200781/*
782 * Return TRUE if the cursor line in window "wp" may be concealed, according
783 * to the 'concealcursor' option.
784 */
785 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100786conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200787{
788 int c;
789
790 if (*wp->w_p_cocu == NUL)
791 return FALSE;
792 if (get_real_state() & VISUAL)
793 c = 'v';
794 else if (State & INSERT)
795 c = 'i';
796 else if (State & NORMAL)
797 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200798 else if (State & CMDLINE)
799 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200800 else
801 return FALSE;
802 return vim_strchr(wp->w_p_cocu, c) != NULL;
803}
804
805/*
806 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
807 */
808 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100809conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200810{
811 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
812 {
813 need_cursor_line_redraw = TRUE;
814 /* Need to recompute cursor column, e.g., when starting Visual mode
815 * without concealing. */
816 curs_columns(TRUE);
817 }
818}
819
Bram Moolenaar860cae12010-06-05 23:22:07 +0200820 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100821update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200822{
823 int row;
824 int j;
825
Bram Moolenaar908be432016-05-24 10:51:30 +0200826 /* Don't do anything if the screen structures are (not yet) valid. */
827 if (!screen_valid(TRUE))
828 return;
829
Bram Moolenaar860cae12010-06-05 23:22:07 +0200830 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200831 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200832 {
833# ifdef FEAT_GUI
834 /* Remove the cursor before starting to do anything, because scrolling
835 * may make it difficult to redraw the text under it. */
836 if (gui.in_use)
837 gui_undraw_cursor();
838# endif
839 row = 0;
840 for (j = 0; j < wp->w_lines_valid; ++j)
841 {
842 if (lnum == wp->w_lines[j].wl_lnum)
843 {
844 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200845# ifdef FEAT_SEARCH_EXTRA
846 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200847 start_search_hl();
848 prepare_search_hl(wp, lnum);
849# endif
850 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
851# if defined(FEAT_SEARCH_EXTRA)
852 end_search_hl();
853# endif
854 break;
855 }
856 row += wp->w_lines[j].wl_size;
857 }
858# ifdef FEAT_GUI
859 /* Redraw the cursor */
860 if (gui.in_use)
861 {
862 out_flush(); /* required before updating the cursor */
863 gui_update_cursor(FALSE, FALSE);
864 }
865# endif
866 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200867 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200868}
869#endif
870
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100872static void update_prepare(void);
873static void update_finish(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875/*
876 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000877 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 */
879 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100880update_prepare(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881{
882 cursor_off();
883 updating_screen = TRUE;
884#ifdef FEAT_GUI
885 /* Remove the cursor before starting to do anything, because scrolling may
886 * make it difficult to redraw the text under it. */
887 if (gui.in_use)
888 gui_undraw_cursor();
889#endif
890#ifdef FEAT_SEARCH_EXTRA
891 start_search_hl();
892#endif
893}
894
895/*
896 * Finish updating one or more windows.
897 */
898 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100899update_finish(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900{
901 if (redraw_cmdline)
902 showmode();
903
904# ifdef FEAT_SEARCH_EXTRA
905 end_search_hl();
906# endif
907
908 updating_screen = FALSE;
909
910# ifdef FEAT_GUI
911 gui_may_resize_shell();
912
913 /* Redraw the cursor and update the scrollbars when all screen updating is
914 * done. */
915 if (gui.in_use)
916 {
917 out_flush(); /* required before updating the cursor */
918 gui_update_cursor(FALSE, FALSE);
919 gui_update_scrollbars(FALSE);
920 }
921# endif
922}
923#endif
924
925#if defined(FEAT_SIGNS) || defined(PROTO)
926 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100927update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 win_T *wp;
930 int doit = FALSE;
931
932# ifdef FEAT_FOLDING
933 win_foldinfo.fi_level = 0;
934# endif
935
936 /* update/delete a specific mark */
937 FOR_ALL_WINDOWS(wp)
938 {
939 if (buf != NULL && lnum > 0)
940 {
941 if (wp->w_buffer == buf && lnum >= wp->w_topline
942 && lnum < wp->w_botline)
943 {
944 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
945 wp->w_redraw_top = lnum;
946 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
947 wp->w_redraw_bot = lnum;
948 redraw_win_later(wp, VALID);
949 }
950 }
951 else
952 redraw_win_later(wp, VALID);
953 if (wp->w_redr_type != 0)
954 doit = TRUE;
955 }
956
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100957 /* Return when there is nothing to do, screen updating is already
958 * happening (recursive call) or still starting up. */
959 if (!doit || updating_screen
960#ifdef FEAT_GUI
961 || gui.starting
962#endif
963 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 return;
965
966 /* update all windows that need updating */
967 update_prepare();
968
969# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +0200970 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
972 if (wp->w_redr_type != 0)
973 win_update(wp);
974 if (wp->w_redr_status)
975 win_redr_status(wp);
976 }
977# else
978 if (curwin->w_redr_type != 0)
979 win_update(curwin);
980# endif
981
982 update_finish();
983}
984#endif
985
986
987#if defined(FEAT_GUI) || defined(PROTO)
988/*
989 * Update a single window, its status line and maybe the command line msg.
990 * Used for the GUI scrollbar.
991 */
992 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100993updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000995 /* return if already busy updating */
996 if (updating_screen)
997 return;
998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 update_prepare();
1000
1001#ifdef FEAT_CLIPBOARD
1002 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001003 if (clip_star.available && clip_isautosel_star())
1004 clip_update_selection(&clip_star);
1005 if (clip_plus.available && clip_isautosel_plus())
1006 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001010
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001012 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001013 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001014 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (wp->w_redr_status
1017# ifdef FEAT_CMDL_INFO
1018 || p_ru
1019# endif
1020# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001021 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022# endif
1023 )
1024 win_redr_status(wp);
1025#endif
1026
1027 update_finish();
1028}
1029#endif
1030
1031/*
1032 * Update a single window.
1033 *
1034 * This may cause the windows below it also to be redrawn (when clearing the
1035 * screen or scrolling lines).
1036 *
1037 * How the window is redrawn depends on wp->w_redr_type. Each type also
1038 * implies the one below it.
1039 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001040 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1042 * INVERTED redraw the changed part of the Visual area
1043 * INVERTED_ALL redraw the whole Visual area
1044 * VALID 1. scroll up/down to adjust for a changed w_topline
1045 * 2. update lines at the top when scrolled down
1046 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001047 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 * b_mod_top and b_mod_bot.
1049 * - if wp->w_redraw_top non-zero, redraw lines between
1050 * wp->w_redraw_top and wp->w_redr_bot.
1051 * - continue redrawing when syntax status is invalid.
1052 * 4. if scrolled up, update lines at the bottom.
1053 * This results in three areas that may need updating:
1054 * top: from first row to top_end (when scrolled down)
1055 * mid: from mid_start to mid_end (update inversion or changed text)
1056 * bot: from bot_start to last row (when scrolled up)
1057 */
1058 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001059win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
1061 buf_T *buf = wp->w_buffer;
1062 int type;
1063 int top_end = 0; /* Below last row of the top area that needs
1064 updating. 0 when no top area updating. */
1065 int mid_start = 999;/* first row of the mid area that needs
1066 updating. 999 when no mid area updating. */
1067 int mid_end = 0; /* Below last row of the mid area that needs
1068 updating. 0 when no mid area updating. */
1069 int bot_start = 999;/* first row of the bot area that needs
1070 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 int scrolled_down = FALSE; /* TRUE when scrolled down when
1072 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001074 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 int top_to_mod = FALSE; /* redraw above mod_top */
1076#endif
1077
1078 int row; /* current window row to display */
1079 linenr_T lnum; /* current buffer lnum to display */
1080 int idx; /* current index in w_lines[] */
1081 int srow; /* starting row of the current line */
1082
1083 int eof = FALSE; /* if TRUE, we hit the end of the file */
1084 int didline = FALSE; /* if TRUE, we finished the last line */
1085 int i;
1086 long j;
1087 static int recursive = FALSE; /* being called recursively */
1088 int old_botline = wp->w_botline;
1089#ifdef FEAT_FOLDING
1090 long fold_count;
1091#endif
1092#ifdef FEAT_SYN_HL
1093 /* remember what happened to the previous line, to know if
1094 * check_visual_highlight() can be used */
1095#define DID_NONE 1 /* didn't update a line */
1096#define DID_LINE 2 /* updated a normal line */
1097#define DID_FOLD 3 /* updated a folded line */
1098 int did_update = DID_NONE;
1099 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1100#endif
1101 linenr_T mod_top = 0;
1102 linenr_T mod_bot = 0;
1103#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1104 int save_got_int;
1105#endif
1106
1107 type = wp->w_redr_type;
1108
1109 if (type == NOT_VALID)
1110 {
1111#ifdef FEAT_WINDOWS
1112 wp->w_redr_status = TRUE;
1113#endif
1114 wp->w_lines_valid = 0;
1115 }
1116
1117 /* Window is zero-height: nothing to draw. */
1118 if (wp->w_height == 0)
1119 {
1120 wp->w_redr_type = 0;
1121 return;
1122 }
1123
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001124#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 /* Window is zero-width: Only need to draw the separator. */
1126 if (wp->w_width == 0)
1127 {
1128 /* draw the vertical separator right of this window */
1129 draw_vsep_win(wp, 0);
1130 wp->w_redr_type = 0;
1131 return;
1132 }
1133#endif
1134
1135#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001136 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#endif
1138
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001139#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001140 /* Force redraw when width of 'number' or 'relativenumber' column
1141 * changes. */
1142 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001143 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001144 {
1145 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001146 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001147 }
1148 else
1149#endif
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1152 {
1153 /*
1154 * When there are both inserted/deleted lines and specific lines to be
1155 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1156 * everything (only happens when redrawing is off for while).
1157 */
1158 type = NOT_VALID;
1159 }
1160 else
1161 {
1162 /*
1163 * Set mod_top to the first line that needs displaying because of
1164 * changes. Set mod_bot to the first line after the changes.
1165 */
1166 mod_top = wp->w_redraw_top;
1167 if (wp->w_redraw_bot != 0)
1168 mod_bot = wp->w_redraw_bot + 1;
1169 else
1170 mod_bot = 0;
1171 wp->w_redraw_top = 0; /* reset for next time */
1172 wp->w_redraw_bot = 0;
1173 if (buf->b_mod_set)
1174 {
1175 if (mod_top == 0 || mod_top > buf->b_mod_top)
1176 {
1177 mod_top = buf->b_mod_top;
1178#ifdef FEAT_SYN_HL
1179 /* Need to redraw lines above the change that may be included
1180 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001181 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001183 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 if (mod_top < 1)
1185 mod_top = 1;
1186 }
1187#endif
1188 }
1189 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1190 mod_bot = buf->b_mod_bot;
1191
1192#ifdef FEAT_SEARCH_EXTRA
1193 /* When 'hlsearch' is on and using a multi-line search pattern, a
1194 * change in one line may make the Search highlighting in a
1195 * previous line invalid. Simple solution: redraw all visible
1196 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001197 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001199 if (search_hl.rm.regprog != NULL
1200 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001202 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001203 {
1204 cur = wp->w_match_head;
1205 while (cur != NULL)
1206 {
1207 if (cur->match.regprog != NULL
1208 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001209 {
1210 top_to_mod = TRUE;
1211 break;
1212 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001213 cur = cur->next;
1214 }
1215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216#endif
1217 }
1218#ifdef FEAT_FOLDING
1219 if (mod_top != 0 && hasAnyFolding(wp))
1220 {
1221 linenr_T lnumt, lnumb;
1222
1223 /*
1224 * A change in a line can cause lines above it to become folded or
1225 * unfolded. Find the top most buffer line that may be affected.
1226 * If the line was previously folded and displayed, get the first
1227 * line of that fold. If the line is folded now, get the first
1228 * folded line. Use the minimum of these two.
1229 */
1230
1231 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1232 * the line below it. If there is no valid entry, use w_topline.
1233 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1234 * to this line. If there is no valid entry, use MAXLNUM. */
1235 lnumt = wp->w_topline;
1236 lnumb = MAXLNUM;
1237 for (i = 0; i < wp->w_lines_valid; ++i)
1238 if (wp->w_lines[i].wl_valid)
1239 {
1240 if (wp->w_lines[i].wl_lastlnum < mod_top)
1241 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1242 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1243 {
1244 lnumb = wp->w_lines[i].wl_lnum;
1245 /* When there is a fold column it might need updating
1246 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001247 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 ++lnumb;
1249 }
1250 }
1251
1252 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1253 if (mod_top > lnumt)
1254 mod_top = lnumt;
1255
1256 /* Now do the same for the bottom line (one above mod_bot). */
1257 --mod_bot;
1258 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1259 ++mod_bot;
1260 if (mod_bot < lnumb)
1261 mod_bot = lnumb;
1262 }
1263#endif
1264
1265 /* When a change starts above w_topline and the end is below
1266 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001267 * If the end of the change is above w_topline: do like no change was
1268 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (mod_top != 0 && mod_top < wp->w_topline)
1270 {
1271 if (mod_bot > wp->w_topline)
1272 mod_top = wp->w_topline;
1273#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001274 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 top_end = 1;
1276#endif
1277 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001278
1279 /* When line numbers are displayed need to redraw all lines below
1280 * inserted/deleted lines. */
1281 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1282 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284
1285 /*
1286 * When only displaying the lines at the top, set top_end. Used when
1287 * window has scrolled down for msg_scrolled.
1288 */
1289 if (type == REDRAW_TOP)
1290 {
1291 j = 0;
1292 for (i = 0; i < wp->w_lines_valid; ++i)
1293 {
1294 j += wp->w_lines[i].wl_size;
1295 if (j >= wp->w_upd_rows)
1296 {
1297 top_end = j;
1298 break;
1299 }
1300 }
1301 if (top_end == 0)
1302 /* not found (cannot happen?): redraw everything */
1303 type = NOT_VALID;
1304 else
1305 /* top area defined, the rest is VALID */
1306 type = VALID;
1307 }
1308
Bram Moolenaar367329b2007-08-30 11:53:22 +00001309 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001310 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1311 * non-zero and thus not FALSE) will indicate that screenclear() was not
1312 * called. */
1313 if (screen_cleared)
1314 screen_cleared = MAYBE;
1315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 /*
1317 * If there are no changes on the screen that require a complete redraw,
1318 * handle three cases:
1319 * 1: we are off the top of the screen by a few lines: scroll down
1320 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1321 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1322 * w_lines[] that needs updating.
1323 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001324 if ((type == VALID || type == SOME_VALID
1325 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326#ifdef FEAT_DIFF
1327 && !wp->w_botfill && !wp->w_old_botfill
1328#endif
1329 )
1330 {
1331 if (mod_top != 0 && wp->w_topline == mod_top)
1332 {
1333 /*
1334 * w_topline is the first changed line, the scrolling will be done
1335 * further down.
1336 */
1337 }
1338 else if (wp->w_lines[0].wl_valid
1339 && (wp->w_topline < wp->w_lines[0].wl_lnum
1340#ifdef FEAT_DIFF
1341 || (wp->w_topline == wp->w_lines[0].wl_lnum
1342 && wp->w_topfill > wp->w_old_topfill)
1343#endif
1344 ))
1345 {
1346 /*
1347 * New topline is above old topline: May scroll down.
1348 */
1349#ifdef FEAT_FOLDING
1350 if (hasAnyFolding(wp))
1351 {
1352 linenr_T ln;
1353
1354 /* count the number of lines we are off, counting a sequence
1355 * of folded lines as one */
1356 j = 0;
1357 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1358 {
1359 ++j;
1360 if (j >= wp->w_height - 2)
1361 break;
1362 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1363 }
1364 }
1365 else
1366#endif
1367 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1368 if (j < wp->w_height - 2) /* not too far off */
1369 {
1370 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1371#ifdef FEAT_DIFF
1372 /* insert extra lines for previously invisible filler lines */
1373 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1374 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1375 - wp->w_old_topfill;
1376#endif
1377 if (i < wp->w_height - 2) /* less than a screen off */
1378 {
1379 /*
1380 * Try to insert the correct number of lines.
1381 * If not the last window, delete the lines at the bottom.
1382 * win_ins_lines may fail when the terminal can't do it.
1383 */
1384 if (i > 0)
1385 check_for_delay(FALSE);
1386 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1387 {
1388 if (wp->w_lines_valid != 0)
1389 {
1390 /* Need to update rows that are new, stop at the
1391 * first one that scrolled down. */
1392 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
1395 /* Move the entries that were scrolled, disable
1396 * the entries for the lines to be redrawn. */
1397 if ((wp->w_lines_valid += j) > wp->w_height)
1398 wp->w_lines_valid = wp->w_height;
1399 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1400 wp->w_lines[idx] = wp->w_lines[idx - j];
1401 while (idx >= 0)
1402 wp->w_lines[idx--].wl_valid = FALSE;
1403 }
1404 }
1405 else
1406 mid_start = 0; /* redraw all lines */
1407 }
1408 else
1409 mid_start = 0; /* redraw all lines */
1410 }
1411 else
1412 mid_start = 0; /* redraw all lines */
1413 }
1414 else
1415 {
1416 /*
1417 * New topline is at or below old topline: May scroll up.
1418 * When topline didn't change, find first entry in w_lines[] that
1419 * needs updating.
1420 */
1421
1422 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1423 j = -1;
1424 row = 0;
1425 for (i = 0; i < wp->w_lines_valid; i++)
1426 {
1427 if (wp->w_lines[i].wl_valid
1428 && wp->w_lines[i].wl_lnum == wp->w_topline)
1429 {
1430 j = i;
1431 break;
1432 }
1433 row += wp->w_lines[i].wl_size;
1434 }
1435 if (j == -1)
1436 {
1437 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1438 * lines */
1439 mid_start = 0;
1440 }
1441 else
1442 {
1443 /*
1444 * Try to delete the correct number of lines.
1445 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1446 */
1447#ifdef FEAT_DIFF
1448 /* If the topline didn't change, delete old filler lines,
1449 * otherwise delete filler lines of the new topline... */
1450 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1451 row += wp->w_old_topfill;
1452 else
1453 row += diff_check_fill(wp, wp->w_topline);
1454 /* ... but don't delete new filler lines. */
1455 row -= wp->w_topfill;
1456#endif
1457 if (row > 0)
1458 {
1459 check_for_delay(FALSE);
1460 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1461 bot_start = wp->w_height - row;
1462 else
1463 mid_start = 0; /* redraw all lines */
1464 }
1465 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1466 {
1467 /*
1468 * Skip the lines (below the deleted lines) that are still
1469 * valid and don't need redrawing. Copy their info
1470 * upwards, to compensate for the deleted lines. Set
1471 * bot_start to the first row that needs redrawing.
1472 */
1473 bot_start = 0;
1474 idx = 0;
1475 for (;;)
1476 {
1477 wp->w_lines[idx] = wp->w_lines[j];
1478 /* stop at line that didn't fit, unless it is still
1479 * valid (no lines deleted) */
1480 if (row > 0 && bot_start + row
1481 + (int)wp->w_lines[j].wl_size > wp->w_height)
1482 {
1483 wp->w_lines_valid = idx + 1;
1484 break;
1485 }
1486 bot_start += wp->w_lines[idx++].wl_size;
1487
1488 /* stop at the last valid entry in w_lines[].wl_size */
1489 if (++j >= wp->w_lines_valid)
1490 {
1491 wp->w_lines_valid = idx;
1492 break;
1493 }
1494 }
1495#ifdef FEAT_DIFF
1496 /* Correct the first entry for filler lines at the top
1497 * when it won't get updated below. */
1498 if (wp->w_p_diff && bot_start > 0)
1499 wp->w_lines[0].wl_size =
1500 plines_win_nofill(wp, wp->w_topline, TRUE)
1501 + wp->w_topfill;
1502#endif
1503 }
1504 }
1505 }
1506
1507 /* When starting redraw in the first line, redraw all lines. When
1508 * there is only one window it's probably faster to clear the screen
1509 * first. */
1510 if (mid_start == 0)
1511 {
1512 mid_end = wp->w_height;
1513 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001514 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001515 /* Clear the screen when it was not done by win_del_lines() or
1516 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1517 * then. */
1518 if (screen_cleared != TRUE)
1519 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001520#ifdef FEAT_WINDOWS
1521 /* The screen was cleared, redraw the tab pages line. */
1522 if (redraw_tabline)
1523 draw_tabline();
1524#endif
1525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001527
1528 /* When win_del_lines() or win_ins_lines() caused the screen to be
1529 * cleared (only happens for the first window) or when screenclear()
1530 * was called directly above, "must_redraw" will have been set to
1531 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1532 if (screen_cleared == TRUE)
1533 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
1535 else
1536 {
1537 /* Not VALID or INVERTED: redraw all lines. */
1538 mid_start = 0;
1539 mid_end = wp->w_height;
1540 }
1541
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001542 if (type == SOME_VALID)
1543 {
1544 /* SOME_VALID: redraw all lines. */
1545 mid_start = 0;
1546 mid_end = wp->w_height;
1547 type = NOT_VALID;
1548 }
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 /* check if we are updating or removing the inverted part */
1551 if ((VIsual_active && buf == curwin->w_buffer)
1552 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1553 {
1554 linenr_T from, to;
1555
1556 if (VIsual_active)
1557 {
1558 if (VIsual_active
1559 && (VIsual_mode != wp->w_old_visual_mode
1560 || type == INVERTED_ALL))
1561 {
1562 /*
1563 * If the type of Visual selection changed, redraw the whole
1564 * selection. Also when the ownership of the X selection is
1565 * gained or lost.
1566 */
1567 if (curwin->w_cursor.lnum < VIsual.lnum)
1568 {
1569 from = curwin->w_cursor.lnum;
1570 to = VIsual.lnum;
1571 }
1572 else
1573 {
1574 from = VIsual.lnum;
1575 to = curwin->w_cursor.lnum;
1576 }
1577 /* redraw more when the cursor moved as well */
1578 if (wp->w_old_cursor_lnum < from)
1579 from = wp->w_old_cursor_lnum;
1580 if (wp->w_old_cursor_lnum > to)
1581 to = wp->w_old_cursor_lnum;
1582 if (wp->w_old_visual_lnum < from)
1583 from = wp->w_old_visual_lnum;
1584 if (wp->w_old_visual_lnum > to)
1585 to = wp->w_old_visual_lnum;
1586 }
1587 else
1588 {
1589 /*
1590 * Find the line numbers that need to be updated: The lines
1591 * between the old cursor position and the current cursor
1592 * position. Also check if the Visual position changed.
1593 */
1594 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1595 {
1596 from = curwin->w_cursor.lnum;
1597 to = wp->w_old_cursor_lnum;
1598 }
1599 else
1600 {
1601 from = wp->w_old_cursor_lnum;
1602 to = curwin->w_cursor.lnum;
1603 if (from == 0) /* Visual mode just started */
1604 from = to;
1605 }
1606
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001607 if (VIsual.lnum != wp->w_old_visual_lnum
1608 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {
1610 if (wp->w_old_visual_lnum < from
1611 && wp->w_old_visual_lnum != 0)
1612 from = wp->w_old_visual_lnum;
1613 if (wp->w_old_visual_lnum > to)
1614 to = wp->w_old_visual_lnum;
1615 if (VIsual.lnum < from)
1616 from = VIsual.lnum;
1617 if (VIsual.lnum > to)
1618 to = VIsual.lnum;
1619 }
1620 }
1621
1622 /*
1623 * If in block mode and changed column or curwin->w_curswant:
1624 * update all lines.
1625 * First compute the actual start and end column.
1626 */
1627 if (VIsual_mode == Ctrl_V)
1628 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001629 colnr_T fromc, toc;
1630#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1631 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaar404406a2014-10-09 13:24:43 +02001633 if (curwin->w_p_lbr)
1634 ve_flags = VE_ALL;
1635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001637#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1638 ve_flags = save_ve_flags;
1639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 ++toc;
1641 if (curwin->w_curswant == MAXCOL)
1642 toc = MAXCOL;
1643
1644 if (fromc != wp->w_old_cursor_fcol
1645 || toc != wp->w_old_cursor_lcol)
1646 {
1647 if (from > VIsual.lnum)
1648 from = VIsual.lnum;
1649 if (to < VIsual.lnum)
1650 to = VIsual.lnum;
1651 }
1652 wp->w_old_cursor_fcol = fromc;
1653 wp->w_old_cursor_lcol = toc;
1654 }
1655 }
1656 else
1657 {
1658 /* Use the line numbers of the old Visual area. */
1659 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1660 {
1661 from = wp->w_old_cursor_lnum;
1662 to = wp->w_old_visual_lnum;
1663 }
1664 else
1665 {
1666 from = wp->w_old_visual_lnum;
1667 to = wp->w_old_cursor_lnum;
1668 }
1669 }
1670
1671 /*
1672 * There is no need to update lines above the top of the window.
1673 */
1674 if (from < wp->w_topline)
1675 from = wp->w_topline;
1676
1677 /*
1678 * If we know the value of w_botline, use it to restrict the update to
1679 * the lines that are visible in the window.
1680 */
1681 if (wp->w_valid & VALID_BOTLINE)
1682 {
1683 if (from >= wp->w_botline)
1684 from = wp->w_botline - 1;
1685 if (to >= wp->w_botline)
1686 to = wp->w_botline - 1;
1687 }
1688
1689 /*
1690 * Find the minimal part to be updated.
1691 * Watch out for scrolling that made entries in w_lines[] invalid.
1692 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1693 * top_end; need to redraw from top_end to the "to" line.
1694 * A middle mouse click with a Visual selection may change the text
1695 * above the Visual area and reset wl_valid, do count these for
1696 * mid_end (in srow).
1697 */
1698 if (mid_start > 0)
1699 {
1700 lnum = wp->w_topline;
1701 idx = 0;
1702 srow = 0;
1703 if (scrolled_down)
1704 mid_start = top_end;
1705 else
1706 mid_start = 0;
1707 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1708 {
1709 if (wp->w_lines[idx].wl_valid)
1710 mid_start += wp->w_lines[idx].wl_size;
1711 else if (!scrolled_down)
1712 srow += wp->w_lines[idx].wl_size;
1713 ++idx;
1714# ifdef FEAT_FOLDING
1715 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1716 lnum = wp->w_lines[idx].wl_lnum;
1717 else
1718# endif
1719 ++lnum;
1720 }
1721 srow += mid_start;
1722 mid_end = wp->w_height;
1723 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1724 {
1725 if (wp->w_lines[idx].wl_valid
1726 && wp->w_lines[idx].wl_lnum >= to + 1)
1727 {
1728 /* Only update until first row of this line */
1729 mid_end = srow;
1730 break;
1731 }
1732 srow += wp->w_lines[idx].wl_size;
1733 }
1734 }
1735 }
1736
1737 if (VIsual_active && buf == curwin->w_buffer)
1738 {
1739 wp->w_old_visual_mode = VIsual_mode;
1740 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1741 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001742 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 wp->w_old_curswant = curwin->w_curswant;
1744 }
1745 else
1746 {
1747 wp->w_old_visual_mode = 0;
1748 wp->w_old_cursor_lnum = 0;
1749 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001750 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
1753#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1754 /* reset got_int, otherwise regexp won't work */
1755 save_got_int = got_int;
1756 got_int = 0;
1757#endif
1758#ifdef FEAT_FOLDING
1759 win_foldinfo.fi_level = 0;
1760#endif
1761
1762 /*
1763 * Update all the window rows.
1764 */
1765 idx = 0; /* first entry in w_lines[].wl_size */
1766 row = 0;
1767 srow = 0;
1768 lnum = wp->w_topline; /* first line shown in window */
1769 for (;;)
1770 {
1771 /* stop updating when reached the end of the window (check for _past_
1772 * the end of the window is at the end of the loop) */
1773 if (row == wp->w_height)
1774 {
1775 didline = TRUE;
1776 break;
1777 }
1778
1779 /* stop updating when hit the end of the file */
1780 if (lnum > buf->b_ml.ml_line_count)
1781 {
1782 eof = TRUE;
1783 break;
1784 }
1785
1786 /* Remember the starting row of the line that is going to be dealt
1787 * with. It is used further down when the line doesn't fit. */
1788 srow = row;
1789
1790 /*
1791 * Update a line when it is in an area that needs updating, when it
1792 * has changes or w_lines[idx] is invalid.
1793 * bot_start may be halfway a wrapped line after using
1794 * win_del_lines(), check if the current line includes it.
1795 * When syntax folding is being used, the saved syntax states will
1796 * already have been updated, we can't see where the syntax state is
1797 * the same again, just update until the end of the window.
1798 */
1799 if (row < top_end
1800 || (row >= mid_start && row < mid_end)
1801#ifdef FEAT_SEARCH_EXTRA
1802 || top_to_mod
1803#endif
1804 || idx >= wp->w_lines_valid
1805 || (row + wp->w_lines[idx].wl_size > bot_start)
1806 || (mod_top != 0
1807 && (lnum == mod_top
1808 || (lnum >= mod_top
1809 && (lnum < mod_bot
1810#ifdef FEAT_SYN_HL
1811 || did_update == DID_FOLD
1812 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001813 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 && (
1815# ifdef FEAT_FOLDING
1816 (foldmethodIsSyntax(wp)
1817 && hasAnyFolding(wp)) ||
1818# endif
1819 syntax_check_changed(lnum)))
1820#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001821#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001822 /* match in fixed position might need redraw
1823 * if lines were inserted or deleted */
1824 || (wp->w_match_head != NULL
1825 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 )))))
1828 {
1829#ifdef FEAT_SEARCH_EXTRA
1830 if (lnum == mod_top)
1831 top_to_mod = FALSE;
1832#endif
1833
1834 /*
1835 * When at start of changed lines: May scroll following lines
1836 * up or down to minimize redrawing.
1837 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001838 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 */
1840 if (lnum == mod_top
1841 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001842 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 {
1844 int old_rows = 0;
1845 int new_rows = 0;
1846 int xtra_rows;
1847 linenr_T l;
1848
1849 /* Count the old number of window rows, using w_lines[], which
1850 * should still contain the sizes for the lines as they are
1851 * currently displayed. */
1852 for (i = idx; i < wp->w_lines_valid; ++i)
1853 {
1854 /* Only valid lines have a meaningful wl_lnum. Invalid
1855 * lines are part of the changed area. */
1856 if (wp->w_lines[i].wl_valid
1857 && wp->w_lines[i].wl_lnum == mod_bot)
1858 break;
1859 old_rows += wp->w_lines[i].wl_size;
1860#ifdef FEAT_FOLDING
1861 if (wp->w_lines[i].wl_valid
1862 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1863 {
1864 /* Must have found the last valid entry above mod_bot.
1865 * Add following invalid entries. */
1866 ++i;
1867 while (i < wp->w_lines_valid
1868 && !wp->w_lines[i].wl_valid)
1869 old_rows += wp->w_lines[i++].wl_size;
1870 break;
1871 }
1872#endif
1873 }
1874
1875 if (i >= wp->w_lines_valid)
1876 {
1877 /* We can't find a valid line below the changed lines,
1878 * need to redraw until the end of the window.
1879 * Inserting/deleting lines has no use. */
1880 bot_start = 0;
1881 }
1882 else
1883 {
1884 /* Able to count old number of rows: Count new window
1885 * rows, and may insert/delete lines */
1886 j = idx;
1887 for (l = lnum; l < mod_bot; ++l)
1888 {
1889#ifdef FEAT_FOLDING
1890 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1891 ++new_rows;
1892 else
1893#endif
1894#ifdef FEAT_DIFF
1895 if (l == wp->w_topline)
1896 new_rows += plines_win_nofill(wp, l, TRUE)
1897 + wp->w_topfill;
1898 else
1899#endif
1900 new_rows += plines_win(wp, l, TRUE);
1901 ++j;
1902 if (new_rows > wp->w_height - row - 2)
1903 {
1904 /* it's getting too much, must redraw the rest */
1905 new_rows = 9999;
1906 break;
1907 }
1908 }
1909 xtra_rows = new_rows - old_rows;
1910 if (xtra_rows < 0)
1911 {
1912 /* May scroll text up. If there is not enough
1913 * remaining text or scrolling fails, must redraw the
1914 * rest. If scrolling works, must redraw the text
1915 * below the scrolled text. */
1916 if (row - xtra_rows >= wp->w_height - 2)
1917 mod_bot = MAXLNUM;
1918 else
1919 {
1920 check_for_delay(FALSE);
1921 if (win_del_lines(wp, row,
1922 -xtra_rows, FALSE, FALSE) == FAIL)
1923 mod_bot = MAXLNUM;
1924 else
1925 bot_start = wp->w_height + xtra_rows;
1926 }
1927 }
1928 else if (xtra_rows > 0)
1929 {
1930 /* May scroll text down. If there is not enough
1931 * remaining text of scrolling fails, must redraw the
1932 * rest. */
1933 if (row + xtra_rows >= wp->w_height - 2)
1934 mod_bot = MAXLNUM;
1935 else
1936 {
1937 check_for_delay(FALSE);
1938 if (win_ins_lines(wp, row + old_rows,
1939 xtra_rows, FALSE, FALSE) == FAIL)
1940 mod_bot = MAXLNUM;
1941 else if (top_end > row + old_rows)
1942 /* Scrolled the part at the top that requires
1943 * updating down. */
1944 top_end += xtra_rows;
1945 }
1946 }
1947
1948 /* When not updating the rest, may need to move w_lines[]
1949 * entries. */
1950 if (mod_bot != MAXLNUM && i != j)
1951 {
1952 if (j < i)
1953 {
1954 int x = row + new_rows;
1955
1956 /* move entries in w_lines[] upwards */
1957 for (;;)
1958 {
1959 /* stop at last valid entry in w_lines[] */
1960 if (i >= wp->w_lines_valid)
1961 {
1962 wp->w_lines_valid = j;
1963 break;
1964 }
1965 wp->w_lines[j] = wp->w_lines[i];
1966 /* stop at a line that won't fit */
1967 if (x + (int)wp->w_lines[j].wl_size
1968 > wp->w_height)
1969 {
1970 wp->w_lines_valid = j + 1;
1971 break;
1972 }
1973 x += wp->w_lines[j++].wl_size;
1974 ++i;
1975 }
1976 if (bot_start > x)
1977 bot_start = x;
1978 }
1979 else /* j > i */
1980 {
1981 /* move entries in w_lines[] downwards */
1982 j -= i;
1983 wp->w_lines_valid += j;
1984 if (wp->w_lines_valid > wp->w_height)
1985 wp->w_lines_valid = wp->w_height;
1986 for (i = wp->w_lines_valid; i - j >= idx; --i)
1987 wp->w_lines[i] = wp->w_lines[i - j];
1988
1989 /* The w_lines[] entries for inserted lines are
1990 * now invalid, but wl_size may be used above.
1991 * Reset to zero. */
1992 while (i >= idx)
1993 {
1994 wp->w_lines[i].wl_size = 0;
1995 wp->w_lines[i--].wl_valid = FALSE;
1996 }
1997 }
1998 }
1999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 }
2001
2002#ifdef FEAT_FOLDING
2003 /*
2004 * When lines are folded, display one line for all of them.
2005 * Otherwise, display normally (can be several display lines when
2006 * 'wrap' is on).
2007 */
2008 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2009 if (fold_count != 0)
2010 {
2011 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2012 ++row;
2013 --fold_count;
2014 wp->w_lines[idx].wl_folded = TRUE;
2015 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2016# ifdef FEAT_SYN_HL
2017 did_update = DID_FOLD;
2018# endif
2019 }
2020 else
2021#endif
2022 if (idx < wp->w_lines_valid
2023 && wp->w_lines[idx].wl_valid
2024 && wp->w_lines[idx].wl_lnum == lnum
2025 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002026 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 && srow + wp->w_lines[idx].wl_size > wp->w_height
2028#ifdef FEAT_DIFF
2029 && diff_check_fill(wp, lnum) == 0
2030#endif
2031 )
2032 {
2033 /* This line is not going to fit. Don't draw anything here,
2034 * will draw "@ " lines below. */
2035 row = wp->w_height + 1;
2036 }
2037 else
2038 {
2039#ifdef FEAT_SEARCH_EXTRA
2040 prepare_search_hl(wp, lnum);
2041#endif
2042#ifdef FEAT_SYN_HL
2043 /* Let the syntax stuff know we skipped a few lines. */
2044 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002045 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 syntax_end_parsing(syntax_last_parsed + 1);
2047#endif
2048
2049 /*
2050 * Display one line.
2051 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002052 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054#ifdef FEAT_FOLDING
2055 wp->w_lines[idx].wl_folded = FALSE;
2056 wp->w_lines[idx].wl_lastlnum = lnum;
2057#endif
2058#ifdef FEAT_SYN_HL
2059 did_update = DID_LINE;
2060 syntax_last_parsed = lnum;
2061#endif
2062 }
2063
2064 wp->w_lines[idx].wl_lnum = lnum;
2065 wp->w_lines[idx].wl_valid = TRUE;
2066 if (row > wp->w_height) /* past end of screen */
2067 {
2068 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002069 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2071 ++idx;
2072 break;
2073 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002074 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 wp->w_lines[idx].wl_size = row - srow;
2076 ++idx;
2077#ifdef FEAT_FOLDING
2078 lnum += fold_count + 1;
2079#else
2080 ++lnum;
2081#endif
2082 }
2083 else
2084 {
2085 /* This line does not need updating, advance to the next one */
2086 row += wp->w_lines[idx++].wl_size;
2087 if (row > wp->w_height) /* past end of screen */
2088 break;
2089#ifdef FEAT_FOLDING
2090 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2091#else
2092 ++lnum;
2093#endif
2094#ifdef FEAT_SYN_HL
2095 did_update = DID_NONE;
2096#endif
2097 }
2098
2099 if (lnum > buf->b_ml.ml_line_count)
2100 {
2101 eof = TRUE;
2102 break;
2103 }
2104 }
2105 /*
2106 * End of loop over all window lines.
2107 */
2108
2109
2110 if (idx > wp->w_lines_valid)
2111 wp->w_lines_valid = idx;
2112
2113#ifdef FEAT_SYN_HL
2114 /*
2115 * Let the syntax stuff know we stop parsing here.
2116 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002117 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 syntax_end_parsing(syntax_last_parsed + 1);
2119#endif
2120
2121 /*
2122 * If we didn't hit the end of the file, and we didn't finish the last
2123 * line we were working on, then the line didn't fit.
2124 */
2125 wp->w_empty_rows = 0;
2126#ifdef FEAT_DIFF
2127 wp->w_filler_rows = 0;
2128#endif
2129 if (!eof && !didline)
2130 {
2131 if (lnum == wp->w_topline)
2132 {
2133 /*
2134 * Single line that does not fit!
2135 * Don't overwrite it, it can be edited.
2136 */
2137 wp->w_botline = lnum + 1;
2138 }
2139#ifdef FEAT_DIFF
2140 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2141 {
2142 /* Window ends in filler lines. */
2143 wp->w_botline = lnum;
2144 wp->w_filler_rows = wp->w_height - srow;
2145 }
2146#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002147 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2148 {
2149 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2150
2151 /*
2152 * Last line isn't finished: Display "@@@" in the last screen line.
2153 */
2154 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
2155 hl_attr(HLF_AT));
2156 screen_fill(scr_row, scr_row + 1,
2157 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
2158 '@', ' ', hl_attr(HLF_AT));
2159 set_empty_rows(wp, srow);
2160 wp->w_botline = lnum;
2161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2163 {
2164 /*
2165 * Last line isn't finished: Display "@@@" at the end.
2166 */
2167 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2168 W_WINROW(wp) + wp->w_height,
2169 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2170 '@', '@', hl_attr(HLF_AT));
2171 set_empty_rows(wp, srow);
2172 wp->w_botline = lnum;
2173 }
2174 else
2175 {
2176 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2177 wp->w_botline = lnum;
2178 }
2179 }
2180 else
2181 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002182#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 draw_vsep_win(wp, row);
2184#endif
2185 if (eof) /* we hit the end of the file */
2186 {
2187 wp->w_botline = buf->b_ml.ml_line_count + 1;
2188#ifdef FEAT_DIFF
2189 j = diff_check_fill(wp, wp->w_botline);
2190 if (j > 0 && !wp->w_botfill)
2191 {
2192 /*
2193 * Display filler lines at the end of the file
2194 */
2195 if (char2cells(fill_diff) > 1)
2196 i = '-';
2197 else
2198 i = fill_diff;
2199 if (row + j > wp->w_height)
2200 j = wp->w_height - row;
2201 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2202 row += j;
2203 }
2204#endif
2205 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002206 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 wp->w_botline = lnum;
2208
2209 /* make sure the rest of the screen is blank */
2210 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002211 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 }
2213
2214 /* Reset the type of redrawing required, the window has been updated. */
2215 wp->w_redr_type = 0;
2216#ifdef FEAT_DIFF
2217 wp->w_old_topfill = wp->w_topfill;
2218 wp->w_old_botfill = wp->w_botfill;
2219#endif
2220
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002221 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
2223 /*
2224 * There is a trick with w_botline. If we invalidate it on each
2225 * change that might modify it, this will cause a lot of expensive
2226 * calls to plines() in update_topline() each time. Therefore the
2227 * value of w_botline is often approximated, and this value is used to
2228 * compute the value of w_topline. If the value of w_botline was
2229 * wrong, check that the value of w_topline is correct (cursor is on
2230 * the visible part of the text). If it's not, we need to redraw
2231 * again. Mostly this just means scrolling up a few lines, so it
2232 * doesn't look too bad. Only do this for the current window (where
2233 * changes are relevant).
2234 */
2235 wp->w_valid |= VALID_BOTLINE;
2236 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2237 {
2238 recursive = TRUE;
2239 curwin->w_valid &= ~VALID_TOPLINE;
2240 update_topline(); /* may invalidate w_botline again */
2241 if (must_redraw != 0)
2242 {
2243 /* Don't update for changes in buffer again. */
2244 i = curbuf->b_mod_set;
2245 curbuf->b_mod_set = FALSE;
2246 win_update(curwin);
2247 must_redraw = 0;
2248 curbuf->b_mod_set = i;
2249 }
2250 recursive = FALSE;
2251 }
2252 }
2253
2254#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2255 /* restore got_int, unless CTRL-C was hit while redrawing */
2256 if (!got_int)
2257 got_int = save_got_int;
2258#endif
2259}
2260
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261/*
2262 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2263 * as the filler character.
2264 */
2265 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002266win_draw_end(
2267 win_T *wp,
2268 int c1,
2269 int c2,
2270 int row,
2271 int endrow,
2272 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
2274#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2275 int n = 0;
2276# define FDC_OFF n
2277#else
2278# define FDC_OFF 0
2279#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002280#ifdef FEAT_FOLDING
2281 int fdc = compute_foldcolumn(wp, 0);
2282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
2284#ifdef FEAT_RIGHTLEFT
2285 if (wp->w_p_rl)
2286 {
2287 /* No check for cmdline window: should never be right-left. */
2288# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002289 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
2291 if (n > 0)
2292 {
2293 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002294 if (n > W_WIDTH(wp))
2295 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2297 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2298 ' ', ' ', hl_attr(HLF_FC));
2299 }
2300# endif
2301# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002302 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 int nn = n + 2;
2305
2306 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002307 if (nn > W_WIDTH(wp))
2308 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2310 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2311 ' ', ' ', hl_attr(HLF_SC));
2312 n = nn;
2313 }
2314# endif
2315 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2316 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2317 c2, c2, hl_attr(hl));
2318 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2319 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2320 c1, c2, hl_attr(hl));
2321 }
2322 else
2323#endif
2324 {
2325#ifdef FEAT_CMDWIN
2326 if (cmdwin_type != 0 && wp == curwin)
2327 {
2328 /* draw the cmdline character in the leftmost column */
2329 n = 1;
2330 if (n > wp->w_width)
2331 n = wp->w_width;
2332 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2333 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2334 cmdwin_type, ' ', hl_attr(HLF_AT));
2335 }
2336#endif
2337#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002338 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002340 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
2342 /* draw the fold column at the left */
2343 if (nn > W_WIDTH(wp))
2344 nn = W_WIDTH(wp);
2345 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2346 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2347 ' ', ' ', hl_attr(HLF_FC));
2348 n = nn;
2349 }
2350#endif
2351#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002352 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
2354 int nn = n + 2;
2355
2356 /* draw the sign column after the fold column */
2357 if (nn > W_WIDTH(wp))
2358 nn = W_WIDTH(wp);
2359 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2360 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2361 ' ', ' ', hl_attr(HLF_SC));
2362 n = nn;
2363 }
2364#endif
2365 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2366 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2367 c1, c2, hl_attr(hl));
2368 }
2369 set_empty_rows(wp, row);
2370}
2371
Bram Moolenaar1a384422010-07-14 19:53:30 +02002372#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002373static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002374
2375/*
2376 * Advance **color_cols and return TRUE when there are columns to draw.
2377 */
2378 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002379advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002380{
2381 while (**color_cols >= 0 && vcol > **color_cols)
2382 ++*color_cols;
2383 return (**color_cols >= 0);
2384}
2385#endif
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#ifdef FEAT_FOLDING
2388/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002389 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2390 * space is available for window "wp", minus "col".
2391 */
2392 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002393compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002394{
2395 int fdc = wp->w_p_fdc;
2396 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2397 int wwidth = W_WIDTH(wp);
2398
2399 if (fdc > wwidth - (col + wmw))
2400 fdc = wwidth - (col + wmw);
2401 return fdc;
2402}
2403
2404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 * Display one folded line.
2406 */
2407 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002408fold_line(
2409 win_T *wp,
2410 long fold_count,
2411 foldinfo_T *foldinfo,
2412 linenr_T lnum,
2413 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002415 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 pos_T *top, *bot;
2417 linenr_T lnume = lnum + fold_count - 1;
2418 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002419 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 int col;
2422 int txtcol;
2423 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002424 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 /* Build the fold line:
2427 * 1. Add the cmdwin_type for the command-line window
2428 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002429 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 * 4. Compose the text
2431 * 5. Add the text
2432 * 6. set highlighting for the Visual area an other text
2433 */
2434 col = 0;
2435
2436 /*
2437 * 1. Add the cmdwin_type for the command-line window
2438 * Ignores 'rightleft', this window is never right-left.
2439 */
2440#ifdef FEAT_CMDWIN
2441 if (cmdwin_type != 0 && wp == curwin)
2442 {
2443 ScreenLines[off] = cmdwin_type;
2444 ScreenAttrs[off] = hl_attr(HLF_AT);
2445#ifdef FEAT_MBYTE
2446 if (enc_utf8)
2447 ScreenLinesUC[off] = 0;
2448#endif
2449 ++col;
2450 }
2451#endif
2452
2453 /*
2454 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002455 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002457 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 if (fdc > 0)
2459 {
2460 fill_foldcolumn(buf, wp, TRUE, lnum);
2461#ifdef FEAT_RIGHTLEFT
2462 if (wp->w_p_rl)
2463 {
2464 int i;
2465
2466 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2467 hl_attr(HLF_FC));
2468 /* reverse the fold column */
2469 for (i = 0; i < fdc; ++i)
2470 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2471 }
2472 else
2473#endif
2474 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2475 col += fdc;
2476 }
2477
2478#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002479# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2480 for (ri = 0; ri < l; ++ri) \
2481 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2482 else \
2483 for (ri = 0; ri < l; ++ri) \
2484 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002486# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2487 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488#endif
2489
Bram Moolenaar64486672010-05-16 15:46:46 +02002490 /* Set all attributes of the 'number' or 'relativenumber' column and the
2491 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002492 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494#ifdef FEAT_SIGNS
2495 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002496 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 {
2498 len = W_WIDTH(wp) - col;
2499 if (len > 0)
2500 {
2501 if (len > 2)
2502 len = 2;
2503# ifdef FEAT_RIGHTLEFT
2504 if (wp->w_p_rl)
2505 /* the line number isn't reversed */
2506 copy_text_attr(off + W_WIDTH(wp) - len - col,
2507 (char_u *)" ", len, hl_attr(HLF_FL));
2508 else
2509# endif
2510 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2511 col += len;
2512 }
2513 }
2514#endif
2515
2516 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002517 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002519 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 len = W_WIDTH(wp) - col;
2522 if (len > 0)
2523 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002524 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002525 long num;
2526 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002527
2528 if (len > w + 1)
2529 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002530
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002531 if (wp->w_p_nu && !wp->w_p_rnu)
2532 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002533 num = (long)lnum;
2534 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002535 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002536 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002537 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002538 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002539 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002540 /* 'number' + 'relativenumber': cursor line shows absolute
2541 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002542 num = lnum;
2543 fmt = "%-*ld ";
2544 }
2545 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002546
Bram Moolenaar700e7342013-01-30 12:31:36 +01002547 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548#ifdef FEAT_RIGHTLEFT
2549 if (wp->w_p_rl)
2550 /* the line number isn't reversed */
2551 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2552 hl_attr(HLF_FL));
2553 else
2554#endif
2555 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2556 col += len;
2557 }
2558 }
2559
2560 /*
2561 * 4. Compose the folded-line string with 'foldtext', if set.
2562 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002563 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
2565 txtcol = col; /* remember where text starts */
2566
2567 /*
2568 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2569 * Right-left text is put in columns 0 - number-col, normal text is put
2570 * in columns number-col - window-width.
2571 */
2572#ifdef FEAT_MBYTE
2573 if (has_mbyte)
2574 {
2575 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002576 int u8c, u8cc[MAX_MCO];
2577 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 int idx;
2579 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002580 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581# ifdef FEAT_ARABIC
2582 int prev_c = 0; /* previous Arabic character */
2583 int prev_c1 = 0; /* first composing char for prev_c */
2584# endif
2585
2586# ifdef FEAT_RIGHTLEFT
2587 if (wp->w_p_rl)
2588 idx = off;
2589 else
2590# endif
2591 idx = off + col;
2592
2593 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2594 for (p = text; *p != NUL; )
2595 {
2596 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002597 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 if (col + cells > W_WIDTH(wp)
2599# ifdef FEAT_RIGHTLEFT
2600 - (wp->w_p_rl ? col : 0)
2601# endif
2602 )
2603 break;
2604 ScreenLines[idx] = *p;
2605 if (enc_utf8)
2606 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002607 u8c = utfc_ptr2char(p, u8cc);
2608 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
2610 ScreenLinesUC[idx] = 0;
2611#ifdef FEAT_ARABIC
2612 prev_c = u8c;
2613#endif
2614 }
2615 else
2616 {
2617#ifdef FEAT_ARABIC
2618 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2619 {
2620 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002621 int pc, pc1, nc;
2622 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 int firstbyte = *p;
2624
2625 /* The idea of what is the previous and next
2626 * character depends on 'rightleft'. */
2627 if (wp->w_p_rl)
2628 {
2629 pc = prev_c;
2630 pc1 = prev_c1;
2631 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002632 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
2634 else
2635 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002636 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002638 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
2640 prev_c = u8c;
2641
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002642 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 pc, pc1, nc);
2644 ScreenLines[idx] = firstbyte;
2645 }
2646 else
2647 prev_c = u8c;
2648#endif
2649 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002650#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (u8c >= 0x10000)
2652 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2653 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002656 for (i = 0; i < Screen_mco; ++i)
2657 {
2658 ScreenLinesC[i][idx] = u8cc[i];
2659 if (u8cc[i] == 0)
2660 break;
2661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663 if (cells > 1)
2664 ScreenLines[idx + 1] = 0;
2665 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002666 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2667 /* double-byte single width character */
2668 ScreenLines2[idx] = p[1];
2669 else if (cells > 1)
2670 /* double-width character */
2671 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 col += cells;
2673 idx += cells;
2674 p += c_len;
2675 }
2676 }
2677 else
2678#endif
2679 {
2680 len = (int)STRLEN(text);
2681 if (len > W_WIDTH(wp) - col)
2682 len = W_WIDTH(wp) - col;
2683 if (len > 0)
2684 {
2685#ifdef FEAT_RIGHTLEFT
2686 if (wp->w_p_rl)
2687 STRNCPY(current_ScreenLine, text, len);
2688 else
2689#endif
2690 STRNCPY(current_ScreenLine + col, text, len);
2691 col += len;
2692 }
2693 }
2694
2695 /* Fill the rest of the line with the fold filler */
2696#ifdef FEAT_RIGHTLEFT
2697 if (wp->w_p_rl)
2698 col -= txtcol;
2699#endif
2700 while (col < W_WIDTH(wp)
2701#ifdef FEAT_RIGHTLEFT
2702 - (wp->w_p_rl ? txtcol : 0)
2703#endif
2704 )
2705 {
2706#ifdef FEAT_MBYTE
2707 if (enc_utf8)
2708 {
2709 if (fill_fold >= 0x80)
2710 {
2711 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002712 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 else
2715 ScreenLinesUC[off + col] = 0;
2716 }
2717#endif
2718 ScreenLines[off + col++] = fill_fold;
2719 }
2720
2721 if (text != buf)
2722 vim_free(text);
2723
2724 /*
2725 * 6. set highlighting for the Visual area an other text.
2726 * If all folded lines are in the Visual area, highlight the line.
2727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2729 {
2730 if (ltoreq(curwin->w_cursor, VIsual))
2731 {
2732 /* Visual is after curwin->w_cursor */
2733 top = &curwin->w_cursor;
2734 bot = &VIsual;
2735 }
2736 else
2737 {
2738 /* Visual is before curwin->w_cursor */
2739 top = &VIsual;
2740 bot = &curwin->w_cursor;
2741 }
2742 if (lnum >= top->lnum
2743 && lnume <= bot->lnum
2744 && (VIsual_mode != 'v'
2745 || ((lnum > top->lnum
2746 || (lnum == top->lnum
2747 && top->col == 0))
2748 && (lnume < bot->lnum
2749 || (lnume == bot->lnum
2750 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002751 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 {
2753 if (VIsual_mode == Ctrl_V)
2754 {
2755 /* Visual block mode: highlight the chars part of the block */
2756 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2757 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002758 if (wp->w_old_cursor_lcol != MAXCOL
2759 && wp->w_old_cursor_lcol + txtcol
2760 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 len = wp->w_old_cursor_lcol;
2762 else
2763 len = W_WIDTH(wp) - txtcol;
2764 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002765 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767 }
2768 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002771 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
2774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002776#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002777 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2778 if (wp->w_p_cc_cols)
2779 {
2780 int i = 0;
2781 int j = wp->w_p_cc_cols[i];
2782 int old_txtcol = txtcol;
2783
2784 while (j > -1)
2785 {
2786 txtcol += j;
2787 if (wp->w_p_wrap)
2788 txtcol -= wp->w_skipcol;
2789 else
2790 txtcol -= wp->w_leftcol;
2791 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2792 ScreenAttrs[off + txtcol] = hl_combine_attr(
2793 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2794 txtcol = old_txtcol;
2795 j = wp->w_p_cc_cols[++i];
2796 }
2797 }
2798
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002799 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002800 if (wp->w_p_cuc)
2801 {
2802 txtcol += wp->w_virtcol;
2803 if (wp->w_p_wrap)
2804 txtcol -= wp->w_skipcol;
2805 else
2806 txtcol -= wp->w_leftcol;
2807 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2808 ScreenAttrs[off + txtcol] = hl_combine_attr(
2809 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2810 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2814 (int)W_WIDTH(wp), FALSE);
2815
2816 /*
2817 * Update w_cline_height and w_cline_folded if the cursor line was
2818 * updated (saves a call to plines() later).
2819 */
2820 if (wp == curwin
2821 && lnum <= curwin->w_cursor.lnum
2822 && lnume >= curwin->w_cursor.lnum)
2823 {
2824 curwin->w_cline_row = row;
2825 curwin->w_cline_height = 1;
2826 curwin->w_cline_folded = TRUE;
2827 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2828 }
2829}
2830
2831/*
2832 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2833 */
2834 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002835copy_text_attr(
2836 int off,
2837 char_u *buf,
2838 int len,
2839 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002841 int i;
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 mch_memmove(ScreenLines + off, buf, (size_t)len);
2844# ifdef FEAT_MBYTE
2845 if (enc_utf8)
2846 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2847# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002848 for (i = 0; i < len; ++i)
2849 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850}
2851
2852/*
2853 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002854 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 */
2856 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002857fill_foldcolumn(
2858 char_u *p,
2859 win_T *wp,
2860 int closed, /* TRUE of FALSE */
2861 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 int i = 0;
2864 int level;
2865 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002866 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002867 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
2869 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002870 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
2872 level = win_foldinfo.fi_level;
2873 if (level > 0)
2874 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002875 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002876 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 /* If the column is too narrow, we start at the lowest level that
2879 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002880 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (first_level < 1)
2882 first_level = 1;
2883
Bram Moolenaar1c934292015-01-27 16:39:29 +01002884 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
2886 if (win_foldinfo.fi_lnum == lnum
2887 && first_level + i >= win_foldinfo.fi_low_level)
2888 p[i] = '-';
2889 else if (first_level == 1)
2890 p[i] = '|';
2891 else if (first_level + i <= 9)
2892 p[i] = '0' + first_level + i;
2893 else
2894 p[i] = '>';
2895 if (first_level + i == level)
2896 break;
2897 }
2898 }
2899 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002900 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901}
2902#endif /* FEAT_FOLDING */
2903
2904/*
2905 * Display line "lnum" of window 'wp' on the screen.
2906 * Start at row "startrow", stop when "endrow" is reached.
2907 * wp->w_virtcol needs to be valid.
2908 *
2909 * Return the number of last row the line occupies.
2910 */
2911 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002912win_line(
2913 win_T *wp,
2914 linenr_T lnum,
2915 int startrow,
2916 int endrow,
2917 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
2919 int col; /* visual column on screen */
2920 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2921 int c = 0; /* init for GCC */
2922 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002923#ifdef FEAT_LINEBREAK
2924 long vcol_sbr = -1; /* virtual column after showbreak */
2925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 long vcol_prev = -1; /* "vcol" of previous character */
2927 char_u *line; /* current line */
2928 char_u *ptr; /* current position in "line" */
2929 int row; /* row in the window, excl w_winrow */
2930 int screen_row; /* row on the screen, incl w_winrow */
2931
2932 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2933 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002934 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002935 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 int c_extra = NUL; /* extra chars, all the same */
2937 int extra_attr = 0; /* attributes when n_extra != 0 */
2938 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2939 displaying lcs_eol at end-of-line */
2940 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2941 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2942
2943 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2944 int saved_n_extra = 0;
2945 char_u *saved_p_extra = NULL;
2946 int saved_c_extra = 0;
2947 int saved_char_attr = 0;
2948
2949 int n_attr = 0; /* chars with special attr */
2950 int saved_attr2 = 0; /* char_attr saved for n_attr */
2951 int n_attr3 = 0; /* chars with overruling special attr */
2952 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2953
2954 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2955
2956 int fromcol, tocol; /* start/end of inverting */
2957 int fromcol_prev = -2; /* start of inverting after cursor */
2958 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002960 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 pos_T pos;
2962 long v;
2963
2964 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002965 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2967 in this line */
2968 int attr = 0; /* attributes for area highlighting */
2969 int area_attr = 0; /* attributes desired by highlighting */
2970 int search_attr = 0; /* attributes desired by 'hlsearch' */
2971#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002972 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 int syntax_attr = 0; /* attributes desired by syntax */
2974 int has_syntax = FALSE; /* this buffer has syntax highl. */
2975 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002976 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002977 int draw_color_col = FALSE; /* highlight colorcolumn */
2978 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002979#endif
2980#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002981 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002982# define SPWORDLEN 150
2983 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002984 int nextlinecol = 0; /* column where nextline[] starts */
2985 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002986 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002987 int spell_attr = 0; /* attributes desired by spelling */
2988 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002989 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2990 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002991 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002992 static int cap_col = -1; /* column to check for Cap word */
2993 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002994 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995#endif
2996 int extra_check; /* has syntax or linebreak */
2997#ifdef FEAT_MBYTE
2998 int multi_attr = 0; /* attributes desired by multibyte */
2999 int mb_l = 1; /* multi-byte byte length */
3000 int mb_c = 0; /* decoded multi-byte character */
3001 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003002 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003#endif
3004#ifdef FEAT_DIFF
3005 int filler_lines; /* nr of filler lines to be drawn */
3006 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003007 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 int change_start = MAXCOL; /* first col of changed area */
3009 int change_end = -1; /* last col of changed area */
3010#endif
3011 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3012#ifdef FEAT_LINEBREAK
3013 int need_showbreak = FALSE;
3014#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003015#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3016 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003018 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019#endif
3020#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003021 matchitem_T *cur; /* points to the match list */
3022 match_T *shl; /* points to search_hl or a match */
3023 int shl_flag; /* flag to indicate whether search_hl
3024 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003025 int pos_inprogress; /* marks that position match search is
3026 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003027 int prevcol_hl_flag; /* flag to indicate whether prevcol
3028 equals startcol of search_hl or one
3029 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030#endif
3031#ifdef FEAT_ARABIC
3032 int prev_c = 0; /* previous Arabic character */
3033 int prev_c1 = 0; /* first composing char for prev_c */
3034#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003035#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003036 int did_line_attr = 0;
3037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
3039 /* draw_state: items that are drawn in sequence: */
3040#define WL_START 0 /* nothing done yet */
3041#ifdef FEAT_CMDWIN
3042# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3043#else
3044# define WL_CMDLINE WL_START
3045#endif
3046#ifdef FEAT_FOLDING
3047# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3048#else
3049# define WL_FOLD WL_CMDLINE
3050#endif
3051#ifdef FEAT_SIGNS
3052# define WL_SIGN WL_FOLD + 1 /* column for signs */
3053#else
3054# define WL_SIGN WL_FOLD /* column for signs */
3055#endif
3056#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003057#ifdef FEAT_LINEBREAK
3058# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003060# define WL_BRI WL_NR
3061#endif
3062#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3063# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3064#else
3065# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066#endif
3067#define WL_LINE WL_SBR + 1 /* text in the line */
3068 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003069#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 int feedback_col = 0;
3071 int feedback_old_attr = -1;
3072#endif
3073
Bram Moolenaar860cae12010-06-05 23:22:07 +02003074#ifdef FEAT_CONCEAL
3075 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003076 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003077 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003078 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003079 int is_concealing = FALSE;
3080 int boguscols = 0; /* nonexistent columns added to force
3081 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003082 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003083 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003084 int match_conc = 0; /* cchar for match functions */
3085 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003086 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003087# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003088# define FIX_FOR_BOGUSCOLS \
3089 { \
3090 n_extra += vcol_off; \
3091 vcol -= vcol_off; \
3092 vcol_off = 0; \
3093 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003094 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003095 boguscols = 0; \
3096 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003097#else
3098# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
3101 if (startrow > endrow) /* past the end already! */
3102 return startrow;
3103
3104 row = startrow;
3105 screen_row = row + W_WINROW(wp);
3106
3107 /*
3108 * To speed up the loop below, set extra_check when there is linebreak,
3109 * trailing white space and/or syntax processing to be done.
3110 */
3111#ifdef FEAT_LINEBREAK
3112 extra_check = wp->w_p_lbr;
3113#else
3114 extra_check = 0;
3115#endif
3116#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003117 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 {
3119 /* Prepare for syntax highlighting in this line. When there is an
3120 * error, stop syntax highlighting. */
3121 save_did_emsg = did_emsg;
3122 did_emsg = FALSE;
3123 syntax_start(wp, lnum);
3124 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003125 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 else
3127 {
3128 did_emsg = save_did_emsg;
3129 has_syntax = TRUE;
3130 extra_check = TRUE;
3131 }
3132 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003133
3134 /* Check for columns to display for 'colorcolumn'. */
3135 color_cols = wp->w_p_cc_cols;
3136 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003137 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003138#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003139
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003140#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003141 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003142 && *wp->w_s->b_p_spl != NUL
3143 && wp->w_s->b_langp.ga_len > 0
3144 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003145 {
3146 /* Prepare for spell checking. */
3147 has_spell = TRUE;
3148 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003149
3150 /* Get the start of the next line, so that words that wrap to the next
3151 * line are found too: "et<line-break>al.".
3152 * Trick: skip a few chars for C/shell/Vim comments */
3153 nextline[SPWORDLEN] = NUL;
3154 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3155 {
3156 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3157 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3158 }
3159
3160 /* When a word wrapped from the previous line the start of the current
3161 * line is valid. */
3162 if (lnum == checked_lnum)
3163 cur_checked_col = checked_col;
3164 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003165
3166 /* When there was a sentence end in the previous line may require a
3167 * word starting with capital in this line. In line 1 always check
3168 * the first word. */
3169 if (lnum != capcol_lnum)
3170 cap_col = -1;
3171 if (lnum == 1)
3172 cap_col = 0;
3173 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175#endif
3176
3177 /*
3178 * handle visual active in this window
3179 */
3180 fromcol = -10;
3181 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3183 {
3184 /* Visual is after curwin->w_cursor */
3185 if (ltoreq(curwin->w_cursor, VIsual))
3186 {
3187 top = &curwin->w_cursor;
3188 bot = &VIsual;
3189 }
3190 else /* Visual is before curwin->w_cursor */
3191 {
3192 top = &VIsual;
3193 bot = &curwin->w_cursor;
3194 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003195 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 if (VIsual_mode == Ctrl_V) /* block mode */
3197 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003198 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 {
3200 fromcol = wp->w_old_cursor_fcol;
3201 tocol = wp->w_old_cursor_lcol;
3202 }
3203 }
3204 else /* non-block mode */
3205 {
3206 if (lnum > top->lnum && lnum <= bot->lnum)
3207 fromcol = 0;
3208 else if (lnum == top->lnum)
3209 {
3210 if (VIsual_mode == 'V') /* linewise */
3211 fromcol = 0;
3212 else
3213 {
3214 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3215 if (gchar_pos(top) == NUL)
3216 tocol = fromcol + 1;
3217 }
3218 }
3219 if (VIsual_mode != 'V' && lnum == bot->lnum)
3220 {
3221 if (*p_sel == 'e' && bot->col == 0
3222#ifdef FEAT_VIRTUALEDIT
3223 && bot->coladd == 0
3224#endif
3225 )
3226 {
3227 fromcol = -10;
3228 tocol = MAXCOL;
3229 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003230 else if (bot->col == MAXCOL)
3231 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 else
3233 {
3234 pos = *bot;
3235 if (*p_sel == 'e')
3236 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3237 else
3238 {
3239 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3240 ++tocol;
3241 }
3242 }
3243 }
3244 }
3245
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 /* Check if the character under the cursor should not be inverted */
3247 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003248#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 )
3252 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
3254 /* if inverting in this line set area_highlighting */
3255 if (fromcol >= 0)
3256 {
3257 area_highlighting = TRUE;
3258 attr = hl_attr(HLF_V);
3259#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003260 if ((clip_star.available && !clip_star.owned
3261 && clip_isautosel_star())
3262 || (clip_plus.available && !clip_plus.owned
3263 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 attr = hl_attr(HLF_VNC);
3265#endif
3266 }
3267 }
3268
3269 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003270 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003272 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 && wp == curwin
3274 && lnum >= curwin->w_cursor.lnum
3275 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3276 {
3277 if (lnum == curwin->w_cursor.lnum)
3278 getvcol(curwin, &(curwin->w_cursor),
3279 (colnr_T *)&fromcol, NULL, NULL);
3280 else
3281 fromcol = 0;
3282 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3283 {
3284 pos.lnum = lnum;
3285 pos.col = search_match_endcol;
3286 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3287 }
3288 else
3289 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003290 /* do at least one character; happens when past end of line */
3291 if (fromcol == tocol)
3292 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 area_highlighting = TRUE;
3294 attr = hl_attr(HLF_I);
3295 }
3296
3297#ifdef FEAT_DIFF
3298 filler_lines = diff_check(wp, lnum);
3299 if (filler_lines < 0)
3300 {
3301 if (filler_lines == -1)
3302 {
3303 if (diff_find_change(wp, lnum, &change_start, &change_end))
3304 diff_hlf = HLF_ADD; /* added line */
3305 else if (change_start == 0)
3306 diff_hlf = HLF_TXD; /* changed text */
3307 else
3308 diff_hlf = HLF_CHD; /* changed line */
3309 }
3310 else
3311 diff_hlf = HLF_ADD; /* added line */
3312 filler_lines = 0;
3313 area_highlighting = TRUE;
3314 }
3315 if (lnum == wp->w_topline)
3316 filler_lines = wp->w_topfill;
3317 filler_todo = filler_lines;
3318#endif
3319
3320#ifdef LINE_ATTR
3321# ifdef FEAT_SIGNS
3322 /* If this line has a sign with line highlighting set line_attr. */
3323 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3324 if (v != 0)
3325 line_attr = sign_get_attr((int)v, TRUE);
3326# endif
3327# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3328 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003329 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 line_attr = hl_attr(HLF_L);
3331# endif
3332 if (line_attr != 0)
3333 area_highlighting = TRUE;
3334#endif
3335
3336 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3337 ptr = line;
3338
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003339#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003340 if (has_spell)
3341 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003342 /* For checking first word with a capital skip white space. */
3343 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003344 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003345
Bram Moolenaar30abd282005-06-22 22:35:10 +00003346 /* To be able to spell-check over line boundaries copy the end of the
3347 * current line into nextline[]. Above the start of the next line was
3348 * copied to nextline[SPWORDLEN]. */
3349 if (nextline[SPWORDLEN] == NUL)
3350 {
3351 /* No next line or it is empty. */
3352 nextlinecol = MAXCOL;
3353 nextline_idx = 0;
3354 }
3355 else
3356 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003357 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003358 if (v < SPWORDLEN)
3359 {
3360 /* Short line, use it completely and append the start of the
3361 * next line. */
3362 nextlinecol = 0;
3363 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003364 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003365 nextline_idx = v + 1;
3366 }
3367 else
3368 {
3369 /* Long line, use only the last SPWORDLEN bytes. */
3370 nextlinecol = v - SPWORDLEN;
3371 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3372 nextline_idx = SPWORDLEN + 1;
3373 }
3374 }
3375 }
3376#endif
3377
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003378 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003380 if (lcs_space || lcs_trail)
3381 extra_check = TRUE;
3382 /* find start of trailing whitespace */
3383 if (lcs_trail)
3384 {
3385 trailcol = (colnr_T)STRLEN(ptr);
3386 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3387 --trailcol;
3388 trailcol += (colnr_T) (ptr - line);
3389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
3391
3392 /*
3393 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3394 * first character to be displayed.
3395 */
3396 if (wp->w_p_wrap)
3397 v = wp->w_skipcol;
3398 else
3399 v = wp->w_leftcol;
3400 if (v > 0)
3401 {
3402#ifdef FEAT_MBYTE
3403 char_u *prev_ptr = ptr;
3404#endif
3405 while (vcol < v && *ptr != NUL)
3406 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003407 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 vcol += c;
3409#ifdef FEAT_MBYTE
3410 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003412 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
3414
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003415 /* When:
3416 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003417 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003418 * - 'virtualedit' is set, or
3419 * - the visual mode is active,
3420 * the end of the line may be before the start of the displayed part.
3421 */
3422 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003423#ifdef FEAT_SYN_HL
3424 wp->w_p_cuc || draw_color_col ||
3425#endif
3426#ifdef FEAT_VIRTUALEDIT
3427 virtual_active() ||
3428#endif
3429 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434 /* Handle a character that's not completely on the screen: Put ptr at
3435 * that character but skip the first few screen characters. */
3436 if (vcol > v)
3437 {
3438 vcol -= c;
3439#ifdef FEAT_MBYTE
3440 ptr = prev_ptr;
3441#else
3442 --ptr;
3443#endif
3444 n_skip = v - vcol;
3445 }
3446
3447 /*
3448 * Adjust for when the inverted text is before the screen,
3449 * and when the start of the inverted text is before the screen.
3450 */
3451 if (tocol <= vcol)
3452 fromcol = 0;
3453 else if (fromcol >= 0 && fromcol < vcol)
3454 fromcol = vcol;
3455
3456#ifdef FEAT_LINEBREAK
3457 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3458 if (wp->w_p_wrap)
3459 need_showbreak = TRUE;
3460#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003461#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003462 /* When spell checking a word we need to figure out the start of the
3463 * word and if it's badly spelled or not. */
3464 if (has_spell)
3465 {
3466 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003467 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003468 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003469
3470 pos = wp->w_cursor;
3471 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003472 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003473 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003474
3475 /* spell_move_to() may call ml_get() and make "line" invalid */
3476 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3477 ptr = line + linecol;
3478
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003479 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003480 {
3481 /* no bad word found at line start, don't check until end of a
3482 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003483 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003484 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003485 }
3486 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003487 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003488 /* bad word found, use attributes until end of word */
3489 word_end = wp->w_cursor.col + len + 1;
3490
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003491 /* Turn index into actual attributes. */
3492 if (spell_hlf != HLF_COUNT)
3493 spell_attr = highlight_attr[spell_hlf];
3494 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003495 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003496
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003497# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003498 /* Need to restart syntax highlighting for this line. */
3499 if (has_syntax)
3500 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003501# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003502 }
3503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505
3506 /*
3507 * Correct highlighting for cursor that can't be disabled.
3508 * Avoids having to check this for each character.
3509 */
3510 if (fromcol >= 0)
3511 {
3512 if (noinvcur)
3513 {
3514 if ((colnr_T)fromcol == wp->w_virtcol)
3515 {
3516 /* highlighting starts at cursor, let it start just after the
3517 * cursor */
3518 fromcol_prev = fromcol;
3519 fromcol = -1;
3520 }
3521 else if ((colnr_T)fromcol < wp->w_virtcol)
3522 /* restart highlighting after the cursor */
3523 fromcol_prev = wp->w_virtcol;
3524 }
3525 if (fromcol >= tocol)
3526 fromcol = -1;
3527 }
3528
3529#ifdef FEAT_SEARCH_EXTRA
3530 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003531 * Handle highlighting the last used search pattern and matches.
3532 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003534 cur = wp->w_match_head;
3535 shl_flag = FALSE;
3536 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003538 if (shl_flag == FALSE)
3539 {
3540 shl = &search_hl;
3541 shl_flag = TRUE;
3542 }
3543 else
3544 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003545 shl->startcol = MAXCOL;
3546 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003548 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003549 v = (long)(ptr - line);
3550 if (cur != NULL)
3551 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003552 next_search_hl(wp, shl, lnum, (colnr_T)v,
3553 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003554
3555 /* Need to get the line again, a multi-line regexp may have made it
3556 * invalid. */
3557 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3558 ptr = line + v;
3559
3560 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003562 if (shl->lnum == lnum)
3563 shl->startcol = shl->rm.startpos[0].col;
3564 else
3565 shl->startcol = 0;
3566 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3567 - shl->rm.startpos[0].lnum)
3568 shl->endcol = shl->rm.endpos[0].col;
3569 else
3570 shl->endcol = MAXCOL;
3571 /* Highlight one character for an empty match. */
3572 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003575 if (has_mbyte && line[shl->endcol] != NUL)
3576 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3577 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003579 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003581 if ((long)shl->startcol < v) /* match at leftcol */
3582 {
3583 shl->attr_cur = shl->attr;
3584 search_attr = shl->attr;
3585 }
3586 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003588 if (shl != &search_hl && cur != NULL)
3589 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
3591#endif
3592
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003593#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003594 /* Cursor line highlighting for 'cursorline' in the current window. Not
3595 * when Visual mode is active, because it's not clear what is selected
3596 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003597 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003598 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003599 {
3600 line_attr = hl_attr(HLF_CUL);
3601 area_highlighting = TRUE;
3602 }
3603#endif
3604
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003605 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 col = 0;
3607#ifdef FEAT_RIGHTLEFT
3608 if (wp->w_p_rl)
3609 {
3610 /* Rightleft window: process the text in the normal direction, but put
3611 * it in current_ScreenLine[] from right to left. Start at the
3612 * rightmost column of the window. */
3613 col = W_WIDTH(wp) - 1;
3614 off += col;
3615 }
3616#endif
3617
3618 /*
3619 * Repeat for the whole displayed line.
3620 */
3621 for (;;)
3622 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003623#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003624 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 /* Skip this quickly when working on the text. */
3627 if (draw_state != WL_LINE)
3628 {
3629#ifdef FEAT_CMDWIN
3630 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3631 {
3632 draw_state = WL_CMDLINE;
3633 if (cmdwin_type != 0 && wp == curwin)
3634 {
3635 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003637 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 char_attr = hl_attr(HLF_AT);
3639 }
3640 }
3641#endif
3642
3643#ifdef FEAT_FOLDING
3644 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3645 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003646 int fdc = compute_foldcolumn(wp, 0);
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003649 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
3651 /* Draw the 'foldcolumn'. */
3652 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003653 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003655 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 c_extra = NUL;
3657 char_attr = hl_attr(HLF_FC);
3658 }
3659 }
3660#endif
3661
3662#ifdef FEAT_SIGNS
3663 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3664 {
3665 draw_state = WL_SIGN;
3666 /* Show the sign column when there are any signs in this
3667 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003668 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003670 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003672 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673# endif
3674
3675 /* Draw two cells with the sign value or blank. */
3676 c_extra = ' ';
3677 char_attr = hl_attr(HLF_SC);
3678 n_extra = 2;
3679
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003680 if (row == startrow
3681#ifdef FEAT_DIFF
3682 + filler_lines && filler_todo <= 0
3683#endif
3684 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
3686 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3687 SIGN_TEXT);
3688# ifdef FEAT_SIGN_ICONS
3689 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3690 SIGN_ICON);
3691 if (gui.in_use && icon_sign != 0)
3692 {
3693 /* Use the image in this position. */
3694 c_extra = SIGN_BYTE;
3695# ifdef FEAT_NETBEANS_INTG
3696 if (buf_signcount(wp->w_buffer, lnum) > 1)
3697 c_extra = MULTISIGN_BYTE;
3698# endif
3699 char_attr = icon_sign;
3700 }
3701 else
3702# endif
3703 if (text_sign != 0)
3704 {
3705 p_extra = sign_get_text(text_sign);
3706 if (p_extra != NULL)
3707 {
3708 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003709 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711 char_attr = sign_get_attr(text_sign, FALSE);
3712 }
3713 }
3714 }
3715 }
3716#endif
3717
3718 if (draw_state == WL_NR - 1 && n_extra == 0)
3719 {
3720 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003721 /* Display the absolute or relative line number. After the
3722 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3723 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 && (row == startrow
3725#ifdef FEAT_DIFF
3726 + filler_lines
3727#endif
3728 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3729 {
3730 /* Draw the line number (empty space after wrapping). */
3731 if (row == startrow
3732#ifdef FEAT_DIFF
3733 + filler_lines
3734#endif
3735 )
3736 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003737 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003738 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003739
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003740 if (wp->w_p_nu && !wp->w_p_rnu)
3741 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003742 num = (long)lnum;
3743 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003744 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003745 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003746 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003747 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003748 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003749 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003750 num = lnum;
3751 fmt = "%-*ld ";
3752 }
3753 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003754
Bram Moolenaar700e7342013-01-30 12:31:36 +01003755 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003756 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 if (wp->w_skipcol > 0)
3758 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3759 *p_extra = '-';
3760#ifdef FEAT_RIGHTLEFT
3761 if (wp->w_p_rl) /* reverse line numbers */
3762 rl_mirror(extra);
3763#endif
3764 p_extra = extra;
3765 c_extra = NUL;
3766 }
3767 else
3768 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003769 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003771#ifdef FEAT_SYN_HL
3772 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003773 * the current line differently.
3774 * TODO: Can we use CursorLine instead of CursorLineNr
3775 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003776 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003777 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003778 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 }
3781 }
3782
Bram Moolenaar597a4222014-06-25 14:39:50 +02003783#ifdef FEAT_LINEBREAK
3784 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3785 && n_extra == 0 && *p_sbr != NUL)
3786 /* draw indent after showbreak value */
3787 draw_state = WL_BRI;
3788 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3789 /* After the showbreak, draw the breakindent */
3790 draw_state = WL_BRI - 1;
3791
3792 /* draw 'breakindent': indent wrapped text accordingly */
3793 if (draw_state == WL_BRI - 1 && n_extra == 0)
3794 {
3795 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003796 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003797# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003798 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003799# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003800 )
3801 {
3802 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003803# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003804 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003805 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003806 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003807# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003808 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3809 char_attr = hl_combine_attr(char_attr,
3810 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003811# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003812 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003813# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003814 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003815 c_extra = ' ';
3816 n_extra = get_breakindent_win(wp,
3817 ml_get_buf(wp->w_buffer, lnum, FALSE));
3818 /* Correct end of highlighted area for 'breakindent',
3819 * required when 'linebreak' is also set. */
3820 if (tocol == vcol)
3821 tocol += n_extra;
3822 }
3823 }
3824#endif
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3827 if (draw_state == WL_SBR - 1 && n_extra == 0)
3828 {
3829 draw_state = WL_SBR;
3830# ifdef FEAT_DIFF
3831 if (filler_todo > 0)
3832 {
3833 /* Draw "deleted" diff line(s). */
3834 if (char2cells(fill_diff) > 1)
3835 c_extra = '-';
3836 else
3837 c_extra = fill_diff;
3838# ifdef FEAT_RIGHTLEFT
3839 if (wp->w_p_rl)
3840 n_extra = col + 1;
3841 else
3842# endif
3843 n_extra = W_WIDTH(wp) - col;
3844 char_attr = hl_attr(HLF_DED);
3845 }
3846# endif
3847# ifdef FEAT_LINEBREAK
3848 if (*p_sbr != NUL && need_showbreak)
3849 {
3850 /* Draw 'showbreak' at the start of each broken line. */
3851 p_extra = p_sbr;
3852 c_extra = NUL;
3853 n_extra = (int)STRLEN(p_sbr);
3854 char_attr = hl_attr(HLF_AT);
3855 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003856 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 /* Correct end of highlighted area for 'showbreak',
3858 * required when 'linebreak' is also set. */
3859 if (tocol == vcol)
3860 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003861#ifdef FEAT_SYN_HL
3862 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003863 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003864 char_attr = hl_combine_attr(char_attr,
3865 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
3868# endif
3869 }
3870#endif
3871
3872 if (draw_state == WL_LINE - 1 && n_extra == 0)
3873 {
3874 draw_state = WL_LINE;
3875 if (saved_n_extra)
3876 {
3877 /* Continue item from end of wrapped line. */
3878 n_extra = saved_n_extra;
3879 c_extra = saved_c_extra;
3880 p_extra = saved_p_extra;
3881 char_attr = saved_char_attr;
3882 }
3883 else
3884 char_attr = 0;
3885 }
3886 }
3887
3888 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003889 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003890 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891#ifdef FEAT_DIFF
3892 && filler_todo <= 0
3893#endif
3894 )
3895 {
3896 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3897 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003898 /* Pretend we have finished updating the window. Except when
3899 * 'cursorcolumn' is set. */
3900#ifdef FEAT_SYN_HL
3901 if (wp->w_p_cuc)
3902 row = wp->w_cline_row + wp->w_cline_height;
3903 else
3904#endif
3905 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 break;
3907 }
3908
3909 if (draw_state == WL_LINE && area_highlighting)
3910 {
3911 /* handle Visual or match highlighting in this line */
3912 if (vcol == fromcol
3913#ifdef FEAT_MBYTE
3914 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3915 && (*mb_ptr2cells)(ptr) > 1)
3916#endif
3917 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003918 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 && vcol < tocol))
3920 area_attr = attr; /* start highlighting */
3921 else if (area_attr != 0
3922 && (vcol == tocol
3923 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
3926#ifdef FEAT_SEARCH_EXTRA
3927 if (!n_extra)
3928 {
3929 /*
3930 * Check for start/end of search pattern match.
3931 * After end, check for start/end of next match.
3932 * When another match, have to check for start again.
3933 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003934 * Do this for 'search_hl' and the match list (ordered by
3935 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003937 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003938 cur = wp->w_match_head;
3939 shl_flag = FALSE;
3940 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003942 if (shl_flag == FALSE
3943 && ((cur != NULL
3944 && cur->priority > SEARCH_HL_PRIORITY)
3945 || cur == NULL))
3946 {
3947 shl = &search_hl;
3948 shl_flag = TRUE;
3949 }
3950 else
3951 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003952 if (cur != NULL)
3953 cur->pos.cur = 0;
3954 pos_inprogress = TRUE;
3955 while (shl->rm.regprog != NULL
3956 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003958 if (shl->startcol != MAXCOL
3959 && v >= (long)shl->startcol
3960 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003962#ifdef FEAT_MBYTE
3963 int tmp_col = v + MB_PTR2LEN(ptr);
3964
3965 if (shl->endcol < tmp_col)
3966 shl->endcol = tmp_col;
3967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003969#ifdef FEAT_CONCEAL
3970 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3971 == cur->hlg_id)
3972 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02003973 has_match_conc =
3974 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003975 match_conc = cur->conceal_char;
3976 }
3977 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02003978 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003981 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
3983 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003984#ifdef FEAT_CONCEAL
3985 prev_syntax_id = 0;
3986#endif
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003987 next_search_hl(wp, shl, lnum, (colnr_T)v,
3988 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003989 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003990 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
3992 /* Need to get the line again, a multi-line regexp
3993 * may have made it invalid. */
3994 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3995 ptr = line + v;
3996
3997 if (shl->lnum == lnum)
3998 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003999 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004001 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004003 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004005 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
4007 /* highlight empty match, try again after
4008 * it */
4009#ifdef FEAT_MBYTE
4010 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004011 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004012 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 else
4014#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004015 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017
4018 /* Loop to check if the match starts at the
4019 * current position */
4020 continue;
4021 }
4022 }
4023 break;
4024 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004025 if (shl != &search_hl && cur != NULL)
4026 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004028
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004029 /* Use attributes from match with highest priority among
4030 * 'search_hl' and the match list. */
4031 search_attr = search_hl.attr_cur;
4032 cur = wp->w_match_head;
4033 shl_flag = FALSE;
4034 while (cur != NULL || shl_flag == FALSE)
4035 {
4036 if (shl_flag == FALSE
4037 && ((cur != NULL
4038 && cur->priority > SEARCH_HL_PRIORITY)
4039 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004040 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004041 shl = &search_hl;
4042 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004043 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004044 else
4045 shl = &cur->hl;
4046 if (shl->attr_cur != 0)
4047 search_attr = shl->attr_cur;
4048 if (shl != &search_hl && cur != NULL)
4049 cur = cur->next;
4050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
4052#endif
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004055 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004057 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4058 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004060 if (diff_hlf == HLF_TXD && ptr - line > change_end
4061 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004063 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004064 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4065 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004068
4069 /* Decide which of the highlight attributes to use. */
4070 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004071#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004072 if (area_attr != 0)
4073 char_attr = hl_combine_attr(line_attr, area_attr);
4074 else if (search_attr != 0)
4075 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004076 /* Use line_attr when not in the Visual or 'incsearch' area
4077 * (area_attr may be 0 when "noinvcur" is set). */
4078 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004079 || vcol < fromcol || vcol_prev < fromcol_prev
4080 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004081 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004082#else
4083 if (area_attr != 0)
4084 char_attr = area_attr;
4085 else if (search_attr != 0)
4086 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004087#endif
4088 else
4089 {
4090 attr_pri = FALSE;
4091#ifdef FEAT_SYN_HL
4092 if (has_syntax)
4093 char_attr = syntax_attr;
4094 else
4095#endif
4096 char_attr = 0;
4097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 }
4099
4100 /*
4101 * Get the next character to put on the screen.
4102 */
4103 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004104 * The "p_extra" points to the extra stuff that is inserted to
4105 * represent special characters (non-printable stuff) and other
4106 * things. When all characters are the same, c_extra is used.
4107 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4108 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4110 */
4111 if (n_extra > 0)
4112 {
4113 if (c_extra != NUL)
4114 {
4115 c = c_extra;
4116#ifdef FEAT_MBYTE
4117 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4118 if (enc_utf8 && (*mb_char2len)(c) > 1)
4119 {
4120 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004121 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004122 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124 else
4125 mb_utf8 = FALSE;
4126#endif
4127 }
4128 else
4129 {
4130 c = *p_extra;
4131#ifdef FEAT_MBYTE
4132 if (has_mbyte)
4133 {
4134 mb_c = c;
4135 if (enc_utf8)
4136 {
4137 /* If the UTF-8 character is more than one byte:
4138 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004139 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 mb_utf8 = FALSE;
4141 if (mb_l > n_extra)
4142 mb_l = 1;
4143 else if (mb_l > 1)
4144 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004145 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004147 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149 }
4150 else
4151 {
4152 /* if this is a DBCS character, put it in "mb_c" */
4153 mb_l = MB_BYTE2LEN(c);
4154 if (mb_l >= n_extra)
4155 mb_l = 1;
4156 else if (mb_l > 1)
4157 mb_c = (c << 8) + p_extra[1];
4158 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004159 if (mb_l == 0) /* at the NUL at end-of-line */
4160 mb_l = 1;
4161
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 /* If a double-width char doesn't fit display a '>' in the
4163 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004164 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165# ifdef FEAT_RIGHTLEFT
4166 wp->w_p_rl ? (col <= 0) :
4167# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004168 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 && (*mb_char2cells)(mb_c) == 2)
4170 {
4171 c = '>';
4172 mb_c = c;
4173 mb_l = 1;
4174 mb_utf8 = FALSE;
4175 multi_attr = hl_attr(HLF_AT);
4176 /* put the pointer back to output the double-width
4177 * character at the start of the next line. */
4178 ++n_extra;
4179 --p_extra;
4180 }
4181 else
4182 {
4183 n_extra -= mb_l - 1;
4184 p_extra += mb_l - 1;
4185 }
4186 }
4187#endif
4188 ++p_extra;
4189 }
4190 --n_extra;
4191 }
4192 else
4193 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004194 if (p_extra_free != NULL)
4195 {
4196 vim_free(p_extra_free);
4197 p_extra_free = NULL;
4198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 /*
4200 * Get a character from the line itself.
4201 */
4202 c = *ptr;
4203#ifdef FEAT_MBYTE
4204 if (has_mbyte)
4205 {
4206 mb_c = c;
4207 if (enc_utf8)
4208 {
4209 /* If the UTF-8 character is more than one byte: Decode it
4210 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004211 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 mb_utf8 = FALSE;
4213 if (mb_l > 1)
4214 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004215 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 /* Overlong encoded ASCII or ASCII with composing char
4217 * is displayed normally, except a NUL. */
4218 if (mb_c < 0x80)
4219 c = mb_c;
4220 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004221
4222 /* At start of the line we can have a composing char.
4223 * Draw it as a space with a composing char. */
4224 if (utf_iscomposing(mb_c))
4225 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004226 int i;
4227
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004228 for (i = Screen_mco - 1; i > 0; --i)
4229 u8cc[i] = u8cc[i - 1];
4230 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004231 mb_c = ' ';
4232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234
4235 if ((mb_l == 1 && c >= 0x80)
4236 || (mb_l >= 1 && mb_c == 0)
4237 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004238# ifdef UNICODE16
4239 || mb_c >= 0x10000
4240# endif
4241 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
4243 /*
4244 * Illegal UTF-8 byte: display as <xx>.
4245 * Non-BMP character : display as ? or fullwidth ?.
4246 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004247# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 {
4251 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004252# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (wp->w_p_rl) /* reverse */
4254 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004257# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 else if (utf_char2cells(mb_c) != 2)
4259 STRCPY(extra, "?");
4260 else
4261 /* 0xff1f in UTF-8: full-width '?' */
4262 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264
4265 p_extra = extra;
4266 c = *p_extra;
4267 mb_c = mb_ptr2char_adv(&p_extra);
4268 mb_utf8 = (c >= 0x80);
4269 n_extra = (int)STRLEN(p_extra);
4270 c_extra = NUL;
4271 if (area_attr == 0 && search_attr == 0)
4272 {
4273 n_attr = n_extra + 1;
4274 extra_attr = hl_attr(HLF_8);
4275 saved_attr2 = char_attr; /* save current attr */
4276 }
4277 }
4278 else if (mb_l == 0) /* at the NUL at end-of-line */
4279 mb_l = 1;
4280#ifdef FEAT_ARABIC
4281 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4282 {
4283 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004284 int pc, pc1, nc;
4285 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287 /* The idea of what is the previous and next
4288 * character depends on 'rightleft'. */
4289 if (wp->w_p_rl)
4290 {
4291 pc = prev_c;
4292 pc1 = prev_c1;
4293 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004294 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 else
4297 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004298 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004300 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 prev_c = mb_c;
4303
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004304 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 else
4307 prev_c = mb_c;
4308#endif
4309 }
4310 else /* enc_dbcs */
4311 {
4312 mb_l = MB_BYTE2LEN(c);
4313 if (mb_l == 0) /* at the NUL at end-of-line */
4314 mb_l = 1;
4315 else if (mb_l > 1)
4316 {
4317 /* We assume a second byte below 32 is illegal.
4318 * Hopefully this is OK for all double-byte encodings!
4319 */
4320 if (ptr[1] >= 32)
4321 mb_c = (c << 8) + ptr[1];
4322 else
4323 {
4324 if (ptr[1] == NUL)
4325 {
4326 /* head byte at end of line */
4327 mb_l = 1;
4328 transchar_nonprint(extra, c);
4329 }
4330 else
4331 {
4332 /* illegal tail byte */
4333 mb_l = 2;
4334 STRCPY(extra, "XX");
4335 }
4336 p_extra = extra;
4337 n_extra = (int)STRLEN(extra) - 1;
4338 c_extra = NUL;
4339 c = *p_extra++;
4340 if (area_attr == 0 && search_attr == 0)
4341 {
4342 n_attr = n_extra + 1;
4343 extra_attr = hl_attr(HLF_8);
4344 saved_attr2 = char_attr; /* save current attr */
4345 }
4346 mb_c = c;
4347 }
4348 }
4349 }
4350 /* If a double-width char doesn't fit display a '>' in the
4351 * last column; the character is displayed at the start of the
4352 * next line. */
4353 if ((
4354# ifdef FEAT_RIGHTLEFT
4355 wp->w_p_rl ? (col <= 0) :
4356# endif
4357 (col >= W_WIDTH(wp) - 1))
4358 && (*mb_char2cells)(mb_c) == 2)
4359 {
4360 c = '>';
4361 mb_c = c;
4362 mb_utf8 = FALSE;
4363 mb_l = 1;
4364 multi_attr = hl_attr(HLF_AT);
4365 /* Put pointer back so that the character will be
4366 * displayed at the start of the next line. */
4367 --ptr;
4368 }
4369 else if (*ptr != NUL)
4370 ptr += mb_l - 1;
4371
4372 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004373 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004374 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004375 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004378 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 c = ' ';
4380 if (area_attr == 0 && search_attr == 0)
4381 {
4382 n_attr = n_extra + 1;
4383 extra_attr = hl_attr(HLF_AT);
4384 saved_attr2 = char_attr; /* save current attr */
4385 }
4386 mb_c = c;
4387 mb_utf8 = FALSE;
4388 mb_l = 1;
4389 }
4390
4391 }
4392#endif
4393 ++ptr;
4394
4395 if (extra_check)
4396 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004397#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004398 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004399#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004400
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004401#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 /* Get syntax attribute, unless still at the start of the line
4403 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004404 v = (long)(ptr - line);
4405 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 {
4407 /* Get the syntax attribute for the character. If there
4408 * is an error, disable syntax highlighting. */
4409 save_did_emsg = did_emsg;
4410 did_emsg = FALSE;
4411
Bram Moolenaar217ad922005-03-20 22:37:15 +00004412 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004413# ifdef FEAT_SPELL
4414 has_spell ? &can_spell :
4415# endif
4416 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004419 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004420 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004421 has_syntax = FALSE;
4422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 else
4424 did_emsg = save_did_emsg;
4425
4426 /* Need to get the line again, a multi-line regexp may
4427 * have made it invalid. */
4428 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4429 ptr = line + v;
4430
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004431 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004433 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004434 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004435# ifdef FEAT_CONCEAL
4436 /* no concealing past the end of the line, it interferes
4437 * with line highlighting */
4438 if (c == NUL)
4439 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004440 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004441 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004444#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004445
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004446#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004447 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004448 * Only do this when there is no syntax highlighting, the
4449 * @Spell cluster is not used or the current syntax item
4450 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004451 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004452 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004453 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004454# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004455 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004456 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004457# endif
4458 if (c != 0 && (
4459# ifdef FEAT_SYN_HL
4460 !has_syntax ||
4461# endif
4462 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004463 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004464 char_u *prev_ptr, *p;
4465 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004466 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004467# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004468 if (has_mbyte)
4469 {
4470 prev_ptr = ptr - mb_l;
4471 v -= mb_l - 1;
4472 }
4473 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004474# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004475 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004476
4477 /* Use nextline[] if possible, it has the start of the
4478 * next line concatenated. */
4479 if ((prev_ptr - line) - nextlinecol >= 0)
4480 p = nextline + (prev_ptr - line) - nextlinecol;
4481 else
4482 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004483 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004484 len = spell_check(wp, p, &spell_hlf, &cap_col,
4485 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004486 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004487
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004488 /* In Insert mode only highlight a word that
4489 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004490 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004491 && (State & INSERT) != 0
4492 && wp->w_cursor.lnum == lnum
4493 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004494 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004495 && wp->w_cursor.col < (colnr_T)word_end)
4496 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004497 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004498 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004499 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004500
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004501 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004502 && (p - nextline) + len > nextline_idx)
4503 {
4504 /* Remember that the good word continues at the
4505 * start of the next line. */
4506 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004507 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004508 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004509
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004510 /* Turn index into actual attributes. */
4511 if (spell_hlf != HLF_COUNT)
4512 spell_attr = highlight_attr[spell_hlf];
4513
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004514 if (cap_col > 0)
4515 {
4516 if (p != prev_ptr
4517 && (p - nextline) + cap_col >= nextline_idx)
4518 {
4519 /* Remember that the word in the next line
4520 * must start with a capital. */
4521 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004522 cap_col = (int)((p - nextline) + cap_col
4523 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004524 }
4525 else
4526 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004527 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004528 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004529 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004530 }
4531 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004532 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004533 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004534 char_attr = hl_combine_attr(char_attr, spell_attr);
4535 else
4536 char_attr = hl_combine_attr(spell_attr, char_attr);
4537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538#endif
4539#ifdef FEAT_LINEBREAK
4540 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004541 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004543 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004545# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004546 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004547# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004548 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004550 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004552 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004553
Bram Moolenaar597a4222014-06-25 14:39:50 +02004554 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004555 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004556 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004557 if (c == TAB && n_extra + col > W_WIDTH(wp))
4558 n_extra = (int)wp->w_buffer->b_p_ts
4559 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4560
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004561# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004562 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004563# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004567 {
4568#ifdef FEAT_CONCEAL
4569 if (c == TAB)
4570 /* See "Tab alignment" below. */
4571 FIX_FOR_BOGUSCOLS;
4572#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004573 if (!wp->w_p_list)
4574 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 }
4577#endif
4578
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004579 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4580 */
4581 if (wp->w_p_list
4582 && (((c == 160
4583#ifdef FEAT_MBYTE
4584 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4585#endif
4586 ) && lcs_nbsp)
4587 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4588 {
4589 c = (c == ' ') ? lcs_space : lcs_nbsp;
4590 if (area_attr == 0 && search_attr == 0)
4591 {
4592 n_attr = 1;
4593 extra_attr = hl_attr(HLF_8);
4594 saved_attr2 = char_attr; /* save current attr */
4595 }
4596#ifdef FEAT_MBYTE
4597 mb_c = c;
4598 if (enc_utf8 && (*mb_char2len)(c) > 1)
4599 {
4600 mb_utf8 = TRUE;
4601 u8cc[0] = 0;
4602 c = 0xc0;
4603 }
4604 else
4605 mb_utf8 = FALSE;
4606#endif
4607 }
4608
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4610 {
4611 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004612 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
4614 n_attr = 1;
4615 extra_attr = hl_attr(HLF_8);
4616 saved_attr2 = char_attr; /* save current attr */
4617 }
4618#ifdef FEAT_MBYTE
4619 mb_c = c;
4620 if (enc_utf8 && (*mb_char2len)(c) > 1)
4621 {
4622 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004623 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004624 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626 else
4627 mb_utf8 = FALSE;
4628#endif
4629 }
4630 }
4631
4632 /*
4633 * Handling of non-printable characters.
4634 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004635 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
4637 /*
4638 * when getting a character from the file, we may have to
4639 * turn it into something else on the way to putting it
4640 * into "ScreenLines".
4641 */
4642 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4643 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004644 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004645 long vcol_adjusted = vcol; /* removed showbreak length */
4646#ifdef FEAT_LINEBREAK
4647 /* only adjust the tab_len, when at the first column
4648 * after the showbreak value was drawn */
4649 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4650 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004653 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004654 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4655
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004656#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004657 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004658#endif
4659 /* tab amount depends on current column */
4660 n_extra = tab_len;
4661#ifdef FEAT_LINEBREAK
4662 else
4663 {
4664 char_u *p;
4665 int len = n_extra;
4666 int i;
4667 int saved_nextra = n_extra;
4668
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004669#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004670 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004671 /* there are characters to conceal */
4672 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004673 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4674 */
4675 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4676 && n_extra > tab_len)
4677 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004678#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004679
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004680 /* if n_extra > 0, it gives the number of chars, to
4681 * use for a tab, else we need to calculate the width
4682 * for a tab */
4683#ifdef FEAT_MBYTE
4684 len = (tab_len * mb_char2len(lcs_tab2));
4685 if (n_extra > 0)
4686 len += n_extra - tab_len;
4687#endif
4688 c = lcs_tab1;
4689 p = alloc((unsigned)(len + 1));
4690 vim_memset(p, ' ', len);
4691 p[len] = NUL;
4692 p_extra_free = p;
4693 for (i = 0; i < tab_len; i++)
4694 {
4695#ifdef FEAT_MBYTE
4696 mb_char2bytes(lcs_tab2, p);
4697 p += mb_char2len(lcs_tab2);
4698 n_extra += mb_char2len(lcs_tab2)
4699 - (saved_nextra > 0 ? 1 : 0);
4700#else
4701 p[i] = lcs_tab2;
4702#endif
4703 }
4704 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004705#ifdef FEAT_CONCEAL
4706 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4707 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004708 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004709 n_extra -= vcol_off;
4710#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004711 }
4712#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004713#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004714 {
4715 int vc_saved = vcol_off;
4716
4717 /* Tab alignment should be identical regardless of
4718 * 'conceallevel' value. So tab compensates of all
4719 * previous concealed characters, and thus resets
4720 * vcol_off and boguscols accumulated so far in the
4721 * line. Note that the tab can be longer than
4722 * 'tabstop' when there are concealed characters. */
4723 FIX_FOR_BOGUSCOLS;
4724
4725 /* Make sure, the highlighting for the tab char will be
4726 * correctly set further below (effectively reverts the
4727 * FIX_FOR_BOGSUCOLS macro */
4728 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004729 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004730 tab_len += vc_saved;
4731 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733#ifdef FEAT_MBYTE
4734 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4735#endif
4736 if (wp->w_p_list)
4737 {
4738 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004739#ifdef FEAT_LINEBREAK
4740 if (wp->w_p_lbr)
4741 c_extra = NUL; /* using p_extra from above */
4742 else
4743#endif
4744 c_extra = lcs_tab2;
4745 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 extra_attr = hl_attr(HLF_8);
4747 saved_attr2 = char_attr; /* save current attr */
4748#ifdef FEAT_MBYTE
4749 mb_c = c;
4750 if (enc_utf8 && (*mb_char2len)(c) > 1)
4751 {
4752 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004753 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004754 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
4756#endif
4757 }
4758 else
4759 {
4760 c_extra = ' ';
4761 c = ' ';
4762 }
4763 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004764 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004765 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004766 || ((fromcol >= 0 || fromcol_prev >= 0)
4767 && tocol > vcol
4768 && VIsual_mode != Ctrl_V
4769 && (
4770# ifdef FEAT_RIGHTLEFT
4771 wp->w_p_rl ? (col >= 0) :
4772# endif
4773 (col < W_WIDTH(wp)))
4774 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004775 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004776 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004777 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004779 /* Display a '$' after the line or highlight an extra
4780 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4782 /* For a diff line the highlighting continues after the
4783 * "$". */
4784 if (
4785# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004786 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787# ifdef LINE_ATTR
4788 &&
4789# endif
4790# endif
4791# ifdef LINE_ATTR
4792 line_attr == 0
4793# endif
4794 )
4795#endif
4796 {
4797#ifdef FEAT_VIRTUALEDIT
4798 /* In virtualedit, visual selections may extend
4799 * beyond end of line. */
4800 if (area_highlighting && virtual_active()
4801 && tocol != MAXCOL && vcol < tocol)
4802 n_extra = 0;
4803 else
4804#endif
4805 {
4806 p_extra = at_end_str;
4807 n_extra = 1;
4808 c_extra = NUL;
4809 }
4810 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004811 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004812 c = lcs_eol;
4813 else
4814 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 lcs_eol_one = -1;
4816 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004817 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
4819 extra_attr = hl_attr(HLF_AT);
4820 n_attr = 1;
4821 }
4822#ifdef FEAT_MBYTE
4823 mb_c = c;
4824 if (enc_utf8 && (*mb_char2len)(c) > 1)
4825 {
4826 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004827 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004828 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
4830 else
4831 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4832#endif
4833 }
4834 else if (c != NUL)
4835 {
4836 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004837 if (n_extra == 0)
4838 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839#ifdef FEAT_RIGHTLEFT
4840 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4841 rl_mirror(p_extra); /* reverse "<12>" */
4842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004844#ifdef FEAT_LINEBREAK
4845 if (wp->w_p_lbr)
4846 {
4847 char_u *p;
4848
4849 c = *p_extra;
4850 p = alloc((unsigned)n_extra + 1);
4851 vim_memset(p, ' ', n_extra);
4852 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4853 p[n_extra] = NUL;
4854 p_extra_free = p_extra = p;
4855 }
4856 else
4857#endif
4858 {
4859 n_extra = byte2cells(c) - 1;
4860 c = *p_extra++;
4861 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004862 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
4864 n_attr = n_extra + 1;
4865 extra_attr = hl_attr(HLF_8);
4866 saved_attr2 = char_attr; /* save current attr */
4867 }
4868#ifdef FEAT_MBYTE
4869 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4870#endif
4871 }
4872#ifdef FEAT_VIRTUALEDIT
4873 else if (VIsual_active
4874 && (VIsual_mode == Ctrl_V
4875 || VIsual_mode == 'v')
4876 && virtual_active()
4877 && tocol != MAXCOL
4878 && vcol < tocol
4879 && (
4880# ifdef FEAT_RIGHTLEFT
4881 wp->w_p_rl ? (col >= 0) :
4882# endif
4883 (col < W_WIDTH(wp))))
4884 {
4885 c = ' ';
4886 --ptr; /* put it back at the NUL */
4887 }
4888#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004889#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 else if ((
4891# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004892 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 ) && (
4896# ifdef FEAT_RIGHTLEFT
4897 wp->w_p_rl ? (col >= 0) :
4898# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004899 (col
4900# ifdef FEAT_CONCEAL
4901 - boguscols
4902# endif
4903 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
4905 /* Highlight until the right side of the window */
4906 c = ' ';
4907 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004908
4909 /* Remember we do the char for line highlighting. */
4910 ++did_line_attr;
4911
4912 /* don't do search HL for the rest of the line */
4913 if (line_attr != 0 && char_attr == search_attr && col > 0)
4914 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915# ifdef FEAT_DIFF
4916 if (diff_hlf == HLF_TXD)
4917 {
4918 diff_hlf = HLF_CHD;
4919 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004922 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4923 char_attr = hl_combine_attr(char_attr,
4924 hl_attr(HLF_CUL));
4925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927# endif
4928 }
4929#endif
4930 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004931
4932#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004933 if ( wp->w_p_cole > 0
4934 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004935 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02004936 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004937 && !(lnum_in_visual_area
4938 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004939 {
4940 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02004941 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004942 && (syn_get_sub_char() != NUL || match_conc
4943 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004944 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004945 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004946 /* First time at this concealed item: display one
4947 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004948 if (match_conc)
4949 c = match_conc;
4950 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004951 c = syn_get_sub_char();
4952 else if (lcs_conceal != NUL)
4953 c = lcs_conceal;
4954 else
4955 c = ' ';
4956
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004957 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004958
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004959 if (n_extra > 0)
4960 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004961 vcol += n_extra;
4962 if (wp->w_p_wrap && n_extra > 0)
4963 {
4964# ifdef FEAT_RIGHTLEFT
4965 if (wp->w_p_rl)
4966 {
4967 col -= n_extra;
4968 boguscols -= n_extra;
4969 }
4970 else
4971# endif
4972 {
4973 boguscols += n_extra;
4974 col += n_extra;
4975 }
4976 }
4977 n_extra = 0;
4978 n_attr = 0;
4979 }
4980 else if (n_skip == 0)
4981 {
4982 is_concealing = TRUE;
4983 n_skip = 1;
4984 }
4985# ifdef FEAT_MBYTE
4986 mb_c = c;
4987 if (enc_utf8 && (*mb_char2len)(c) > 1)
4988 {
4989 mb_utf8 = TRUE;
4990 u8cc[0] = 0;
4991 c = 0xc0;
4992 }
4993 else
4994 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4995# endif
4996 }
4997 else
4998 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004999 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005000 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005001 }
5002#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 }
5004
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005005#ifdef FEAT_CONCEAL
5006 /* In the cursor line and we may be concealing characters: correct
5007 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005008 if (!did_wcol && draw_state == WL_LINE
5009 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005010 && conceal_cursor_line(wp)
5011 && (int)wp->w_virtcol <= vcol + n_skip)
5012 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005013# ifdef FEAT_RIGHTLEFT
5014 if (wp->w_p_rl)
5015 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5016 else
5017# endif
5018 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005019 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005020 did_wcol = TRUE;
5021 }
5022#endif
5023
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 /* Don't override visual selection highlighting. */
5025 if (n_attr > 0
5026 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005027 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 char_attr = extra_attr;
5029
Bram Moolenaar81695252004-12-29 20:58:21 +00005030#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 /* XIM don't send preedit_start and preedit_end, but they send
5032 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5033 * im_is_preediting() here. */
5034 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005035 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 && (State & INSERT)
5037 && !p_imdisable
5038 && im_is_preediting()
5039 && draw_state == WL_LINE)
5040 {
5041 colnr_T tcol;
5042
5043 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005044 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 else
5046 tcol = preedit_end_col;
5047 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5048 {
5049 if (feedback_old_attr < 0)
5050 {
5051 feedback_col = 0;
5052 feedback_old_attr = char_attr;
5053 }
5054 char_attr = im_get_feedback_attr(feedback_col);
5055 if (char_attr < 0)
5056 char_attr = feedback_old_attr;
5057 feedback_col++;
5058 }
5059 else if (feedback_old_attr >= 0)
5060 {
5061 char_attr = feedback_old_attr;
5062 feedback_old_attr = -1;
5063 feedback_col = 0;
5064 }
5065 }
5066#endif
5067 /*
5068 * Handle the case where we are in column 0 but not on the first
5069 * character of the line and the user wants us to show us a
5070 * special character (via 'listchars' option "precedes:<char>".
5071 */
5072 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005073 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5075#ifdef FEAT_DIFF
5076 && filler_todo <= 0
5077#endif
5078 && draw_state > WL_NR
5079 && c != NUL)
5080 {
5081 c = lcs_prec;
5082 lcs_prec_todo = NUL;
5083#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005084 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5085 {
5086 /* Double-width character being overwritten by the "precedes"
5087 * character, need to fill up half the character. */
5088 c_extra = MB_FILLER_CHAR;
5089 n_extra = 1;
5090 n_attr = 2;
5091 extra_attr = hl_attr(HLF_AT);
5092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 mb_c = c;
5094 if (enc_utf8 && (*mb_char2len)(c) > 1)
5095 {
5096 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005097 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005098 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
5100 else
5101 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5102#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005103 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 saved_attr3 = char_attr; /* save current attr */
5106 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5107 n_attr3 = 1;
5108 }
5109 }
5110
5111 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005112 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005114 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005115#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005116 || did_line_attr == 1
5117#endif
5118 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005120#ifdef FEAT_SEARCH_EXTRA
5121 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005122
5123 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005124 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005125 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005126#endif
5127
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005128 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 * highlight match at end of line. If it's beyond the last
5130 * char on the screen, just overwrite that one (tricky!) Not
5131 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005132#ifdef FEAT_SEARCH_EXTRA
5133 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005134 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005135 prevcol_hl_flag = TRUE;
5136 else
5137 {
5138 cur = wp->w_match_head;
5139 while (cur != NULL)
5140 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005141 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005142 {
5143 prevcol_hl_flag = TRUE;
5144 break;
5145 }
5146 cur = cur->next;
5147 }
5148 }
5149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005151 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005152 && (VIsual_mode != Ctrl_V
5153 || lnum == VIsual.lnum
5154 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005155 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156#ifdef FEAT_SEARCH_EXTRA
5157 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005158 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005159# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005160 && did_line_attr <= 1
5161# endif
5162 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163#endif
5164 ))
5165 {
5166 int n = 0;
5167
5168#ifdef FEAT_RIGHTLEFT
5169 if (wp->w_p_rl)
5170 {
5171 if (col < 0)
5172 n = 1;
5173 }
5174 else
5175#endif
5176 {
5177 if (col >= W_WIDTH(wp))
5178 n = -1;
5179 }
5180 if (n != 0)
5181 {
5182 /* At the window boundary, highlight the last character
5183 * instead (better than nothing). */
5184 off += n;
5185 col += n;
5186 }
5187 else
5188 {
5189 /* Add a blank character to highlight. */
5190 ScreenLines[off] = ' ';
5191#ifdef FEAT_MBYTE
5192 if (enc_utf8)
5193 ScreenLinesUC[off] = 0;
5194#endif
5195 }
5196#ifdef FEAT_SEARCH_EXTRA
5197 if (area_attr == 0)
5198 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005199 /* Use attributes from match with highest priority among
5200 * 'search_hl' and the match list. */
5201 char_attr = search_hl.attr;
5202 cur = wp->w_match_head;
5203 shl_flag = FALSE;
5204 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005205 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005206 if (shl_flag == FALSE
5207 && ((cur != NULL
5208 && cur->priority > SEARCH_HL_PRIORITY)
5209 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005210 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005211 shl = &search_hl;
5212 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005213 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005214 else
5215 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005216 if ((ptr - line) - 1 == (long)shl->startcol
5217 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005218 char_attr = shl->attr;
5219 if (shl != &search_hl && cur != NULL)
5220 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
5223#endif
5224 ScreenAttrs[off] = char_attr;
5225#ifdef FEAT_RIGHTLEFT
5226 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005229 --off;
5230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 else
5232#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005235 ++off;
5236 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005237 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005238#ifdef FEAT_SYN_HL
5239 eol_hl_off = 1;
5240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243
Bram Moolenaar91170f82006-05-05 21:15:17 +00005244 /*
5245 * At end of the text line.
5246 */
5247 if (c == NUL)
5248 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005249#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005250 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5251 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005252 {
5253 /* highlight last char after line */
5254 --col;
5255 --off;
5256 --vcol;
5257 }
5258
Bram Moolenaar1a384422010-07-14 19:53:30 +02005259 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005260 if (wp->w_p_wrap)
5261 v = wp->w_skipcol;
5262 else
5263 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005264
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005265 /* check if line ends before left margin */
5266 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005267 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005268#ifdef FEAT_CONCEAL
5269 /* Get rid of the boguscols now, we want to draw until the right
5270 * edge for 'cursorcolumn'. */
5271 col -= boguscols;
5272 boguscols = 0;
5273#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005274
5275 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005276 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005277
5278 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005279 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5280 && (int)wp->w_virtcol <
5281 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005282 && lnum != wp->w_cursor.lnum)
5283 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005284# ifdef FEAT_RIGHTLEFT
5285 && !wp->w_p_rl
5286# endif
5287 )
5288 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005289 int rightmost_vcol = 0;
5290 int i;
5291
5292 if (wp->w_p_cuc)
5293 rightmost_vcol = wp->w_virtcol;
5294 if (draw_color_col)
5295 /* determine rightmost colorcolumn to possibly draw */
5296 for (i = 0; color_cols[i] >= 0; ++i)
5297 if (rightmost_vcol < color_cols[i])
5298 rightmost_vcol = color_cols[i];
5299
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005300 while (col < W_WIDTH(wp))
5301 {
5302 ScreenLines[off] = ' ';
5303#ifdef FEAT_MBYTE
5304 if (enc_utf8)
5305 ScreenLinesUC[off] = 0;
5306#endif
5307 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005308 if (draw_color_col)
5309 draw_color_col = advance_color_col(VCOL_HLC,
5310 &color_cols);
5311
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005312 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005313 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005314 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005315 ScreenAttrs[off++] = hl_attr(HLF_MC);
5316 else
5317 ScreenAttrs[off++] = 0;
5318
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005319 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005320 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005321
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005322 ++vcol;
5323 }
5324 }
5325#endif
5326
Bram Moolenaar860cae12010-06-05 23:22:07 +02005327 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5328 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 row++;
5330
5331 /*
5332 * Update w_cline_height and w_cline_folded if the cursor line was
5333 * updated (saves a call to plines() later).
5334 */
5335 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5336 {
5337 curwin->w_cline_row = startrow;
5338 curwin->w_cline_height = row - startrow;
5339#ifdef FEAT_FOLDING
5340 curwin->w_cline_folded = FALSE;
5341#endif
5342 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5343 }
5344
5345 break;
5346 }
5347
5348 /* line continues beyond line end */
5349 if (lcs_ext
5350 && !wp->w_p_wrap
5351#ifdef FEAT_DIFF
5352 && filler_todo <= 0
5353#endif
5354 && (
5355#ifdef FEAT_RIGHTLEFT
5356 wp->w_p_rl ? col == 0 :
5357#endif
5358 col == W_WIDTH(wp) - 1)
5359 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005360 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5362 {
5363 c = lcs_ext;
5364 char_attr = hl_attr(HLF_AT);
5365#ifdef FEAT_MBYTE
5366 mb_c = c;
5367 if (enc_utf8 && (*mb_char2len)(c) > 1)
5368 {
5369 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005370 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005371 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
5373 else
5374 mb_utf8 = FALSE;
5375#endif
5376 }
5377
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005378#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005379 /* advance to the next 'colorcolumn' */
5380 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005381 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005382
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005383 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005384 * highlight the cursor position itself.
5385 * Also highlight the 'colorcolumn' if it is different than
5386 * 'cursorcolumn' */
5387 vcol_save_attr = -1;
5388 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005389 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005390 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005391 && lnum != wp->w_cursor.lnum)
5392 {
5393 vcol_save_attr = char_attr;
5394 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5395 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005396 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005397 {
5398 vcol_save_attr = char_attr;
5399 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5400 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005401 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005402#endif
5403
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 /*
5405 * Store character to be displayed.
5406 * Skip characters that are left of the screen for 'nowrap'.
5407 */
5408 vcol_prev = vcol;
5409 if (draw_state < WL_LINE || n_skip <= 0)
5410 {
5411 /*
5412 * Store the character.
5413 */
5414#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5415 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5416 {
5417 /* A double-wide character is: put first halve in left cell. */
5418 --off;
5419 --col;
5420 }
5421#endif
5422 ScreenLines[off] = c;
5423#ifdef FEAT_MBYTE
5424 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005425 {
5426 if ((mb_c & 0xff00) == 0x8e00)
5427 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 else if (enc_utf8)
5431 {
5432 if (mb_utf8)
5433 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005434 int i;
5435
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005437 if ((c & 0xff) == 0)
5438 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005439 for (i = 0; i < Screen_mco; ++i)
5440 {
5441 ScreenLinesC[i][off] = u8cc[i];
5442 if (u8cc[i] == 0)
5443 break;
5444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
5446 else
5447 ScreenLinesUC[off] = 0;
5448 }
5449 if (multi_attr)
5450 {
5451 ScreenAttrs[off] = multi_attr;
5452 multi_attr = 0;
5453 }
5454 else
5455#endif
5456 ScreenAttrs[off] = char_attr;
5457
5458#ifdef FEAT_MBYTE
5459 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5460 {
5461 /* Need to fill two screen columns. */
5462 ++off;
5463 ++col;
5464 if (enc_utf8)
5465 /* UTF-8: Put a 0 in the second screen char. */
5466 ScreenLines[off] = 0;
5467 else
5468 /* DBCS: Put second byte in the second screen char. */
5469 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005470 if (draw_state > WL_NR
5471#ifdef FEAT_DIFF
5472 && filler_todo <= 0
5473#endif
5474 )
5475 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 /* When "tocol" is halfway a character, set it to the end of
5477 * the character, otherwise highlighting won't stop. */
5478 if (tocol == vcol)
5479 ++tocol;
5480#ifdef FEAT_RIGHTLEFT
5481 if (wp->w_p_rl)
5482 {
5483 /* now it's time to backup one cell */
5484 --off;
5485 --col;
5486 }
5487#endif
5488 }
5489#endif
5490#ifdef FEAT_RIGHTLEFT
5491 if (wp->w_p_rl)
5492 {
5493 --off;
5494 --col;
5495 }
5496 else
5497#endif
5498 {
5499 ++off;
5500 ++col;
5501 }
5502 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005503#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005504 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005505 {
5506 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005507 ++vcol_off;
5508 if (n_extra > 0)
5509 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005510 if (wp->w_p_wrap)
5511 {
5512 /*
5513 * Special voodoo required if 'wrap' is on.
5514 *
5515 * Advance the column indicator to force the line
5516 * drawing to wrap early. This will make the line
5517 * take up the same screen space when parts are concealed,
5518 * so that cursor line computations aren't messed up.
5519 *
5520 * To avoid the fictitious advance of 'col' causing
5521 * trailing junk to be written out of the screen line
5522 * we are building, 'boguscols' keeps track of the number
5523 * of bad columns we have advanced.
5524 */
5525 if (n_extra > 0)
5526 {
5527 vcol += n_extra;
5528# ifdef FEAT_RIGHTLEFT
5529 if (wp->w_p_rl)
5530 {
5531 col -= n_extra;
5532 boguscols -= n_extra;
5533 }
5534 else
5535# endif
5536 {
5537 col += n_extra;
5538 boguscols += n_extra;
5539 }
5540 n_extra = 0;
5541 n_attr = 0;
5542 }
5543
5544
5545# ifdef FEAT_MBYTE
5546 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5547 {
5548 /* Need to fill two screen columns. */
5549# ifdef FEAT_RIGHTLEFT
5550 if (wp->w_p_rl)
5551 {
5552 --boguscols;
5553 --col;
5554 }
5555 else
5556# endif
5557 {
5558 ++boguscols;
5559 ++col;
5560 }
5561 }
5562# endif
5563
5564# ifdef FEAT_RIGHTLEFT
5565 if (wp->w_p_rl)
5566 {
5567 --boguscols;
5568 --col;
5569 }
5570 else
5571# endif
5572 {
5573 ++boguscols;
5574 ++col;
5575 }
5576 }
5577 else
5578 {
5579 if (n_extra > 0)
5580 {
5581 vcol += n_extra;
5582 n_extra = 0;
5583 n_attr = 0;
5584 }
5585 }
5586
5587 }
5588#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 else
5590 --n_skip;
5591
Bram Moolenaar64486672010-05-16 15:46:46 +02005592 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5593 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005594 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595#ifdef FEAT_DIFF
5596 && filler_todo <= 0
5597#endif
5598 )
5599 ++vcol;
5600
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005601#ifdef FEAT_SYN_HL
5602 if (vcol_save_attr >= 0)
5603 char_attr = vcol_save_attr;
5604#endif
5605
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 /* restore attributes after "predeces" in 'listchars' */
5607 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5608 char_attr = saved_attr3;
5609
5610 /* restore attributes after last 'listchars' or 'number' char */
5611 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5612 char_attr = saved_attr2;
5613
5614 /*
5615 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005616 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 */
5618 if ((
5619#ifdef FEAT_RIGHTLEFT
5620 wp->w_p_rl ? (col < 0) :
5621#endif
5622 (col >= W_WIDTH(wp)))
5623 && (*ptr != NUL
5624#ifdef FEAT_DIFF
5625 || filler_todo > 0
5626#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005627 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5629 )
5630 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005631#ifdef FEAT_CONCEAL
5632 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5633 (int)W_WIDTH(wp), wp->w_p_rl);
5634 boguscols = 0;
5635#else
5636 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5637 (int)W_WIDTH(wp), wp->w_p_rl);
5638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 ++row;
5640 ++screen_row;
5641
5642 /* When not wrapping and finished diff lines, or when displayed
5643 * '$' and highlighting until last column, break here. */
5644 if ((!wp->w_p_wrap
5645#ifdef FEAT_DIFF
5646 && filler_todo <= 0
5647#endif
5648 ) || lcs_eol_one == -1)
5649 break;
5650
5651 /* When the window is too narrow draw all "@" lines. */
5652 if (draw_state != WL_LINE
5653#ifdef FEAT_DIFF
5654 && filler_todo <= 0
5655#endif
5656 )
5657 {
5658 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005659#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 draw_vsep_win(wp, row);
5661#endif
5662 row = endrow;
5663 }
5664
5665 /* When line got too long for screen break here. */
5666 if (row == endrow)
5667 {
5668 ++row;
5669 break;
5670 }
5671
5672 if (screen_cur_row == screen_row - 1
5673#ifdef FEAT_DIFF
5674 && filler_todo <= 0
5675#endif
5676 && W_WIDTH(wp) == Columns)
5677 {
5678 /* Remember that the line wraps, used for modeless copy. */
5679 LineWraps[screen_row - 1] = TRUE;
5680
5681 /*
5682 * Special trick to make copy/paste of wrapped lines work with
5683 * xterm/screen: write an extra character beyond the end of
5684 * the line. This will work with all terminal types
5685 * (regardless of the xn,am settings).
5686 * Only do this on a fast tty.
5687 * Only do this if the cursor is on the current line
5688 * (something has been written in it).
5689 * Don't do this for the GUI.
5690 * Don't do this for double-width characters.
5691 * Don't do this for a window not at the right screen border.
5692 */
5693 if (p_tf
5694#ifdef FEAT_GUI
5695 && !gui.in_use
5696#endif
5697#ifdef FEAT_MBYTE
5698 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005699 && ((*mb_off2cells)(LineOffset[screen_row],
5700 LineOffset[screen_row] + screen_Columns)
5701 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005703 + (int)Columns - 2,
5704 LineOffset[screen_row] + screen_Columns)
5705 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706#endif
5707 )
5708 {
5709 /* First make sure we are at the end of the screen line,
5710 * then output the same character again to let the
5711 * terminal know about the wrap. If the terminal doesn't
5712 * auto-wrap, we overwrite the character. */
5713 if (screen_cur_col != W_WIDTH(wp))
5714 screen_char(LineOffset[screen_row - 1]
5715 + (unsigned)Columns - 1,
5716 screen_row - 1, (int)(Columns - 1));
5717
5718#ifdef FEAT_MBYTE
5719 /* When there is a multi-byte character, just output a
5720 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005721 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5722 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 out_char(' ');
5724 else
5725#endif
5726 out_char(ScreenLines[LineOffset[screen_row - 1]
5727 + (Columns - 1)]);
5728 /* force a redraw of the first char on the next line */
5729 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5730 screen_start(); /* don't know where cursor is now */
5731 }
5732 }
5733
5734 col = 0;
5735 off = (unsigned)(current_ScreenLine - ScreenLines);
5736#ifdef FEAT_RIGHTLEFT
5737 if (wp->w_p_rl)
5738 {
5739 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5740 off += col;
5741 }
5742#endif
5743
5744 /* reset the drawing state for the start of a wrapped line */
5745 draw_state = WL_START;
5746 saved_n_extra = n_extra;
5747 saved_p_extra = p_extra;
5748 saved_c_extra = c_extra;
5749 saved_char_attr = char_attr;
5750 n_extra = 0;
5751 lcs_prec_todo = lcs_prec;
5752#ifdef FEAT_LINEBREAK
5753# ifdef FEAT_DIFF
5754 if (filler_todo <= 0)
5755# endif
5756 need_showbreak = TRUE;
5757#endif
5758#ifdef FEAT_DIFF
5759 --filler_todo;
5760 /* When the filler lines are actually below the last line of the
5761 * file, don't draw the line itself, break here. */
5762 if (filler_todo == 0 && wp->w_botfill)
5763 break;
5764#endif
5765 }
5766
5767 } /* for every character in the line */
5768
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005769#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005770 /* After an empty line check first word for capital. */
5771 if (*skipwhite(line) == NUL)
5772 {
5773 capcol_lnum = lnum + 1;
5774 cap_col = 0;
5775 }
5776#endif
5777
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 Moolenaarb3414592014-06-17 17:48:32 +02007776 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007777next_search_hl_pos(
7778 match_T *shl, /* points to a match */
7779 linenr_T lnum,
7780 posmatch_T *posmatch, /* match positions */
7781 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007782{
7783 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007784 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007785
7786 shl->lnum = 0;
7787 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7788 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007789 llpos_T *pos = &posmatch->pos[i];
7790
7791 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007792 break;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007793 if (pos->col + pos->len - 1 <= mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007794 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007795 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007796 {
7797 if (shl->lnum == lnum)
7798 {
7799 /* partially sort positions by column numbers
7800 * on the same line */
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007801 if (pos->col < posmatch->pos[bot].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007802 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007803 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007804
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007805 *pos = posmatch->pos[bot];
Bram Moolenaarb3414592014-06-17 17:48:32 +02007806 posmatch->pos[bot] = tmp;
7807 }
7808 }
7809 else
7810 {
7811 bot = i;
7812 shl->lnum = lnum;
7813 }
7814 }
7815 }
7816 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007817 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007818 {
7819 colnr_T start = posmatch->pos[bot].col == 0
7820 ? 0 : posmatch->pos[bot].col - 1;
7821 colnr_T end = posmatch->pos[bot].col == 0
7822 ? MAXCOL : start + posmatch->pos[bot].len;
7823
7824 shl->rm.startpos[0].lnum = 0;
7825 shl->rm.startpos[0].col = start;
7826 shl->rm.endpos[0].lnum = 0;
7827 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007828 shl->is_addpos = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007829 posmatch->cur = bot + 1;
7830 return TRUE;
7831 }
7832 return FALSE;
7833}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007834#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007835
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007837screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
7839 attrentry_T *aep = NULL;
7840
7841 screen_attr = attr;
7842 if (full_screen
7843#ifdef WIN3264
7844 && termcap_active
7845#endif
7846 )
7847 {
7848#ifdef FEAT_GUI
7849 if (gui.in_use)
7850 {
7851 char buf[20];
7852
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007853 /* The GUI handles this internally. */
7854 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 OUT_STR(buf);
7856 }
7857 else
7858#endif
7859 {
7860 if (attr > HL_ALL) /* special HL attr. */
7861 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007862 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 aep = syn_cterm_attr2entry(attr);
7864 else
7865 aep = syn_term_attr2entry(attr);
7866 if (aep == NULL) /* did ":syntax clear" */
7867 attr = 0;
7868 else
7869 attr = aep->ae_attr;
7870 }
7871 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7872 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007873 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007874#ifdef FEAT_TERMGUICOLORS
7875 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007876 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007877#endif
7878 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007879#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007880 )
7881#endif
7882 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007883 /* If the Normal FG color has BOLD attribute and the new HL
7884 * has a FG color defined, clear BOLD. */
7885 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7887 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007888 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7889 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 out_str(T_US);
7891 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7892 out_str(T_CZH);
7893 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7894 out_str(T_MR);
7895
7896 /*
7897 * Output the color or start string after bold etc., in case the
7898 * bold etc. override the color setting.
7899 */
7900 if (aep != NULL)
7901 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007902#ifdef FEAT_TERMGUICOLORS
7903 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007905 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007906 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007907 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007908 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 }
7910 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007913 if (t_colors > 1)
7914 {
7915 if (aep->ae_u.cterm.fg_color)
7916 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7917 if (aep->ae_u.cterm.bg_color)
7918 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7919 }
7920 else
7921 {
7922 if (aep->ae_u.term.start != NULL)
7923 out_str(aep->ae_u.term.start);
7924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 }
7926 }
7927 }
7928 }
7929}
7930
7931 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007932screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934 int do_ME = FALSE; /* output T_ME code */
7935
7936 if (screen_attr != 0
7937#ifdef WIN3264
7938 && termcap_active
7939#endif
7940 )
7941 {
7942#ifdef FEAT_GUI
7943 if (gui.in_use)
7944 {
7945 char buf[20];
7946
7947 /* use internal GUI code */
7948 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7949 OUT_STR(buf);
7950 }
7951 else
7952#endif
7953 {
7954 if (screen_attr > HL_ALL) /* special HL attr. */
7955 {
7956 attrentry_T *aep;
7957
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007958 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {
7960 /*
7961 * Assume that t_me restores the original colors!
7962 */
7963 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007964 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007965#ifdef FEAT_TERMGUICOLORS
7966 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007967 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
7968 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007969#endif
7970 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007971#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007972 )
7973#endif
7974 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 do_ME = TRUE;
7976 }
7977 else
7978 {
7979 aep = syn_term_attr2entry(screen_attr);
7980 if (aep != NULL && aep->ae_u.term.stop != NULL)
7981 {
7982 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7983 do_ME = TRUE;
7984 else
7985 out_str(aep->ae_u.term.stop);
7986 }
7987 }
7988 if (aep == NULL) /* did ":syntax clear" */
7989 screen_attr = 0;
7990 else
7991 screen_attr = aep->ae_attr;
7992 }
7993
7994 /*
7995 * Often all ending-codes are equal to T_ME. Avoid outputting the
7996 * same sequence several times.
7997 */
7998 if (screen_attr & HL_STANDOUT)
7999 {
8000 if (STRCMP(T_SE, T_ME) == 0)
8001 do_ME = TRUE;
8002 else
8003 out_str(T_SE);
8004 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008005 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {
8007 if (STRCMP(T_UE, T_ME) == 0)
8008 do_ME = TRUE;
8009 else
8010 out_str(T_UE);
8011 }
8012 if (screen_attr & HL_ITALIC)
8013 {
8014 if (STRCMP(T_CZR, T_ME) == 0)
8015 do_ME = TRUE;
8016 else
8017 out_str(T_CZR);
8018 }
8019 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8020 out_str(T_ME);
8021
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008022#ifdef FEAT_TERMGUICOLORS
8023 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008025 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008026 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008027 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008028 term_bg_rgb_color(cterm_normal_bg_gui_color);
8029 }
8030 else
8031#endif
8032 {
8033 if (t_colors > 1)
8034 {
8035 /* set Normal cterm colors */
8036 if (cterm_normal_fg_color != 0)
8037 term_fg_color(cterm_normal_fg_color - 1);
8038 if (cterm_normal_bg_color != 0)
8039 term_bg_color(cterm_normal_bg_color - 1);
8040 if (cterm_normal_fg_bold)
8041 out_str(T_MD);
8042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 }
8044 }
8045 }
8046 screen_attr = 0;
8047}
8048
8049/*
8050 * Reset the colors for a cterm. Used when leaving Vim.
8051 * The machine specific code may override this again.
8052 */
8053 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008054reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008056 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {
8058 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008059#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008060 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8061 || cterm_normal_bg_gui_color != INVALCOLOR)
8062 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008063#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {
8067 out_str(T_OP);
8068 screen_attr = -1;
8069 }
8070 if (cterm_normal_fg_bold)
8071 {
8072 out_str(T_ME);
8073 screen_attr = -1;
8074 }
8075 }
8076}
8077
8078/*
8079 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8080 * using the attributes from ScreenAttrs["off"].
8081 */
8082 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008083screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 int attr;
8086
8087 /* Check for illegal values, just in case (could happen just after
8088 * resizing). */
8089 if (row >= screen_Rows || col >= screen_Columns)
8090 return;
8091
Bram Moolenaar494838a2015-02-10 19:20:37 +01008092 /* Outputting a character in the last cell on the screen may scroll the
8093 * screen up. Only do it when the "xn" termcap property is set, otherwise
8094 * mark the character invalid (update it when scrolled up). */
8095 if (*T_XN == NUL
8096 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097#ifdef FEAT_RIGHTLEFT
8098 /* account for first command-line character in rightleft mode */
8099 && !cmdmsg_rl
8100#endif
8101 )
8102 {
8103 ScreenAttrs[off] = (sattr_T)-1;
8104 return;
8105 }
8106
8107 /*
8108 * Stop highlighting first, so it's easier to move the cursor.
8109 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008110#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 if (screen_char_attr != 0)
8112 attr = screen_char_attr;
8113 else
8114#endif
8115 attr = ScreenAttrs[off];
8116 if (screen_attr != attr)
8117 screen_stop_highlight();
8118
8119 windgoto(row, col);
8120
8121 if (screen_attr != attr)
8122 screen_start_highlight(attr);
8123
8124#ifdef FEAT_MBYTE
8125 if (enc_utf8 && ScreenLinesUC[off] != 0)
8126 {
8127 char_u buf[MB_MAXBYTES + 1];
8128
8129 /* Convert UTF-8 character to bytes and write it. */
8130
8131 buf[utfc_char2bytes(off, buf)] = NUL;
8132
8133 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008134 if (utf_ambiguous_width(ScreenLinesUC[off]))
8135 screen_cur_col = 9999;
8136 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 ++screen_cur_col;
8138 }
8139 else
8140#endif
8141 {
8142#ifdef FEAT_MBYTE
8143 out_flush_check();
8144#endif
8145 out_char(ScreenLines[off]);
8146#ifdef FEAT_MBYTE
8147 /* double-byte character in single-width cell */
8148 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8149 out_char(ScreenLines2[off]);
8150#endif
8151 }
8152
8153 screen_cur_col++;
8154}
8155
8156#ifdef FEAT_MBYTE
8157
8158/*
8159 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8160 * on the screen at position 'row' and 'col'.
8161 * The attributes of the first byte is used for all. This is required to
8162 * output the two bytes of a double-byte character with nothing in between.
8163 */
8164 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008165screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166{
8167 /* Check for illegal values (could be wrong when screen was resized). */
8168 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8169 return;
8170
8171 /* Outputting the last character on the screen may scrollup the screen.
8172 * Don't to it! Mark the character invalid (update it when scrolled up) */
8173 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8174 {
8175 ScreenAttrs[off] = (sattr_T)-1;
8176 return;
8177 }
8178
8179 /* Output the first byte normally (positions the cursor), then write the
8180 * second byte directly. */
8181 screen_char(off, row, col);
8182 out_char(ScreenLines[off + 1]);
8183 ++screen_cur_col;
8184}
8185#endif
8186
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008187#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188/*
8189 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8190 * This uses the contents of ScreenLines[] and doesn't change it.
8191 */
8192 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008193screen_draw_rectangle(
8194 int row,
8195 int col,
8196 int height,
8197 int width,
8198 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
8200 int r, c;
8201 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008202#ifdef FEAT_MBYTE
8203 int max_off;
8204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008206 /* Can't use ScreenLines unless initialized */
8207 if (ScreenLines == NULL)
8208 return;
8209
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 if (invert)
8211 screen_char_attr = HL_INVERSE;
8212 for (r = row; r < row + height; ++r)
8213 {
8214 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008215#ifdef FEAT_MBYTE
8216 max_off = off + screen_Columns;
8217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 for (c = col; c < col + width; ++c)
8219 {
8220#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008221 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {
8223 screen_char_2(off + c, r, c);
8224 ++c;
8225 }
8226 else
8227#endif
8228 {
8229 screen_char(off + c, r, c);
8230#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008231 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 ++c;
8233#endif
8234 }
8235 }
8236 }
8237 screen_char_attr = 0;
8238}
8239#endif
8240
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008241#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242/*
8243 * Redraw the characters for a vertically split window.
8244 */
8245 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008246redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247{
8248 int col;
8249 int width;
8250
8251# ifdef FEAT_CLIPBOARD
8252 clip_may_clear_selection(row, end - 1);
8253# endif
8254
8255 if (wp == NULL)
8256 {
8257 col = 0;
8258 width = Columns;
8259 }
8260 else
8261 {
8262 col = wp->w_wincol;
8263 width = wp->w_width;
8264 }
8265 screen_draw_rectangle(row, col, end - row, width, FALSE);
8266}
8267#endif
8268
8269/*
8270 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8271 * with character 'c1' in first column followed by 'c2' in the other columns.
8272 * Use attributes 'attr'.
8273 */
8274 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008275screen_fill(
8276 int start_row,
8277 int end_row,
8278 int start_col,
8279 int end_col,
8280 int c1,
8281 int c2,
8282 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283{
8284 int row;
8285 int col;
8286 int off;
8287 int end_off;
8288 int did_delete;
8289 int c;
8290 int norm_term;
8291#if defined(FEAT_GUI) || defined(UNIX)
8292 int force_next = FALSE;
8293#endif
8294
8295 if (end_row > screen_Rows) /* safety check */
8296 end_row = screen_Rows;
8297 if (end_col > screen_Columns) /* safety check */
8298 end_col = screen_Columns;
8299 if (ScreenLines == NULL
8300 || start_row >= end_row
8301 || start_col >= end_col) /* nothing to do */
8302 return;
8303
8304 /* it's a "normal" terminal when not in a GUI or cterm */
8305 norm_term = (
8306#ifdef FEAT_GUI
8307 !gui.in_use &&
8308#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008309 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 for (row = start_row; row < end_row; ++row)
8311 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008312#ifdef FEAT_MBYTE
8313 if (has_mbyte
8314# ifdef FEAT_GUI
8315 && !gui.in_use
8316# endif
8317 )
8318 {
8319 /* When drawing over the right halve of a double-wide char clear
8320 * out the left halve. When drawing over the left halve of a
8321 * double wide-char clear out the right halve. Only needed in a
8322 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008323 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008324 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008325 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008326 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008327 }
8328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 /*
8330 * Try to use delete-line termcap code, when no attributes or in a
8331 * "normal" terminal, where a bold/italic space is just a
8332 * space.
8333 */
8334 did_delete = FALSE;
8335 if (c2 == ' '
8336 && end_col == Columns
8337 && can_clear(T_CE)
8338 && (attr == 0
8339 || (norm_term
8340 && attr <= HL_ALL
8341 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8342 {
8343 /*
8344 * check if we really need to clear something
8345 */
8346 col = start_col;
8347 if (c1 != ' ') /* don't clear first char */
8348 ++col;
8349
8350 off = LineOffset[row] + col;
8351 end_off = LineOffset[row] + end_col;
8352
8353 /* skip blanks (used often, keep it fast!) */
8354#ifdef FEAT_MBYTE
8355 if (enc_utf8)
8356 while (off < end_off && ScreenLines[off] == ' '
8357 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8358 ++off;
8359 else
8360#endif
8361 while (off < end_off && ScreenLines[off] == ' '
8362 && ScreenAttrs[off] == 0)
8363 ++off;
8364 if (off < end_off) /* something to be cleared */
8365 {
8366 col = off - LineOffset[row];
8367 screen_stop_highlight();
8368 term_windgoto(row, col);/* clear rest of this screen line */
8369 out_str(T_CE);
8370 screen_start(); /* don't know where cursor is now */
8371 col = end_col - col;
8372 while (col--) /* clear chars in ScreenLines */
8373 {
8374 ScreenLines[off] = ' ';
8375#ifdef FEAT_MBYTE
8376 if (enc_utf8)
8377 ScreenLinesUC[off] = 0;
8378#endif
8379 ScreenAttrs[off] = 0;
8380 ++off;
8381 }
8382 }
8383 did_delete = TRUE; /* the chars are cleared now */
8384 }
8385
8386 off = LineOffset[row] + start_col;
8387 c = c1;
8388 for (col = start_col; col < end_col; ++col)
8389 {
8390 if (ScreenLines[off] != c
8391#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008392 || (enc_utf8 && (int)ScreenLinesUC[off]
8393 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394#endif
8395 || ScreenAttrs[off] != attr
8396#if defined(FEAT_GUI) || defined(UNIX)
8397 || force_next
8398#endif
8399 )
8400 {
8401#if defined(FEAT_GUI) || defined(UNIX)
8402 /* The bold trick may make a single row of pixels appear in
8403 * the next character. When a bold character is removed, the
8404 * next character should be redrawn too. This happens for our
8405 * own GUI and for some xterms. */
8406 if (
8407# ifdef FEAT_GUI
8408 gui.in_use
8409# endif
8410# if defined(FEAT_GUI) && defined(UNIX)
8411 ||
8412# endif
8413# ifdef UNIX
8414 term_is_xterm
8415# endif
8416 )
8417 {
8418 if (ScreenLines[off] != ' '
8419 && (ScreenAttrs[off] > HL_ALL
8420 || ScreenAttrs[off] & HL_BOLD))
8421 force_next = TRUE;
8422 else
8423 force_next = FALSE;
8424 }
8425#endif
8426 ScreenLines[off] = c;
8427#ifdef FEAT_MBYTE
8428 if (enc_utf8)
8429 {
8430 if (c >= 0x80)
8431 {
8432 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008433 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 }
8435 else
8436 ScreenLinesUC[off] = 0;
8437 }
8438#endif
8439 ScreenAttrs[off] = attr;
8440 if (!did_delete || c != ' ')
8441 screen_char(off, row, col);
8442 }
8443 ++off;
8444 if (col == start_col)
8445 {
8446 if (did_delete)
8447 break;
8448 c = c2;
8449 }
8450 }
8451 if (end_col == Columns)
8452 LineWraps[row] = FALSE;
8453 if (row == Rows - 1) /* overwritten the command line */
8454 {
8455 redraw_cmdline = TRUE;
8456 if (c1 == ' ' && c2 == ' ')
8457 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008458 if (start_col == 0)
8459 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 }
8461 }
8462}
8463
8464/*
8465 * Check if there should be a delay. Used before clearing or redrawing the
8466 * screen or the command line.
8467 */
8468 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008469check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
8471 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8472 && !did_wait_return
8473 && emsg_silent == 0)
8474 {
8475 out_flush();
8476 ui_delay(1000L, TRUE);
8477 emsg_on_display = FALSE;
8478 if (check_msg_scroll)
8479 msg_scroll = FALSE;
8480 }
8481}
8482
8483/*
8484 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008485 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 * Returns TRUE if there is a valid screen to write to.
8487 * Returns FALSE when starting up and screen not initialized yet.
8488 */
8489 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008490screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008492 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 return (ScreenLines != NULL);
8494}
8495
8496/*
8497 * Resize the shell to Rows and Columns.
8498 * Allocate ScreenLines[] and associated items.
8499 *
8500 * There may be some time between setting Rows and Columns and (re)allocating
8501 * ScreenLines[]. This happens when starting up and when (manually) changing
8502 * the shell size. Always use screen_Rows and screen_Columns to access items
8503 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8504 * final size of the shell is needed.
8505 */
8506 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008507screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
8509 int new_row, old_row;
8510#ifdef FEAT_GUI
8511 int old_Rows;
8512#endif
8513 win_T *wp;
8514 int outofmem = FALSE;
8515 int len;
8516 schar_T *new_ScreenLines;
8517#ifdef FEAT_MBYTE
8518 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008519 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008521 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522#endif
8523 sattr_T *new_ScreenAttrs;
8524 unsigned *new_LineOffset;
8525 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008526#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008527 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008528 tabpage_T *tp;
8529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008531 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008532#ifdef FEAT_AUTOCMD
8533 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008535retry:
8536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 /*
8538 * Allocation of the screen buffers is done only when the size changes and
8539 * when Rows and Columns have been set and we have started doing full
8540 * screen stuff.
8541 */
8542 if ((ScreenLines != NULL
8543 && Rows == screen_Rows
8544 && Columns == screen_Columns
8545#ifdef FEAT_MBYTE
8546 && enc_utf8 == (ScreenLinesUC != NULL)
8547 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008548 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549#endif
8550 )
8551 || Rows == 0
8552 || Columns == 0
8553 || (!full_screen && ScreenLines == NULL))
8554 return;
8555
8556 /*
8557 * It's possible that we produce an out-of-memory message below, which
8558 * will cause this function to be called again. To break the loop, just
8559 * return here.
8560 */
8561 if (entered)
8562 return;
8563 entered = TRUE;
8564
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008565 /*
8566 * Note that the window sizes are updated before reallocating the arrays,
8567 * thus we must not redraw here!
8568 */
8569 ++RedrawingDisabled;
8570
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 win_new_shellsize(); /* fit the windows in the new sized shell */
8572
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 comp_col(); /* recompute columns for shown command and ruler */
8574
8575 /*
8576 * We're changing the size of the screen.
8577 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8578 * - Move lines from the old arrays into the new arrays, clear extra
8579 * lines (unless the screen is going to be cleared).
8580 * - Free the old arrays.
8581 *
8582 * If anything fails, make ScreenLines NULL, so we don't do anything!
8583 * Continuing with the old ScreenLines may result in a crash, because the
8584 * size is wrong.
8585 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008586 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008588#ifdef FEAT_AUTOCMD
8589 if (aucmd_win != NULL)
8590 win_free_lsize(aucmd_win);
8591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592
8593 new_ScreenLines = (schar_T *)lalloc((long_u)(
8594 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8595#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008596 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 if (enc_utf8)
8598 {
8599 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8600 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008601 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008602 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8604 }
8605 if (enc_dbcs == DBCS_JPNU)
8606 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8607 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8608#endif
8609 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8610 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8611 new_LineOffset = (unsigned *)lalloc((long_u)(
8612 Rows * sizeof(unsigned)), FALSE);
8613 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008614#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008615 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008618 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 {
8620 if (win_alloc_lines(wp) == FAIL)
8621 {
8622 outofmem = TRUE;
8623#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008624 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625#endif
8626 }
8627 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008628#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008629 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8630 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008631 outofmem = TRUE;
8632#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008633#ifdef FEAT_WINDOWS
8634give_up:
8635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008637#ifdef FEAT_MBYTE
8638 for (i = 0; i < p_mco; ++i)
8639 if (new_ScreenLinesC[i] == NULL)
8640 break;
8641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 if (new_ScreenLines == NULL
8643#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008644 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8646#endif
8647 || new_ScreenAttrs == NULL
8648 || new_LineOffset == NULL
8649 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008650#ifdef FEAT_WINDOWS
8651 || new_TabPageIdxs == NULL
8652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 || outofmem)
8654 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008655 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008656 {
8657 /* guess the size */
8658 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8659
8660 /* Remember we did this to avoid getting outofmem messages over
8661 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008662 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 vim_free(new_ScreenLines);
8665 new_ScreenLines = NULL;
8666#ifdef FEAT_MBYTE
8667 vim_free(new_ScreenLinesUC);
8668 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008669 for (i = 0; i < p_mco; ++i)
8670 {
8671 vim_free(new_ScreenLinesC[i]);
8672 new_ScreenLinesC[i] = NULL;
8673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 vim_free(new_ScreenLines2);
8675 new_ScreenLines2 = NULL;
8676#endif
8677 vim_free(new_ScreenAttrs);
8678 new_ScreenAttrs = NULL;
8679 vim_free(new_LineOffset);
8680 new_LineOffset = NULL;
8681 vim_free(new_LineWraps);
8682 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008683#ifdef FEAT_WINDOWS
8684 vim_free(new_TabPageIdxs);
8685 new_TabPageIdxs = NULL;
8686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 }
8688 else
8689 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008690 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008691
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 for (new_row = 0; new_row < Rows; ++new_row)
8693 {
8694 new_LineOffset[new_row] = new_row * Columns;
8695 new_LineWraps[new_row] = FALSE;
8696
8697 /*
8698 * If the screen is not going to be cleared, copy as much as
8699 * possible from the old screen to the new one and clear the rest
8700 * (used when resizing the window at the "--more--" prompt or when
8701 * executing an external command, for the GUI).
8702 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008703 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 {
8705 (void)vim_memset(new_ScreenLines + new_row * Columns,
8706 ' ', (size_t)Columns * sizeof(schar_T));
8707#ifdef FEAT_MBYTE
8708 if (enc_utf8)
8709 {
8710 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8711 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008712 for (i = 0; i < p_mco; ++i)
8713 (void)vim_memset(new_ScreenLinesC[i]
8714 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 0, (size_t)Columns * sizeof(u8char_T));
8716 }
8717 if (enc_dbcs == DBCS_JPNU)
8718 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8719 0, (size_t)Columns * sizeof(schar_T));
8720#endif
8721 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8722 0, (size_t)Columns * sizeof(sattr_T));
8723 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008724 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 {
8726 if (screen_Columns < Columns)
8727 len = screen_Columns;
8728 else
8729 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008730#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008731 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008732 * may be invalid now. Also when p_mco changes. */
8733 if (!(enc_utf8 && ScreenLinesUC == NULL)
8734 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008735#endif
8736 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8737 ScreenLines + LineOffset[old_row],
8738 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008740 if (enc_utf8 && ScreenLinesUC != NULL
8741 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 {
8743 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8744 ScreenLinesUC + LineOffset[old_row],
8745 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008746 for (i = 0; i < p_mco; ++i)
8747 mch_memmove(new_ScreenLinesC[i]
8748 + new_LineOffset[new_row],
8749 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 (size_t)len * sizeof(u8char_T));
8751 }
8752 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8753 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8754 ScreenLines2 + LineOffset[old_row],
8755 (size_t)len * sizeof(schar_T));
8756#endif
8757 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8758 ScreenAttrs + LineOffset[old_row],
8759 (size_t)len * sizeof(sattr_T));
8760 }
8761 }
8762 }
8763 /* Use the last line of the screen for the current line. */
8764 current_ScreenLine = new_ScreenLines + Rows * Columns;
8765 }
8766
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008767 free_screenlines();
8768
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 ScreenLines = new_ScreenLines;
8770#ifdef FEAT_MBYTE
8771 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008772 for (i = 0; i < p_mco; ++i)
8773 ScreenLinesC[i] = new_ScreenLinesC[i];
8774 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 ScreenLines2 = new_ScreenLines2;
8776#endif
8777 ScreenAttrs = new_ScreenAttrs;
8778 LineOffset = new_LineOffset;
8779 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008780#ifdef FEAT_WINDOWS
8781 TabPageIdxs = new_TabPageIdxs;
8782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784 /* It's important that screen_Rows and screen_Columns reflect the actual
8785 * size of ScreenLines[]. Set them before calling anything. */
8786#ifdef FEAT_GUI
8787 old_Rows = screen_Rows;
8788#endif
8789 screen_Rows = Rows;
8790 screen_Columns = Columns;
8791
8792 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008793 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 screenclear2();
8795
8796#ifdef FEAT_GUI
8797 else if (gui.in_use
8798 && !gui.starting
8799 && ScreenLines != NULL
8800 && old_Rows != Rows)
8801 {
8802 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8803 /*
8804 * Adjust the position of the cursor, for when executing an external
8805 * command.
8806 */
8807 if (msg_row >= Rows) /* Rows got smaller */
8808 msg_row = Rows - 1; /* put cursor at last row */
8809 else if (Rows > old_Rows) /* Rows got bigger */
8810 msg_row += Rows - old_Rows; /* put cursor in same place */
8811 if (msg_col >= Columns) /* Columns got smaller */
8812 msg_col = Columns - 1; /* put cursor at last column */
8813 }
8814#endif
8815
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008817 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008818
8819#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008820 /*
8821 * Do not apply autocommands more than 3 times to avoid an endless loop
8822 * in case applying autocommands always changes Rows or Columns.
8823 */
8824 if (starting == 0 && ++retry_count <= 3)
8825 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008826 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008827 /* In rare cases, autocommands may have altered Rows or Columns,
8828 * jump back to check if we need to allocate the screen again. */
8829 goto retry;
8830 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832}
8833
8834 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008835free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008836{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008837#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008838 int i;
8839
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008840 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008841 for (i = 0; i < Screen_mco; ++i)
8842 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008843 vim_free(ScreenLines2);
8844#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008845 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008846 vim_free(ScreenAttrs);
8847 vim_free(LineOffset);
8848 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008849#ifdef FEAT_WINDOWS
8850 vim_free(TabPageIdxs);
8851#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008852}
8853
8854 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008855screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856{
8857 check_for_delay(FALSE);
8858 screenalloc(FALSE); /* allocate screen buffers if size changed */
8859 screenclear2(); /* clear the screen */
8860}
8861
8862 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008863screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864{
8865 int i;
8866
8867 if (starting == NO_SCREEN || ScreenLines == NULL
8868#ifdef FEAT_GUI
8869 || (gui.in_use && gui.starting)
8870#endif
8871 )
8872 return;
8873
8874#ifdef FEAT_GUI
8875 if (!gui.in_use)
8876#endif
8877 screen_attr = -1; /* force setting the Normal colors */
8878 screen_stop_highlight(); /* don't want highlighting here */
8879
8880#ifdef FEAT_CLIPBOARD
8881 /* disable selection without redrawing it */
8882 clip_scroll_selection(9999);
8883#endif
8884
8885 /* blank out ScreenLines */
8886 for (i = 0; i < Rows; ++i)
8887 {
8888 lineclear(LineOffset[i], (int)Columns);
8889 LineWraps[i] = FALSE;
8890 }
8891
8892 if (can_clear(T_CL))
8893 {
8894 out_str(T_CL); /* clear the display */
8895 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008896 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 }
8898 else
8899 {
8900 /* can't clear the screen, mark all chars with invalid attributes */
8901 for (i = 0; i < Rows; ++i)
8902 lineinvalid(LineOffset[i], (int)Columns);
8903 clear_cmdline = TRUE;
8904 }
8905
8906 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8907
8908 win_rest_invalid(firstwin);
8909 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008910#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008911 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 if (must_redraw == CLEAR) /* no need to clear again */
8914 must_redraw = NOT_VALID;
8915 compute_cmdrow();
8916 msg_row = cmdline_row; /* put cursor on last line for messages */
8917 msg_col = 0;
8918 screen_start(); /* don't know where cursor is now */
8919 msg_scrolled = 0; /* can't scroll back */
8920 msg_didany = FALSE;
8921 msg_didout = FALSE;
8922}
8923
8924/*
8925 * Clear one line in ScreenLines.
8926 */
8927 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008928lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929{
8930 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8931#ifdef FEAT_MBYTE
8932 if (enc_utf8)
8933 (void)vim_memset(ScreenLinesUC + off, 0,
8934 (size_t)width * sizeof(u8char_T));
8935#endif
8936 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8937}
8938
8939/*
8940 * Mark one line in ScreenLines invalid by setting the attributes to an
8941 * invalid value.
8942 */
8943 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008944lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945{
8946 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8947}
8948
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008949#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950/*
8951 * Copy part of a Screenline for vertically split window "wp".
8952 */
8953 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008954linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
8956 unsigned off_to = LineOffset[to] + wp->w_wincol;
8957 unsigned off_from = LineOffset[from] + wp->w_wincol;
8958
8959 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8960 wp->w_width * sizeof(schar_T));
8961# ifdef FEAT_MBYTE
8962 if (enc_utf8)
8963 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008964 int i;
8965
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8967 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008968 for (i = 0; i < p_mco; ++i)
8969 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8970 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 }
8972 if (enc_dbcs == DBCS_JPNU)
8973 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8974 wp->w_width * sizeof(schar_T));
8975# endif
8976 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8977 wp->w_width * sizeof(sattr_T));
8978}
8979#endif
8980
8981/*
8982 * Return TRUE if clearing with term string "p" would work.
8983 * It can't work when the string is empty or it won't set the right background.
8984 */
8985 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008986can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
8988 return (*p != NUL && (t_colors <= 1
8989#ifdef FEAT_GUI
8990 || gui.in_use
8991#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008992#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008993 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008994 || (!p_tgc && cterm_normal_bg_color == 0)
8995#else
8996 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008997#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02008998 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999}
9000
9001/*
9002 * Reset cursor position. Use whenever cursor was moved because of outputting
9003 * something directly to the screen (shell commands) or a terminal control
9004 * code.
9005 */
9006 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009007screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
9009 screen_cur_row = screen_cur_col = 9999;
9010}
9011
9012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 * Move the cursor to position "row","col" in the screen.
9014 * This tries to find the most efficient way to move, minimizing the number of
9015 * characters sent to the terminal.
9016 */
9017 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009018windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009020 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 int i;
9022 int plan;
9023 int cost;
9024 int wouldbe_col;
9025 int noinvcurs;
9026 char_u *bs;
9027 int goto_cost;
9028 int attr;
9029
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009030#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9032
9033#define PLAN_LE 1
9034#define PLAN_CR 2
9035#define PLAN_NL 3
9036#define PLAN_WRITE 4
9037 /* Can't use ScreenLines unless initialized */
9038 if (ScreenLines == NULL)
9039 return;
9040
9041 if (col != screen_cur_col || row != screen_cur_row)
9042 {
9043 /* Check for valid position. */
9044 if (row < 0) /* window without text lines? */
9045 row = 0;
9046 if (row >= screen_Rows)
9047 row = screen_Rows - 1;
9048 if (col >= screen_Columns)
9049 col = screen_Columns - 1;
9050
9051 /* check if no cursor movement is allowed in highlight mode */
9052 if (screen_attr && *T_MS == NUL)
9053 noinvcurs = HIGHL_COST;
9054 else
9055 noinvcurs = 0;
9056 goto_cost = GOTO_COST + noinvcurs;
9057
9058 /*
9059 * Plan how to do the positioning:
9060 * 1. Use CR to move it to column 0, same row.
9061 * 2. Use T_LE to move it a few columns to the left.
9062 * 3. Use NL to move a few lines down, column 0.
9063 * 4. Move a few columns to the right with T_ND or by writing chars.
9064 *
9065 * Don't do this if the cursor went beyond the last column, the cursor
9066 * position is unknown then (some terminals wrap, some don't )
9067 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009068 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 * characters to move the cursor to the right.
9070 */
9071 if (row >= screen_cur_row && screen_cur_col < Columns)
9072 {
9073 /*
9074 * If the cursor is in the same row, bigger col, we can use CR
9075 * or T_LE.
9076 */
9077 bs = NULL; /* init for GCC */
9078 attr = screen_attr;
9079 if (row == screen_cur_row && col < screen_cur_col)
9080 {
9081 /* "le" is preferred over "bc", because "bc" is obsolete */
9082 if (*T_LE)
9083 bs = T_LE; /* "cursor left" */
9084 else
9085 bs = T_BC; /* "backspace character (old) */
9086 if (*bs)
9087 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9088 else
9089 cost = 999;
9090 if (col + 1 < cost) /* using CR is less characters */
9091 {
9092 plan = PLAN_CR;
9093 wouldbe_col = 0;
9094 cost = 1; /* CR is just one character */
9095 }
9096 else
9097 {
9098 plan = PLAN_LE;
9099 wouldbe_col = col;
9100 }
9101 if (noinvcurs) /* will stop highlighting */
9102 {
9103 cost += noinvcurs;
9104 attr = 0;
9105 }
9106 }
9107
9108 /*
9109 * If the cursor is above where we want to be, we can use CR LF.
9110 */
9111 else if (row > screen_cur_row)
9112 {
9113 plan = PLAN_NL;
9114 wouldbe_col = 0;
9115 cost = (row - screen_cur_row) * 2; /* CR LF */
9116 if (noinvcurs) /* will stop highlighting */
9117 {
9118 cost += noinvcurs;
9119 attr = 0;
9120 }
9121 }
9122
9123 /*
9124 * If the cursor is in the same row, smaller col, just use write.
9125 */
9126 else
9127 {
9128 plan = PLAN_WRITE;
9129 wouldbe_col = screen_cur_col;
9130 cost = 0;
9131 }
9132
9133 /*
9134 * Check if any characters that need to be written have the
9135 * correct attributes. Also avoid UTF-8 characters.
9136 */
9137 i = col - wouldbe_col;
9138 if (i > 0)
9139 cost += i;
9140 if (cost < goto_cost && i > 0)
9141 {
9142 /*
9143 * Check if the attributes are correct without additionally
9144 * stopping highlighting.
9145 */
9146 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9147 while (i && *p++ == attr)
9148 --i;
9149 if (i != 0)
9150 {
9151 /*
9152 * Try if it works when highlighting is stopped here.
9153 */
9154 if (*--p == 0)
9155 {
9156 cost += noinvcurs;
9157 while (i && *p++ == 0)
9158 --i;
9159 }
9160 if (i != 0)
9161 cost = 999; /* different attributes, don't do it */
9162 }
9163#ifdef FEAT_MBYTE
9164 if (enc_utf8)
9165 {
9166 /* Don't use an UTF-8 char for positioning, it's slow. */
9167 for (i = wouldbe_col; i < col; ++i)
9168 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9169 {
9170 cost = 999;
9171 break;
9172 }
9173 }
9174#endif
9175 }
9176
9177 /*
9178 * We can do it without term_windgoto()!
9179 */
9180 if (cost < goto_cost)
9181 {
9182 if (plan == PLAN_LE)
9183 {
9184 if (noinvcurs)
9185 screen_stop_highlight();
9186 while (screen_cur_col > col)
9187 {
9188 out_str(bs);
9189 --screen_cur_col;
9190 }
9191 }
9192 else if (plan == PLAN_CR)
9193 {
9194 if (noinvcurs)
9195 screen_stop_highlight();
9196 out_char('\r');
9197 screen_cur_col = 0;
9198 }
9199 else if (plan == PLAN_NL)
9200 {
9201 if (noinvcurs)
9202 screen_stop_highlight();
9203 while (screen_cur_row < row)
9204 {
9205 out_char('\n');
9206 ++screen_cur_row;
9207 }
9208 screen_cur_col = 0;
9209 }
9210
9211 i = col - screen_cur_col;
9212 if (i > 0)
9213 {
9214 /*
9215 * Use cursor-right if it's one character only. Avoids
9216 * removing a line of pixels from the last bold char, when
9217 * using the bold trick in the GUI.
9218 */
9219 if (T_ND[0] != NUL && T_ND[1] == NUL)
9220 {
9221 while (i-- > 0)
9222 out_char(*T_ND);
9223 }
9224 else
9225 {
9226 int off;
9227
9228 off = LineOffset[row] + screen_cur_col;
9229 while (i-- > 0)
9230 {
9231 if (ScreenAttrs[off] != screen_attr)
9232 screen_stop_highlight();
9233#ifdef FEAT_MBYTE
9234 out_flush_check();
9235#endif
9236 out_char(ScreenLines[off]);
9237#ifdef FEAT_MBYTE
9238 if (enc_dbcs == DBCS_JPNU
9239 && ScreenLines[off] == 0x8e)
9240 out_char(ScreenLines2[off]);
9241#endif
9242 ++off;
9243 }
9244 }
9245 }
9246 }
9247 }
9248 else
9249 cost = 999;
9250
9251 if (cost >= goto_cost)
9252 {
9253 if (noinvcurs)
9254 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009255 if (row == screen_cur_row && (col > screen_cur_col)
9256 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 term_cursor_right(col - screen_cur_col);
9258 else
9259 term_windgoto(row, col);
9260 }
9261 screen_cur_row = row;
9262 screen_cur_col = col;
9263 }
9264}
9265
9266/*
9267 * Set cursor to its position in the current window.
9268 */
9269 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009270setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271{
9272 if (redrawing())
9273 {
9274 validate_cursor();
9275 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9276 W_WINCOL(curwin) + (
9277#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009278 /* With 'rightleft' set and the cursor on a double-wide
9279 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9281# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009282 (has_mbyte
9283 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9284 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285# endif
9286 1)) :
9287#endif
9288 curwin->w_wcol));
9289 }
9290}
9291
9292
9293/*
9294 * insert 'line_count' lines at 'row' in window 'wp'
9295 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9296 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9297 * scrolling.
9298 * Returns FAIL if the lines are not inserted, OK for success.
9299 */
9300 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009301win_ins_lines(
9302 win_T *wp,
9303 int row,
9304 int line_count,
9305 int invalid,
9306 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307{
9308 int did_delete;
9309 int nextrow;
9310 int lastrow;
9311 int retval;
9312
9313 if (invalid)
9314 wp->w_lines_valid = 0;
9315
9316 if (wp->w_height < 5)
9317 return FAIL;
9318
9319 if (line_count > wp->w_height - row)
9320 line_count = wp->w_height - row;
9321
9322 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9323 if (retval != MAYBE)
9324 return retval;
9325
9326 /*
9327 * If there is a next window or a status line, we first try to delete the
9328 * lines at the bottom to avoid messing what is after the window.
9329 * If this fails and there are following windows, don't do anything to avoid
9330 * messing up those windows, better just redraw.
9331 */
9332 did_delete = FALSE;
9333#ifdef FEAT_WINDOWS
9334 if (wp->w_next != NULL || wp->w_status_height)
9335 {
9336 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9337 line_count, (int)Rows, FALSE, NULL) == OK)
9338 did_delete = TRUE;
9339 else if (wp->w_next)
9340 return FAIL;
9341 }
9342#endif
9343 /*
9344 * if no lines deleted, blank the lines that will end up below the window
9345 */
9346 if (!did_delete)
9347 {
9348#ifdef FEAT_WINDOWS
9349 wp->w_redr_status = TRUE;
9350#endif
9351 redraw_cmdline = TRUE;
9352 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9353 lastrow = nextrow + line_count;
9354 if (lastrow > Rows)
9355 lastrow = Rows;
9356 screen_fill(nextrow - line_count, lastrow - line_count,
9357 W_WINCOL(wp), (int)W_ENDCOL(wp),
9358 ' ', ' ', 0);
9359 }
9360
9361 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9362 == FAIL)
9363 {
9364 /* deletion will have messed up other windows */
9365 if (did_delete)
9366 {
9367#ifdef FEAT_WINDOWS
9368 wp->w_redr_status = TRUE;
9369#endif
9370 win_rest_invalid(W_NEXT(wp));
9371 }
9372 return FAIL;
9373 }
9374
9375 return OK;
9376}
9377
9378/*
9379 * delete "line_count" window lines at "row" in window "wp"
9380 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9381 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9382 * scrolling
9383 * Return OK for success, FAIL if the lines are not deleted.
9384 */
9385 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009386win_del_lines(
9387 win_T *wp,
9388 int row,
9389 int line_count,
9390 int invalid,
9391 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392{
9393 int retval;
9394
9395 if (invalid)
9396 wp->w_lines_valid = 0;
9397
9398 if (line_count > wp->w_height - row)
9399 line_count = wp->w_height - row;
9400
9401 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9402 if (retval != MAYBE)
9403 return retval;
9404
9405 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9406 (int)Rows, FALSE, NULL) == FAIL)
9407 return FAIL;
9408
9409#ifdef FEAT_WINDOWS
9410 /*
9411 * If there are windows or status lines below, try to put them at the
9412 * correct place. If we can't do that, they have to be redrawn.
9413 */
9414 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9415 {
9416 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9417 line_count, (int)Rows, NULL) == FAIL)
9418 {
9419 wp->w_redr_status = TRUE;
9420 win_rest_invalid(wp->w_next);
9421 }
9422 }
9423 /*
9424 * If this is the last window and there is no status line, redraw the
9425 * command line later.
9426 */
9427 else
9428#endif
9429 redraw_cmdline = TRUE;
9430 return OK;
9431}
9432
9433/*
9434 * Common code for win_ins_lines() and win_del_lines().
9435 * Returns OK or FAIL when the work has been done.
9436 * Returns MAYBE when not finished yet.
9437 */
9438 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009439win_do_lines(
9440 win_T *wp,
9441 int row,
9442 int line_count,
9443 int mayclear,
9444 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
9446 int retval;
9447
9448 if (!redrawing() || line_count <= 0)
9449 return FAIL;
9450
9451 /* only a few lines left: redraw is faster */
9452 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009453#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 && wp->w_width == Columns
9455#endif
9456 )
9457 {
9458 screenclear(); /* will set wp->w_lines_valid to 0 */
9459 return FAIL;
9460 }
9461
9462 /*
9463 * Delete all remaining lines
9464 */
9465 if (row + line_count >= wp->w_height)
9466 {
9467 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9468 W_WINCOL(wp), (int)W_ENDCOL(wp),
9469 ' ', ' ', 0);
9470 return OK;
9471 }
9472
9473 /*
9474 * when scrolling, the message on the command line should be cleared,
9475 * otherwise it will stay there forever.
9476 */
9477 clear_cmdline = TRUE;
9478
9479 /*
9480 * If the terminal can set a scroll region, use that.
9481 * Always do this in a vertically split window. This will redraw from
9482 * ScreenLines[] when t_CV isn't defined. That's faster than using
9483 * win_line().
9484 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009485 * a character in the lower right corner of the scroll region may cause a
9486 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 */
9488 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009489#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 || W_WIDTH(wp) != Columns
9491#endif
9492 )
9493 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009494#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9496#endif
9497 scroll_region_set(wp, row);
9498 if (del)
9499 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9500 wp->w_height - row, FALSE, wp);
9501 else
9502 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9503 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009504#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9506#endif
9507 scroll_region_reset();
9508 return retval;
9509 }
9510
9511#ifdef FEAT_WINDOWS
9512 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9513 return FAIL;
9514#endif
9515
9516 return MAYBE;
9517}
9518
9519/*
9520 * window 'wp' and everything after it is messed up, mark it for redraw
9521 */
9522 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009523win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524{
9525#ifdef FEAT_WINDOWS
9526 while (wp != NULL)
9527#else
9528 if (wp != NULL)
9529#endif
9530 {
9531 redraw_win_later(wp, NOT_VALID);
9532#ifdef FEAT_WINDOWS
9533 wp->w_redr_status = TRUE;
9534 wp = wp->w_next;
9535#endif
9536 }
9537 redraw_cmdline = TRUE;
9538}
9539
9540/*
9541 * The rest of the routines in this file perform screen manipulations. The
9542 * given operation is performed physically on the screen. The corresponding
9543 * change is also made to the internal screen image. In this way, the editor
9544 * anticipates the effect of editing changes on the appearance of the screen.
9545 * That way, when we call screenupdate a complete redraw isn't usually
9546 * necessary. Another advantage is that we can keep adding code to anticipate
9547 * screen changes, and in the meantime, everything still works.
9548 */
9549
9550/*
9551 * types for inserting or deleting lines
9552 */
9553#define USE_T_CAL 1
9554#define USE_T_CDL 2
9555#define USE_T_AL 3
9556#define USE_T_CE 4
9557#define USE_T_DL 5
9558#define USE_T_SR 6
9559#define USE_NL 7
9560#define USE_T_CD 8
9561#define USE_REDRAW 9
9562
9563/*
9564 * insert lines on the screen and update ScreenLines[]
9565 * 'end' is the line after the scrolled part. Normally it is Rows.
9566 * When scrolling region used 'off' is the offset from the top for the region.
9567 * 'row' and 'end' are relative to the start of the region.
9568 *
9569 * return FAIL for failure, OK for success.
9570 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009571 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009572screen_ins_lines(
9573 int off,
9574 int row,
9575 int line_count,
9576 int end,
9577 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
9579 int i;
9580 int j;
9581 unsigned temp;
9582 int cursor_row;
9583 int type;
9584 int result_empty;
9585 int can_ce = can_clear(T_CE);
9586
9587 /*
9588 * FAIL if
9589 * - there is no valid screen
9590 * - the screen has to be redrawn completely
9591 * - the line count is less than one
9592 * - the line count is more than 'ttyscroll'
9593 */
9594 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9595 return FAIL;
9596
9597 /*
9598 * There are seven ways to insert lines:
9599 * 0. When in a vertically split window and t_CV isn't set, redraw the
9600 * characters from ScreenLines[].
9601 * 1. Use T_CD (clear to end of display) if it exists and the result of
9602 * the insert is just empty lines
9603 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9604 * present or line_count > 1. It looks better if we do all the inserts
9605 * at once.
9606 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9607 * insert is just empty lines and T_CE is not present or line_count >
9608 * 1.
9609 * 4. Use T_AL (insert line) if it exists.
9610 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9611 * just empty lines.
9612 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9613 * just empty lines.
9614 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9615 * the 'da' flag is not set or we have clear line capability.
9616 * 8. redraw the characters from ScreenLines[].
9617 *
9618 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9619 * the scrollbar for the window. It does have insert line, use that if it
9620 * exists.
9621 */
9622 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009623#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9625 type = USE_REDRAW;
9626 else
9627#endif
9628 if (can_clear(T_CD) && result_empty)
9629 type = USE_T_CD;
9630 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9631 type = USE_T_CAL;
9632 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9633 type = USE_T_CDL;
9634 else if (*T_AL != NUL)
9635 type = USE_T_AL;
9636 else if (can_ce && result_empty)
9637 type = USE_T_CE;
9638 else if (*T_DL != NUL && result_empty)
9639 type = USE_T_DL;
9640 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9641 type = USE_T_SR;
9642 else
9643 return FAIL;
9644
9645 /*
9646 * For clearing the lines screen_del_lines() is used. This will also take
9647 * care of t_db if necessary.
9648 */
9649 if (type == USE_T_CD || type == USE_T_CDL ||
9650 type == USE_T_CE || type == USE_T_DL)
9651 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9652
9653 /*
9654 * If text is retained below the screen, first clear or delete as many
9655 * lines at the bottom of the window as are about to be inserted so that
9656 * the deleted lines won't later surface during a screen_del_lines.
9657 */
9658 if (*T_DB)
9659 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9660
9661#ifdef FEAT_CLIPBOARD
9662 /* Remove a modeless selection when inserting lines halfway the screen
9663 * or not the full width of the screen. */
9664 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009665# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 || (wp != NULL && wp->w_width != Columns)
9667# endif
9668 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009669 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 else
9671 clip_scroll_selection(-line_count);
9672#endif
9673
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674#ifdef FEAT_GUI
9675 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9676 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009677 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678#endif
9679
9680 if (*T_CCS != NUL) /* cursor relative to region */
9681 cursor_row = row;
9682 else
9683 cursor_row = row + off;
9684
9685 /*
9686 * Shift LineOffset[] line_count down to reflect the inserted lines.
9687 * Clear the inserted lines in ScreenLines[].
9688 */
9689 row += off;
9690 end += off;
9691 for (i = 0; i < line_count; ++i)
9692 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009693#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 if (wp != NULL && wp->w_width != Columns)
9695 {
9696 /* need to copy part of a line */
9697 j = end - 1 - i;
9698 while ((j -= line_count) >= row)
9699 linecopy(j + line_count, j, wp);
9700 j += line_count;
9701 if (can_clear((char_u *)" "))
9702 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9703 else
9704 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9705 LineWraps[j] = FALSE;
9706 }
9707 else
9708#endif
9709 {
9710 j = end - 1 - i;
9711 temp = LineOffset[j];
9712 while ((j -= line_count) >= row)
9713 {
9714 LineOffset[j + line_count] = LineOffset[j];
9715 LineWraps[j + line_count] = LineWraps[j];
9716 }
9717 LineOffset[j + line_count] = temp;
9718 LineWraps[j + line_count] = FALSE;
9719 if (can_clear((char_u *)" "))
9720 lineclear(temp, (int)Columns);
9721 else
9722 lineinvalid(temp, (int)Columns);
9723 }
9724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725
9726 screen_stop_highlight();
9727 windgoto(cursor_row, 0);
9728
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009729#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 /* redraw the characters */
9731 if (type == USE_REDRAW)
9732 redraw_block(row, end, wp);
9733 else
9734#endif
9735 if (type == USE_T_CAL)
9736 {
9737 term_append_lines(line_count);
9738 screen_start(); /* don't know where cursor is now */
9739 }
9740 else
9741 {
9742 for (i = 0; i < line_count; i++)
9743 {
9744 if (type == USE_T_AL)
9745 {
9746 if (i && cursor_row != 0)
9747 windgoto(cursor_row, 0);
9748 out_str(T_AL);
9749 }
9750 else /* type == USE_T_SR */
9751 out_str(T_SR);
9752 screen_start(); /* don't know where cursor is now */
9753 }
9754 }
9755
9756 /*
9757 * With scroll-reverse and 'da' flag set we need to clear the lines that
9758 * have been scrolled down into the region.
9759 */
9760 if (type == USE_T_SR && *T_DA)
9761 {
9762 for (i = 0; i < line_count; ++i)
9763 {
9764 windgoto(off + i, 0);
9765 out_str(T_CE);
9766 screen_start(); /* don't know where cursor is now */
9767 }
9768 }
9769
9770#ifdef FEAT_GUI
9771 gui_can_update_cursor();
9772 if (gui.in_use)
9773 out_flush(); /* always flush after a scroll */
9774#endif
9775 return OK;
9776}
9777
9778/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009779 * Delete lines on the screen and update ScreenLines[].
9780 * "end" is the line after the scrolled part. Normally it is Rows.
9781 * When scrolling region used "off" is the offset from the top for the region.
9782 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 *
9784 * Return OK for success, FAIL if the lines are not deleted.
9785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009787screen_del_lines(
9788 int off,
9789 int row,
9790 int line_count,
9791 int end,
9792 int force, /* even when line_count > p_ttyscroll */
9793 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794{
9795 int j;
9796 int i;
9797 unsigned temp;
9798 int cursor_row;
9799 int cursor_end;
9800 int result_empty; /* result is empty until end of region */
9801 int can_delete; /* deleting line codes can be used */
9802 int type;
9803
9804 /*
9805 * FAIL if
9806 * - there is no valid screen
9807 * - the screen has to be redrawn completely
9808 * - the line count is less than one
9809 * - the line count is more than 'ttyscroll'
9810 */
9811 if (!screen_valid(TRUE) || line_count <= 0 ||
9812 (!force && line_count > p_ttyscroll))
9813 return FAIL;
9814
9815 /*
9816 * Check if the rest of the current region will become empty.
9817 */
9818 result_empty = row + line_count >= end;
9819
9820 /*
9821 * We can delete lines only when 'db' flag not set or when 'ce' option
9822 * available.
9823 */
9824 can_delete = (*T_DB == NUL || can_clear(T_CE));
9825
9826 /*
9827 * There are six ways to delete lines:
9828 * 0. When in a vertically split window and t_CV isn't set, redraw the
9829 * characters from ScreenLines[].
9830 * 1. Use T_CD if it exists and the result is empty.
9831 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9832 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9833 * none of the other ways work.
9834 * 4. Use T_CE (erase line) if the result is empty.
9835 * 5. Use T_DL (delete line) if it exists.
9836 * 6. redraw the characters from ScreenLines[].
9837 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009838#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9840 type = USE_REDRAW;
9841 else
9842#endif
9843 if (can_clear(T_CD) && result_empty)
9844 type = USE_T_CD;
9845#if defined(__BEOS__) && defined(BEOS_DR8)
9846 /*
9847 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9848 * its internal termcap... this works okay for tests which test *T_DB !=
9849 * NUL. It has the disadvantage that the user cannot use any :set t_*
9850 * command to get T_DB (back) to empty_option, only :set term=... will do
9851 * the trick...
9852 * Anyway, this hack will hopefully go away with the next OS release.
9853 * (Olaf Seibert)
9854 */
9855 else if (row == 0 && T_DB == empty_option
9856 && (line_count == 1 || *T_CDL == NUL))
9857#else
9858 else if (row == 0 && (
9859#ifndef AMIGA
9860 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9861 * up, so use delete-line command */
9862 line_count == 1 ||
9863#endif
9864 *T_CDL == NUL))
9865#endif
9866 type = USE_NL;
9867 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9868 type = USE_T_CDL;
9869 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009870#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 && (wp == NULL || wp->w_width == Columns)
9872#endif
9873 )
9874 type = USE_T_CE;
9875 else if (*T_DL != NUL && can_delete)
9876 type = USE_T_DL;
9877 else if (*T_CDL != NUL && can_delete)
9878 type = USE_T_CDL;
9879 else
9880 return FAIL;
9881
9882#ifdef FEAT_CLIPBOARD
9883 /* Remove a modeless selection when deleting lines halfway the screen or
9884 * not the full width of the screen. */
9885 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009886# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 || (wp != NULL && wp->w_width != Columns)
9888# endif
9889 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009890 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 else
9892 clip_scroll_selection(line_count);
9893#endif
9894
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895#ifdef FEAT_GUI
9896 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9897 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009898 gui_dont_update_cursor(gui.cursor_row >= row + off
9899 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900#endif
9901
9902 if (*T_CCS != NUL) /* cursor relative to region */
9903 {
9904 cursor_row = row;
9905 cursor_end = end;
9906 }
9907 else
9908 {
9909 cursor_row = row + off;
9910 cursor_end = end + off;
9911 }
9912
9913 /*
9914 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9915 * Clear the inserted lines in ScreenLines[].
9916 */
9917 row += off;
9918 end += off;
9919 for (i = 0; i < line_count; ++i)
9920 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009921#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 if (wp != NULL && wp->w_width != Columns)
9923 {
9924 /* need to copy part of a line */
9925 j = row + i;
9926 while ((j += line_count) <= end - 1)
9927 linecopy(j - line_count, j, wp);
9928 j -= line_count;
9929 if (can_clear((char_u *)" "))
9930 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9931 else
9932 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9933 LineWraps[j] = FALSE;
9934 }
9935 else
9936#endif
9937 {
9938 /* whole width, moving the line pointers is faster */
9939 j = row + i;
9940 temp = LineOffset[j];
9941 while ((j += line_count) <= end - 1)
9942 {
9943 LineOffset[j - line_count] = LineOffset[j];
9944 LineWraps[j - line_count] = LineWraps[j];
9945 }
9946 LineOffset[j - line_count] = temp;
9947 LineWraps[j - line_count] = FALSE;
9948 if (can_clear((char_u *)" "))
9949 lineclear(temp, (int)Columns);
9950 else
9951 lineinvalid(temp, (int)Columns);
9952 }
9953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954
9955 screen_stop_highlight();
9956
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009957#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 /* redraw the characters */
9959 if (type == USE_REDRAW)
9960 redraw_block(row, end, wp);
9961 else
9962#endif
9963 if (type == USE_T_CD) /* delete the lines */
9964 {
9965 windgoto(cursor_row, 0);
9966 out_str(T_CD);
9967 screen_start(); /* don't know where cursor is now */
9968 }
9969 else if (type == USE_T_CDL)
9970 {
9971 windgoto(cursor_row, 0);
9972 term_delete_lines(line_count);
9973 screen_start(); /* don't know where cursor is now */
9974 }
9975 /*
9976 * Deleting lines at top of the screen or scroll region: Just scroll
9977 * the whole screen (scroll region) up by outputting newlines on the
9978 * last line.
9979 */
9980 else if (type == USE_NL)
9981 {
9982 windgoto(cursor_end - 1, 0);
9983 for (i = line_count; --i >= 0; )
9984 out_char('\n'); /* cursor will remain on same line */
9985 }
9986 else
9987 {
9988 for (i = line_count; --i >= 0; )
9989 {
9990 if (type == USE_T_DL)
9991 {
9992 windgoto(cursor_row, 0);
9993 out_str(T_DL); /* delete a line */
9994 }
9995 else /* type == USE_T_CE */
9996 {
9997 windgoto(cursor_row + i, 0);
9998 out_str(T_CE); /* erase a line */
9999 }
10000 screen_start(); /* don't know where cursor is now */
10001 }
10002 }
10003
10004 /*
10005 * If the 'db' flag is set, we need to clear the lines that have been
10006 * scrolled up at the bottom of the region.
10007 */
10008 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10009 {
10010 for (i = line_count; i > 0; --i)
10011 {
10012 windgoto(cursor_end - i, 0);
10013 out_str(T_CE); /* erase a line */
10014 screen_start(); /* don't know where cursor is now */
10015 }
10016 }
10017
10018#ifdef FEAT_GUI
10019 gui_can_update_cursor();
10020 if (gui.in_use)
10021 out_flush(); /* always flush after a scroll */
10022#endif
10023
10024 return OK;
10025}
10026
10027/*
10028 * show the current mode and ruler
10029 *
10030 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10031 * If clear_cmdline is FALSE there may be a message there that needs to be
10032 * cleared only if a mode is shown.
10033 * Return the length of the message (0 if no message).
10034 */
10035 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010036showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037{
10038 int need_clear;
10039 int length = 0;
10040 int do_mode;
10041 int attr;
10042 int nwr_save;
10043#ifdef FEAT_INS_EXPAND
10044 int sub_attr;
10045#endif
10046
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010047 do_mode = ((p_smd && msg_silent == 0)
10048 && ((State & INSERT)
10049 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010050 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 if (do_mode || Recording)
10052 {
10053 /*
10054 * Don't show mode right now, when not redrawing or inside a mapping.
10055 * Call char_avail() only when we are going to show something, because
10056 * it takes a bit of time.
10057 */
10058 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10059 {
10060 redraw_cmdline = TRUE; /* show mode later */
10061 return 0;
10062 }
10063
10064 nwr_save = need_wait_return;
10065
10066 /* wait a bit before overwriting an important message */
10067 check_for_delay(FALSE);
10068
10069 /* if the cmdline is more than one line high, erase top lines */
10070 need_clear = clear_cmdline;
10071 if (clear_cmdline && cmdline_row < Rows - 1)
10072 msg_clr_cmdline(); /* will reset clear_cmdline */
10073
10074 /* Position on the last line in the window, column 0 */
10075 msg_pos_mode();
10076 cursor_off();
10077 attr = hl_attr(HLF_CM); /* Highlight mode */
10078 if (do_mode)
10079 {
10080 MSG_PUTS_ATTR("--", attr);
10081#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010082 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010083# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010084 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010085# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010086 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010087# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010088 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010089# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 MSG_PUTS_ATTR(" IM", attr);
10091# else
10092 MSG_PUTS_ATTR(" XIM", attr);
10093# endif
10094#endif
10095#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10096 if (gui.in_use)
10097 {
10098 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010099 {
10100 /* HANGUL */
10101 if (enc_utf8)
10102 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10103 else
10104 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 }
10107#endif
10108#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010109 /* CTRL-X in Insert mode */
10110 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 {
10112 /* These messages can get long, avoid a wrap in a narrow
10113 * window. Prefer showing edit_submode_extra. */
10114 length = (Rows - msg_row) * Columns - 3;
10115 if (edit_submode_extra != NULL)
10116 length -= vim_strsize(edit_submode_extra);
10117 if (length > 0)
10118 {
10119 if (edit_submode_pre != NULL)
10120 length -= vim_strsize(edit_submode_pre);
10121 if (length - vim_strsize(edit_submode) > 0)
10122 {
10123 if (edit_submode_pre != NULL)
10124 msg_puts_attr(edit_submode_pre, attr);
10125 msg_puts_attr(edit_submode, attr);
10126 }
10127 if (edit_submode_extra != NULL)
10128 {
10129 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10130 if ((int)edit_submode_highl < (int)HLF_COUNT)
10131 sub_attr = hl_attr(edit_submode_highl);
10132 else
10133 sub_attr = attr;
10134 msg_puts_attr(edit_submode_extra, sub_attr);
10135 }
10136 }
10137 length = 0;
10138 }
10139 else
10140#endif
10141 {
10142#ifdef FEAT_VREPLACE
10143 if (State & VREPLACE_FLAG)
10144 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10145 else
10146#endif
10147 if (State & REPLACE_FLAG)
10148 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10149 else if (State & INSERT)
10150 {
10151#ifdef FEAT_RIGHTLEFT
10152 if (p_ri)
10153 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10154#endif
10155 MSG_PUTS_ATTR(_(" INSERT"), attr);
10156 }
10157 else if (restart_edit == 'I')
10158 MSG_PUTS_ATTR(_(" (insert)"), attr);
10159 else if (restart_edit == 'R')
10160 MSG_PUTS_ATTR(_(" (replace)"), attr);
10161 else if (restart_edit == 'V')
10162 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10163#ifdef FEAT_RIGHTLEFT
10164 if (p_hkmap)
10165 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10166# ifdef FEAT_FKMAP
10167 if (p_fkmap)
10168 MSG_PUTS_ATTR(farsi_text_5, attr);
10169# endif
10170#endif
10171#ifdef FEAT_KEYMAP
10172 if (State & LANGMAP)
10173 {
10174# ifdef FEAT_ARABIC
10175 if (curwin->w_p_arab)
10176 MSG_PUTS_ATTR(_(" Arabic"), attr);
10177 else
10178# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010179 if (get_keymap_str(curwin, (char_u *)" (%s)",
10180 NameBuff, MAXPATHL))
10181 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 }
10183#endif
10184 if ((State & INSERT) && p_paste)
10185 MSG_PUTS_ATTR(_(" (paste)"), attr);
10186
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 if (VIsual_active)
10188 {
10189 char *p;
10190
10191 /* Don't concatenate separate words to avoid translation
10192 * problems. */
10193 switch ((VIsual_select ? 4 : 0)
10194 + (VIsual_mode == Ctrl_V) * 2
10195 + (VIsual_mode == 'V'))
10196 {
10197 case 0: p = N_(" VISUAL"); break;
10198 case 1: p = N_(" VISUAL LINE"); break;
10199 case 2: p = N_(" VISUAL BLOCK"); break;
10200 case 4: p = N_(" SELECT"); break;
10201 case 5: p = N_(" SELECT LINE"); break;
10202 default: p = N_(" SELECT BLOCK"); break;
10203 }
10204 MSG_PUTS_ATTR(_(p), attr);
10205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 MSG_PUTS_ATTR(" --", attr);
10207 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010208
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 need_clear = TRUE;
10210 }
10211 if (Recording
10212#ifdef FEAT_INS_EXPAND
10213 && edit_submode == NULL /* otherwise it gets too long */
10214#endif
10215 )
10216 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010217 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 need_clear = TRUE;
10219 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010220
10221 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 if (need_clear || clear_cmdline)
10223 msg_clr_eos();
10224 msg_didout = FALSE; /* overwrite this message */
10225 length = msg_col;
10226 msg_col = 0;
10227 need_wait_return = nwr_save; /* never ask for hit-return for this */
10228 }
10229 else if (clear_cmdline && msg_silent == 0)
10230 /* Clear the whole command line. Will reset "clear_cmdline". */
10231 msg_clr_cmdline();
10232
10233#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 /* In Visual mode the size of the selected area must be redrawn. */
10235 if (VIsual_active)
10236 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237
10238 /* If the last window has no status line, the ruler is after the mode
10239 * message and must be redrawn */
10240 if (redrawing()
10241# ifdef FEAT_WINDOWS
10242 && lastwin->w_status_height == 0
10243# endif
10244 )
10245 win_redr_ruler(lastwin, TRUE);
10246#endif
10247 redraw_cmdline = FALSE;
10248 clear_cmdline = FALSE;
10249
10250 return length;
10251}
10252
10253/*
10254 * Position for a mode message.
10255 */
10256 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010257msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258{
10259 msg_col = 0;
10260 msg_row = Rows - 1;
10261}
10262
10263/*
10264 * Delete mode message. Used when ESC is typed which is expected to end
10265 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010266 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 */
10268 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010269unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270{
10271 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010272 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 */
10274 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10275 redraw_cmdline = TRUE; /* delete mode later */
10276 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010277 clearmode();
10278}
10279
10280/*
10281 * Clear the mode message.
10282 */
10283 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010284clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010285{
10286 msg_pos_mode();
10287 if (Recording)
10288 recording_mode(hl_attr(HLF_CM));
10289 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290}
10291
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010292 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010293recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010294{
10295 MSG_PUTS_ATTR(_("recording"), attr);
10296 if (!shortmess(SHM_RECORDING))
10297 {
10298 char_u s[4];
10299 sprintf((char *)s, " @%c", Recording);
10300 MSG_PUTS_ATTR(s, attr);
10301 }
10302}
10303
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010304#if defined(FEAT_WINDOWS)
10305/*
10306 * Draw the tab pages line at the top of the Vim window.
10307 */
10308 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010309draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010310{
10311 int tabcount = 0;
10312 tabpage_T *tp;
10313 int tabwidth;
10314 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010315 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010316 int attr;
10317 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010318 win_T *cwp;
10319 int wincount;
10320 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010321 int c;
10322 int len;
10323 int attr_sel = hl_attr(HLF_TPS);
10324 int attr_nosel = hl_attr(HLF_TP);
10325 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010326 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010327 int room;
10328 int use_sep_chars = (t_colors < 8
10329#ifdef FEAT_GUI
10330 && !gui.in_use
10331#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010332#ifdef FEAT_TERMGUICOLORS
10333 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010334#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010335 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010336
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010337 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010338
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010339#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010340 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010341 if (gui_use_tabline())
10342 {
10343 gui_update_tabline();
10344 return;
10345 }
10346#endif
10347
10348 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010349 return;
10350
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010351#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010352
10353 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10354 for (scol = 0; scol < Columns; ++scol)
10355 TabPageIdxs[scol] = 0;
10356
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010357 /* Use the 'tabline' option if it's set. */
10358 if (*p_tal != NUL)
10359 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010360 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010361
10362 /* Check for an error. If there is one we would loop in redrawing the
10363 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010364 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010365 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010366 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010367 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010368 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010369 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010370 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010371 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010372#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010373 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010374 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010375 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010376
Bram Moolenaar238a5642006-02-21 22:12:05 +000010377 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10378 if (tabwidth < 6)
10379 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010380
Bram Moolenaar238a5642006-02-21 22:12:05 +000010381 attr = attr_nosel;
10382 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010383 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010384 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10385 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010386 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010387 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010388
Bram Moolenaar238a5642006-02-21 22:12:05 +000010389 if (tp->tp_topframe == topframe)
10390 attr = attr_sel;
10391 if (use_sep_chars && col > 0)
10392 screen_putchar('|', 0, col++, attr);
10393
10394 if (tp->tp_topframe != topframe)
10395 attr = attr_nosel;
10396
10397 screen_putchar(' ', 0, col++, attr);
10398
10399 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010400 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010401 cwp = curwin;
10402 wp = firstwin;
10403 }
10404 else
10405 {
10406 cwp = tp->tp_curwin;
10407 wp = tp->tp_firstwin;
10408 }
10409
10410 modified = FALSE;
10411 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10412 if (bufIsChanged(wp->w_buffer))
10413 modified = TRUE;
10414 if (modified || wincount > 1)
10415 {
10416 if (wincount > 1)
10417 {
10418 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010419 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010420 if (col + len >= Columns - 3)
10421 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010422 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010423#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010424 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010425#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010426 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010427#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010428 );
10429 col += len;
10430 }
10431 if (modified)
10432 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10433 screen_putchar(' ', 0, col++, attr);
10434 }
10435
10436 room = scol - col + tabwidth - 1;
10437 if (room > 0)
10438 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010439 /* Get buffer name in NameBuff[] */
10440 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010441 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010442 len = vim_strsize(NameBuff);
10443 p = NameBuff;
10444#ifdef FEAT_MBYTE
10445 if (has_mbyte)
10446 while (len > room)
10447 {
10448 len -= ptr2cells(p);
10449 mb_ptr_adv(p);
10450 }
10451 else
10452#endif
10453 if (len > room)
10454 {
10455 p += len - room;
10456 len = room;
10457 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010458 if (len > Columns - col - 1)
10459 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010460
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010461 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010462 col += len;
10463 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010464 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010465
10466 /* Store the tab page number in TabPageIdxs[], so that
10467 * jump_to_mouse() knows where each one is. */
10468 ++tabcount;
10469 while (scol < col)
10470 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010471 }
10472
Bram Moolenaar238a5642006-02-21 22:12:05 +000010473 if (use_sep_chars)
10474 c = '_';
10475 else
10476 c = ' ';
10477 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010478
10479 /* Put an "X" for closing the current tab if there are several. */
10480 if (first_tabpage->tp_next != NULL)
10481 {
10482 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10483 TabPageIdxs[Columns - 1] = -999;
10484 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010485 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010486
10487 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10488 * set. */
10489 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010490}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010491
10492/*
10493 * Get buffer name for "buf" into NameBuff[].
10494 * Takes care of special buffer names and translates special characters.
10495 */
10496 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010497get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010498{
10499 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010500 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010501 else
10502 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10503 trans_characters(NameBuff, MAXPATHL);
10504}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010505#endif
10506
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10508/*
10509 * Get the character to use in a status line. Get its attributes in "*attr".
10510 */
10511 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010512fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513{
10514 int fill;
10515 if (is_curwin)
10516 {
10517 *attr = hl_attr(HLF_S);
10518 fill = fill_stl;
10519 }
10520 else
10521 {
10522 *attr = hl_attr(HLF_SNC);
10523 fill = fill_stlnc;
10524 }
10525 /* Use fill when there is highlighting, and highlighting of current
10526 * window differs, or the fillchars differ, or this is not the
10527 * current window */
10528 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10529 || !is_curwin || firstwin == lastwin)
10530 || (fill_stl != fill_stlnc)))
10531 return fill;
10532 if (is_curwin)
10533 return '^';
10534 return '=';
10535}
10536#endif
10537
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010538#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539/*
10540 * Get the character to use in a separator between vertically split windows.
10541 * Get its attributes in "*attr".
10542 */
10543 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010544fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545{
10546 *attr = hl_attr(HLF_C);
10547 if (*attr == 0 && fill_vert == ' ')
10548 return '|';
10549 else
10550 return fill_vert;
10551}
10552#endif
10553
10554/*
10555 * Return TRUE if redrawing should currently be done.
10556 */
10557 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010558redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559{
10560 return (!RedrawingDisabled
10561 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10562}
10563
10564/*
10565 * Return TRUE if printing messages should currently be done.
10566 */
10567 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010568messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569{
10570 return (!(p_lz && char_avail() && !KeyTyped));
10571}
10572
10573/*
10574 * Show current status info in ruler and various other places
10575 * If always is FALSE, only show ruler if position has changed.
10576 */
10577 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010578showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579{
10580 if (!always && !redrawing())
10581 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010582#ifdef FEAT_INS_EXPAND
10583 if (pum_visible())
10584 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010585# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010586 /* Don't redraw right now, do it later. */
10587 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010588# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010589 return;
10590 }
10591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010593 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010594 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010595 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 else
10598#endif
10599#ifdef FEAT_CMDL_INFO
10600 win_redr_ruler(curwin, always);
10601#endif
10602
10603#ifdef FEAT_TITLE
10604 if (need_maketitle
10605# ifdef FEAT_STL_OPT
10606 || (p_icon && (stl_syntax & STL_IN_ICON))
10607 || (p_title && (stl_syntax & STL_IN_TITLE))
10608# endif
10609 )
10610 maketitle();
10611#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010612#ifdef FEAT_WINDOWS
10613 /* Redraw the tab pages line if needed. */
10614 if (redraw_tabline)
10615 draw_tabline();
10616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617}
10618
10619#ifdef FEAT_CMDL_INFO
10620 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010621win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010623#define RULER_BUF_LEN 70
10624 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 int row;
10626 int fillchar;
10627 int attr;
10628 int empty_line = FALSE;
10629 colnr_T virtcol;
10630 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010631 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010633#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 int this_ru_col;
10635 int off = 0;
10636 int width = Columns;
10637# define WITH_OFF(x) x
10638# define WITH_WIDTH(x) x
10639#else
10640# define WITH_OFF(x) 0
10641# define WITH_WIDTH(x) Columns
10642# define this_ru_col ru_col
10643#endif
10644
10645 /* If 'ruler' off or redrawing disabled, don't do anything */
10646 if (!p_ru)
10647 return;
10648
10649 /*
10650 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10651 * after deleting lines, before cursor.lnum is corrected.
10652 */
10653 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10654 return;
10655
10656#ifdef FEAT_INS_EXPAND
10657 /* Don't draw the ruler while doing insert-completion, it might overwrite
10658 * the (long) mode message. */
10659# ifdef FEAT_WINDOWS
10660 if (wp == lastwin && lastwin->w_status_height == 0)
10661# endif
10662 if (edit_submode != NULL)
10663 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010664 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10665 if (pum_visible())
10666 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667#endif
10668
10669#ifdef FEAT_STL_OPT
10670 if (*p_ruf)
10671 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010672 int save_called_emsg = called_emsg;
10673
10674 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010676 if (called_emsg)
10677 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010678 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010679 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 return;
10681 }
10682#endif
10683
10684 /*
10685 * Check if not in Insert mode and the line is empty (will show "0-1").
10686 */
10687 if (!(State & INSERT)
10688 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10689 empty_line = TRUE;
10690
10691 /*
10692 * Only draw the ruler when something changed.
10693 */
10694 validate_virtcol_win(wp);
10695 if ( redraw_cmdline
10696 || always
10697 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10698 || wp->w_cursor.col != wp->w_ru_cursor.col
10699 || wp->w_virtcol != wp->w_ru_virtcol
10700#ifdef FEAT_VIRTUALEDIT
10701 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10702#endif
10703 || wp->w_topline != wp->w_ru_topline
10704 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10705#ifdef FEAT_DIFF
10706 || wp->w_topfill != wp->w_ru_topfill
10707#endif
10708 || empty_line != wp->w_ru_empty)
10709 {
10710 cursor_off();
10711#ifdef FEAT_WINDOWS
10712 if (wp->w_status_height)
10713 {
10714 row = W_WINROW(wp) + wp->w_height;
10715 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 off = W_WINCOL(wp);
10717 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 }
10719 else
10720#endif
10721 {
10722 row = Rows - 1;
10723 fillchar = ' ';
10724 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010725#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 width = Columns;
10727 off = 0;
10728#endif
10729 }
10730
10731 /* In list mode virtcol needs to be recomputed */
10732 virtcol = wp->w_virtcol;
10733 if (wp->w_p_list && lcs_tab1 == NUL)
10734 {
10735 wp->w_p_list = FALSE;
10736 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10737 wp->w_p_list = TRUE;
10738 }
10739
10740 /*
10741 * Some sprintfs return the length, some return a pointer.
10742 * To avoid portability problems we use strlen() here.
10743 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010744 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10746 ? 0L
10747 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010748 len = STRLEN(buffer);
10749 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10751 (int)virtcol + 1);
10752
10753 /*
10754 * Add a "50%" if there is room for it.
10755 * On the last line, don't print in the last column (scrolls the
10756 * screen up on some terminals).
10757 */
10758 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010759 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 o = i + vim_strsize(buffer + i + 1);
10761#ifdef FEAT_WINDOWS
10762 if (wp->w_status_height == 0) /* can't use last char of screen */
10763#endif
10764 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010765#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 this_ru_col = ru_col - (Columns - width);
10767 if (this_ru_col < 0)
10768 this_ru_col = 0;
10769#endif
10770 /* Never use more than half the window/screen width, leave the other
10771 * half for the filename. */
10772 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10773 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10774 if (this_ru_col + o < WITH_WIDTH(width))
10775 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010776 /* need at least 3 chars left for get_rel_pos() + NUL */
10777 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 {
10779#ifdef FEAT_MBYTE
10780 if (has_mbyte)
10781 i += (*mb_char2bytes)(fillchar, buffer + i);
10782 else
10783#endif
10784 buffer[i++] = fillchar;
10785 ++o;
10786 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010787 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 }
10789 /* Truncate at window boundary. */
10790#ifdef FEAT_MBYTE
10791 if (has_mbyte)
10792 {
10793 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010794 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 {
10796 o += (*mb_ptr2cells)(buffer + i);
10797 if (this_ru_col + o > WITH_WIDTH(width))
10798 {
10799 buffer[i] = NUL;
10800 break;
10801 }
10802 }
10803 }
10804 else
10805#endif
10806 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10807 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10808
10809 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10810 i = redraw_cmdline;
10811 screen_fill(row, row + 1,
10812 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10813 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10814 fillchar, fillchar, attr);
10815 /* don't redraw the cmdline because of showing the ruler */
10816 redraw_cmdline = i;
10817 wp->w_ru_cursor = wp->w_cursor;
10818 wp->w_ru_virtcol = wp->w_virtcol;
10819 wp->w_ru_empty = empty_line;
10820 wp->w_ru_topline = wp->w_topline;
10821 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10822#ifdef FEAT_DIFF
10823 wp->w_ru_topfill = wp->w_topfill;
10824#endif
10825 }
10826}
10827#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010828
10829#if defined(FEAT_LINEBREAK) || defined(PROTO)
10830/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010831 * Return the width of the 'number' and 'relativenumber' column.
10832 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010833 * Otherwise it depends on 'numberwidth' and the line count.
10834 */
10835 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010836number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010837{
10838 int n;
10839 linenr_T lnum;
10840
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010841 if (wp->w_p_rnu && !wp->w_p_nu)
10842 /* cursor line shows "0" */
10843 lnum = wp->w_height;
10844 else
10845 /* cursor line shows absolute line number */
10846 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010847
Bram Moolenaar6b314672015-03-20 15:42:10 +010010848 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010849 return wp->w_nrwidth_width;
10850 wp->w_nrwidth_line_count = lnum;
10851
10852 n = 0;
10853 do
10854 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010855 lnum /= 10;
10856 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010857 } while (lnum > 0);
10858
10859 /* 'numberwidth' gives the minimal width plus one */
10860 if (n < wp->w_p_nuw - 1)
10861 n = wp->w_p_nuw - 1;
10862
10863 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010864 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010865 return n;
10866}
10867#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010868
10869/*
10870 * Return the current cursor column. This is the actual position on the
10871 * screen. First column is 0.
10872 */
10873 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010874screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010875{
10876 return screen_cur_col;
10877}
10878
10879/*
10880 * Return the current cursor row. This is the actual position on the screen.
10881 * First row is 0.
10882 */
10883 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010884screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010885{
10886 return screen_cur_row;
10887}