blob: ee348e9b53a25a574d25b4a527d0163973ce7f9e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
419redraw_after_callback()
420{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100421 if (State == HITRETURN || State == ASKMORE)
422 ; /* do nothing */
423 else if (State & CMDLINE)
424 redrawcmdline();
425 else if ((State & NORMAL) || (State & INSERT))
426 {
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 {
435 gui_update_cursor(TRUE, FALSE);
436 gui_mch_flush();
437 }
438#endif
439}
440
441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 * Changed something in the current window, at buffer line "lnum", that
443 * requires that line and possibly other lines to be redrawn.
444 * Used when entering/leaving Insert mode with the cursor on a folded line.
445 * Used to remove the "$" from a change command.
446 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
447 * may become invalid and the whole window will have to be redrawn.
448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100450redrawWinline(
451 linenr_T lnum,
452 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453{
454#ifdef FEAT_FOLDING
455 int i;
456#endif
457
458 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
459 curwin->w_redraw_top = lnum;
460 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
461 curwin->w_redraw_bot = lnum;
462 redraw_later(VALID);
463
464#ifdef FEAT_FOLDING
465 if (invalid)
466 {
467 /* A w_lines[] entry for this lnum has become invalid. */
468 i = find_wl_entry(curwin, lnum);
469 if (i >= 0)
470 curwin->w_lines[i].wl_valid = FALSE;
471 }
472#endif
473}
474
475/*
476 * update all windows that are editing the current buffer
477 */
478 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100479update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
481 redraw_curbuf_later(type);
482 update_screen(type);
483}
484
485/*
486 * update_screen()
487 *
488 * Based on the current value of curwin->w_topline, transfer a screenfull
489 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
490 */
491 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100492update_screen(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 win_T *wp;
495 static int did_intro = FALSE;
496#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
497 int did_one;
498#endif
499
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000500 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 if (!screen_valid(TRUE))
502 return;
503
504 if (must_redraw)
505 {
506 if (type < must_redraw) /* use maximal type */
507 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000508
509 /* must_redraw is reset here, so that when we run into some weird
510 * reason to redraw while busy redrawing (e.g., asynchronous
511 * scrolling), or update_topline() in win_update() will cause a
512 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 must_redraw = 0;
514 }
515
516 /* Need to update w_lines[]. */
517 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
518 type = NOT_VALID;
519
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000520 /* Postpone the redrawing when it's not needed and when being called
521 * recursively. */
522 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {
524 redraw_later(type); /* remember type for next time */
525 must_redraw = type;
526 if (type > INVERTED_ALL)
527 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
528 return;
529 }
530
531 updating_screen = TRUE;
532#ifdef FEAT_SYN_HL
533 ++display_tick; /* let syntax code know we're in a next round of
534 * display updating */
535#endif
536
537 /*
538 * if the screen was scrolled up when displaying a message, scroll it down
539 */
540 if (msg_scrolled)
541 {
542 clear_cmdline = TRUE;
543 if (msg_scrolled > Rows - 5) /* clearing is faster */
544 type = CLEAR;
545 else if (type != CLEAR)
546 {
547 check_for_delay(FALSE);
548 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
549 type = CLEAR;
550 FOR_ALL_WINDOWS(wp)
551 {
552 if (W_WINROW(wp) < msg_scrolled)
553 {
554 if (W_WINROW(wp) + wp->w_height > msg_scrolled
555 && wp->w_redr_type < REDRAW_TOP
556 && wp->w_lines_valid > 0
557 && wp->w_topline == wp->w_lines[0].wl_lnum)
558 {
559 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
560 wp->w_redr_type = REDRAW_TOP;
561 }
562 else
563 {
564 wp->w_redr_type = NOT_VALID;
565#ifdef FEAT_WINDOWS
566 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
567 <= msg_scrolled)
568 wp->w_redr_status = TRUE;
569#endif
570 }
571 }
572 }
573 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000574#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000575 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 }
578 msg_scrolled = 0;
579 need_wait_return = FALSE;
580 }
581
582 /* reset cmdline_row now (may have been changed temporarily) */
583 compute_cmdrow();
584
585 /* Check for changed highlighting */
586 if (need_highlight_changed)
587 highlight_changed();
588
589 if (type == CLEAR) /* first clear screen */
590 {
591 screenclear(); /* will reset clear_cmdline */
592 type = NOT_VALID;
593 }
594
595 if (clear_cmdline) /* going to clear cmdline (done below) */
596 check_for_delay(FALSE);
597
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000598#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200599 /* Force redraw when width of 'number' or 'relativenumber' column
600 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000601 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200602 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
603 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000604 curwin->w_redr_type = NOT_VALID;
605#endif
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 /*
608 * Only start redrawing if there is really something to do.
609 */
610 if (type == INVERTED)
611 update_curswant();
612 if (curwin->w_redr_type < type
613 && !((type == VALID
614 && curwin->w_lines[0].wl_valid
615#ifdef FEAT_DIFF
616 && curwin->w_topfill == curwin->w_old_topfill
617 && curwin->w_botfill == curwin->w_old_botfill
618#endif
619 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000621 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
623 && curwin->w_old_visual_mode == VIsual_mode
624 && (curwin->w_valid & VALID_VIRTCOL)
625 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 ))
627 curwin->w_redr_type = type;
628
Bram Moolenaar5a305422006-04-28 22:38:25 +0000629#ifdef FEAT_WINDOWS
630 /* Redraw the tab pages line if needed. */
631 if (redraw_tabline || type >= NOT_VALID)
632 draw_tabline();
633#endif
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#ifdef FEAT_SYN_HL
636 /*
637 * Correct stored syntax highlighting info for changes in each displayed
638 * buffer. Each buffer must only be done once.
639 */
640 FOR_ALL_WINDOWS(wp)
641 {
642 if (wp->w_buffer->b_mod_set)
643 {
644# ifdef FEAT_WINDOWS
645 win_T *wwp;
646
647 /* Check if we already did this buffer. */
648 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
649 if (wwp->w_buffer == wp->w_buffer)
650 break;
651# endif
652 if (
653# ifdef FEAT_WINDOWS
654 wwp == wp &&
655# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200656 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 syn_stack_apply_changes(wp->w_buffer);
658 }
659 }
660#endif
661
662 /*
663 * Go from top to bottom through the windows, redrawing the ones that need
664 * it.
665 */
666#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
667 did_one = FALSE;
668#endif
669#ifdef FEAT_SEARCH_EXTRA
670 search_hl.rm.regprog = NULL;
671#endif
672 FOR_ALL_WINDOWS(wp)
673 {
674 if (wp->w_redr_type != 0)
675 {
676 cursor_off();
677#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
678 if (!did_one)
679 {
680 did_one = TRUE;
681# ifdef FEAT_SEARCH_EXTRA
682 start_search_hl();
683# endif
684# ifdef FEAT_CLIPBOARD
685 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200686 if (clip_star.available && clip_isautosel_star())
687 clip_update_selection(&clip_star);
688 if (clip_plus.available && clip_isautosel_plus())
689 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690# endif
691#ifdef FEAT_GUI
692 /* Remove the cursor before starting to do anything, because
693 * scrolling may make it difficult to redraw the text under
694 * it. */
695 if (gui.in_use)
696 gui_undraw_cursor();
697#endif
698 }
699#endif
700 win_update(wp);
701 }
702
703#ifdef FEAT_WINDOWS
704 /* redraw status line after the window to minimize cursor movement */
705 if (wp->w_redr_status)
706 {
707 cursor_off();
708 win_redr_status(wp);
709 }
710#endif
711 }
712#if defined(FEAT_SEARCH_EXTRA)
713 end_search_hl();
714#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100715#ifdef FEAT_INS_EXPAND
716 /* May need to redraw the popup menu. */
717 if (pum_visible())
718 pum_redraw();
719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
721#ifdef FEAT_WINDOWS
722 /* Reset b_mod_set flags. Going through all windows is probably faster
723 * than going through all buffers (there could be many buffers). */
724 for (wp = firstwin; wp != NULL; wp = wp->w_next)
725 wp->w_buffer->b_mod_set = FALSE;
726#else
727 curbuf->b_mod_set = FALSE;
728#endif
729
730 updating_screen = FALSE;
731#ifdef FEAT_GUI
732 gui_may_resize_shell();
733#endif
734
735 /* Clear or redraw the command line. Done last, because scrolling may
736 * mess up the command line. */
737 if (clear_cmdline || redraw_cmdline)
738 showmode();
739
740 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200741 if (!did_intro)
742 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 did_intro = TRUE;
744
745#ifdef FEAT_GUI
746 /* Redraw the cursor and update the scrollbars when all screen updating is
747 * done. */
748 if (gui.in_use)
749 {
750 out_flush(); /* required before updating the cursor */
751 if (did_one)
752 gui_update_cursor(FALSE, FALSE);
753 gui_update_scrollbars(FALSE);
754 }
755#endif
756}
757
Bram Moolenaar860cae12010-06-05 23:22:07 +0200758#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200759/*
760 * Return TRUE if the cursor line in window "wp" may be concealed, according
761 * to the 'concealcursor' option.
762 */
763 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100764conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200765{
766 int c;
767
768 if (*wp->w_p_cocu == NUL)
769 return FALSE;
770 if (get_real_state() & VISUAL)
771 c = 'v';
772 else if (State & INSERT)
773 c = 'i';
774 else if (State & NORMAL)
775 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200776 else if (State & CMDLINE)
777 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200778 else
779 return FALSE;
780 return vim_strchr(wp->w_p_cocu, c) != NULL;
781}
782
783/*
784 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
785 */
786 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100787conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200788{
789 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
790 {
791 need_cursor_line_redraw = TRUE;
792 /* Need to recompute cursor column, e.g., when starting Visual mode
793 * without concealing. */
794 curs_columns(TRUE);
795 }
796}
797
Bram Moolenaar860cae12010-06-05 23:22:07 +0200798 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100799update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200800{
801 int row;
802 int j;
803
804 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200805 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200806 {
807# ifdef FEAT_GUI
808 /* Remove the cursor before starting to do anything, because scrolling
809 * may make it difficult to redraw the text under it. */
810 if (gui.in_use)
811 gui_undraw_cursor();
812# endif
813 row = 0;
814 for (j = 0; j < wp->w_lines_valid; ++j)
815 {
816 if (lnum == wp->w_lines[j].wl_lnum)
817 {
818 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200819# ifdef FEAT_SEARCH_EXTRA
820 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200821 start_search_hl();
822 prepare_search_hl(wp, lnum);
823# endif
824 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
825# if defined(FEAT_SEARCH_EXTRA)
826 end_search_hl();
827# endif
828 break;
829 }
830 row += wp->w_lines[j].wl_size;
831 }
832# ifdef FEAT_GUI
833 /* Redraw the cursor */
834 if (gui.in_use)
835 {
836 out_flush(); /* required before updating the cursor */
837 gui_update_cursor(FALSE, FALSE);
838 }
839# endif
840 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200841 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200842}
843#endif
844
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100846static void update_prepare(void);
847static void update_finish(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
849/*
850 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000851 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 */
853 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100854update_prepare(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
856 cursor_off();
857 updating_screen = TRUE;
858#ifdef FEAT_GUI
859 /* Remove the cursor before starting to do anything, because scrolling may
860 * make it difficult to redraw the text under it. */
861 if (gui.in_use)
862 gui_undraw_cursor();
863#endif
864#ifdef FEAT_SEARCH_EXTRA
865 start_search_hl();
866#endif
867}
868
869/*
870 * Finish updating one or more windows.
871 */
872 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100873update_finish(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874{
875 if (redraw_cmdline)
876 showmode();
877
878# ifdef FEAT_SEARCH_EXTRA
879 end_search_hl();
880# endif
881
882 updating_screen = FALSE;
883
884# ifdef FEAT_GUI
885 gui_may_resize_shell();
886
887 /* Redraw the cursor and update the scrollbars when all screen updating is
888 * done. */
889 if (gui.in_use)
890 {
891 out_flush(); /* required before updating the cursor */
892 gui_update_cursor(FALSE, FALSE);
893 gui_update_scrollbars(FALSE);
894 }
895# endif
896}
897#endif
898
899#if defined(FEAT_SIGNS) || defined(PROTO)
900 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100901update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902{
903 win_T *wp;
904 int doit = FALSE;
905
906# ifdef FEAT_FOLDING
907 win_foldinfo.fi_level = 0;
908# endif
909
910 /* update/delete a specific mark */
911 FOR_ALL_WINDOWS(wp)
912 {
913 if (buf != NULL && lnum > 0)
914 {
915 if (wp->w_buffer == buf && lnum >= wp->w_topline
916 && lnum < wp->w_botline)
917 {
918 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
919 wp->w_redraw_top = lnum;
920 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
921 wp->w_redraw_bot = lnum;
922 redraw_win_later(wp, VALID);
923 }
924 }
925 else
926 redraw_win_later(wp, VALID);
927 if (wp->w_redr_type != 0)
928 doit = TRUE;
929 }
930
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100931 /* Return when there is nothing to do, screen updating is already
932 * happening (recursive call) or still starting up. */
933 if (!doit || updating_screen
934#ifdef FEAT_GUI
935 || gui.starting
936#endif
937 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 return;
939
940 /* update all windows that need updating */
941 update_prepare();
942
943# ifdef FEAT_WINDOWS
944 for (wp = firstwin; wp; wp = wp->w_next)
945 {
946 if (wp->w_redr_type != 0)
947 win_update(wp);
948 if (wp->w_redr_status)
949 win_redr_status(wp);
950 }
951# else
952 if (curwin->w_redr_type != 0)
953 win_update(curwin);
954# endif
955
956 update_finish();
957}
958#endif
959
960
961#if defined(FEAT_GUI) || defined(PROTO)
962/*
963 * Update a single window, its status line and maybe the command line msg.
964 * Used for the GUI scrollbar.
965 */
966 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100967updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000969 /* return if already busy updating */
970 if (updating_screen)
971 return;
972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 update_prepare();
974
975#ifdef FEAT_CLIPBOARD
976 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200977 if (clip_star.available && clip_isautosel_star())
978 clip_update_selection(&clip_star);
979 if (clip_plus.available && clip_isautosel_plus())
980 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000986 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000987 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000988 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 if (wp->w_redr_status
991# ifdef FEAT_CMDL_INFO
992 || p_ru
993# endif
994# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000995 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996# endif
997 )
998 win_redr_status(wp);
999#endif
1000
1001 update_finish();
1002}
1003#endif
1004
1005/*
1006 * Update a single window.
1007 *
1008 * This may cause the windows below it also to be redrawn (when clearing the
1009 * screen or scrolling lines).
1010 *
1011 * How the window is redrawn depends on wp->w_redr_type. Each type also
1012 * implies the one below it.
1013 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001014 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1016 * INVERTED redraw the changed part of the Visual area
1017 * INVERTED_ALL redraw the whole Visual area
1018 * VALID 1. scroll up/down to adjust for a changed w_topline
1019 * 2. update lines at the top when scrolled down
1020 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001021 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 * b_mod_top and b_mod_bot.
1023 * - if wp->w_redraw_top non-zero, redraw lines between
1024 * wp->w_redraw_top and wp->w_redr_bot.
1025 * - continue redrawing when syntax status is invalid.
1026 * 4. if scrolled up, update lines at the bottom.
1027 * This results in three areas that may need updating:
1028 * top: from first row to top_end (when scrolled down)
1029 * mid: from mid_start to mid_end (update inversion or changed text)
1030 * bot: from bot_start to last row (when scrolled up)
1031 */
1032 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001033win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
1035 buf_T *buf = wp->w_buffer;
1036 int type;
1037 int top_end = 0; /* Below last row of the top area that needs
1038 updating. 0 when no top area updating. */
1039 int mid_start = 999;/* first row of the mid area that needs
1040 updating. 999 when no mid area updating. */
1041 int mid_end = 0; /* Below last row of the mid area that needs
1042 updating. 0 when no mid area updating. */
1043 int bot_start = 999;/* first row of the bot area that needs
1044 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 int scrolled_down = FALSE; /* TRUE when scrolled down when
1046 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001048 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 int top_to_mod = FALSE; /* redraw above mod_top */
1050#endif
1051
1052 int row; /* current window row to display */
1053 linenr_T lnum; /* current buffer lnum to display */
1054 int idx; /* current index in w_lines[] */
1055 int srow; /* starting row of the current line */
1056
1057 int eof = FALSE; /* if TRUE, we hit the end of the file */
1058 int didline = FALSE; /* if TRUE, we finished the last line */
1059 int i;
1060 long j;
1061 static int recursive = FALSE; /* being called recursively */
1062 int old_botline = wp->w_botline;
1063#ifdef FEAT_FOLDING
1064 long fold_count;
1065#endif
1066#ifdef FEAT_SYN_HL
1067 /* remember what happened to the previous line, to know if
1068 * check_visual_highlight() can be used */
1069#define DID_NONE 1 /* didn't update a line */
1070#define DID_LINE 2 /* updated a normal line */
1071#define DID_FOLD 3 /* updated a folded line */
1072 int did_update = DID_NONE;
1073 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1074#endif
1075 linenr_T mod_top = 0;
1076 linenr_T mod_bot = 0;
1077#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1078 int save_got_int;
1079#endif
1080
1081 type = wp->w_redr_type;
1082
1083 if (type == NOT_VALID)
1084 {
1085#ifdef FEAT_WINDOWS
1086 wp->w_redr_status = TRUE;
1087#endif
1088 wp->w_lines_valid = 0;
1089 }
1090
1091 /* Window is zero-height: nothing to draw. */
1092 if (wp->w_height == 0)
1093 {
1094 wp->w_redr_type = 0;
1095 return;
1096 }
1097
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001098#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 /* Window is zero-width: Only need to draw the separator. */
1100 if (wp->w_width == 0)
1101 {
1102 /* draw the vertical separator right of this window */
1103 draw_vsep_win(wp, 0);
1104 wp->w_redr_type = 0;
1105 return;
1106 }
1107#endif
1108
1109#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001110 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#endif
1112
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001113#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001114 /* Force redraw when width of 'number' or 'relativenumber' column
1115 * changes. */
1116 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001117 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001118 {
1119 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001120 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001121 }
1122 else
1123#endif
1124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1126 {
1127 /*
1128 * When there are both inserted/deleted lines and specific lines to be
1129 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1130 * everything (only happens when redrawing is off for while).
1131 */
1132 type = NOT_VALID;
1133 }
1134 else
1135 {
1136 /*
1137 * Set mod_top to the first line that needs displaying because of
1138 * changes. Set mod_bot to the first line after the changes.
1139 */
1140 mod_top = wp->w_redraw_top;
1141 if (wp->w_redraw_bot != 0)
1142 mod_bot = wp->w_redraw_bot + 1;
1143 else
1144 mod_bot = 0;
1145 wp->w_redraw_top = 0; /* reset for next time */
1146 wp->w_redraw_bot = 0;
1147 if (buf->b_mod_set)
1148 {
1149 if (mod_top == 0 || mod_top > buf->b_mod_top)
1150 {
1151 mod_top = buf->b_mod_top;
1152#ifdef FEAT_SYN_HL
1153 /* Need to redraw lines above the change that may be included
1154 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001155 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001157 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (mod_top < 1)
1159 mod_top = 1;
1160 }
1161#endif
1162 }
1163 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1164 mod_bot = buf->b_mod_bot;
1165
1166#ifdef FEAT_SEARCH_EXTRA
1167 /* When 'hlsearch' is on and using a multi-line search pattern, a
1168 * change in one line may make the Search highlighting in a
1169 * previous line invalid. Simple solution: redraw all visible
1170 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001171 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001173 if (search_hl.rm.regprog != NULL
1174 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001176 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001177 {
1178 cur = wp->w_match_head;
1179 while (cur != NULL)
1180 {
1181 if (cur->match.regprog != NULL
1182 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001183 {
1184 top_to_mod = TRUE;
1185 break;
1186 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001187 cur = cur->next;
1188 }
1189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190#endif
1191 }
1192#ifdef FEAT_FOLDING
1193 if (mod_top != 0 && hasAnyFolding(wp))
1194 {
1195 linenr_T lnumt, lnumb;
1196
1197 /*
1198 * A change in a line can cause lines above it to become folded or
1199 * unfolded. Find the top most buffer line that may be affected.
1200 * If the line was previously folded and displayed, get the first
1201 * line of that fold. If the line is folded now, get the first
1202 * folded line. Use the minimum of these two.
1203 */
1204
1205 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1206 * the line below it. If there is no valid entry, use w_topline.
1207 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1208 * to this line. If there is no valid entry, use MAXLNUM. */
1209 lnumt = wp->w_topline;
1210 lnumb = MAXLNUM;
1211 for (i = 0; i < wp->w_lines_valid; ++i)
1212 if (wp->w_lines[i].wl_valid)
1213 {
1214 if (wp->w_lines[i].wl_lastlnum < mod_top)
1215 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1216 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1217 {
1218 lnumb = wp->w_lines[i].wl_lnum;
1219 /* When there is a fold column it might need updating
1220 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001221 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 ++lnumb;
1223 }
1224 }
1225
1226 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1227 if (mod_top > lnumt)
1228 mod_top = lnumt;
1229
1230 /* Now do the same for the bottom line (one above mod_bot). */
1231 --mod_bot;
1232 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1233 ++mod_bot;
1234 if (mod_bot < lnumb)
1235 mod_bot = lnumb;
1236 }
1237#endif
1238
1239 /* When a change starts above w_topline and the end is below
1240 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001241 * If the end of the change is above w_topline: do like no change was
1242 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (mod_top != 0 && mod_top < wp->w_topline)
1244 {
1245 if (mod_bot > wp->w_topline)
1246 mod_top = wp->w_topline;
1247#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001248 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 top_end = 1;
1250#endif
1251 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001252
1253 /* When line numbers are displayed need to redraw all lines below
1254 * inserted/deleted lines. */
1255 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1256 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258
1259 /*
1260 * When only displaying the lines at the top, set top_end. Used when
1261 * window has scrolled down for msg_scrolled.
1262 */
1263 if (type == REDRAW_TOP)
1264 {
1265 j = 0;
1266 for (i = 0; i < wp->w_lines_valid; ++i)
1267 {
1268 j += wp->w_lines[i].wl_size;
1269 if (j >= wp->w_upd_rows)
1270 {
1271 top_end = j;
1272 break;
1273 }
1274 }
1275 if (top_end == 0)
1276 /* not found (cannot happen?): redraw everything */
1277 type = NOT_VALID;
1278 else
1279 /* top area defined, the rest is VALID */
1280 type = VALID;
1281 }
1282
Bram Moolenaar367329b2007-08-30 11:53:22 +00001283 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001284 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1285 * non-zero and thus not FALSE) will indicate that screenclear() was not
1286 * called. */
1287 if (screen_cleared)
1288 screen_cleared = MAYBE;
1289
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 /*
1291 * If there are no changes on the screen that require a complete redraw,
1292 * handle three cases:
1293 * 1: we are off the top of the screen by a few lines: scroll down
1294 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1295 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1296 * w_lines[] that needs updating.
1297 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001298 if ((type == VALID || type == SOME_VALID
1299 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#ifdef FEAT_DIFF
1301 && !wp->w_botfill && !wp->w_old_botfill
1302#endif
1303 )
1304 {
1305 if (mod_top != 0 && wp->w_topline == mod_top)
1306 {
1307 /*
1308 * w_topline is the first changed line, the scrolling will be done
1309 * further down.
1310 */
1311 }
1312 else if (wp->w_lines[0].wl_valid
1313 && (wp->w_topline < wp->w_lines[0].wl_lnum
1314#ifdef FEAT_DIFF
1315 || (wp->w_topline == wp->w_lines[0].wl_lnum
1316 && wp->w_topfill > wp->w_old_topfill)
1317#endif
1318 ))
1319 {
1320 /*
1321 * New topline is above old topline: May scroll down.
1322 */
1323#ifdef FEAT_FOLDING
1324 if (hasAnyFolding(wp))
1325 {
1326 linenr_T ln;
1327
1328 /* count the number of lines we are off, counting a sequence
1329 * of folded lines as one */
1330 j = 0;
1331 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1332 {
1333 ++j;
1334 if (j >= wp->w_height - 2)
1335 break;
1336 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1337 }
1338 }
1339 else
1340#endif
1341 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1342 if (j < wp->w_height - 2) /* not too far off */
1343 {
1344 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1345#ifdef FEAT_DIFF
1346 /* insert extra lines for previously invisible filler lines */
1347 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1348 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1349 - wp->w_old_topfill;
1350#endif
1351 if (i < wp->w_height - 2) /* less than a screen off */
1352 {
1353 /*
1354 * Try to insert the correct number of lines.
1355 * If not the last window, delete the lines at the bottom.
1356 * win_ins_lines may fail when the terminal can't do it.
1357 */
1358 if (i > 0)
1359 check_for_delay(FALSE);
1360 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1361 {
1362 if (wp->w_lines_valid != 0)
1363 {
1364 /* Need to update rows that are new, stop at the
1365 * first one that scrolled down. */
1366 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 /* Move the entries that were scrolled, disable
1370 * the entries for the lines to be redrawn. */
1371 if ((wp->w_lines_valid += j) > wp->w_height)
1372 wp->w_lines_valid = wp->w_height;
1373 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1374 wp->w_lines[idx] = wp->w_lines[idx - j];
1375 while (idx >= 0)
1376 wp->w_lines[idx--].wl_valid = FALSE;
1377 }
1378 }
1379 else
1380 mid_start = 0; /* redraw all lines */
1381 }
1382 else
1383 mid_start = 0; /* redraw all lines */
1384 }
1385 else
1386 mid_start = 0; /* redraw all lines */
1387 }
1388 else
1389 {
1390 /*
1391 * New topline is at or below old topline: May scroll up.
1392 * When topline didn't change, find first entry in w_lines[] that
1393 * needs updating.
1394 */
1395
1396 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1397 j = -1;
1398 row = 0;
1399 for (i = 0; i < wp->w_lines_valid; i++)
1400 {
1401 if (wp->w_lines[i].wl_valid
1402 && wp->w_lines[i].wl_lnum == wp->w_topline)
1403 {
1404 j = i;
1405 break;
1406 }
1407 row += wp->w_lines[i].wl_size;
1408 }
1409 if (j == -1)
1410 {
1411 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1412 * lines */
1413 mid_start = 0;
1414 }
1415 else
1416 {
1417 /*
1418 * Try to delete the correct number of lines.
1419 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1420 */
1421#ifdef FEAT_DIFF
1422 /* If the topline didn't change, delete old filler lines,
1423 * otherwise delete filler lines of the new topline... */
1424 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1425 row += wp->w_old_topfill;
1426 else
1427 row += diff_check_fill(wp, wp->w_topline);
1428 /* ... but don't delete new filler lines. */
1429 row -= wp->w_topfill;
1430#endif
1431 if (row > 0)
1432 {
1433 check_for_delay(FALSE);
1434 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1435 bot_start = wp->w_height - row;
1436 else
1437 mid_start = 0; /* redraw all lines */
1438 }
1439 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1440 {
1441 /*
1442 * Skip the lines (below the deleted lines) that are still
1443 * valid and don't need redrawing. Copy their info
1444 * upwards, to compensate for the deleted lines. Set
1445 * bot_start to the first row that needs redrawing.
1446 */
1447 bot_start = 0;
1448 idx = 0;
1449 for (;;)
1450 {
1451 wp->w_lines[idx] = wp->w_lines[j];
1452 /* stop at line that didn't fit, unless it is still
1453 * valid (no lines deleted) */
1454 if (row > 0 && bot_start + row
1455 + (int)wp->w_lines[j].wl_size > wp->w_height)
1456 {
1457 wp->w_lines_valid = idx + 1;
1458 break;
1459 }
1460 bot_start += wp->w_lines[idx++].wl_size;
1461
1462 /* stop at the last valid entry in w_lines[].wl_size */
1463 if (++j >= wp->w_lines_valid)
1464 {
1465 wp->w_lines_valid = idx;
1466 break;
1467 }
1468 }
1469#ifdef FEAT_DIFF
1470 /* Correct the first entry for filler lines at the top
1471 * when it won't get updated below. */
1472 if (wp->w_p_diff && bot_start > 0)
1473 wp->w_lines[0].wl_size =
1474 plines_win_nofill(wp, wp->w_topline, TRUE)
1475 + wp->w_topfill;
1476#endif
1477 }
1478 }
1479 }
1480
1481 /* When starting redraw in the first line, redraw all lines. When
1482 * there is only one window it's probably faster to clear the screen
1483 * first. */
1484 if (mid_start == 0)
1485 {
1486 mid_end = wp->w_height;
1487 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001488 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001489 /* Clear the screen when it was not done by win_del_lines() or
1490 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1491 * then. */
1492 if (screen_cleared != TRUE)
1493 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001494#ifdef FEAT_WINDOWS
1495 /* The screen was cleared, redraw the tab pages line. */
1496 if (redraw_tabline)
1497 draw_tabline();
1498#endif
1499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001501
1502 /* When win_del_lines() or win_ins_lines() caused the screen to be
1503 * cleared (only happens for the first window) or when screenclear()
1504 * was called directly above, "must_redraw" will have been set to
1505 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1506 if (screen_cleared == TRUE)
1507 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
1509 else
1510 {
1511 /* Not VALID or INVERTED: redraw all lines. */
1512 mid_start = 0;
1513 mid_end = wp->w_height;
1514 }
1515
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001516 if (type == SOME_VALID)
1517 {
1518 /* SOME_VALID: redraw all lines. */
1519 mid_start = 0;
1520 mid_end = wp->w_height;
1521 type = NOT_VALID;
1522 }
1523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 /* check if we are updating or removing the inverted part */
1525 if ((VIsual_active && buf == curwin->w_buffer)
1526 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1527 {
1528 linenr_T from, to;
1529
1530 if (VIsual_active)
1531 {
1532 if (VIsual_active
1533 && (VIsual_mode != wp->w_old_visual_mode
1534 || type == INVERTED_ALL))
1535 {
1536 /*
1537 * If the type of Visual selection changed, redraw the whole
1538 * selection. Also when the ownership of the X selection is
1539 * gained or lost.
1540 */
1541 if (curwin->w_cursor.lnum < VIsual.lnum)
1542 {
1543 from = curwin->w_cursor.lnum;
1544 to = VIsual.lnum;
1545 }
1546 else
1547 {
1548 from = VIsual.lnum;
1549 to = curwin->w_cursor.lnum;
1550 }
1551 /* redraw more when the cursor moved as well */
1552 if (wp->w_old_cursor_lnum < from)
1553 from = wp->w_old_cursor_lnum;
1554 if (wp->w_old_cursor_lnum > to)
1555 to = wp->w_old_cursor_lnum;
1556 if (wp->w_old_visual_lnum < from)
1557 from = wp->w_old_visual_lnum;
1558 if (wp->w_old_visual_lnum > to)
1559 to = wp->w_old_visual_lnum;
1560 }
1561 else
1562 {
1563 /*
1564 * Find the line numbers that need to be updated: The lines
1565 * between the old cursor position and the current cursor
1566 * position. Also check if the Visual position changed.
1567 */
1568 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1569 {
1570 from = curwin->w_cursor.lnum;
1571 to = wp->w_old_cursor_lnum;
1572 }
1573 else
1574 {
1575 from = wp->w_old_cursor_lnum;
1576 to = curwin->w_cursor.lnum;
1577 if (from == 0) /* Visual mode just started */
1578 from = to;
1579 }
1580
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001581 if (VIsual.lnum != wp->w_old_visual_lnum
1582 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
1584 if (wp->w_old_visual_lnum < from
1585 && wp->w_old_visual_lnum != 0)
1586 from = wp->w_old_visual_lnum;
1587 if (wp->w_old_visual_lnum > to)
1588 to = wp->w_old_visual_lnum;
1589 if (VIsual.lnum < from)
1590 from = VIsual.lnum;
1591 if (VIsual.lnum > to)
1592 to = VIsual.lnum;
1593 }
1594 }
1595
1596 /*
1597 * If in block mode and changed column or curwin->w_curswant:
1598 * update all lines.
1599 * First compute the actual start and end column.
1600 */
1601 if (VIsual_mode == Ctrl_V)
1602 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001603 colnr_T fromc, toc;
1604#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1605 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaar404406a2014-10-09 13:24:43 +02001607 if (curwin->w_p_lbr)
1608 ve_flags = VE_ALL;
1609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001611#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1612 ve_flags = save_ve_flags;
1613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 ++toc;
1615 if (curwin->w_curswant == MAXCOL)
1616 toc = MAXCOL;
1617
1618 if (fromc != wp->w_old_cursor_fcol
1619 || toc != wp->w_old_cursor_lcol)
1620 {
1621 if (from > VIsual.lnum)
1622 from = VIsual.lnum;
1623 if (to < VIsual.lnum)
1624 to = VIsual.lnum;
1625 }
1626 wp->w_old_cursor_fcol = fromc;
1627 wp->w_old_cursor_lcol = toc;
1628 }
1629 }
1630 else
1631 {
1632 /* Use the line numbers of the old Visual area. */
1633 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1634 {
1635 from = wp->w_old_cursor_lnum;
1636 to = wp->w_old_visual_lnum;
1637 }
1638 else
1639 {
1640 from = wp->w_old_visual_lnum;
1641 to = wp->w_old_cursor_lnum;
1642 }
1643 }
1644
1645 /*
1646 * There is no need to update lines above the top of the window.
1647 */
1648 if (from < wp->w_topline)
1649 from = wp->w_topline;
1650
1651 /*
1652 * If we know the value of w_botline, use it to restrict the update to
1653 * the lines that are visible in the window.
1654 */
1655 if (wp->w_valid & VALID_BOTLINE)
1656 {
1657 if (from >= wp->w_botline)
1658 from = wp->w_botline - 1;
1659 if (to >= wp->w_botline)
1660 to = wp->w_botline - 1;
1661 }
1662
1663 /*
1664 * Find the minimal part to be updated.
1665 * Watch out for scrolling that made entries in w_lines[] invalid.
1666 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1667 * top_end; need to redraw from top_end to the "to" line.
1668 * A middle mouse click with a Visual selection may change the text
1669 * above the Visual area and reset wl_valid, do count these for
1670 * mid_end (in srow).
1671 */
1672 if (mid_start > 0)
1673 {
1674 lnum = wp->w_topline;
1675 idx = 0;
1676 srow = 0;
1677 if (scrolled_down)
1678 mid_start = top_end;
1679 else
1680 mid_start = 0;
1681 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1682 {
1683 if (wp->w_lines[idx].wl_valid)
1684 mid_start += wp->w_lines[idx].wl_size;
1685 else if (!scrolled_down)
1686 srow += wp->w_lines[idx].wl_size;
1687 ++idx;
1688# ifdef FEAT_FOLDING
1689 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1690 lnum = wp->w_lines[idx].wl_lnum;
1691 else
1692# endif
1693 ++lnum;
1694 }
1695 srow += mid_start;
1696 mid_end = wp->w_height;
1697 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1698 {
1699 if (wp->w_lines[idx].wl_valid
1700 && wp->w_lines[idx].wl_lnum >= to + 1)
1701 {
1702 /* Only update until first row of this line */
1703 mid_end = srow;
1704 break;
1705 }
1706 srow += wp->w_lines[idx].wl_size;
1707 }
1708 }
1709 }
1710
1711 if (VIsual_active && buf == curwin->w_buffer)
1712 {
1713 wp->w_old_visual_mode = VIsual_mode;
1714 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1715 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001716 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 wp->w_old_curswant = curwin->w_curswant;
1718 }
1719 else
1720 {
1721 wp->w_old_visual_mode = 0;
1722 wp->w_old_cursor_lnum = 0;
1723 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001724 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
1727#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1728 /* reset got_int, otherwise regexp won't work */
1729 save_got_int = got_int;
1730 got_int = 0;
1731#endif
1732#ifdef FEAT_FOLDING
1733 win_foldinfo.fi_level = 0;
1734#endif
1735
1736 /*
1737 * Update all the window rows.
1738 */
1739 idx = 0; /* first entry in w_lines[].wl_size */
1740 row = 0;
1741 srow = 0;
1742 lnum = wp->w_topline; /* first line shown in window */
1743 for (;;)
1744 {
1745 /* stop updating when reached the end of the window (check for _past_
1746 * the end of the window is at the end of the loop) */
1747 if (row == wp->w_height)
1748 {
1749 didline = TRUE;
1750 break;
1751 }
1752
1753 /* stop updating when hit the end of the file */
1754 if (lnum > buf->b_ml.ml_line_count)
1755 {
1756 eof = TRUE;
1757 break;
1758 }
1759
1760 /* Remember the starting row of the line that is going to be dealt
1761 * with. It is used further down when the line doesn't fit. */
1762 srow = row;
1763
1764 /*
1765 * Update a line when it is in an area that needs updating, when it
1766 * has changes or w_lines[idx] is invalid.
1767 * bot_start may be halfway a wrapped line after using
1768 * win_del_lines(), check if the current line includes it.
1769 * When syntax folding is being used, the saved syntax states will
1770 * already have been updated, we can't see where the syntax state is
1771 * the same again, just update until the end of the window.
1772 */
1773 if (row < top_end
1774 || (row >= mid_start && row < mid_end)
1775#ifdef FEAT_SEARCH_EXTRA
1776 || top_to_mod
1777#endif
1778 || idx >= wp->w_lines_valid
1779 || (row + wp->w_lines[idx].wl_size > bot_start)
1780 || (mod_top != 0
1781 && (lnum == mod_top
1782 || (lnum >= mod_top
1783 && (lnum < mod_bot
1784#ifdef FEAT_SYN_HL
1785 || did_update == DID_FOLD
1786 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001787 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 && (
1789# ifdef FEAT_FOLDING
1790 (foldmethodIsSyntax(wp)
1791 && hasAnyFolding(wp)) ||
1792# endif
1793 syntax_check_changed(lnum)))
1794#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001795#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001796 /* match in fixed position might need redraw
1797 * if lines were inserted or deleted */
1798 || (wp->w_match_head != NULL
1799 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 )))))
1802 {
1803#ifdef FEAT_SEARCH_EXTRA
1804 if (lnum == mod_top)
1805 top_to_mod = FALSE;
1806#endif
1807
1808 /*
1809 * When at start of changed lines: May scroll following lines
1810 * up or down to minimize redrawing.
1811 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001812 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 */
1814 if (lnum == mod_top
1815 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001816 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 {
1818 int old_rows = 0;
1819 int new_rows = 0;
1820 int xtra_rows;
1821 linenr_T l;
1822
1823 /* Count the old number of window rows, using w_lines[], which
1824 * should still contain the sizes for the lines as they are
1825 * currently displayed. */
1826 for (i = idx; i < wp->w_lines_valid; ++i)
1827 {
1828 /* Only valid lines have a meaningful wl_lnum. Invalid
1829 * lines are part of the changed area. */
1830 if (wp->w_lines[i].wl_valid
1831 && wp->w_lines[i].wl_lnum == mod_bot)
1832 break;
1833 old_rows += wp->w_lines[i].wl_size;
1834#ifdef FEAT_FOLDING
1835 if (wp->w_lines[i].wl_valid
1836 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1837 {
1838 /* Must have found the last valid entry above mod_bot.
1839 * Add following invalid entries. */
1840 ++i;
1841 while (i < wp->w_lines_valid
1842 && !wp->w_lines[i].wl_valid)
1843 old_rows += wp->w_lines[i++].wl_size;
1844 break;
1845 }
1846#endif
1847 }
1848
1849 if (i >= wp->w_lines_valid)
1850 {
1851 /* We can't find a valid line below the changed lines,
1852 * need to redraw until the end of the window.
1853 * Inserting/deleting lines has no use. */
1854 bot_start = 0;
1855 }
1856 else
1857 {
1858 /* Able to count old number of rows: Count new window
1859 * rows, and may insert/delete lines */
1860 j = idx;
1861 for (l = lnum; l < mod_bot; ++l)
1862 {
1863#ifdef FEAT_FOLDING
1864 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1865 ++new_rows;
1866 else
1867#endif
1868#ifdef FEAT_DIFF
1869 if (l == wp->w_topline)
1870 new_rows += plines_win_nofill(wp, l, TRUE)
1871 + wp->w_topfill;
1872 else
1873#endif
1874 new_rows += plines_win(wp, l, TRUE);
1875 ++j;
1876 if (new_rows > wp->w_height - row - 2)
1877 {
1878 /* it's getting too much, must redraw the rest */
1879 new_rows = 9999;
1880 break;
1881 }
1882 }
1883 xtra_rows = new_rows - old_rows;
1884 if (xtra_rows < 0)
1885 {
1886 /* May scroll text up. If there is not enough
1887 * remaining text or scrolling fails, must redraw the
1888 * rest. If scrolling works, must redraw the text
1889 * below the scrolled text. */
1890 if (row - xtra_rows >= wp->w_height - 2)
1891 mod_bot = MAXLNUM;
1892 else
1893 {
1894 check_for_delay(FALSE);
1895 if (win_del_lines(wp, row,
1896 -xtra_rows, FALSE, FALSE) == FAIL)
1897 mod_bot = MAXLNUM;
1898 else
1899 bot_start = wp->w_height + xtra_rows;
1900 }
1901 }
1902 else if (xtra_rows > 0)
1903 {
1904 /* May scroll text down. If there is not enough
1905 * remaining text of scrolling fails, must redraw the
1906 * rest. */
1907 if (row + xtra_rows >= wp->w_height - 2)
1908 mod_bot = MAXLNUM;
1909 else
1910 {
1911 check_for_delay(FALSE);
1912 if (win_ins_lines(wp, row + old_rows,
1913 xtra_rows, FALSE, FALSE) == FAIL)
1914 mod_bot = MAXLNUM;
1915 else if (top_end > row + old_rows)
1916 /* Scrolled the part at the top that requires
1917 * updating down. */
1918 top_end += xtra_rows;
1919 }
1920 }
1921
1922 /* When not updating the rest, may need to move w_lines[]
1923 * entries. */
1924 if (mod_bot != MAXLNUM && i != j)
1925 {
1926 if (j < i)
1927 {
1928 int x = row + new_rows;
1929
1930 /* move entries in w_lines[] upwards */
1931 for (;;)
1932 {
1933 /* stop at last valid entry in w_lines[] */
1934 if (i >= wp->w_lines_valid)
1935 {
1936 wp->w_lines_valid = j;
1937 break;
1938 }
1939 wp->w_lines[j] = wp->w_lines[i];
1940 /* stop at a line that won't fit */
1941 if (x + (int)wp->w_lines[j].wl_size
1942 > wp->w_height)
1943 {
1944 wp->w_lines_valid = j + 1;
1945 break;
1946 }
1947 x += wp->w_lines[j++].wl_size;
1948 ++i;
1949 }
1950 if (bot_start > x)
1951 bot_start = x;
1952 }
1953 else /* j > i */
1954 {
1955 /* move entries in w_lines[] downwards */
1956 j -= i;
1957 wp->w_lines_valid += j;
1958 if (wp->w_lines_valid > wp->w_height)
1959 wp->w_lines_valid = wp->w_height;
1960 for (i = wp->w_lines_valid; i - j >= idx; --i)
1961 wp->w_lines[i] = wp->w_lines[i - j];
1962
1963 /* The w_lines[] entries for inserted lines are
1964 * now invalid, but wl_size may be used above.
1965 * Reset to zero. */
1966 while (i >= idx)
1967 {
1968 wp->w_lines[i].wl_size = 0;
1969 wp->w_lines[i--].wl_valid = FALSE;
1970 }
1971 }
1972 }
1973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975
1976#ifdef FEAT_FOLDING
1977 /*
1978 * When lines are folded, display one line for all of them.
1979 * Otherwise, display normally (can be several display lines when
1980 * 'wrap' is on).
1981 */
1982 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1983 if (fold_count != 0)
1984 {
1985 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1986 ++row;
1987 --fold_count;
1988 wp->w_lines[idx].wl_folded = TRUE;
1989 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1990# ifdef FEAT_SYN_HL
1991 did_update = DID_FOLD;
1992# endif
1993 }
1994 else
1995#endif
1996 if (idx < wp->w_lines_valid
1997 && wp->w_lines[idx].wl_valid
1998 && wp->w_lines[idx].wl_lnum == lnum
1999 && lnum > wp->w_topline
2000 && !(dy_flags & DY_LASTLINE)
2001 && srow + wp->w_lines[idx].wl_size > wp->w_height
2002#ifdef FEAT_DIFF
2003 && diff_check_fill(wp, lnum) == 0
2004#endif
2005 )
2006 {
2007 /* This line is not going to fit. Don't draw anything here,
2008 * will draw "@ " lines below. */
2009 row = wp->w_height + 1;
2010 }
2011 else
2012 {
2013#ifdef FEAT_SEARCH_EXTRA
2014 prepare_search_hl(wp, lnum);
2015#endif
2016#ifdef FEAT_SYN_HL
2017 /* Let the syntax stuff know we skipped a few lines. */
2018 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002019 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 syntax_end_parsing(syntax_last_parsed + 1);
2021#endif
2022
2023 /*
2024 * Display one line.
2025 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002026 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
2028#ifdef FEAT_FOLDING
2029 wp->w_lines[idx].wl_folded = FALSE;
2030 wp->w_lines[idx].wl_lastlnum = lnum;
2031#endif
2032#ifdef FEAT_SYN_HL
2033 did_update = DID_LINE;
2034 syntax_last_parsed = lnum;
2035#endif
2036 }
2037
2038 wp->w_lines[idx].wl_lnum = lnum;
2039 wp->w_lines[idx].wl_valid = TRUE;
2040 if (row > wp->w_height) /* past end of screen */
2041 {
2042 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002043 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2045 ++idx;
2046 break;
2047 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002048 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 wp->w_lines[idx].wl_size = row - srow;
2050 ++idx;
2051#ifdef FEAT_FOLDING
2052 lnum += fold_count + 1;
2053#else
2054 ++lnum;
2055#endif
2056 }
2057 else
2058 {
2059 /* This line does not need updating, advance to the next one */
2060 row += wp->w_lines[idx++].wl_size;
2061 if (row > wp->w_height) /* past end of screen */
2062 break;
2063#ifdef FEAT_FOLDING
2064 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2065#else
2066 ++lnum;
2067#endif
2068#ifdef FEAT_SYN_HL
2069 did_update = DID_NONE;
2070#endif
2071 }
2072
2073 if (lnum > buf->b_ml.ml_line_count)
2074 {
2075 eof = TRUE;
2076 break;
2077 }
2078 }
2079 /*
2080 * End of loop over all window lines.
2081 */
2082
2083
2084 if (idx > wp->w_lines_valid)
2085 wp->w_lines_valid = idx;
2086
2087#ifdef FEAT_SYN_HL
2088 /*
2089 * Let the syntax stuff know we stop parsing here.
2090 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002091 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 syntax_end_parsing(syntax_last_parsed + 1);
2093#endif
2094
2095 /*
2096 * If we didn't hit the end of the file, and we didn't finish the last
2097 * line we were working on, then the line didn't fit.
2098 */
2099 wp->w_empty_rows = 0;
2100#ifdef FEAT_DIFF
2101 wp->w_filler_rows = 0;
2102#endif
2103 if (!eof && !didline)
2104 {
2105 if (lnum == wp->w_topline)
2106 {
2107 /*
2108 * Single line that does not fit!
2109 * Don't overwrite it, it can be edited.
2110 */
2111 wp->w_botline = lnum + 1;
2112 }
2113#ifdef FEAT_DIFF
2114 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2115 {
2116 /* Window ends in filler lines. */
2117 wp->w_botline = lnum;
2118 wp->w_filler_rows = wp->w_height - srow;
2119 }
2120#endif
2121 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2122 {
2123 /*
2124 * Last line isn't finished: Display "@@@" at the end.
2125 */
2126 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2127 W_WINROW(wp) + wp->w_height,
2128 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2129 '@', '@', hl_attr(HLF_AT));
2130 set_empty_rows(wp, srow);
2131 wp->w_botline = lnum;
2132 }
2133 else
2134 {
2135 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2136 wp->w_botline = lnum;
2137 }
2138 }
2139 else
2140 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002141#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 draw_vsep_win(wp, row);
2143#endif
2144 if (eof) /* we hit the end of the file */
2145 {
2146 wp->w_botline = buf->b_ml.ml_line_count + 1;
2147#ifdef FEAT_DIFF
2148 j = diff_check_fill(wp, wp->w_botline);
2149 if (j > 0 && !wp->w_botfill)
2150 {
2151 /*
2152 * Display filler lines at the end of the file
2153 */
2154 if (char2cells(fill_diff) > 1)
2155 i = '-';
2156 else
2157 i = fill_diff;
2158 if (row + j > wp->w_height)
2159 j = wp->w_height - row;
2160 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2161 row += j;
2162 }
2163#endif
2164 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002165 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 wp->w_botline = lnum;
2167
2168 /* make sure the rest of the screen is blank */
2169 /* put '~'s on rows that aren't part of the file. */
2170 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2171 }
2172
2173 /* Reset the type of redrawing required, the window has been updated. */
2174 wp->w_redr_type = 0;
2175#ifdef FEAT_DIFF
2176 wp->w_old_topfill = wp->w_topfill;
2177 wp->w_old_botfill = wp->w_botfill;
2178#endif
2179
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002180 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
2182 /*
2183 * There is a trick with w_botline. If we invalidate it on each
2184 * change that might modify it, this will cause a lot of expensive
2185 * calls to plines() in update_topline() each time. Therefore the
2186 * value of w_botline is often approximated, and this value is used to
2187 * compute the value of w_topline. If the value of w_botline was
2188 * wrong, check that the value of w_topline is correct (cursor is on
2189 * the visible part of the text). If it's not, we need to redraw
2190 * again. Mostly this just means scrolling up a few lines, so it
2191 * doesn't look too bad. Only do this for the current window (where
2192 * changes are relevant).
2193 */
2194 wp->w_valid |= VALID_BOTLINE;
2195 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2196 {
2197 recursive = TRUE;
2198 curwin->w_valid &= ~VALID_TOPLINE;
2199 update_topline(); /* may invalidate w_botline again */
2200 if (must_redraw != 0)
2201 {
2202 /* Don't update for changes in buffer again. */
2203 i = curbuf->b_mod_set;
2204 curbuf->b_mod_set = FALSE;
2205 win_update(curwin);
2206 must_redraw = 0;
2207 curbuf->b_mod_set = i;
2208 }
2209 recursive = FALSE;
2210 }
2211 }
2212
2213#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2214 /* restore got_int, unless CTRL-C was hit while redrawing */
2215 if (!got_int)
2216 got_int = save_got_int;
2217#endif
2218}
2219
2220#ifdef FEAT_SIGNS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002221static int draw_signcolumn(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223/*
2224 * Return TRUE when window "wp" has a column to draw signs in.
2225 */
2226 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002227draw_signcolumn(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229 return (wp->w_buffer->b_signlist != NULL
2230# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01002231 || wp->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232# endif
2233 );
2234}
2235#endif
2236
2237/*
2238 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2239 * as the filler character.
2240 */
2241 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002242win_draw_end(
2243 win_T *wp,
2244 int c1,
2245 int c2,
2246 int row,
2247 int endrow,
2248 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249{
2250#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2251 int n = 0;
2252# define FDC_OFF n
2253#else
2254# define FDC_OFF 0
2255#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002256#ifdef FEAT_FOLDING
2257 int fdc = compute_foldcolumn(wp, 0);
2258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259
2260#ifdef FEAT_RIGHTLEFT
2261 if (wp->w_p_rl)
2262 {
2263 /* No check for cmdline window: should never be right-left. */
2264# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002265 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266
2267 if (n > 0)
2268 {
2269 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002270 if (n > W_WIDTH(wp))
2271 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2273 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2274 ' ', ' ', hl_attr(HLF_FC));
2275 }
2276# endif
2277# ifdef FEAT_SIGNS
2278 if (draw_signcolumn(wp))
2279 {
2280 int nn = n + 2;
2281
2282 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002283 if (nn > W_WIDTH(wp))
2284 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2286 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2287 ' ', ' ', hl_attr(HLF_SC));
2288 n = nn;
2289 }
2290# endif
2291 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2292 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2293 c2, c2, hl_attr(hl));
2294 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2295 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2296 c1, c2, hl_attr(hl));
2297 }
2298 else
2299#endif
2300 {
2301#ifdef FEAT_CMDWIN
2302 if (cmdwin_type != 0 && wp == curwin)
2303 {
2304 /* draw the cmdline character in the leftmost column */
2305 n = 1;
2306 if (n > wp->w_width)
2307 n = wp->w_width;
2308 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2309 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2310 cmdwin_type, ' ', hl_attr(HLF_AT));
2311 }
2312#endif
2313#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002314 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002316 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 /* draw the fold column at the left */
2319 if (nn > W_WIDTH(wp))
2320 nn = W_WIDTH(wp);
2321 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2322 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2323 ' ', ' ', hl_attr(HLF_FC));
2324 n = nn;
2325 }
2326#endif
2327#ifdef FEAT_SIGNS
2328 if (draw_signcolumn(wp))
2329 {
2330 int nn = n + 2;
2331
2332 /* draw the sign column after the fold column */
2333 if (nn > W_WIDTH(wp))
2334 nn = W_WIDTH(wp);
2335 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2336 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2337 ' ', ' ', hl_attr(HLF_SC));
2338 n = nn;
2339 }
2340#endif
2341 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2342 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2343 c1, c2, hl_attr(hl));
2344 }
2345 set_empty_rows(wp, row);
2346}
2347
Bram Moolenaar1a384422010-07-14 19:53:30 +02002348#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002349static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002350
2351/*
2352 * Advance **color_cols and return TRUE when there are columns to draw.
2353 */
2354 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002355advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002356{
2357 while (**color_cols >= 0 && vcol > **color_cols)
2358 ++*color_cols;
2359 return (**color_cols >= 0);
2360}
2361#endif
2362
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363#ifdef FEAT_FOLDING
2364/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002365 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2366 * space is available for window "wp", minus "col".
2367 */
2368 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002369compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002370{
2371 int fdc = wp->w_p_fdc;
2372 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2373 int wwidth = W_WIDTH(wp);
2374
2375 if (fdc > wwidth - (col + wmw))
2376 fdc = wwidth - (col + wmw);
2377 return fdc;
2378}
2379
2380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 * Display one folded line.
2382 */
2383 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002384fold_line(
2385 win_T *wp,
2386 long fold_count,
2387 foldinfo_T *foldinfo,
2388 linenr_T lnum,
2389 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391 char_u buf[51];
2392 pos_T *top, *bot;
2393 linenr_T lnume = lnum + fold_count - 1;
2394 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002395 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 int col;
2398 int txtcol;
2399 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002400 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
2402 /* Build the fold line:
2403 * 1. Add the cmdwin_type for the command-line window
2404 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002405 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 * 4. Compose the text
2407 * 5. Add the text
2408 * 6. set highlighting for the Visual area an other text
2409 */
2410 col = 0;
2411
2412 /*
2413 * 1. Add the cmdwin_type for the command-line window
2414 * Ignores 'rightleft', this window is never right-left.
2415 */
2416#ifdef FEAT_CMDWIN
2417 if (cmdwin_type != 0 && wp == curwin)
2418 {
2419 ScreenLines[off] = cmdwin_type;
2420 ScreenAttrs[off] = hl_attr(HLF_AT);
2421#ifdef FEAT_MBYTE
2422 if (enc_utf8)
2423 ScreenLinesUC[off] = 0;
2424#endif
2425 ++col;
2426 }
2427#endif
2428
2429 /*
2430 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002431 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002433 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 if (fdc > 0)
2435 {
2436 fill_foldcolumn(buf, wp, TRUE, lnum);
2437#ifdef FEAT_RIGHTLEFT
2438 if (wp->w_p_rl)
2439 {
2440 int i;
2441
2442 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2443 hl_attr(HLF_FC));
2444 /* reverse the fold column */
2445 for (i = 0; i < fdc; ++i)
2446 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2447 }
2448 else
2449#endif
2450 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2451 col += fdc;
2452 }
2453
2454#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002455# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2456 for (ri = 0; ri < l; ++ri) \
2457 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2458 else \
2459 for (ri = 0; ri < l; ++ri) \
2460 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002462# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2463 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464#endif
2465
Bram Moolenaar64486672010-05-16 15:46:46 +02002466 /* Set all attributes of the 'number' or 'relativenumber' column and the
2467 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002468 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
2470#ifdef FEAT_SIGNS
2471 /* If signs are being displayed, add two spaces. */
2472 if (draw_signcolumn(wp))
2473 {
2474 len = W_WIDTH(wp) - col;
2475 if (len > 0)
2476 {
2477 if (len > 2)
2478 len = 2;
2479# ifdef FEAT_RIGHTLEFT
2480 if (wp->w_p_rl)
2481 /* the line number isn't reversed */
2482 copy_text_attr(off + W_WIDTH(wp) - len - col,
2483 (char_u *)" ", len, hl_attr(HLF_FL));
2484 else
2485# endif
2486 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2487 col += len;
2488 }
2489 }
2490#endif
2491
2492 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002493 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002495 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
2497 len = W_WIDTH(wp) - col;
2498 if (len > 0)
2499 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002500 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002501 long num;
2502 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002503
2504 if (len > w + 1)
2505 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002506
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002507 if (wp->w_p_nu && !wp->w_p_rnu)
2508 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002509 num = (long)lnum;
2510 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002511 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002512 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002513 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002514 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002515 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002516 /* 'number' + 'relativenumber': cursor line shows absolute
2517 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002518 num = lnum;
2519 fmt = "%-*ld ";
2520 }
2521 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002522
Bram Moolenaar700e7342013-01-30 12:31:36 +01002523 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524#ifdef FEAT_RIGHTLEFT
2525 if (wp->w_p_rl)
2526 /* the line number isn't reversed */
2527 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2528 hl_attr(HLF_FL));
2529 else
2530#endif
2531 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2532 col += len;
2533 }
2534 }
2535
2536 /*
2537 * 4. Compose the folded-line string with 'foldtext', if set.
2538 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002539 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
2541 txtcol = col; /* remember where text starts */
2542
2543 /*
2544 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2545 * Right-left text is put in columns 0 - number-col, normal text is put
2546 * in columns number-col - window-width.
2547 */
2548#ifdef FEAT_MBYTE
2549 if (has_mbyte)
2550 {
2551 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002552 int u8c, u8cc[MAX_MCO];
2553 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 int idx;
2555 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002556 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557# ifdef FEAT_ARABIC
2558 int prev_c = 0; /* previous Arabic character */
2559 int prev_c1 = 0; /* first composing char for prev_c */
2560# endif
2561
2562# ifdef FEAT_RIGHTLEFT
2563 if (wp->w_p_rl)
2564 idx = off;
2565 else
2566# endif
2567 idx = off + col;
2568
2569 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2570 for (p = text; *p != NUL; )
2571 {
2572 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002573 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if (col + cells > W_WIDTH(wp)
2575# ifdef FEAT_RIGHTLEFT
2576 - (wp->w_p_rl ? col : 0)
2577# endif
2578 )
2579 break;
2580 ScreenLines[idx] = *p;
2581 if (enc_utf8)
2582 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002583 u8c = utfc_ptr2char(p, u8cc);
2584 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
2586 ScreenLinesUC[idx] = 0;
2587#ifdef FEAT_ARABIC
2588 prev_c = u8c;
2589#endif
2590 }
2591 else
2592 {
2593#ifdef FEAT_ARABIC
2594 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2595 {
2596 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002597 int pc, pc1, nc;
2598 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 int firstbyte = *p;
2600
2601 /* The idea of what is the previous and next
2602 * character depends on 'rightleft'. */
2603 if (wp->w_p_rl)
2604 {
2605 pc = prev_c;
2606 pc1 = prev_c1;
2607 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002608 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
2610 else
2611 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002612 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002614 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 }
2616 prev_c = u8c;
2617
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002618 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 pc, pc1, nc);
2620 ScreenLines[idx] = firstbyte;
2621 }
2622 else
2623 prev_c = u8c;
2624#endif
2625 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002626#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 if (u8c >= 0x10000)
2628 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2629 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002632 for (i = 0; i < Screen_mco; ++i)
2633 {
2634 ScreenLinesC[i][idx] = u8cc[i];
2635 if (u8cc[i] == 0)
2636 break;
2637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639 if (cells > 1)
2640 ScreenLines[idx + 1] = 0;
2641 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002642 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2643 /* double-byte single width character */
2644 ScreenLines2[idx] = p[1];
2645 else if (cells > 1)
2646 /* double-width character */
2647 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 col += cells;
2649 idx += cells;
2650 p += c_len;
2651 }
2652 }
2653 else
2654#endif
2655 {
2656 len = (int)STRLEN(text);
2657 if (len > W_WIDTH(wp) - col)
2658 len = W_WIDTH(wp) - col;
2659 if (len > 0)
2660 {
2661#ifdef FEAT_RIGHTLEFT
2662 if (wp->w_p_rl)
2663 STRNCPY(current_ScreenLine, text, len);
2664 else
2665#endif
2666 STRNCPY(current_ScreenLine + col, text, len);
2667 col += len;
2668 }
2669 }
2670
2671 /* Fill the rest of the line with the fold filler */
2672#ifdef FEAT_RIGHTLEFT
2673 if (wp->w_p_rl)
2674 col -= txtcol;
2675#endif
2676 while (col < W_WIDTH(wp)
2677#ifdef FEAT_RIGHTLEFT
2678 - (wp->w_p_rl ? txtcol : 0)
2679#endif
2680 )
2681 {
2682#ifdef FEAT_MBYTE
2683 if (enc_utf8)
2684 {
2685 if (fill_fold >= 0x80)
2686 {
2687 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002688 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
2690 else
2691 ScreenLinesUC[off + col] = 0;
2692 }
2693#endif
2694 ScreenLines[off + col++] = fill_fold;
2695 }
2696
2697 if (text != buf)
2698 vim_free(text);
2699
2700 /*
2701 * 6. set highlighting for the Visual area an other text.
2702 * If all folded lines are in the Visual area, highlight the line.
2703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2705 {
2706 if (ltoreq(curwin->w_cursor, VIsual))
2707 {
2708 /* Visual is after curwin->w_cursor */
2709 top = &curwin->w_cursor;
2710 bot = &VIsual;
2711 }
2712 else
2713 {
2714 /* Visual is before curwin->w_cursor */
2715 top = &VIsual;
2716 bot = &curwin->w_cursor;
2717 }
2718 if (lnum >= top->lnum
2719 && lnume <= bot->lnum
2720 && (VIsual_mode != 'v'
2721 || ((lnum > top->lnum
2722 || (lnum == top->lnum
2723 && top->col == 0))
2724 && (lnume < bot->lnum
2725 || (lnume == bot->lnum
2726 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002727 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
2729 if (VIsual_mode == Ctrl_V)
2730 {
2731 /* Visual block mode: highlight the chars part of the block */
2732 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2733 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002734 if (wp->w_old_cursor_lcol != MAXCOL
2735 && wp->w_old_cursor_lcol + txtcol
2736 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 len = wp->w_old_cursor_lcol;
2738 else
2739 len = W_WIDTH(wp) - txtcol;
2740 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002741 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
2743 }
2744 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002745 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002747 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002752#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002753 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2754 if (wp->w_p_cc_cols)
2755 {
2756 int i = 0;
2757 int j = wp->w_p_cc_cols[i];
2758 int old_txtcol = txtcol;
2759
2760 while (j > -1)
2761 {
2762 txtcol += j;
2763 if (wp->w_p_wrap)
2764 txtcol -= wp->w_skipcol;
2765 else
2766 txtcol -= wp->w_leftcol;
2767 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2768 ScreenAttrs[off + txtcol] = hl_combine_attr(
2769 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2770 txtcol = old_txtcol;
2771 j = wp->w_p_cc_cols[++i];
2772 }
2773 }
2774
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002775 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002776 if (wp->w_p_cuc)
2777 {
2778 txtcol += wp->w_virtcol;
2779 if (wp->w_p_wrap)
2780 txtcol -= wp->w_skipcol;
2781 else
2782 txtcol -= wp->w_leftcol;
2783 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2784 ScreenAttrs[off + txtcol] = hl_combine_attr(
2785 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2786 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
2789 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2790 (int)W_WIDTH(wp), FALSE);
2791
2792 /*
2793 * Update w_cline_height and w_cline_folded if the cursor line was
2794 * updated (saves a call to plines() later).
2795 */
2796 if (wp == curwin
2797 && lnum <= curwin->w_cursor.lnum
2798 && lnume >= curwin->w_cursor.lnum)
2799 {
2800 curwin->w_cline_row = row;
2801 curwin->w_cline_height = 1;
2802 curwin->w_cline_folded = TRUE;
2803 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2804 }
2805}
2806
2807/*
2808 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2809 */
2810 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002811copy_text_attr(
2812 int off,
2813 char_u *buf,
2814 int len,
2815 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002817 int i;
2818
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 mch_memmove(ScreenLines + off, buf, (size_t)len);
2820# ifdef FEAT_MBYTE
2821 if (enc_utf8)
2822 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2823# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002824 for (i = 0; i < len; ++i)
2825 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826}
2827
2828/*
2829 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002830 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 */
2832 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002833fill_foldcolumn(
2834 char_u *p,
2835 win_T *wp,
2836 int closed, /* TRUE of FALSE */
2837 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
2839 int i = 0;
2840 int level;
2841 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002842 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002843 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
2845 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002846 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847
2848 level = win_foldinfo.fi_level;
2849 if (level > 0)
2850 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002851 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002852 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002853
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 /* If the column is too narrow, we start at the lowest level that
2855 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002856 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 if (first_level < 1)
2858 first_level = 1;
2859
Bram Moolenaar1c934292015-01-27 16:39:29 +01002860 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
2862 if (win_foldinfo.fi_lnum == lnum
2863 && first_level + i >= win_foldinfo.fi_low_level)
2864 p[i] = '-';
2865 else if (first_level == 1)
2866 p[i] = '|';
2867 else if (first_level + i <= 9)
2868 p[i] = '0' + first_level + i;
2869 else
2870 p[i] = '>';
2871 if (first_level + i == level)
2872 break;
2873 }
2874 }
2875 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002876 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877}
2878#endif /* FEAT_FOLDING */
2879
2880/*
2881 * Display line "lnum" of window 'wp' on the screen.
2882 * Start at row "startrow", stop when "endrow" is reached.
2883 * wp->w_virtcol needs to be valid.
2884 *
2885 * Return the number of last row the line occupies.
2886 */
2887 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002888win_line(
2889 win_T *wp,
2890 linenr_T lnum,
2891 int startrow,
2892 int endrow,
2893 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
2895 int col; /* visual column on screen */
2896 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2897 int c = 0; /* init for GCC */
2898 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002899#ifdef FEAT_LINEBREAK
2900 long vcol_sbr = -1; /* virtual column after showbreak */
2901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 long vcol_prev = -1; /* "vcol" of previous character */
2903 char_u *line; /* current line */
2904 char_u *ptr; /* current position in "line" */
2905 int row; /* row in the window, excl w_winrow */
2906 int screen_row; /* row on the screen, incl w_winrow */
2907
2908 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2909 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002910 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002911 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 int c_extra = NUL; /* extra chars, all the same */
2913 int extra_attr = 0; /* attributes when n_extra != 0 */
2914 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2915 displaying lcs_eol at end-of-line */
2916 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2917 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2918
2919 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2920 int saved_n_extra = 0;
2921 char_u *saved_p_extra = NULL;
2922 int saved_c_extra = 0;
2923 int saved_char_attr = 0;
2924
2925 int n_attr = 0; /* chars with special attr */
2926 int saved_attr2 = 0; /* char_attr saved for n_attr */
2927 int n_attr3 = 0; /* chars with overruling special attr */
2928 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2929
2930 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2931
2932 int fromcol, tocol; /* start/end of inverting */
2933 int fromcol_prev = -2; /* start of inverting after cursor */
2934 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002936 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 pos_T pos;
2938 long v;
2939
2940 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002941 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2943 in this line */
2944 int attr = 0; /* attributes for area highlighting */
2945 int area_attr = 0; /* attributes desired by highlighting */
2946 int search_attr = 0; /* attributes desired by 'hlsearch' */
2947#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002948 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 int syntax_attr = 0; /* attributes desired by syntax */
2950 int has_syntax = FALSE; /* this buffer has syntax highl. */
2951 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002952 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002953 int draw_color_col = FALSE; /* highlight colorcolumn */
2954 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002955#endif
2956#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002957 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002958# define SPWORDLEN 150
2959 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002960 int nextlinecol = 0; /* column where nextline[] starts */
2961 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002962 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002963 int spell_attr = 0; /* attributes desired by spelling */
2964 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002965 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2966 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002967 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002968 static int cap_col = -1; /* column to check for Cap word */
2969 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002970 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971#endif
2972 int extra_check; /* has syntax or linebreak */
2973#ifdef FEAT_MBYTE
2974 int multi_attr = 0; /* attributes desired by multibyte */
2975 int mb_l = 1; /* multi-byte byte length */
2976 int mb_c = 0; /* decoded multi-byte character */
2977 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002978 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979#endif
2980#ifdef FEAT_DIFF
2981 int filler_lines; /* nr of filler lines to be drawn */
2982 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002983 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 int change_start = MAXCOL; /* first col of changed area */
2985 int change_end = -1; /* last col of changed area */
2986#endif
2987 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2988#ifdef FEAT_LINEBREAK
2989 int need_showbreak = FALSE;
2990#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002991#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2992 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002994 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995#endif
2996#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002997 matchitem_T *cur; /* points to the match list */
2998 match_T *shl; /* points to search_hl or a match */
2999 int shl_flag; /* flag to indicate whether search_hl
3000 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003001 int pos_inprogress; /* marks that position match search is
3002 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003003 int prevcol_hl_flag; /* flag to indicate whether prevcol
3004 equals startcol of search_hl or one
3005 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006#endif
3007#ifdef FEAT_ARABIC
3008 int prev_c = 0; /* previous Arabic character */
3009 int prev_c1 = 0; /* first composing char for prev_c */
3010#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003011#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003012 int did_line_attr = 0;
3013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 /* draw_state: items that are drawn in sequence: */
3016#define WL_START 0 /* nothing done yet */
3017#ifdef FEAT_CMDWIN
3018# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3019#else
3020# define WL_CMDLINE WL_START
3021#endif
3022#ifdef FEAT_FOLDING
3023# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3024#else
3025# define WL_FOLD WL_CMDLINE
3026#endif
3027#ifdef FEAT_SIGNS
3028# define WL_SIGN WL_FOLD + 1 /* column for signs */
3029#else
3030# define WL_SIGN WL_FOLD /* column for signs */
3031#endif
3032#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003033#ifdef FEAT_LINEBREAK
3034# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003036# define WL_BRI WL_NR
3037#endif
3038#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3039# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3040#else
3041# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042#endif
3043#define WL_LINE WL_SBR + 1 /* text in the line */
3044 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003045#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 int feedback_col = 0;
3047 int feedback_old_attr = -1;
3048#endif
3049
Bram Moolenaar860cae12010-06-05 23:22:07 +02003050#ifdef FEAT_CONCEAL
3051 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003052 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003053 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003054 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003055 int is_concealing = FALSE;
3056 int boguscols = 0; /* nonexistent columns added to force
3057 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003058 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003059 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003060 int match_conc = 0; /* cchar for match functions */
3061 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003062 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003063# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003064# define FIX_FOR_BOGUSCOLS \
3065 { \
3066 n_extra += vcol_off; \
3067 vcol -= vcol_off; \
3068 vcol_off = 0; \
3069 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003070 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003071 boguscols = 0; \
3072 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003073#else
3074# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
3077 if (startrow > endrow) /* past the end already! */
3078 return startrow;
3079
3080 row = startrow;
3081 screen_row = row + W_WINROW(wp);
3082
3083 /*
3084 * To speed up the loop below, set extra_check when there is linebreak,
3085 * trailing white space and/or syntax processing to be done.
3086 */
3087#ifdef FEAT_LINEBREAK
3088 extra_check = wp->w_p_lbr;
3089#else
3090 extra_check = 0;
3091#endif
3092#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003093 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
3095 /* Prepare for syntax highlighting in this line. When there is an
3096 * error, stop syntax highlighting. */
3097 save_did_emsg = did_emsg;
3098 did_emsg = FALSE;
3099 syntax_start(wp, lnum);
3100 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003101 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 else
3103 {
3104 did_emsg = save_did_emsg;
3105 has_syntax = TRUE;
3106 extra_check = TRUE;
3107 }
3108 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003109
3110 /* Check for columns to display for 'colorcolumn'. */
3111 color_cols = wp->w_p_cc_cols;
3112 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003113 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003114#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003115
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003116#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003117 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003118 && *wp->w_s->b_p_spl != NUL
3119 && wp->w_s->b_langp.ga_len > 0
3120 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003121 {
3122 /* Prepare for spell checking. */
3123 has_spell = TRUE;
3124 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003125
3126 /* Get the start of the next line, so that words that wrap to the next
3127 * line are found too: "et<line-break>al.".
3128 * Trick: skip a few chars for C/shell/Vim comments */
3129 nextline[SPWORDLEN] = NUL;
3130 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3131 {
3132 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3133 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3134 }
3135
3136 /* When a word wrapped from the previous line the start of the current
3137 * line is valid. */
3138 if (lnum == checked_lnum)
3139 cur_checked_col = checked_col;
3140 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003141
3142 /* When there was a sentence end in the previous line may require a
3143 * word starting with capital in this line. In line 1 always check
3144 * the first word. */
3145 if (lnum != capcol_lnum)
3146 cap_col = -1;
3147 if (lnum == 1)
3148 cap_col = 0;
3149 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151#endif
3152
3153 /*
3154 * handle visual active in this window
3155 */
3156 fromcol = -10;
3157 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3159 {
3160 /* Visual is after curwin->w_cursor */
3161 if (ltoreq(curwin->w_cursor, VIsual))
3162 {
3163 top = &curwin->w_cursor;
3164 bot = &VIsual;
3165 }
3166 else /* Visual is before curwin->w_cursor */
3167 {
3168 top = &VIsual;
3169 bot = &curwin->w_cursor;
3170 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003171 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (VIsual_mode == Ctrl_V) /* block mode */
3173 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003174 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
3176 fromcol = wp->w_old_cursor_fcol;
3177 tocol = wp->w_old_cursor_lcol;
3178 }
3179 }
3180 else /* non-block mode */
3181 {
3182 if (lnum > top->lnum && lnum <= bot->lnum)
3183 fromcol = 0;
3184 else if (lnum == top->lnum)
3185 {
3186 if (VIsual_mode == 'V') /* linewise */
3187 fromcol = 0;
3188 else
3189 {
3190 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3191 if (gchar_pos(top) == NUL)
3192 tocol = fromcol + 1;
3193 }
3194 }
3195 if (VIsual_mode != 'V' && lnum == bot->lnum)
3196 {
3197 if (*p_sel == 'e' && bot->col == 0
3198#ifdef FEAT_VIRTUALEDIT
3199 && bot->coladd == 0
3200#endif
3201 )
3202 {
3203 fromcol = -10;
3204 tocol = MAXCOL;
3205 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003206 else if (bot->col == MAXCOL)
3207 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 else
3209 {
3210 pos = *bot;
3211 if (*p_sel == 'e')
3212 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3213 else
3214 {
3215 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3216 ++tocol;
3217 }
3218 }
3219 }
3220 }
3221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 /* Check if the character under the cursor should not be inverted */
3223 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003224#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 )
3228 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
3230 /* if inverting in this line set area_highlighting */
3231 if (fromcol >= 0)
3232 {
3233 area_highlighting = TRUE;
3234 attr = hl_attr(HLF_V);
3235#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003236 if ((clip_star.available && !clip_star.owned
3237 && clip_isautosel_star())
3238 || (clip_plus.available && !clip_plus.owned
3239 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 attr = hl_attr(HLF_VNC);
3241#endif
3242 }
3243 }
3244
3245 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003246 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003248 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 && wp == curwin
3250 && lnum >= curwin->w_cursor.lnum
3251 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3252 {
3253 if (lnum == curwin->w_cursor.lnum)
3254 getvcol(curwin, &(curwin->w_cursor),
3255 (colnr_T *)&fromcol, NULL, NULL);
3256 else
3257 fromcol = 0;
3258 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3259 {
3260 pos.lnum = lnum;
3261 pos.col = search_match_endcol;
3262 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3263 }
3264 else
3265 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003266 /* do at least one character; happens when past end of line */
3267 if (fromcol == tocol)
3268 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 area_highlighting = TRUE;
3270 attr = hl_attr(HLF_I);
3271 }
3272
3273#ifdef FEAT_DIFF
3274 filler_lines = diff_check(wp, lnum);
3275 if (filler_lines < 0)
3276 {
3277 if (filler_lines == -1)
3278 {
3279 if (diff_find_change(wp, lnum, &change_start, &change_end))
3280 diff_hlf = HLF_ADD; /* added line */
3281 else if (change_start == 0)
3282 diff_hlf = HLF_TXD; /* changed text */
3283 else
3284 diff_hlf = HLF_CHD; /* changed line */
3285 }
3286 else
3287 diff_hlf = HLF_ADD; /* added line */
3288 filler_lines = 0;
3289 area_highlighting = TRUE;
3290 }
3291 if (lnum == wp->w_topline)
3292 filler_lines = wp->w_topfill;
3293 filler_todo = filler_lines;
3294#endif
3295
3296#ifdef LINE_ATTR
3297# ifdef FEAT_SIGNS
3298 /* If this line has a sign with line highlighting set line_attr. */
3299 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3300 if (v != 0)
3301 line_attr = sign_get_attr((int)v, TRUE);
3302# endif
3303# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3304 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003305 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 line_attr = hl_attr(HLF_L);
3307# endif
3308 if (line_attr != 0)
3309 area_highlighting = TRUE;
3310#endif
3311
3312 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3313 ptr = line;
3314
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003315#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003316 if (has_spell)
3317 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003318 /* For checking first word with a capital skip white space. */
3319 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003320 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003321
Bram Moolenaar30abd282005-06-22 22:35:10 +00003322 /* To be able to spell-check over line boundaries copy the end of the
3323 * current line into nextline[]. Above the start of the next line was
3324 * copied to nextline[SPWORDLEN]. */
3325 if (nextline[SPWORDLEN] == NUL)
3326 {
3327 /* No next line or it is empty. */
3328 nextlinecol = MAXCOL;
3329 nextline_idx = 0;
3330 }
3331 else
3332 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003333 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003334 if (v < SPWORDLEN)
3335 {
3336 /* Short line, use it completely and append the start of the
3337 * next line. */
3338 nextlinecol = 0;
3339 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003340 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003341 nextline_idx = v + 1;
3342 }
3343 else
3344 {
3345 /* Long line, use only the last SPWORDLEN bytes. */
3346 nextlinecol = v - SPWORDLEN;
3347 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3348 nextline_idx = SPWORDLEN + 1;
3349 }
3350 }
3351 }
3352#endif
3353
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003354 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003356 if (lcs_space || lcs_trail)
3357 extra_check = TRUE;
3358 /* find start of trailing whitespace */
3359 if (lcs_trail)
3360 {
3361 trailcol = (colnr_T)STRLEN(ptr);
3362 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3363 --trailcol;
3364 trailcol += (colnr_T) (ptr - line);
3365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
3367
3368 /*
3369 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3370 * first character to be displayed.
3371 */
3372 if (wp->w_p_wrap)
3373 v = wp->w_skipcol;
3374 else
3375 v = wp->w_leftcol;
3376 if (v > 0)
3377 {
3378#ifdef FEAT_MBYTE
3379 char_u *prev_ptr = ptr;
3380#endif
3381 while (vcol < v && *ptr != NUL)
3382 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003383 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 vcol += c;
3385#ifdef FEAT_MBYTE
3386 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003388 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003391 /* When:
3392 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003393 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003394 * - 'virtualedit' is set, or
3395 * - the visual mode is active,
3396 * the end of the line may be before the start of the displayed part.
3397 */
3398 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003399#ifdef FEAT_SYN_HL
3400 wp->w_p_cuc || draw_color_col ||
3401#endif
3402#ifdef FEAT_VIRTUALEDIT
3403 virtual_active() ||
3404#endif
3405 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003406 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
3410 /* Handle a character that's not completely on the screen: Put ptr at
3411 * that character but skip the first few screen characters. */
3412 if (vcol > v)
3413 {
3414 vcol -= c;
3415#ifdef FEAT_MBYTE
3416 ptr = prev_ptr;
3417#else
3418 --ptr;
3419#endif
3420 n_skip = v - vcol;
3421 }
3422
3423 /*
3424 * Adjust for when the inverted text is before the screen,
3425 * and when the start of the inverted text is before the screen.
3426 */
3427 if (tocol <= vcol)
3428 fromcol = 0;
3429 else if (fromcol >= 0 && fromcol < vcol)
3430 fromcol = vcol;
3431
3432#ifdef FEAT_LINEBREAK
3433 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3434 if (wp->w_p_wrap)
3435 need_showbreak = TRUE;
3436#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003437#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003438 /* When spell checking a word we need to figure out the start of the
3439 * word and if it's badly spelled or not. */
3440 if (has_spell)
3441 {
3442 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003443 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003444 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003445
3446 pos = wp->w_cursor;
3447 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003448 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003449 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003450
3451 /* spell_move_to() may call ml_get() and make "line" invalid */
3452 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3453 ptr = line + linecol;
3454
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003455 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003456 {
3457 /* no bad word found at line start, don't check until end of a
3458 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003459 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003460 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003461 }
3462 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003463 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003464 /* bad word found, use attributes until end of word */
3465 word_end = wp->w_cursor.col + len + 1;
3466
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003467 /* Turn index into actual attributes. */
3468 if (spell_hlf != HLF_COUNT)
3469 spell_attr = highlight_attr[spell_hlf];
3470 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003471 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003472
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003473# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003474 /* Need to restart syntax highlighting for this line. */
3475 if (has_syntax)
3476 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003477# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003478 }
3479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481
3482 /*
3483 * Correct highlighting for cursor that can't be disabled.
3484 * Avoids having to check this for each character.
3485 */
3486 if (fromcol >= 0)
3487 {
3488 if (noinvcur)
3489 {
3490 if ((colnr_T)fromcol == wp->w_virtcol)
3491 {
3492 /* highlighting starts at cursor, let it start just after the
3493 * cursor */
3494 fromcol_prev = fromcol;
3495 fromcol = -1;
3496 }
3497 else if ((colnr_T)fromcol < wp->w_virtcol)
3498 /* restart highlighting after the cursor */
3499 fromcol_prev = wp->w_virtcol;
3500 }
3501 if (fromcol >= tocol)
3502 fromcol = -1;
3503 }
3504
3505#ifdef FEAT_SEARCH_EXTRA
3506 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003507 * Handle highlighting the last used search pattern and matches.
3508 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003510 cur = wp->w_match_head;
3511 shl_flag = FALSE;
3512 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003514 if (shl_flag == FALSE)
3515 {
3516 shl = &search_hl;
3517 shl_flag = TRUE;
3518 }
3519 else
3520 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003521 shl->startcol = MAXCOL;
3522 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003524 v = (long)(ptr - line);
3525 if (cur != NULL)
3526 cur->pos.cur = 0;
3527 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3528
3529 /* Need to get the line again, a multi-line regexp may have made it
3530 * invalid. */
3531 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3532 ptr = line + v;
3533
3534 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003536 if (shl->lnum == lnum)
3537 shl->startcol = shl->rm.startpos[0].col;
3538 else
3539 shl->startcol = 0;
3540 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3541 - shl->rm.startpos[0].lnum)
3542 shl->endcol = shl->rm.endpos[0].col;
3543 else
3544 shl->endcol = MAXCOL;
3545 /* Highlight one character for an empty match. */
3546 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003549 if (has_mbyte && line[shl->endcol] != NUL)
3550 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003553 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003555 if ((long)shl->startcol < v) /* match at leftcol */
3556 {
3557 shl->attr_cur = shl->attr;
3558 search_attr = shl->attr;
3559 }
3560 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003562 if (shl != &search_hl && cur != NULL)
3563 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565#endif
3566
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003567#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003568 /* Cursor line highlighting for 'cursorline' in the current window. Not
3569 * when Visual mode is active, because it's not clear what is selected
3570 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003571 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003572 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003573 {
3574 line_attr = hl_attr(HLF_CUL);
3575 area_highlighting = TRUE;
3576 }
3577#endif
3578
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003579 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 col = 0;
3581#ifdef FEAT_RIGHTLEFT
3582 if (wp->w_p_rl)
3583 {
3584 /* Rightleft window: process the text in the normal direction, but put
3585 * it in current_ScreenLine[] from right to left. Start at the
3586 * rightmost column of the window. */
3587 col = W_WIDTH(wp) - 1;
3588 off += col;
3589 }
3590#endif
3591
3592 /*
3593 * Repeat for the whole displayed line.
3594 */
3595 for (;;)
3596 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003597#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003598 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 /* Skip this quickly when working on the text. */
3601 if (draw_state != WL_LINE)
3602 {
3603#ifdef FEAT_CMDWIN
3604 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3605 {
3606 draw_state = WL_CMDLINE;
3607 if (cmdwin_type != 0 && wp == curwin)
3608 {
3609 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003611 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 char_attr = hl_attr(HLF_AT);
3613 }
3614 }
3615#endif
3616
3617#ifdef FEAT_FOLDING
3618 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3619 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003620 int fdc = compute_foldcolumn(wp, 0);
3621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003623 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
3625 /* Draw the 'foldcolumn'. */
3626 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003627 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003629 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 c_extra = NUL;
3631 char_attr = hl_attr(HLF_FC);
3632 }
3633 }
3634#endif
3635
3636#ifdef FEAT_SIGNS
3637 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3638 {
3639 draw_state = WL_SIGN;
3640 /* Show the sign column when there are any signs in this
3641 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003642 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003644 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003646 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647# endif
3648
3649 /* Draw two cells with the sign value or blank. */
3650 c_extra = ' ';
3651 char_attr = hl_attr(HLF_SC);
3652 n_extra = 2;
3653
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003654 if (row == startrow
3655#ifdef FEAT_DIFF
3656 + filler_lines && filler_todo <= 0
3657#endif
3658 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3661 SIGN_TEXT);
3662# ifdef FEAT_SIGN_ICONS
3663 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3664 SIGN_ICON);
3665 if (gui.in_use && icon_sign != 0)
3666 {
3667 /* Use the image in this position. */
3668 c_extra = SIGN_BYTE;
3669# ifdef FEAT_NETBEANS_INTG
3670 if (buf_signcount(wp->w_buffer, lnum) > 1)
3671 c_extra = MULTISIGN_BYTE;
3672# endif
3673 char_attr = icon_sign;
3674 }
3675 else
3676# endif
3677 if (text_sign != 0)
3678 {
3679 p_extra = sign_get_text(text_sign);
3680 if (p_extra != NULL)
3681 {
3682 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003683 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685 char_attr = sign_get_attr(text_sign, FALSE);
3686 }
3687 }
3688 }
3689 }
3690#endif
3691
3692 if (draw_state == WL_NR - 1 && n_extra == 0)
3693 {
3694 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003695 /* Display the absolute or relative line number. After the
3696 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3697 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 && (row == startrow
3699#ifdef FEAT_DIFF
3700 + filler_lines
3701#endif
3702 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3703 {
3704 /* Draw the line number (empty space after wrapping). */
3705 if (row == startrow
3706#ifdef FEAT_DIFF
3707 + filler_lines
3708#endif
3709 )
3710 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003711 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003712 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003713
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003714 if (wp->w_p_nu && !wp->w_p_rnu)
3715 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003716 num = (long)lnum;
3717 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003718 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003719 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003720 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003721 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003722 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003723 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003724 num = lnum;
3725 fmt = "%-*ld ";
3726 }
3727 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003728
Bram Moolenaar700e7342013-01-30 12:31:36 +01003729 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003730 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 if (wp->w_skipcol > 0)
3732 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3733 *p_extra = '-';
3734#ifdef FEAT_RIGHTLEFT
3735 if (wp->w_p_rl) /* reverse line numbers */
3736 rl_mirror(extra);
3737#endif
3738 p_extra = extra;
3739 c_extra = NUL;
3740 }
3741 else
3742 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003743 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003745#ifdef FEAT_SYN_HL
3746 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003747 * the current line differently.
3748 * TODO: Can we use CursorLine instead of CursorLineNr
3749 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003750 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003751 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003752 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
3755 }
3756
Bram Moolenaar597a4222014-06-25 14:39:50 +02003757#ifdef FEAT_LINEBREAK
3758 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3759 && n_extra == 0 && *p_sbr != NUL)
3760 /* draw indent after showbreak value */
3761 draw_state = WL_BRI;
3762 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3763 /* After the showbreak, draw the breakindent */
3764 draw_state = WL_BRI - 1;
3765
3766 /* draw 'breakindent': indent wrapped text accordingly */
3767 if (draw_state == WL_BRI - 1 && n_extra == 0)
3768 {
3769 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003770 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003771# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003772 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003773# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003774 )
3775 {
3776 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003777# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003778 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003779 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003780 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003781# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003782 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3783 char_attr = hl_combine_attr(char_attr,
3784 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003785# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003786 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003787# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003788 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003789 c_extra = ' ';
3790 n_extra = get_breakindent_win(wp,
3791 ml_get_buf(wp->w_buffer, lnum, FALSE));
3792 /* Correct end of highlighted area for 'breakindent',
3793 * required when 'linebreak' is also set. */
3794 if (tocol == vcol)
3795 tocol += n_extra;
3796 }
3797 }
3798#endif
3799
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3801 if (draw_state == WL_SBR - 1 && n_extra == 0)
3802 {
3803 draw_state = WL_SBR;
3804# ifdef FEAT_DIFF
3805 if (filler_todo > 0)
3806 {
3807 /* Draw "deleted" diff line(s). */
3808 if (char2cells(fill_diff) > 1)
3809 c_extra = '-';
3810 else
3811 c_extra = fill_diff;
3812# ifdef FEAT_RIGHTLEFT
3813 if (wp->w_p_rl)
3814 n_extra = col + 1;
3815 else
3816# endif
3817 n_extra = W_WIDTH(wp) - col;
3818 char_attr = hl_attr(HLF_DED);
3819 }
3820# endif
3821# ifdef FEAT_LINEBREAK
3822 if (*p_sbr != NUL && need_showbreak)
3823 {
3824 /* Draw 'showbreak' at the start of each broken line. */
3825 p_extra = p_sbr;
3826 c_extra = NUL;
3827 n_extra = (int)STRLEN(p_sbr);
3828 char_attr = hl_attr(HLF_AT);
3829 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003830 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 /* Correct end of highlighted area for 'showbreak',
3832 * required when 'linebreak' is also set. */
3833 if (tocol == vcol)
3834 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003835#ifdef FEAT_SYN_HL
3836 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003837 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003838 char_attr = hl_combine_attr(char_attr,
3839 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842# endif
3843 }
3844#endif
3845
3846 if (draw_state == WL_LINE - 1 && n_extra == 0)
3847 {
3848 draw_state = WL_LINE;
3849 if (saved_n_extra)
3850 {
3851 /* Continue item from end of wrapped line. */
3852 n_extra = saved_n_extra;
3853 c_extra = saved_c_extra;
3854 p_extra = saved_p_extra;
3855 char_attr = saved_char_attr;
3856 }
3857 else
3858 char_attr = 0;
3859 }
3860 }
3861
3862 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003863 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003864 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865#ifdef FEAT_DIFF
3866 && filler_todo <= 0
3867#endif
3868 )
3869 {
3870 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3871 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003872 /* Pretend we have finished updating the window. Except when
3873 * 'cursorcolumn' is set. */
3874#ifdef FEAT_SYN_HL
3875 if (wp->w_p_cuc)
3876 row = wp->w_cline_row + wp->w_cline_height;
3877 else
3878#endif
3879 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 break;
3881 }
3882
3883 if (draw_state == WL_LINE && area_highlighting)
3884 {
3885 /* handle Visual or match highlighting in this line */
3886 if (vcol == fromcol
3887#ifdef FEAT_MBYTE
3888 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3889 && (*mb_ptr2cells)(ptr) > 1)
3890#endif
3891 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003892 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 && vcol < tocol))
3894 area_attr = attr; /* start highlighting */
3895 else if (area_attr != 0
3896 && (vcol == tocol
3897 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899
3900#ifdef FEAT_SEARCH_EXTRA
3901 if (!n_extra)
3902 {
3903 /*
3904 * Check for start/end of search pattern match.
3905 * After end, check for start/end of next match.
3906 * When another match, have to check for start again.
3907 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003908 * Do this for 'search_hl' and the match list (ordered by
3909 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003911 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003912 cur = wp->w_match_head;
3913 shl_flag = FALSE;
3914 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003916 if (shl_flag == FALSE
3917 && ((cur != NULL
3918 && cur->priority > SEARCH_HL_PRIORITY)
3919 || cur == NULL))
3920 {
3921 shl = &search_hl;
3922 shl_flag = TRUE;
3923 }
3924 else
3925 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003926 if (cur != NULL)
3927 cur->pos.cur = 0;
3928 pos_inprogress = TRUE;
3929 while (shl->rm.regprog != NULL
3930 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003932 if (shl->startcol != MAXCOL
3933 && v >= (long)shl->startcol
3934 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003936#ifdef FEAT_MBYTE
3937 int tmp_col = v + MB_PTR2LEN(ptr);
3938
3939 if (shl->endcol < tmp_col)
3940 shl->endcol = tmp_col;
3941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003943#ifdef FEAT_CONCEAL
3944 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3945 == cur->hlg_id)
3946 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02003947 has_match_conc =
3948 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003949 match_conc = cur->conceal_char;
3950 }
3951 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02003952 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003955 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
3957 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003958#ifdef FEAT_CONCEAL
3959 prev_syntax_id = 0;
3960#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003961 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3962 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003963 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964
3965 /* Need to get the line again, a multi-line regexp
3966 * may have made it invalid. */
3967 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3968 ptr = line + v;
3969
3970 if (shl->lnum == lnum)
3971 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003972 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003974 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003976 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003978 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 {
3980 /* highlight empty match, try again after
3981 * it */
3982#ifdef FEAT_MBYTE
3983 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003984 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003985 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 else
3987#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003988 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990
3991 /* Loop to check if the match starts at the
3992 * current position */
3993 continue;
3994 }
3995 }
3996 break;
3997 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003998 if (shl != &search_hl && cur != NULL)
3999 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004001
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004002 /* Use attributes from match with highest priority among
4003 * 'search_hl' and the match list. */
4004 search_attr = search_hl.attr_cur;
4005 cur = wp->w_match_head;
4006 shl_flag = FALSE;
4007 while (cur != NULL || shl_flag == FALSE)
4008 {
4009 if (shl_flag == FALSE
4010 && ((cur != NULL
4011 && cur->priority > SEARCH_HL_PRIORITY)
4012 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004013 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004014 shl = &search_hl;
4015 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004016 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004017 else
4018 shl = &cur->hl;
4019 if (shl->attr_cur != 0)
4020 search_attr = shl->attr_cur;
4021 if (shl != &search_hl && cur != NULL)
4022 cur = cur->next;
4023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
4025#endif
4026
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004028 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004030 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4031 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004033 if (diff_hlf == HLF_TXD && ptr - line > change_end
4034 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004036 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004037 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4038 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
4040#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004041
4042 /* Decide which of the highlight attributes to use. */
4043 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004044#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004045 if (area_attr != 0)
4046 char_attr = hl_combine_attr(line_attr, area_attr);
4047 else if (search_attr != 0)
4048 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004049 /* Use line_attr when not in the Visual or 'incsearch' area
4050 * (area_attr may be 0 when "noinvcur" is set). */
4051 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004052 || vcol < fromcol || vcol_prev < fromcol_prev
4053 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004054 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004055#else
4056 if (area_attr != 0)
4057 char_attr = area_attr;
4058 else if (search_attr != 0)
4059 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004060#endif
4061 else
4062 {
4063 attr_pri = FALSE;
4064#ifdef FEAT_SYN_HL
4065 if (has_syntax)
4066 char_attr = syntax_attr;
4067 else
4068#endif
4069 char_attr = 0;
4070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
4072
4073 /*
4074 * Get the next character to put on the screen.
4075 */
4076 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004077 * The "p_extra" points to the extra stuff that is inserted to
4078 * represent special characters (non-printable stuff) and other
4079 * things. When all characters are the same, c_extra is used.
4080 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4081 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4083 */
4084 if (n_extra > 0)
4085 {
4086 if (c_extra != NUL)
4087 {
4088 c = c_extra;
4089#ifdef FEAT_MBYTE
4090 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4091 if (enc_utf8 && (*mb_char2len)(c) > 1)
4092 {
4093 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004094 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004095 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 else
4098 mb_utf8 = FALSE;
4099#endif
4100 }
4101 else
4102 {
4103 c = *p_extra;
4104#ifdef FEAT_MBYTE
4105 if (has_mbyte)
4106 {
4107 mb_c = c;
4108 if (enc_utf8)
4109 {
4110 /* If the UTF-8 character is more than one byte:
4111 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004112 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 mb_utf8 = FALSE;
4114 if (mb_l > n_extra)
4115 mb_l = 1;
4116 else if (mb_l > 1)
4117 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004118 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004120 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122 }
4123 else
4124 {
4125 /* if this is a DBCS character, put it in "mb_c" */
4126 mb_l = MB_BYTE2LEN(c);
4127 if (mb_l >= n_extra)
4128 mb_l = 1;
4129 else if (mb_l > 1)
4130 mb_c = (c << 8) + p_extra[1];
4131 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004132 if (mb_l == 0) /* at the NUL at end-of-line */
4133 mb_l = 1;
4134
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 /* If a double-width char doesn't fit display a '>' in the
4136 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004137 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138# ifdef FEAT_RIGHTLEFT
4139 wp->w_p_rl ? (col <= 0) :
4140# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004141 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 && (*mb_char2cells)(mb_c) == 2)
4143 {
4144 c = '>';
4145 mb_c = c;
4146 mb_l = 1;
4147 mb_utf8 = FALSE;
4148 multi_attr = hl_attr(HLF_AT);
4149 /* put the pointer back to output the double-width
4150 * character at the start of the next line. */
4151 ++n_extra;
4152 --p_extra;
4153 }
4154 else
4155 {
4156 n_extra -= mb_l - 1;
4157 p_extra += mb_l - 1;
4158 }
4159 }
4160#endif
4161 ++p_extra;
4162 }
4163 --n_extra;
4164 }
4165 else
4166 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004167 if (p_extra_free != NULL)
4168 {
4169 vim_free(p_extra_free);
4170 p_extra_free = NULL;
4171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 /*
4173 * Get a character from the line itself.
4174 */
4175 c = *ptr;
4176#ifdef FEAT_MBYTE
4177 if (has_mbyte)
4178 {
4179 mb_c = c;
4180 if (enc_utf8)
4181 {
4182 /* If the UTF-8 character is more than one byte: Decode it
4183 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004184 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 mb_utf8 = FALSE;
4186 if (mb_l > 1)
4187 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004188 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 /* Overlong encoded ASCII or ASCII with composing char
4190 * is displayed normally, except a NUL. */
4191 if (mb_c < 0x80)
4192 c = mb_c;
4193 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004194
4195 /* At start of the line we can have a composing char.
4196 * Draw it as a space with a composing char. */
4197 if (utf_iscomposing(mb_c))
4198 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004199 int i;
4200
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004201 for (i = Screen_mco - 1; i > 0; --i)
4202 u8cc[i] = u8cc[i - 1];
4203 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004204 mb_c = ' ';
4205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207
4208 if ((mb_l == 1 && c >= 0x80)
4209 || (mb_l >= 1 && mb_c == 0)
4210 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004211# ifdef UNICODE16
4212 || mb_c >= 0x10000
4213# endif
4214 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 {
4216 /*
4217 * Illegal UTF-8 byte: display as <xx>.
4218 * Non-BMP character : display as ? or fullwidth ?.
4219 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004220# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 {
4224 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004225# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 if (wp->w_p_rl) /* reverse */
4227 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004230# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 else if (utf_char2cells(mb_c) != 2)
4232 STRCPY(extra, "?");
4233 else
4234 /* 0xff1f in UTF-8: full-width '?' */
4235 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 p_extra = extra;
4239 c = *p_extra;
4240 mb_c = mb_ptr2char_adv(&p_extra);
4241 mb_utf8 = (c >= 0x80);
4242 n_extra = (int)STRLEN(p_extra);
4243 c_extra = NUL;
4244 if (area_attr == 0 && search_attr == 0)
4245 {
4246 n_attr = n_extra + 1;
4247 extra_attr = hl_attr(HLF_8);
4248 saved_attr2 = char_attr; /* save current attr */
4249 }
4250 }
4251 else if (mb_l == 0) /* at the NUL at end-of-line */
4252 mb_l = 1;
4253#ifdef FEAT_ARABIC
4254 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4255 {
4256 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004257 int pc, pc1, nc;
4258 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
4260 /* The idea of what is the previous and next
4261 * character depends on 'rightleft'. */
4262 if (wp->w_p_rl)
4263 {
4264 pc = prev_c;
4265 pc1 = prev_c1;
4266 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004267 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 else
4270 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004271 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004273 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 prev_c = mb_c;
4276
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004277 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 else
4280 prev_c = mb_c;
4281#endif
4282 }
4283 else /* enc_dbcs */
4284 {
4285 mb_l = MB_BYTE2LEN(c);
4286 if (mb_l == 0) /* at the NUL at end-of-line */
4287 mb_l = 1;
4288 else if (mb_l > 1)
4289 {
4290 /* We assume a second byte below 32 is illegal.
4291 * Hopefully this is OK for all double-byte encodings!
4292 */
4293 if (ptr[1] >= 32)
4294 mb_c = (c << 8) + ptr[1];
4295 else
4296 {
4297 if (ptr[1] == NUL)
4298 {
4299 /* head byte at end of line */
4300 mb_l = 1;
4301 transchar_nonprint(extra, c);
4302 }
4303 else
4304 {
4305 /* illegal tail byte */
4306 mb_l = 2;
4307 STRCPY(extra, "XX");
4308 }
4309 p_extra = extra;
4310 n_extra = (int)STRLEN(extra) - 1;
4311 c_extra = NUL;
4312 c = *p_extra++;
4313 if (area_attr == 0 && search_attr == 0)
4314 {
4315 n_attr = n_extra + 1;
4316 extra_attr = hl_attr(HLF_8);
4317 saved_attr2 = char_attr; /* save current attr */
4318 }
4319 mb_c = c;
4320 }
4321 }
4322 }
4323 /* If a double-width char doesn't fit display a '>' in the
4324 * last column; the character is displayed at the start of the
4325 * next line. */
4326 if ((
4327# ifdef FEAT_RIGHTLEFT
4328 wp->w_p_rl ? (col <= 0) :
4329# endif
4330 (col >= W_WIDTH(wp) - 1))
4331 && (*mb_char2cells)(mb_c) == 2)
4332 {
4333 c = '>';
4334 mb_c = c;
4335 mb_utf8 = FALSE;
4336 mb_l = 1;
4337 multi_attr = hl_attr(HLF_AT);
4338 /* Put pointer back so that the character will be
4339 * displayed at the start of the next line. */
4340 --ptr;
4341 }
4342 else if (*ptr != NUL)
4343 ptr += mb_l - 1;
4344
4345 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004346 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004347 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004348 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004351 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 c = ' ';
4353 if (area_attr == 0 && search_attr == 0)
4354 {
4355 n_attr = n_extra + 1;
4356 extra_attr = hl_attr(HLF_AT);
4357 saved_attr2 = char_attr; /* save current attr */
4358 }
4359 mb_c = c;
4360 mb_utf8 = FALSE;
4361 mb_l = 1;
4362 }
4363
4364 }
4365#endif
4366 ++ptr;
4367
4368 if (extra_check)
4369 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004370#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004371 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004372#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004373
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004374#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 /* Get syntax attribute, unless still at the start of the line
4376 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004377 v = (long)(ptr - line);
4378 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
4380 /* Get the syntax attribute for the character. If there
4381 * is an error, disable syntax highlighting. */
4382 save_did_emsg = did_emsg;
4383 did_emsg = FALSE;
4384
Bram Moolenaar217ad922005-03-20 22:37:15 +00004385 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004386# ifdef FEAT_SPELL
4387 has_spell ? &can_spell :
4388# endif
4389 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004392 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004393 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004394 has_syntax = FALSE;
4395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 else
4397 did_emsg = save_did_emsg;
4398
4399 /* Need to get the line again, a multi-line regexp may
4400 * have made it invalid. */
4401 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4402 ptr = line + v;
4403
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004404 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004406 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004407 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004408# ifdef FEAT_CONCEAL
4409 /* no concealing past the end of the line, it interferes
4410 * with line highlighting */
4411 if (c == NUL)
4412 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004413 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004414 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004417#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004418
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004419#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004420 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004421 * Only do this when there is no syntax highlighting, the
4422 * @Spell cluster is not used or the current syntax item
4423 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004424 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004425 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004426 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004427# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004428 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004429 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004430# endif
4431 if (c != 0 && (
4432# ifdef FEAT_SYN_HL
4433 !has_syntax ||
4434# endif
4435 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004436 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004437 char_u *prev_ptr, *p;
4438 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004439 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004440# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004441 if (has_mbyte)
4442 {
4443 prev_ptr = ptr - mb_l;
4444 v -= mb_l - 1;
4445 }
4446 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004447# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004448 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004449
4450 /* Use nextline[] if possible, it has the start of the
4451 * next line concatenated. */
4452 if ((prev_ptr - line) - nextlinecol >= 0)
4453 p = nextline + (prev_ptr - line) - nextlinecol;
4454 else
4455 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004456 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004457 len = spell_check(wp, p, &spell_hlf, &cap_col,
4458 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004459 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004460
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004461 /* In Insert mode only highlight a word that
4462 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004463 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004464 && (State & INSERT) != 0
4465 && wp->w_cursor.lnum == lnum
4466 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004467 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004468 && wp->w_cursor.col < (colnr_T)word_end)
4469 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004470 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004471 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004472 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004473
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004474 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004475 && (p - nextline) + len > nextline_idx)
4476 {
4477 /* Remember that the good word continues at the
4478 * start of the next line. */
4479 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004480 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004481 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004482
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004483 /* Turn index into actual attributes. */
4484 if (spell_hlf != HLF_COUNT)
4485 spell_attr = highlight_attr[spell_hlf];
4486
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004487 if (cap_col > 0)
4488 {
4489 if (p != prev_ptr
4490 && (p - nextline) + cap_col >= nextline_idx)
4491 {
4492 /* Remember that the word in the next line
4493 * must start with a capital. */
4494 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004495 cap_col = (int)((p - nextline) + cap_col
4496 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004497 }
4498 else
4499 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004500 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004501 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004502 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004503 }
4504 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004505 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004506 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004507 char_attr = hl_combine_attr(char_attr, spell_attr);
4508 else
4509 char_attr = hl_combine_attr(spell_attr, char_attr);
4510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511#endif
4512#ifdef FEAT_LINEBREAK
4513 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004514 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004516 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004518# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004519 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004520# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004521 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004523 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004525 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004526
Bram Moolenaar597a4222014-06-25 14:39:50 +02004527 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004528 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004529 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004530 if (c == TAB && n_extra + col > W_WIDTH(wp))
4531 n_extra = (int)wp->w_buffer->b_p_ts
4532 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4533
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004534# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004535 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004536# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004538# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004540 {
4541#ifdef FEAT_CONCEAL
4542 if (c == TAB)
4543 /* See "Tab alignment" below. */
4544 FIX_FOR_BOGUSCOLS;
4545#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004546 if (!wp->w_p_list)
4547 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 }
4550#endif
4551
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004552 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4553 */
4554 if (wp->w_p_list
4555 && (((c == 160
4556#ifdef FEAT_MBYTE
4557 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4558#endif
4559 ) && lcs_nbsp)
4560 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4561 {
4562 c = (c == ' ') ? lcs_space : lcs_nbsp;
4563 if (area_attr == 0 && search_attr == 0)
4564 {
4565 n_attr = 1;
4566 extra_attr = hl_attr(HLF_8);
4567 saved_attr2 = char_attr; /* save current attr */
4568 }
4569#ifdef FEAT_MBYTE
4570 mb_c = c;
4571 if (enc_utf8 && (*mb_char2len)(c) > 1)
4572 {
4573 mb_utf8 = TRUE;
4574 u8cc[0] = 0;
4575 c = 0xc0;
4576 }
4577 else
4578 mb_utf8 = FALSE;
4579#endif
4580 }
4581
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4583 {
4584 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004585 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
4587 n_attr = 1;
4588 extra_attr = hl_attr(HLF_8);
4589 saved_attr2 = char_attr; /* save current attr */
4590 }
4591#ifdef FEAT_MBYTE
4592 mb_c = c;
4593 if (enc_utf8 && (*mb_char2len)(c) > 1)
4594 {
4595 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004596 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004597 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
4599 else
4600 mb_utf8 = FALSE;
4601#endif
4602 }
4603 }
4604
4605 /*
4606 * Handling of non-printable characters.
4607 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004608 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 {
4610 /*
4611 * when getting a character from the file, we may have to
4612 * turn it into something else on the way to putting it
4613 * into "ScreenLines".
4614 */
4615 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4616 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004617 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004618 long vcol_adjusted = vcol; /* removed showbreak length */
4619#ifdef FEAT_LINEBREAK
4620 /* only adjust the tab_len, when at the first column
4621 * after the showbreak value was drawn */
4622 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4623 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004626 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004627 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4628
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004629#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004630 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004631#endif
4632 /* tab amount depends on current column */
4633 n_extra = tab_len;
4634#ifdef FEAT_LINEBREAK
4635 else
4636 {
4637 char_u *p;
4638 int len = n_extra;
4639 int i;
4640 int saved_nextra = n_extra;
4641
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004642#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004643 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004644 /* there are characters to conceal */
4645 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004646 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4647 */
4648 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4649 && n_extra > tab_len)
4650 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004651#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004652
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004653 /* if n_extra > 0, it gives the number of chars, to
4654 * use for a tab, else we need to calculate the width
4655 * for a tab */
4656#ifdef FEAT_MBYTE
4657 len = (tab_len * mb_char2len(lcs_tab2));
4658 if (n_extra > 0)
4659 len += n_extra - tab_len;
4660#endif
4661 c = lcs_tab1;
4662 p = alloc((unsigned)(len + 1));
4663 vim_memset(p, ' ', len);
4664 p[len] = NUL;
4665 p_extra_free = p;
4666 for (i = 0; i < tab_len; i++)
4667 {
4668#ifdef FEAT_MBYTE
4669 mb_char2bytes(lcs_tab2, p);
4670 p += mb_char2len(lcs_tab2);
4671 n_extra += mb_char2len(lcs_tab2)
4672 - (saved_nextra > 0 ? 1 : 0);
4673#else
4674 p[i] = lcs_tab2;
4675#endif
4676 }
4677 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004678#ifdef FEAT_CONCEAL
4679 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4680 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004681 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004682 n_extra -= vcol_off;
4683#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004684 }
4685#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004686#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004687 {
4688 int vc_saved = vcol_off;
4689
4690 /* Tab alignment should be identical regardless of
4691 * 'conceallevel' value. So tab compensates of all
4692 * previous concealed characters, and thus resets
4693 * vcol_off and boguscols accumulated so far in the
4694 * line. Note that the tab can be longer than
4695 * 'tabstop' when there are concealed characters. */
4696 FIX_FOR_BOGUSCOLS;
4697
4698 /* Make sure, the highlighting for the tab char will be
4699 * correctly set further below (effectively reverts the
4700 * FIX_FOR_BOGSUCOLS macro */
4701 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004702 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004703 tab_len += vc_saved;
4704 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706#ifdef FEAT_MBYTE
4707 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4708#endif
4709 if (wp->w_p_list)
4710 {
4711 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004712#ifdef FEAT_LINEBREAK
4713 if (wp->w_p_lbr)
4714 c_extra = NUL; /* using p_extra from above */
4715 else
4716#endif
4717 c_extra = lcs_tab2;
4718 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 extra_attr = hl_attr(HLF_8);
4720 saved_attr2 = char_attr; /* save current attr */
4721#ifdef FEAT_MBYTE
4722 mb_c = c;
4723 if (enc_utf8 && (*mb_char2len)(c) > 1)
4724 {
4725 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004726 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004727 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 }
4729#endif
4730 }
4731 else
4732 {
4733 c_extra = ' ';
4734 c = ' ';
4735 }
4736 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004737 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004738 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004739 || ((fromcol >= 0 || fromcol_prev >= 0)
4740 && tocol > vcol
4741 && VIsual_mode != Ctrl_V
4742 && (
4743# ifdef FEAT_RIGHTLEFT
4744 wp->w_p_rl ? (col >= 0) :
4745# endif
4746 (col < W_WIDTH(wp)))
4747 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004748 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004749 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004750 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004752 /* Display a '$' after the line or highlight an extra
4753 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4755 /* For a diff line the highlighting continues after the
4756 * "$". */
4757 if (
4758# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004759 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760# ifdef LINE_ATTR
4761 &&
4762# endif
4763# endif
4764# ifdef LINE_ATTR
4765 line_attr == 0
4766# endif
4767 )
4768#endif
4769 {
4770#ifdef FEAT_VIRTUALEDIT
4771 /* In virtualedit, visual selections may extend
4772 * beyond end of line. */
4773 if (area_highlighting && virtual_active()
4774 && tocol != MAXCOL && vcol < tocol)
4775 n_extra = 0;
4776 else
4777#endif
4778 {
4779 p_extra = at_end_str;
4780 n_extra = 1;
4781 c_extra = NUL;
4782 }
4783 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004784 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004785 c = lcs_eol;
4786 else
4787 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 lcs_eol_one = -1;
4789 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004790 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 {
4792 extra_attr = hl_attr(HLF_AT);
4793 n_attr = 1;
4794 }
4795#ifdef FEAT_MBYTE
4796 mb_c = c;
4797 if (enc_utf8 && (*mb_char2len)(c) > 1)
4798 {
4799 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004800 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004801 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
4803 else
4804 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4805#endif
4806 }
4807 else if (c != NUL)
4808 {
4809 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004810 if (n_extra == 0)
4811 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812#ifdef FEAT_RIGHTLEFT
4813 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4814 rl_mirror(p_extra); /* reverse "<12>" */
4815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004817#ifdef FEAT_LINEBREAK
4818 if (wp->w_p_lbr)
4819 {
4820 char_u *p;
4821
4822 c = *p_extra;
4823 p = alloc((unsigned)n_extra + 1);
4824 vim_memset(p, ' ', n_extra);
4825 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4826 p[n_extra] = NUL;
4827 p_extra_free = p_extra = p;
4828 }
4829 else
4830#endif
4831 {
4832 n_extra = byte2cells(c) - 1;
4833 c = *p_extra++;
4834 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004835 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 {
4837 n_attr = n_extra + 1;
4838 extra_attr = hl_attr(HLF_8);
4839 saved_attr2 = char_attr; /* save current attr */
4840 }
4841#ifdef FEAT_MBYTE
4842 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4843#endif
4844 }
4845#ifdef FEAT_VIRTUALEDIT
4846 else if (VIsual_active
4847 && (VIsual_mode == Ctrl_V
4848 || VIsual_mode == 'v')
4849 && virtual_active()
4850 && tocol != MAXCOL
4851 && vcol < tocol
4852 && (
4853# ifdef FEAT_RIGHTLEFT
4854 wp->w_p_rl ? (col >= 0) :
4855# endif
4856 (col < W_WIDTH(wp))))
4857 {
4858 c = ' ';
4859 --ptr; /* put it back at the NUL */
4860 }
4861#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004862#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 else if ((
4864# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004865 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 ) && (
4869# ifdef FEAT_RIGHTLEFT
4870 wp->w_p_rl ? (col >= 0) :
4871# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004872 (col
4873# ifdef FEAT_CONCEAL
4874 - boguscols
4875# endif
4876 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 {
4878 /* Highlight until the right side of the window */
4879 c = ' ';
4880 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004881
4882 /* Remember we do the char for line highlighting. */
4883 ++did_line_attr;
4884
4885 /* don't do search HL for the rest of the line */
4886 if (line_attr != 0 && char_attr == search_attr && col > 0)
4887 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888# ifdef FEAT_DIFF
4889 if (diff_hlf == HLF_TXD)
4890 {
4891 diff_hlf = HLF_CHD;
4892 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004895 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4896 char_attr = hl_combine_attr(char_attr,
4897 hl_attr(HLF_CUL));
4898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900# endif
4901 }
4902#endif
4903 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004904
4905#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004906 if ( wp->w_p_cole > 0
4907 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004908 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02004909 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004910 && !(lnum_in_visual_area
4911 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004912 {
4913 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02004914 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004915 && (syn_get_sub_char() != NUL || match_conc
4916 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004917 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004918 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004919 /* First time at this concealed item: display one
4920 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004921 if (match_conc)
4922 c = match_conc;
4923 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004924 c = syn_get_sub_char();
4925 else if (lcs_conceal != NUL)
4926 c = lcs_conceal;
4927 else
4928 c = ' ';
4929
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004930 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004931
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004932 if (n_extra > 0)
4933 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004934 vcol += n_extra;
4935 if (wp->w_p_wrap && n_extra > 0)
4936 {
4937# ifdef FEAT_RIGHTLEFT
4938 if (wp->w_p_rl)
4939 {
4940 col -= n_extra;
4941 boguscols -= n_extra;
4942 }
4943 else
4944# endif
4945 {
4946 boguscols += n_extra;
4947 col += n_extra;
4948 }
4949 }
4950 n_extra = 0;
4951 n_attr = 0;
4952 }
4953 else if (n_skip == 0)
4954 {
4955 is_concealing = TRUE;
4956 n_skip = 1;
4957 }
4958# ifdef FEAT_MBYTE
4959 mb_c = c;
4960 if (enc_utf8 && (*mb_char2len)(c) > 1)
4961 {
4962 mb_utf8 = TRUE;
4963 u8cc[0] = 0;
4964 c = 0xc0;
4965 }
4966 else
4967 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4968# endif
4969 }
4970 else
4971 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004972 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004973 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004974 }
4975#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
4977
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004978#ifdef FEAT_CONCEAL
4979 /* In the cursor line and we may be concealing characters: correct
4980 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004981 if (!did_wcol && draw_state == WL_LINE
4982 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004983 && conceal_cursor_line(wp)
4984 && (int)wp->w_virtcol <= vcol + n_skip)
4985 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01004986# ifdef FEAT_RIGHTLEFT
4987 if (wp->w_p_rl)
4988 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
4989 else
4990# endif
4991 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004992 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004993 did_wcol = TRUE;
4994 }
4995#endif
4996
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 /* Don't override visual selection highlighting. */
4998 if (n_attr > 0
4999 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005000 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 char_attr = extra_attr;
5002
Bram Moolenaar81695252004-12-29 20:58:21 +00005003#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 /* XIM don't send preedit_start and preedit_end, but they send
5005 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5006 * im_is_preediting() here. */
5007 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005008 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 && (State & INSERT)
5010 && !p_imdisable
5011 && im_is_preediting()
5012 && draw_state == WL_LINE)
5013 {
5014 colnr_T tcol;
5015
5016 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005017 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 else
5019 tcol = preedit_end_col;
5020 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5021 {
5022 if (feedback_old_attr < 0)
5023 {
5024 feedback_col = 0;
5025 feedback_old_attr = char_attr;
5026 }
5027 char_attr = im_get_feedback_attr(feedback_col);
5028 if (char_attr < 0)
5029 char_attr = feedback_old_attr;
5030 feedback_col++;
5031 }
5032 else if (feedback_old_attr >= 0)
5033 {
5034 char_attr = feedback_old_attr;
5035 feedback_old_attr = -1;
5036 feedback_col = 0;
5037 }
5038 }
5039#endif
5040 /*
5041 * Handle the case where we are in column 0 but not on the first
5042 * character of the line and the user wants us to show us a
5043 * special character (via 'listchars' option "precedes:<char>".
5044 */
5045 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005046 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5048#ifdef FEAT_DIFF
5049 && filler_todo <= 0
5050#endif
5051 && draw_state > WL_NR
5052 && c != NUL)
5053 {
5054 c = lcs_prec;
5055 lcs_prec_todo = NUL;
5056#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005057 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5058 {
5059 /* Double-width character being overwritten by the "precedes"
5060 * character, need to fill up half the character. */
5061 c_extra = MB_FILLER_CHAR;
5062 n_extra = 1;
5063 n_attr = 2;
5064 extra_attr = hl_attr(HLF_AT);
5065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 mb_c = c;
5067 if (enc_utf8 && (*mb_char2len)(c) > 1)
5068 {
5069 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005070 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005071 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073 else
5074 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5075#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005076 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
5078 saved_attr3 = char_attr; /* save current attr */
5079 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5080 n_attr3 = 1;
5081 }
5082 }
5083
5084 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005085 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005087 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005088#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005089 || did_line_attr == 1
5090#endif
5091 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005093#ifdef FEAT_SEARCH_EXTRA
5094 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005095
5096 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005097 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005098 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005099#endif
5100
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005101 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 * highlight match at end of line. If it's beyond the last
5103 * char on the screen, just overwrite that one (tricky!) Not
5104 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005105#ifdef FEAT_SEARCH_EXTRA
5106 prevcol_hl_flag = FALSE;
5107 if (prevcol == (long)search_hl.startcol)
5108 prevcol_hl_flag = TRUE;
5109 else
5110 {
5111 cur = wp->w_match_head;
5112 while (cur != NULL)
5113 {
5114 if (prevcol == (long)cur->hl.startcol)
5115 {
5116 prevcol_hl_flag = TRUE;
5117 break;
5118 }
5119 cur = cur->next;
5120 }
5121 }
5122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005124 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005125 && (VIsual_mode != Ctrl_V
5126 || lnum == VIsual.lnum
5127 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005128 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129#ifdef FEAT_SEARCH_EXTRA
5130 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005131 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005132# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005133 && did_line_attr <= 1
5134# endif
5135 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136#endif
5137 ))
5138 {
5139 int n = 0;
5140
5141#ifdef FEAT_RIGHTLEFT
5142 if (wp->w_p_rl)
5143 {
5144 if (col < 0)
5145 n = 1;
5146 }
5147 else
5148#endif
5149 {
5150 if (col >= W_WIDTH(wp))
5151 n = -1;
5152 }
5153 if (n != 0)
5154 {
5155 /* At the window boundary, highlight the last character
5156 * instead (better than nothing). */
5157 off += n;
5158 col += n;
5159 }
5160 else
5161 {
5162 /* Add a blank character to highlight. */
5163 ScreenLines[off] = ' ';
5164#ifdef FEAT_MBYTE
5165 if (enc_utf8)
5166 ScreenLinesUC[off] = 0;
5167#endif
5168 }
5169#ifdef FEAT_SEARCH_EXTRA
5170 if (area_attr == 0)
5171 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005172 /* Use attributes from match with highest priority among
5173 * 'search_hl' and the match list. */
5174 char_attr = search_hl.attr;
5175 cur = wp->w_match_head;
5176 shl_flag = FALSE;
5177 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005178 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005179 if (shl_flag == FALSE
5180 && ((cur != NULL
5181 && cur->priority > SEARCH_HL_PRIORITY)
5182 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005183 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005184 shl = &search_hl;
5185 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005186 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005187 else
5188 shl = &cur->hl;
5189 if ((ptr - line) - 1 == (long)shl->startcol)
5190 char_attr = shl->attr;
5191 if (shl != &search_hl && cur != NULL)
5192 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195#endif
5196 ScreenAttrs[off] = char_attr;
5197#ifdef FEAT_RIGHTLEFT
5198 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005201 --off;
5202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 else
5204#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005207 ++off;
5208 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005209 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005210#ifdef FEAT_SYN_HL
5211 eol_hl_off = 1;
5212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215
Bram Moolenaar91170f82006-05-05 21:15:17 +00005216 /*
5217 * At end of the text line.
5218 */
5219 if (c == NUL)
5220 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005221#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005222 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5223 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005224 {
5225 /* highlight last char after line */
5226 --col;
5227 --off;
5228 --vcol;
5229 }
5230
Bram Moolenaar1a384422010-07-14 19:53:30 +02005231 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005232 if (wp->w_p_wrap)
5233 v = wp->w_skipcol;
5234 else
5235 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005236
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005237 /* check if line ends before left margin */
5238 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005239 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005240#ifdef FEAT_CONCEAL
5241 /* Get rid of the boguscols now, we want to draw until the right
5242 * edge for 'cursorcolumn'. */
5243 col -= boguscols;
5244 boguscols = 0;
5245#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005246
5247 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005248 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005249
5250 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005251 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5252 && (int)wp->w_virtcol <
5253 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005254 && lnum != wp->w_cursor.lnum)
5255 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005256# ifdef FEAT_RIGHTLEFT
5257 && !wp->w_p_rl
5258# endif
5259 )
5260 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005261 int rightmost_vcol = 0;
5262 int i;
5263
5264 if (wp->w_p_cuc)
5265 rightmost_vcol = wp->w_virtcol;
5266 if (draw_color_col)
5267 /* determine rightmost colorcolumn to possibly draw */
5268 for (i = 0; color_cols[i] >= 0; ++i)
5269 if (rightmost_vcol < color_cols[i])
5270 rightmost_vcol = color_cols[i];
5271
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005272 while (col < W_WIDTH(wp))
5273 {
5274 ScreenLines[off] = ' ';
5275#ifdef FEAT_MBYTE
5276 if (enc_utf8)
5277 ScreenLinesUC[off] = 0;
5278#endif
5279 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005280 if (draw_color_col)
5281 draw_color_col = advance_color_col(VCOL_HLC,
5282 &color_cols);
5283
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005284 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005285 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005286 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005287 ScreenAttrs[off++] = hl_attr(HLF_MC);
5288 else
5289 ScreenAttrs[off++] = 0;
5290
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005291 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005292 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005293
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005294 ++vcol;
5295 }
5296 }
5297#endif
5298
Bram Moolenaar860cae12010-06-05 23:22:07 +02005299 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5300 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 row++;
5302
5303 /*
5304 * Update w_cline_height and w_cline_folded if the cursor line was
5305 * updated (saves a call to plines() later).
5306 */
5307 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5308 {
5309 curwin->w_cline_row = startrow;
5310 curwin->w_cline_height = row - startrow;
5311#ifdef FEAT_FOLDING
5312 curwin->w_cline_folded = FALSE;
5313#endif
5314 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5315 }
5316
5317 break;
5318 }
5319
5320 /* line continues beyond line end */
5321 if (lcs_ext
5322 && !wp->w_p_wrap
5323#ifdef FEAT_DIFF
5324 && filler_todo <= 0
5325#endif
5326 && (
5327#ifdef FEAT_RIGHTLEFT
5328 wp->w_p_rl ? col == 0 :
5329#endif
5330 col == W_WIDTH(wp) - 1)
5331 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005332 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5334 {
5335 c = lcs_ext;
5336 char_attr = hl_attr(HLF_AT);
5337#ifdef FEAT_MBYTE
5338 mb_c = c;
5339 if (enc_utf8 && (*mb_char2len)(c) > 1)
5340 {
5341 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005342 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005343 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 }
5345 else
5346 mb_utf8 = FALSE;
5347#endif
5348 }
5349
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005350#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005351 /* advance to the next 'colorcolumn' */
5352 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005353 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005354
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005355 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005356 * highlight the cursor position itself.
5357 * Also highlight the 'colorcolumn' if it is different than
5358 * 'cursorcolumn' */
5359 vcol_save_attr = -1;
5360 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005361 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005362 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005363 && lnum != wp->w_cursor.lnum)
5364 {
5365 vcol_save_attr = char_attr;
5366 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5367 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005368 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005369 {
5370 vcol_save_attr = char_attr;
5371 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5372 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005373 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005374#endif
5375
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 /*
5377 * Store character to be displayed.
5378 * Skip characters that are left of the screen for 'nowrap'.
5379 */
5380 vcol_prev = vcol;
5381 if (draw_state < WL_LINE || n_skip <= 0)
5382 {
5383 /*
5384 * Store the character.
5385 */
5386#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5387 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5388 {
5389 /* A double-wide character is: put first halve in left cell. */
5390 --off;
5391 --col;
5392 }
5393#endif
5394 ScreenLines[off] = c;
5395#ifdef FEAT_MBYTE
5396 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005397 {
5398 if ((mb_c & 0xff00) == 0x8e00)
5399 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 else if (enc_utf8)
5403 {
5404 if (mb_utf8)
5405 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005406 int i;
5407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005409 if ((c & 0xff) == 0)
5410 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005411 for (i = 0; i < Screen_mco; ++i)
5412 {
5413 ScreenLinesC[i][off] = u8cc[i];
5414 if (u8cc[i] == 0)
5415 break;
5416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
5418 else
5419 ScreenLinesUC[off] = 0;
5420 }
5421 if (multi_attr)
5422 {
5423 ScreenAttrs[off] = multi_attr;
5424 multi_attr = 0;
5425 }
5426 else
5427#endif
5428 ScreenAttrs[off] = char_attr;
5429
5430#ifdef FEAT_MBYTE
5431 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5432 {
5433 /* Need to fill two screen columns. */
5434 ++off;
5435 ++col;
5436 if (enc_utf8)
5437 /* UTF-8: Put a 0 in the second screen char. */
5438 ScreenLines[off] = 0;
5439 else
5440 /* DBCS: Put second byte in the second screen char. */
5441 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005442 if (draw_state > WL_NR
5443#ifdef FEAT_DIFF
5444 && filler_todo <= 0
5445#endif
5446 )
5447 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 /* When "tocol" is halfway a character, set it to the end of
5449 * the character, otherwise highlighting won't stop. */
5450 if (tocol == vcol)
5451 ++tocol;
5452#ifdef FEAT_RIGHTLEFT
5453 if (wp->w_p_rl)
5454 {
5455 /* now it's time to backup one cell */
5456 --off;
5457 --col;
5458 }
5459#endif
5460 }
5461#endif
5462#ifdef FEAT_RIGHTLEFT
5463 if (wp->w_p_rl)
5464 {
5465 --off;
5466 --col;
5467 }
5468 else
5469#endif
5470 {
5471 ++off;
5472 ++col;
5473 }
5474 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005475#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005476 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005477 {
5478 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005479 ++vcol_off;
5480 if (n_extra > 0)
5481 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005482 if (wp->w_p_wrap)
5483 {
5484 /*
5485 * Special voodoo required if 'wrap' is on.
5486 *
5487 * Advance the column indicator to force the line
5488 * drawing to wrap early. This will make the line
5489 * take up the same screen space when parts are concealed,
5490 * so that cursor line computations aren't messed up.
5491 *
5492 * To avoid the fictitious advance of 'col' causing
5493 * trailing junk to be written out of the screen line
5494 * we are building, 'boguscols' keeps track of the number
5495 * of bad columns we have advanced.
5496 */
5497 if (n_extra > 0)
5498 {
5499 vcol += n_extra;
5500# ifdef FEAT_RIGHTLEFT
5501 if (wp->w_p_rl)
5502 {
5503 col -= n_extra;
5504 boguscols -= n_extra;
5505 }
5506 else
5507# endif
5508 {
5509 col += n_extra;
5510 boguscols += n_extra;
5511 }
5512 n_extra = 0;
5513 n_attr = 0;
5514 }
5515
5516
5517# ifdef FEAT_MBYTE
5518 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5519 {
5520 /* Need to fill two screen columns. */
5521# ifdef FEAT_RIGHTLEFT
5522 if (wp->w_p_rl)
5523 {
5524 --boguscols;
5525 --col;
5526 }
5527 else
5528# endif
5529 {
5530 ++boguscols;
5531 ++col;
5532 }
5533 }
5534# endif
5535
5536# ifdef FEAT_RIGHTLEFT
5537 if (wp->w_p_rl)
5538 {
5539 --boguscols;
5540 --col;
5541 }
5542 else
5543# endif
5544 {
5545 ++boguscols;
5546 ++col;
5547 }
5548 }
5549 else
5550 {
5551 if (n_extra > 0)
5552 {
5553 vcol += n_extra;
5554 n_extra = 0;
5555 n_attr = 0;
5556 }
5557 }
5558
5559 }
5560#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 else
5562 --n_skip;
5563
Bram Moolenaar64486672010-05-16 15:46:46 +02005564 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5565 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005566 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567#ifdef FEAT_DIFF
5568 && filler_todo <= 0
5569#endif
5570 )
5571 ++vcol;
5572
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005573#ifdef FEAT_SYN_HL
5574 if (vcol_save_attr >= 0)
5575 char_attr = vcol_save_attr;
5576#endif
5577
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 /* restore attributes after "predeces" in 'listchars' */
5579 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5580 char_attr = saved_attr3;
5581
5582 /* restore attributes after last 'listchars' or 'number' char */
5583 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5584 char_attr = saved_attr2;
5585
5586 /*
5587 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005588 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 */
5590 if ((
5591#ifdef FEAT_RIGHTLEFT
5592 wp->w_p_rl ? (col < 0) :
5593#endif
5594 (col >= W_WIDTH(wp)))
5595 && (*ptr != NUL
5596#ifdef FEAT_DIFF
5597 || filler_todo > 0
5598#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005599 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5601 )
5602 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005603#ifdef FEAT_CONCEAL
5604 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5605 (int)W_WIDTH(wp), wp->w_p_rl);
5606 boguscols = 0;
5607#else
5608 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5609 (int)W_WIDTH(wp), wp->w_p_rl);
5610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 ++row;
5612 ++screen_row;
5613
5614 /* When not wrapping and finished diff lines, or when displayed
5615 * '$' and highlighting until last column, break here. */
5616 if ((!wp->w_p_wrap
5617#ifdef FEAT_DIFF
5618 && filler_todo <= 0
5619#endif
5620 ) || lcs_eol_one == -1)
5621 break;
5622
5623 /* When the window is too narrow draw all "@" lines. */
5624 if (draw_state != WL_LINE
5625#ifdef FEAT_DIFF
5626 && filler_todo <= 0
5627#endif
5628 )
5629 {
5630 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005631#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 draw_vsep_win(wp, row);
5633#endif
5634 row = endrow;
5635 }
5636
5637 /* When line got too long for screen break here. */
5638 if (row == endrow)
5639 {
5640 ++row;
5641 break;
5642 }
5643
5644 if (screen_cur_row == screen_row - 1
5645#ifdef FEAT_DIFF
5646 && filler_todo <= 0
5647#endif
5648 && W_WIDTH(wp) == Columns)
5649 {
5650 /* Remember that the line wraps, used for modeless copy. */
5651 LineWraps[screen_row - 1] = TRUE;
5652
5653 /*
5654 * Special trick to make copy/paste of wrapped lines work with
5655 * xterm/screen: write an extra character beyond the end of
5656 * the line. This will work with all terminal types
5657 * (regardless of the xn,am settings).
5658 * Only do this on a fast tty.
5659 * Only do this if the cursor is on the current line
5660 * (something has been written in it).
5661 * Don't do this for the GUI.
5662 * Don't do this for double-width characters.
5663 * Don't do this for a window not at the right screen border.
5664 */
5665 if (p_tf
5666#ifdef FEAT_GUI
5667 && !gui.in_use
5668#endif
5669#ifdef FEAT_MBYTE
5670 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005671 && ((*mb_off2cells)(LineOffset[screen_row],
5672 LineOffset[screen_row] + screen_Columns)
5673 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005675 + (int)Columns - 2,
5676 LineOffset[screen_row] + screen_Columns)
5677 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678#endif
5679 )
5680 {
5681 /* First make sure we are at the end of the screen line,
5682 * then output the same character again to let the
5683 * terminal know about the wrap. If the terminal doesn't
5684 * auto-wrap, we overwrite the character. */
5685 if (screen_cur_col != W_WIDTH(wp))
5686 screen_char(LineOffset[screen_row - 1]
5687 + (unsigned)Columns - 1,
5688 screen_row - 1, (int)(Columns - 1));
5689
5690#ifdef FEAT_MBYTE
5691 /* When there is a multi-byte character, just output a
5692 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005693 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5694 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 out_char(' ');
5696 else
5697#endif
5698 out_char(ScreenLines[LineOffset[screen_row - 1]
5699 + (Columns - 1)]);
5700 /* force a redraw of the first char on the next line */
5701 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5702 screen_start(); /* don't know where cursor is now */
5703 }
5704 }
5705
5706 col = 0;
5707 off = (unsigned)(current_ScreenLine - ScreenLines);
5708#ifdef FEAT_RIGHTLEFT
5709 if (wp->w_p_rl)
5710 {
5711 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5712 off += col;
5713 }
5714#endif
5715
5716 /* reset the drawing state for the start of a wrapped line */
5717 draw_state = WL_START;
5718 saved_n_extra = n_extra;
5719 saved_p_extra = p_extra;
5720 saved_c_extra = c_extra;
5721 saved_char_attr = char_attr;
5722 n_extra = 0;
5723 lcs_prec_todo = lcs_prec;
5724#ifdef FEAT_LINEBREAK
5725# ifdef FEAT_DIFF
5726 if (filler_todo <= 0)
5727# endif
5728 need_showbreak = TRUE;
5729#endif
5730#ifdef FEAT_DIFF
5731 --filler_todo;
5732 /* When the filler lines are actually below the last line of the
5733 * file, don't draw the line itself, break here. */
5734 if (filler_todo == 0 && wp->w_botfill)
5735 break;
5736#endif
5737 }
5738
5739 } /* for every character in the line */
5740
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005741#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005742 /* After an empty line check first word for capital. */
5743 if (*skipwhite(line) == NUL)
5744 {
5745 capcol_lnum = lnum + 1;
5746 cap_col = 0;
5747 }
5748#endif
5749
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 return row;
5751}
5752
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005753#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005754static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005755
5756/*
5757 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005758 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005759 */
5760 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005761comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005762{
5763 int i;
5764
5765 for (i = 0; i < Screen_mco; ++i)
5766 {
5767 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5768 return TRUE;
5769 if (ScreenLinesC[i][off_from] == 0)
5770 break;
5771 }
5772 return FALSE;
5773}
5774#endif
5775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776/*
5777 * Check whether the given character needs redrawing:
5778 * - the (first byte of the) character is different
5779 * - the attributes are different
5780 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005781 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 */
5783 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005784char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
5786 if (cols > 0
5787 && ((ScreenLines[off_from] != ScreenLines[off_to]
5788 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5789
5790#ifdef FEAT_MBYTE
5791 || (enc_dbcs != 0
5792 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5793 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5794 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5795 : (cols > 1 && ScreenLines[off_from + 1]
5796 != ScreenLines[off_to + 1])))
5797 || (enc_utf8
5798 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5799 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005800 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005801 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5802 && ScreenLines[off_from + 1]
5803 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804#endif
5805 ))
5806 return TRUE;
5807 return FALSE;
5808}
5809
5810/*
5811 * Move one "cooked" screen line to the screen, but only the characters that
5812 * have actually changed. Handle insert/delete character.
5813 * "coloff" gives the first column on the screen for this line.
5814 * "endcol" gives the columns where valid characters are.
5815 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5816 * needs to be cleared, negative otherwise.
5817 * "rlflag" is TRUE in a rightleft window:
5818 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5819 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5820 */
5821 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005822screen_line(
5823 int row,
5824 int coloff,
5825 int endcol,
5826 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005828 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005830 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
5832 unsigned off_from;
5833 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005834#ifdef FEAT_MBYTE
5835 unsigned max_off_from;
5836 unsigned max_off_to;
5837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005839#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 int hl;
5841#endif
5842 int force = FALSE; /* force update rest of the line */
5843 int redraw_this /* bool: does character need redraw? */
5844#ifdef FEAT_GUI
5845 = TRUE /* For GUI when while-loop empty */
5846#endif
5847 ;
5848 int redraw_next; /* redraw_this for next character */
5849#ifdef FEAT_MBYTE
5850 int clear_next = FALSE;
5851 int char_cells; /* 1: normal char */
5852 /* 2: occupies two display cells */
5853# define CHAR_CELLS char_cells
5854#else
5855# define CHAR_CELLS 1
5856#endif
5857
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005858 /* Check for illegal row and col, just in case. */
5859 if (row >= Rows)
5860 row = Rows - 1;
5861 if (endcol > Columns)
5862 endcol = Columns;
5863
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864# ifdef FEAT_CLIPBOARD
5865 clip_may_clear_selection(row, row);
5866# endif
5867
5868 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5869 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005870#ifdef FEAT_MBYTE
5871 max_off_from = off_from + screen_Columns;
5872 max_off_to = LineOffset[row] + screen_Columns;
5873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874
5875#ifdef FEAT_RIGHTLEFT
5876 if (rlflag)
5877 {
5878 /* Clear rest first, because it's left of the text. */
5879 if (clear_width > 0)
5880 {
5881 while (col <= endcol && ScreenLines[off_to] == ' '
5882 && ScreenAttrs[off_to] == 0
5883# ifdef FEAT_MBYTE
5884 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5885# endif
5886 )
5887 {
5888 ++off_to;
5889 ++col;
5890 }
5891 if (col <= endcol)
5892 screen_fill(row, row + 1, col + coloff,
5893 endcol + coloff + 1, ' ', ' ', 0);
5894 }
5895 col = endcol + 1;
5896 off_to = LineOffset[row] + col + coloff;
5897 off_from += col;
5898 endcol = (clear_width > 0 ? clear_width : -clear_width);
5899 }
5900#endif /* FEAT_RIGHTLEFT */
5901
5902 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5903
5904 while (col < endcol)
5905 {
5906#ifdef FEAT_MBYTE
5907 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005908 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 else
5910 char_cells = 1;
5911#endif
5912
5913 redraw_this = redraw_next;
5914 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5915 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5916
5917#ifdef FEAT_GUI
5918 /* If the next character was bold, then redraw the current character to
5919 * remove any pixels that might have spilt over into us. This only
5920 * happens in the GUI.
5921 */
5922 if (redraw_next && gui.in_use)
5923 {
5924 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005925 if (hl > HL_ALL)
5926 hl = syn_attr2attr(hl);
5927 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 redraw_this = TRUE;
5929 }
5930#endif
5931
5932 if (redraw_this)
5933 {
5934 /*
5935 * Special handling when 'xs' termcap flag set (hpterm):
5936 * Attributes for characters are stored at the position where the
5937 * cursor is when writing the highlighting code. The
5938 * start-highlighting code must be written with the cursor on the
5939 * first highlighted character. The stop-highlighting code must
5940 * be written with the cursor just after the last highlighted
5941 * character.
5942 * Overwriting a character doesn't remove it's highlighting. Need
5943 * to clear the rest of the line, and force redrawing it
5944 * completely.
5945 */
5946 if ( p_wiv
5947 && !force
5948#ifdef FEAT_GUI
5949 && !gui.in_use
5950#endif
5951 && ScreenAttrs[off_to] != 0
5952 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5953 {
5954 /*
5955 * Need to remove highlighting attributes here.
5956 */
5957 windgoto(row, col + coloff);
5958 out_str(T_CE); /* clear rest of this screen line */
5959 screen_start(); /* don't know where cursor is now */
5960 force = TRUE; /* force redraw of rest of the line */
5961 redraw_next = TRUE; /* or else next char would miss out */
5962
5963 /*
5964 * If the previous character was highlighted, need to stop
5965 * highlighting at this character.
5966 */
5967 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5968 {
5969 screen_attr = ScreenAttrs[off_to - 1];
5970 term_windgoto(row, col + coloff);
5971 screen_stop_highlight();
5972 }
5973 else
5974 screen_attr = 0; /* highlighting has stopped */
5975 }
5976#ifdef FEAT_MBYTE
5977 if (enc_dbcs != 0)
5978 {
5979 /* Check if overwriting a double-byte with a single-byte or
5980 * the other way around requires another character to be
5981 * redrawn. For UTF-8 this isn't needed, because comparing
5982 * ScreenLinesUC[] is sufficient. */
5983 if (char_cells == 1
5984 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005985 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 {
5987 /* Writing a single-cell character over a double-cell
5988 * character: need to redraw the next cell. */
5989 ScreenLines[off_to + 1] = 0;
5990 redraw_next = TRUE;
5991 }
5992 else if (char_cells == 2
5993 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005994 && (*mb_off2cells)(off_to, max_off_to) == 1
5995 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 {
5997 /* Writing the second half of a double-cell character over
5998 * a double-cell character: need to redraw the second
5999 * cell. */
6000 ScreenLines[off_to + 2] = 0;
6001 redraw_next = TRUE;
6002 }
6003
6004 if (enc_dbcs == DBCS_JPNU)
6005 ScreenLines2[off_to] = ScreenLines2[off_from];
6006 }
6007 /* When writing a single-width character over a double-width
6008 * character and at the end of the redrawn text, need to clear out
6009 * the right halve of the old character.
6010 * Also required when writing the right halve of a double-width
6011 * char over the left halve of an existing one. */
6012 if (has_mbyte && col + char_cells == endcol
6013 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006014 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006016 && (*mb_off2cells)(off_to, max_off_to) == 1
6017 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 clear_next = TRUE;
6019#endif
6020
6021 ScreenLines[off_to] = ScreenLines[off_from];
6022#ifdef FEAT_MBYTE
6023 if (enc_utf8)
6024 {
6025 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6026 if (ScreenLinesUC[off_from] != 0)
6027 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006028 int i;
6029
6030 for (i = 0; i < Screen_mco; ++i)
6031 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 }
6033 }
6034 if (char_cells == 2)
6035 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6036#endif
6037
6038#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006039 /* The bold trick makes a single column of pixels appear in the
6040 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 * character should be redrawn too. This happens for our own GUI
6042 * and for some xterms. */
6043 if (
6044# ifdef FEAT_GUI
6045 gui.in_use
6046# endif
6047# if defined(FEAT_GUI) && defined(UNIX)
6048 ||
6049# endif
6050# ifdef UNIX
6051 term_is_xterm
6052# endif
6053 )
6054 {
6055 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006056 if (hl > HL_ALL)
6057 hl = syn_attr2attr(hl);
6058 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 redraw_next = TRUE;
6060 }
6061#endif
6062 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6063#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006064 /* For simplicity set the attributes of second half of a
6065 * double-wide character equal to the first half. */
6066 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006068
6069 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 else
6072#endif
6073 screen_char(off_to, row, col + coloff);
6074 }
6075 else if ( p_wiv
6076#ifdef FEAT_GUI
6077 && !gui.in_use
6078#endif
6079 && col + coloff > 0)
6080 {
6081 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6082 {
6083 /*
6084 * Don't output stop-highlight when moving the cursor, it will
6085 * stop the highlighting when it should continue.
6086 */
6087 screen_attr = 0;
6088 }
6089 else if (screen_attr != 0)
6090 screen_stop_highlight();
6091 }
6092
6093 off_to += CHAR_CELLS;
6094 off_from += CHAR_CELLS;
6095 col += CHAR_CELLS;
6096 }
6097
6098#ifdef FEAT_MBYTE
6099 if (clear_next)
6100 {
6101 /* Clear the second half of a double-wide character of which the left
6102 * half was overwritten with a single-wide character. */
6103 ScreenLines[off_to] = ' ';
6104 if (enc_utf8)
6105 ScreenLinesUC[off_to] = 0;
6106 screen_char(off_to, row, col + coloff);
6107 }
6108#endif
6109
6110 if (clear_width > 0
6111#ifdef FEAT_RIGHTLEFT
6112 && !rlflag
6113#endif
6114 )
6115 {
6116#ifdef FEAT_GUI
6117 int startCol = col;
6118#endif
6119
6120 /* blank out the rest of the line */
6121 while (col < clear_width && ScreenLines[off_to] == ' '
6122 && ScreenAttrs[off_to] == 0
6123#ifdef FEAT_MBYTE
6124 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6125#endif
6126 )
6127 {
6128 ++off_to;
6129 ++col;
6130 }
6131 if (col < clear_width)
6132 {
6133#ifdef FEAT_GUI
6134 /*
6135 * In the GUI, clearing the rest of the line may leave pixels
6136 * behind if the first character cleared was bold. Some bold
6137 * fonts spill over the left. In this case we redraw the previous
6138 * character too. If we didn't skip any blanks above, then we
6139 * only redraw if the character wasn't already redrawn anyway.
6140 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006141 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 {
6143 hl = ScreenAttrs[off_to];
6144 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006145 {
6146 int prev_cells = 1;
6147# ifdef FEAT_MBYTE
6148 if (enc_utf8)
6149 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6150 * that its width is 2. */
6151 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6152 else if (enc_dbcs != 0)
6153 {
6154 /* find previous character by counting from first
6155 * column and get its width. */
6156 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006157 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006158
6159 while (off < off_to)
6160 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006161 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006162 off += prev_cells;
6163 }
6164 }
6165
6166 if (enc_dbcs != 0 && prev_cells > 1)
6167 screen_char_2(off_to - prev_cells, row,
6168 col + coloff - prev_cells);
6169 else
6170# endif
6171 screen_char(off_to - prev_cells, row,
6172 col + coloff - prev_cells);
6173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 }
6175#endif
6176 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6177 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006178#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 off_to += clear_width - col;
6180 col = clear_width;
6181#endif
6182 }
6183 }
6184
6185 if (clear_width > 0)
6186 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006187#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 /* For a window that's left of another, draw the separator char. */
6189 if (col + coloff < Columns)
6190 {
6191 int c;
6192
6193 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006194 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006196 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6197 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198# endif
6199 || ScreenAttrs[off_to] != hl)
6200 {
6201 ScreenLines[off_to] = c;
6202 ScreenAttrs[off_to] = hl;
6203# ifdef FEAT_MBYTE
6204 if (enc_utf8)
6205 {
6206 if (c >= 0x80)
6207 {
6208 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006209 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 }
6211 else
6212 ScreenLinesUC[off_to] = 0;
6213 }
6214# endif
6215 screen_char(off_to, row, col + coloff);
6216 }
6217 }
6218 else
6219#endif
6220 LineWraps[row] = FALSE;
6221 }
6222}
6223
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006224#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006226 * Mirror text "str" for right-left displaying.
6227 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006229 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006230rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 char_u *p1, *p2;
6233 int t;
6234
6235 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6236 {
6237 t = *p1;
6238 *p1 = *p2;
6239 *p2 = t;
6240 }
6241}
6242#endif
6243
6244#if defined(FEAT_WINDOWS) || defined(PROTO)
6245/*
6246 * mark all status lines for redraw; used after first :cd
6247 */
6248 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006249status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
6251 win_T *wp;
6252
6253 for (wp = firstwin; wp; wp = wp->w_next)
6254 if (wp->w_status_height)
6255 {
6256 wp->w_redr_status = TRUE;
6257 redraw_later(VALID);
6258 }
6259}
6260
6261/*
6262 * mark all status lines of the current buffer for redraw
6263 */
6264 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006265status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266{
6267 win_T *wp;
6268
6269 for (wp = firstwin; wp; wp = wp->w_next)
6270 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6271 {
6272 wp->w_redr_status = TRUE;
6273 redraw_later(VALID);
6274 }
6275}
6276
6277/*
6278 * Redraw all status lines that need to be redrawn.
6279 */
6280 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006281redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
6283 win_T *wp;
6284
6285 for (wp = firstwin; wp; wp = wp->w_next)
6286 if (wp->w_redr_status)
6287 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006288 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006289 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290}
6291#endif
6292
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006293#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294/*
6295 * Redraw all status lines at the bottom of frame "frp".
6296 */
6297 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006298win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
6300 if (frp->fr_layout == FR_LEAF)
6301 frp->fr_win->w_redr_status = TRUE;
6302 else if (frp->fr_layout == FR_ROW)
6303 {
6304 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6305 win_redraw_last_status(frp);
6306 }
6307 else /* frp->fr_layout == FR_COL */
6308 {
6309 frp = frp->fr_child;
6310 while (frp->fr_next != NULL)
6311 frp = frp->fr_next;
6312 win_redraw_last_status(frp);
6313 }
6314}
6315#endif
6316
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006317#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318/*
6319 * Draw the verticap separator right of window "wp" starting with line "row".
6320 */
6321 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006322draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323{
6324 int hl;
6325 int c;
6326
6327 if (wp->w_vsep_width)
6328 {
6329 /* draw the vertical separator right of this window */
6330 c = fillchar_vsep(&hl);
6331 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6332 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6333 c, ' ', hl);
6334 }
6335}
6336#endif
6337
6338#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006339static int status_match_len(expand_T *xp, char_u *s);
6340static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341
6342/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006343 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 */
6345 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006346status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347{
6348 int len = 0;
6349
6350#ifdef FEAT_MENU
6351 int emenu = (xp->xp_context == EXPAND_MENUS
6352 || xp->xp_context == EXPAND_MENUNAMES);
6353
6354 /* Check for menu separators - replace with '|'. */
6355 if (emenu && menu_is_separator(s))
6356 return 1;
6357#endif
6358
6359 while (*s != NUL)
6360 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006361 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006362 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006363 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 }
6365
6366 return len;
6367}
6368
6369/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006370 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006371 * These are backslashes used for escaping. Do show backslashes in help tags.
6372 */
6373 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006374skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006375{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006376 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006377#ifdef FEAT_MENU
6378 || ((xp->xp_context == EXPAND_MENUS
6379 || xp->xp_context == EXPAND_MENUNAMES)
6380 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6381#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006382 )
6383 {
6384#ifndef BACKSLASH_IN_FILENAME
6385 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6386 return 2;
6387#endif
6388 return 1;
6389 }
6390 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006391}
6392
6393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 * Show wildchar matches in the status line.
6395 * Show at least the "match" item.
6396 * We start at item 'first_match' in the list and show all matches that fit.
6397 *
6398 * If inversion is possible we use it. Else '=' characters are used.
6399 */
6400 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006401win_redr_status_matches(
6402 expand_T *xp,
6403 int num_matches,
6404 char_u **matches, /* list of matches */
6405 int match,
6406 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407{
6408#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6409 int row;
6410 char_u *buf;
6411 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006412 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 int fillchar;
6414 int attr;
6415 int i;
6416 int highlight = TRUE;
6417 char_u *selstart = NULL;
6418 int selstart_col = 0;
6419 char_u *selend = NULL;
6420 static int first_match = 0;
6421 int add_left = FALSE;
6422 char_u *s;
6423#ifdef FEAT_MENU
6424 int emenu;
6425#endif
6426#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6427 int l;
6428#endif
6429
6430 if (matches == NULL) /* interrupted completion? */
6431 return;
6432
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006433#ifdef FEAT_MBYTE
6434 if (has_mbyte)
6435 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6436 else
6437#endif
6438 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 if (buf == NULL)
6440 return;
6441
6442 if (match == -1) /* don't show match but original text */
6443 {
6444 match = 0;
6445 highlight = FALSE;
6446 }
6447 /* count 1 for the ending ">" */
6448 clen = status_match_len(xp, L_MATCH(match)) + 3;
6449 if (match == 0)
6450 first_match = 0;
6451 else if (match < first_match)
6452 {
6453 /* jumping left, as far as we can go */
6454 first_match = match;
6455 add_left = TRUE;
6456 }
6457 else
6458 {
6459 /* check if match fits on the screen */
6460 for (i = first_match; i < match; ++i)
6461 clen += status_match_len(xp, L_MATCH(i)) + 2;
6462 if (first_match > 0)
6463 clen += 2;
6464 /* jumping right, put match at the left */
6465 if ((long)clen > Columns)
6466 {
6467 first_match = match;
6468 /* if showing the last match, we can add some on the left */
6469 clen = 2;
6470 for (i = match; i < num_matches; ++i)
6471 {
6472 clen += status_match_len(xp, L_MATCH(i)) + 2;
6473 if ((long)clen >= Columns)
6474 break;
6475 }
6476 if (i == num_matches)
6477 add_left = TRUE;
6478 }
6479 }
6480 if (add_left)
6481 while (first_match > 0)
6482 {
6483 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6484 if ((long)clen >= Columns)
6485 break;
6486 --first_match;
6487 }
6488
6489 fillchar = fillchar_status(&attr, TRUE);
6490
6491 if (first_match == 0)
6492 {
6493 *buf = NUL;
6494 len = 0;
6495 }
6496 else
6497 {
6498 STRCPY(buf, "< ");
6499 len = 2;
6500 }
6501 clen = len;
6502
6503 i = first_match;
6504 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6505 {
6506 if (i == match)
6507 {
6508 selstart = buf + len;
6509 selstart_col = clen;
6510 }
6511
6512 s = L_MATCH(i);
6513 /* Check for menu separators - replace with '|' */
6514#ifdef FEAT_MENU
6515 emenu = (xp->xp_context == EXPAND_MENUS
6516 || xp->xp_context == EXPAND_MENUNAMES);
6517 if (emenu && menu_is_separator(s))
6518 {
6519 STRCPY(buf + len, transchar('|'));
6520 l = (int)STRLEN(buf + len);
6521 len += l;
6522 clen += l;
6523 }
6524 else
6525#endif
6526 for ( ; *s != NUL; ++s)
6527 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006528 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 clen += ptr2cells(s);
6530#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006531 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 {
6533 STRNCPY(buf + len, s, l);
6534 s += l - 1;
6535 len += l;
6536 }
6537 else
6538#endif
6539 {
6540 STRCPY(buf + len, transchar_byte(*s));
6541 len += (int)STRLEN(buf + len);
6542 }
6543 }
6544 if (i == match)
6545 selend = buf + len;
6546
6547 *(buf + len++) = ' ';
6548 *(buf + len++) = ' ';
6549 clen += 2;
6550 if (++i == num_matches)
6551 break;
6552 }
6553
6554 if (i != num_matches)
6555 {
6556 *(buf + len++) = '>';
6557 ++clen;
6558 }
6559
6560 buf[len] = NUL;
6561
6562 row = cmdline_row - 1;
6563 if (row >= 0)
6564 {
6565 if (wild_menu_showing == 0)
6566 {
6567 if (msg_scrolled > 0)
6568 {
6569 /* Put the wildmenu just above the command line. If there is
6570 * no room, scroll the screen one line up. */
6571 if (cmdline_row == Rows - 1)
6572 {
6573 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6574 ++msg_scrolled;
6575 }
6576 else
6577 {
6578 ++cmdline_row;
6579 ++row;
6580 }
6581 wild_menu_showing = WM_SCROLLED;
6582 }
6583 else
6584 {
6585 /* Create status line if needed by setting 'laststatus' to 2.
6586 * Set 'winminheight' to zero to avoid that the window is
6587 * resized. */
6588 if (lastwin->w_status_height == 0)
6589 {
6590 save_p_ls = p_ls;
6591 save_p_wmh = p_wmh;
6592 p_ls = 2;
6593 p_wmh = 0;
6594 last_status(FALSE);
6595 }
6596 wild_menu_showing = WM_SHOWN;
6597 }
6598 }
6599
6600 screen_puts(buf, row, 0, attr);
6601 if (selstart != NULL && highlight)
6602 {
6603 *selend = NUL;
6604 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6605 }
6606
6607 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6608 }
6609
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006610#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 win_redraw_last_status(topframe);
6612#else
6613 lastwin->w_redr_status = TRUE;
6614#endif
6615 vim_free(buf);
6616}
6617#endif
6618
6619#if defined(FEAT_WINDOWS) || defined(PROTO)
6620/*
6621 * Redraw the status line of window wp.
6622 *
6623 * If inversion is possible we use it. Else '=' characters are used.
6624 */
6625 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006626win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
6628 int row;
6629 char_u *p;
6630 int len;
6631 int fillchar;
6632 int attr;
6633 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006634 static int busy = FALSE;
6635
6636 /* It's possible to get here recursively when 'statusline' (indirectly)
6637 * invokes ":redrawstatus". Simply ignore the call then. */
6638 if (busy)
6639 return;
6640 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641
6642 wp->w_redr_status = FALSE;
6643 if (wp->w_status_height == 0)
6644 {
6645 /* no status line, can only be last window */
6646 redraw_cmdline = TRUE;
6647 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006648 else if (!redrawing()
6649#ifdef FEAT_INS_EXPAND
6650 /* don't update status line when popup menu is visible and may be
6651 * drawn over it */
6652 || pum_visible()
6653#endif
6654 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
6656 /* Don't redraw right now, do it later. */
6657 wp->w_redr_status = TRUE;
6658 }
6659#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006660 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 {
6662 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006663 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 }
6665#endif
6666 else
6667 {
6668 fillchar = fillchar_status(&attr, wp == curwin);
6669
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006670 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 p = NameBuff;
6672 len = (int)STRLEN(p);
6673
6674 if (wp->w_buffer->b_help
6675#ifdef FEAT_QUICKFIX
6676 || wp->w_p_pvw
6677#endif
6678 || bufIsChanged(wp->w_buffer)
6679 || wp->w_buffer->b_p_ro)
6680 *(p + len++) = ' ';
6681 if (wp->w_buffer->b_help)
6682 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006683 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 len += (int)STRLEN(p + len);
6685 }
6686#ifdef FEAT_QUICKFIX
6687 if (wp->w_p_pvw)
6688 {
6689 STRCPY(p + len, _("[Preview]"));
6690 len += (int)STRLEN(p + len);
6691 }
6692#endif
6693 if (bufIsChanged(wp->w_buffer))
6694 {
6695 STRCPY(p + len, "[+]");
6696 len += 3;
6697 }
6698 if (wp->w_buffer->b_p_ro)
6699 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006700 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 len += 4;
6702 }
6703
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6705 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6706 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6707 if (this_ru_col <= 1)
6708 {
6709 p = (char_u *)"<"; /* No room for file name! */
6710 len = 1;
6711 }
6712 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713#ifdef FEAT_MBYTE
6714 if (has_mbyte)
6715 {
6716 int clen = 0, i;
6717
6718 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006719 clen = mb_string2cells(p, -1);
6720
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 /* Find first character that will fit.
6722 * Going from start to end is much faster for DBCS. */
6723 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006724 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 clen -= (*mb_ptr2cells)(p + i);
6726 len = clen;
6727 if (i > 0)
6728 {
6729 p = p + i - 1;
6730 *p = '<';
6731 ++len;
6732 }
6733
6734 }
6735 else
6736#endif
6737 if (len > this_ru_col - 1)
6738 {
6739 p += len - (this_ru_col - 1);
6740 *p = '<';
6741 len = this_ru_col - 1;
6742 }
6743
6744 row = W_WINROW(wp) + wp->w_height;
6745 screen_puts(p, row, W_WINCOL(wp), attr);
6746 screen_fill(row, row + 1, len + W_WINCOL(wp),
6747 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6748
6749 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6750 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6751 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6752 - 1 + W_WINCOL(wp)), attr);
6753
6754#ifdef FEAT_CMDL_INFO
6755 win_redr_ruler(wp, TRUE);
6756#endif
6757 }
6758
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 /*
6760 * May need to draw the character below the vertical separator.
6761 */
6762 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6763 {
6764 if (stl_connected(wp))
6765 fillchar = fillchar_status(&attr, wp == curwin);
6766 else
6767 fillchar = fillchar_vsep(&attr);
6768 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6769 attr);
6770 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006771 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772}
6773
Bram Moolenaar238a5642006-02-21 22:12:05 +00006774#ifdef FEAT_STL_OPT
6775/*
6776 * Redraw the status line according to 'statusline' and take care of any
6777 * errors encountered.
6778 */
6779 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006780redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006781{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006782 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006783 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006784
6785 /* When called recursively return. This can happen when the statusline
6786 * contains an expression that triggers a redraw. */
6787 if (entered)
6788 return;
6789 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006790
Bram Moolenaara742e082016-04-05 21:10:38 +02006791 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006792 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006793 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006794 {
6795 /* When there is an error disable the statusline, otherwise the
6796 * display is messed up with errors and a redraw triggers the problem
6797 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006798 set_string_option_direct((char_u *)"statusline", -1,
6799 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006800 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006801 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006802 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006803 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006804}
6805#endif
6806
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807/*
6808 * Return TRUE if the status line of window "wp" is connected to the status
6809 * line of the window right of it. If not, then it's a vertical separator.
6810 * Only call if (wp->w_vsep_width != 0).
6811 */
6812 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006813stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814{
6815 frame_T *fr;
6816
6817 fr = wp->w_frame;
6818 while (fr->fr_parent != NULL)
6819 {
6820 if (fr->fr_parent->fr_layout == FR_COL)
6821 {
6822 if (fr->fr_next != NULL)
6823 break;
6824 }
6825 else
6826 {
6827 if (fr->fr_next != NULL)
6828 return TRUE;
6829 }
6830 fr = fr->fr_parent;
6831 }
6832 return FALSE;
6833}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834
6835#endif /* FEAT_WINDOWS */
6836
6837#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6838/*
6839 * Get the value to show for the language mappings, active 'keymap'.
6840 */
6841 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006842get_keymap_str(
6843 win_T *wp,
6844 char_u *buf, /* buffer for the result */
6845 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846{
6847 char_u *p;
6848
6849 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6850 return FALSE;
6851
6852 {
6853#ifdef FEAT_EVAL
6854 buf_T *old_curbuf = curbuf;
6855 win_T *old_curwin = curwin;
6856 char_u *s;
6857
6858 curbuf = wp->w_buffer;
6859 curwin = wp;
6860 STRCPY(buf, "b:keymap_name"); /* must be writable */
6861 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006862 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 --emsg_skip;
6864 curbuf = old_curbuf;
6865 curwin = old_curwin;
6866 if (p == NULL || *p == NUL)
6867#endif
6868 {
6869#ifdef FEAT_KEYMAP
6870 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6871 p = wp->w_buffer->b_p_keymap;
6872 else
6873#endif
6874 p = (char_u *)"lang";
6875 }
6876 if ((int)(STRLEN(p) + 3) < len)
6877 sprintf((char *)buf, "<%s>", p);
6878 else
6879 buf[0] = NUL;
6880#ifdef FEAT_EVAL
6881 vim_free(s);
6882#endif
6883 }
6884 return buf[0] != NUL;
6885}
6886#endif
6887
6888#if defined(FEAT_STL_OPT) || defined(PROTO)
6889/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006890 * Redraw the status line or ruler of window "wp".
6891 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 */
6893 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006894win_redr_custom(
6895 win_T *wp,
6896 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006898 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 int attr;
6900 int curattr;
6901 int row;
6902 int col = 0;
6903 int maxwidth;
6904 int width;
6905 int n;
6906 int len;
6907 int fillchar;
6908 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006909 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006911 struct stl_hlrec hltab[STL_MAX_ITEM];
6912 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006913 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006914 win_T *ewp;
6915 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916
Bram Moolenaar1d633412013-12-11 15:52:01 +01006917 /* There is a tiny chance that this gets called recursively: When
6918 * redrawing a status line triggers redrawing the ruler or tabline.
6919 * Avoid trouble by not allowing recursion. */
6920 if (entered)
6921 return;
6922 entered = TRUE;
6923
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006925 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006927 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006928 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006929 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006930 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006931 attr = hl_attr(HLF_TPF);
6932 maxwidth = Columns;
6933# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006934 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006937 else
6938 {
6939 row = W_WINROW(wp) + wp->w_height;
6940 fillchar = fillchar_status(&attr, wp == curwin);
6941 maxwidth = W_WIDTH(wp);
6942
6943 if (draw_ruler)
6944 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006945 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006946 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006947 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006948 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006949 if (*++stl == '-')
6950 stl++;
6951 if (atoi((char *)stl))
6952 while (VIM_ISDIGIT(*stl))
6953 stl++;
6954 if (*stl++ != '(')
6955 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006956 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006957#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006958 col = ru_col - (Columns - W_WIDTH(wp));
6959 if (col < (W_WIDTH(wp) + 1) / 2)
6960 col = (W_WIDTH(wp) + 1) / 2;
6961#else
6962 col = ru_col;
6963 if (col > (Columns + 1) / 2)
6964 col = (Columns + 1) / 2;
6965#endif
6966 maxwidth = W_WIDTH(wp) - col;
6967#ifdef FEAT_WINDOWS
6968 if (!wp->w_status_height)
6969#endif
6970 {
6971 row = Rows - 1;
6972 --maxwidth; /* writing in last column may cause scrolling */
6973 fillchar = ' ';
6974 attr = 0;
6975 }
6976
6977# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006978 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006979# endif
6980 }
6981 else
6982 {
6983 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006984 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006985 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006986 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006987# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006988 use_sandbox = was_set_insecurely((char_u *)"statusline",
6989 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006990# endif
6991 }
6992
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006993#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006994 col += W_WINCOL(wp);
6995#endif
6996 }
6997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006999 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
Bram Moolenaar61452852011-02-01 18:01:11 +01007001 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7002 * the cursor away and back. */
7003 ewp = wp == NULL ? curwin : wp;
7004 p_crb_save = ewp->w_p_crb;
7005 ewp->w_p_crb = FALSE;
7006
Bram Moolenaar362f3562009-11-03 16:20:34 +00007007 /* Make a copy, because the statusline may include a function call that
7008 * might change the option value and free the memory. */
7009 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007010 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007011 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007012 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007013 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007014 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007016 /* Make all characters printable. */
7017 p = transstr(buf);
7018 if (p != NULL)
7019 {
7020 vim_strncpy(buf, p, sizeof(buf) - 1);
7021 vim_free(p);
7022 }
7023
7024 /* fill up with "fillchar" */
7025 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007026 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {
7028#ifdef FEAT_MBYTE
7029 len += (*mb_char2bytes)(fillchar, buf + len);
7030#else
7031 buf[len++] = fillchar;
7032#endif
7033 ++width;
7034 }
7035 buf[len] = NUL;
7036
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007037 /*
7038 * Draw each snippet with the specified highlighting.
7039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 curattr = attr;
7041 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007042 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007044 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 screen_puts_len(p, len, row, col, curattr);
7046 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007047 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007049 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007051 else if (hltab[n].userhl < 0)
7052 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007054 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007055 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056#endif
7057 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007058 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 }
7060 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007061
7062 if (wp == NULL)
7063 {
7064 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7065 col = 0;
7066 len = 0;
7067 p = buf;
7068 fillchar = 0;
7069 for (n = 0; tabtab[n].start != NULL; n++)
7070 {
7071 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7072 while (col < len)
7073 TabPageIdxs[col++] = fillchar;
7074 p = tabtab[n].start;
7075 fillchar = tabtab[n].userhl;
7076 }
7077 while (col < Columns)
7078 TabPageIdxs[col++] = fillchar;
7079 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007080
7081theend:
7082 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083}
7084
7085#endif /* FEAT_STL_OPT */
7086
7087/*
7088 * Output a single character directly to the screen and update ScreenLines.
7089 */
7090 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007091screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 char_u buf[MB_MAXBYTES + 1];
7094
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007095#ifdef FEAT_MBYTE
7096 if (has_mbyte)
7097 buf[(*mb_char2bytes)(c, buf)] = NUL;
7098 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007100 {
7101 buf[0] = c;
7102 buf[1] = NUL;
7103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 screen_puts(buf, row, col, attr);
7105}
7106
7107/*
7108 * Get a single character directly from ScreenLines into "bytes[]".
7109 * Also return its attribute in *attrp;
7110 */
7111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007112screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
7114 unsigned off;
7115
7116 /* safety check */
7117 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7118 {
7119 off = LineOffset[row] + col;
7120 *attrp = ScreenAttrs[off];
7121 bytes[0] = ScreenLines[off];
7122 bytes[1] = NUL;
7123
7124#ifdef FEAT_MBYTE
7125 if (enc_utf8 && ScreenLinesUC[off] != 0)
7126 bytes[utfc_char2bytes(off, bytes)] = NUL;
7127 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7128 {
7129 bytes[0] = ScreenLines[off];
7130 bytes[1] = ScreenLines2[off];
7131 bytes[2] = NUL;
7132 }
7133 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7134 {
7135 bytes[1] = ScreenLines[off + 1];
7136 bytes[2] = NUL;
7137 }
7138#endif
7139 }
7140}
7141
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007142#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007143static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007144
7145/*
7146 * Return TRUE if composing characters for screen posn "off" differs from
7147 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007148 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007149 */
7150 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007151screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007152{
7153 int i;
7154
7155 for (i = 0; i < Screen_mco; ++i)
7156 {
7157 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7158 return TRUE;
7159 if (u8cc[i] == 0)
7160 break;
7161 }
7162 return FALSE;
7163}
7164#endif
7165
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166/*
7167 * Put string '*text' on the screen at position 'row' and 'col', with
7168 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7169 * Note: only outputs within one row, message is truncated at screen boundary!
7170 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7171 */
7172 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007173screen_puts(
7174 char_u *text,
7175 int row,
7176 int col,
7177 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
7179 screen_puts_len(text, -1, row, col, attr);
7180}
7181
7182/*
7183 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7184 * a NUL.
7185 */
7186 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007187screen_puts_len(
7188 char_u *text,
7189 int textlen,
7190 int row,
7191 int col,
7192 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
7194 unsigned off;
7195 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007196 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 int c;
7198#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007199 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 int mbyte_blen = 1;
7201 int mbyte_cells = 1;
7202 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007203 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 int clear_next_cell = FALSE;
7205# ifdef FEAT_ARABIC
7206 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007207 int pc, nc, nc1;
7208 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209# endif
7210#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007211#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7212 int force_redraw_this;
7213 int force_redraw_next = FALSE;
7214#endif
7215 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
7217 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7218 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007219 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
Bram Moolenaarc236c162008-07-13 17:41:49 +00007221#ifdef FEAT_MBYTE
7222 /* When drawing over the right halve of a double-wide char clear out the
7223 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007224 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007225# ifdef FEAT_GUI
7226 && !gui.in_use
7227# endif
7228 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007229 {
7230 ScreenLines[off - 1] = ' ';
7231 ScreenAttrs[off - 1] = 0;
7232 if (enc_utf8)
7233 {
7234 ScreenLinesUC[off - 1] = 0;
7235 ScreenLinesC[0][off - 1] = 0;
7236 }
7237 /* redraw the previous cell, make it empty */
7238 screen_char(off - 1, row, col - 1);
7239 /* force the cell at "col" to be redrawn */
7240 force_redraw_next = TRUE;
7241 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007242#endif
7243
Bram Moolenaar367329b2007-08-30 11:53:22 +00007244#ifdef FEAT_MBYTE
7245 max_off = LineOffset[row] + screen_Columns;
7246#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007247 while (col < screen_Columns
7248 && (len < 0 || (int)(ptr - text) < len)
7249 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
7251 c = *ptr;
7252#ifdef FEAT_MBYTE
7253 /* check if this is the first byte of a multibyte */
7254 if (has_mbyte)
7255 {
7256 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007257 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007259 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7261 mbyte_cells = 1;
7262 else if (enc_dbcs != 0)
7263 mbyte_cells = mbyte_blen;
7264 else /* enc_utf8 */
7265 {
7266 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007267 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 (int)((text + len) - ptr));
7269 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007270 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007272# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 /* Non-BMP character: display as ? or fullwidth ?. */
7274 if (u8c >= 0x10000)
7275 {
7276 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7277 if (attr == 0)
7278 attr = hl_attr(HLF_8);
7279 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281# ifdef FEAT_ARABIC
7282 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7283 {
7284 /* Do Arabic shaping. */
7285 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7286 {
7287 /* Past end of string to be displayed. */
7288 nc = NUL;
7289 nc1 = NUL;
7290 }
7291 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007292 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007293 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7294 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007295 nc1 = pcc[0];
7296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 pc = prev_c;
7298 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007299 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 }
7301 else
7302 prev_c = u8c;
7303# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007304 if (col + mbyte_cells > screen_Columns)
7305 {
7306 /* Only 1 cell left, but character requires 2 cells:
7307 * display a '>' in the last column to avoid wrapping. */
7308 c = '>';
7309 mbyte_cells = 1;
7310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 }
7312 }
7313#endif
7314
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007315#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7316 force_redraw_this = force_redraw_next;
7317 force_redraw_next = FALSE;
7318#endif
7319
7320 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321#ifdef FEAT_MBYTE
7322 || (mbyte_cells == 2
7323 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7324 || (enc_dbcs == DBCS_JPNU
7325 && c == 0x8e
7326 && ScreenLines2[off] != ptr[1])
7327 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007328 && (ScreenLinesUC[off] !=
7329 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7330 || (ScreenLinesUC[off] != 0
7331 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332#endif
7333 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007334 || exmode_active;
7335
7336 if (need_redraw
7337#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7338 || force_redraw_this
7339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 )
7341 {
7342#if defined(FEAT_GUI) || defined(UNIX)
7343 /* The bold trick makes a single row of pixels appear in the next
7344 * character. When a bold character is removed, the next
7345 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007346 * and for some xterms. */
7347 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348# ifdef FEAT_GUI
7349 gui.in_use
7350# endif
7351# if defined(FEAT_GUI) && defined(UNIX)
7352 ||
7353# endif
7354# ifdef UNIX
7355 term_is_xterm
7356# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007357 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007359 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007361 if (n > HL_ALL)
7362 n = syn_attr2attr(n);
7363 if (n & HL_BOLD)
7364 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 }
7366#endif
7367#ifdef FEAT_MBYTE
7368 /* When at the end of the text and overwriting a two-cell
7369 * character with a one-cell character, need to clear the next
7370 * cell. Also when overwriting the left halve of a two-cell char
7371 * with the right halve of a two-cell char. Do this only once
7372 * (mb_off2cells() may return 2 on the right halve). */
7373 if (clear_next_cell)
7374 clear_next_cell = FALSE;
7375 else if (has_mbyte
7376 && (len < 0 ? ptr[mbyte_blen] == NUL
7377 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007378 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007380 && (*mb_off2cells)(off, max_off) == 1
7381 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 clear_next_cell = TRUE;
7383
7384 /* Make sure we never leave a second byte of a double-byte behind,
7385 * it confuses mb_off2cells(). */
7386 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007387 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007389 && (*mb_off2cells)(off, max_off) == 1
7390 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 ScreenLines[off + mbyte_blen] = 0;
7392#endif
7393 ScreenLines[off] = c;
7394 ScreenAttrs[off] = attr;
7395#ifdef FEAT_MBYTE
7396 if (enc_utf8)
7397 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007398 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 ScreenLinesUC[off] = 0;
7400 else
7401 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007402 int i;
7403
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007405 for (i = 0; i < Screen_mco; ++i)
7406 {
7407 ScreenLinesC[i][off] = u8cc[i];
7408 if (u8cc[i] == 0)
7409 break;
7410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 }
7412 if (mbyte_cells == 2)
7413 {
7414 ScreenLines[off + 1] = 0;
7415 ScreenAttrs[off + 1] = attr;
7416 }
7417 screen_char(off, row, col);
7418 }
7419 else if (mbyte_cells == 2)
7420 {
7421 ScreenLines[off + 1] = ptr[1];
7422 ScreenAttrs[off + 1] = attr;
7423 screen_char_2(off, row, col);
7424 }
7425 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7426 {
7427 ScreenLines2[off] = ptr[1];
7428 screen_char(off, row, col);
7429 }
7430 else
7431#endif
7432 screen_char(off, row, col);
7433 }
7434#ifdef FEAT_MBYTE
7435 if (has_mbyte)
7436 {
7437 off += mbyte_cells;
7438 col += mbyte_cells;
7439 ptr += mbyte_blen;
7440 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007441 {
7442 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007444 len = -1;
7445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 }
7447 else
7448#endif
7449 {
7450 ++off;
7451 ++col;
7452 ++ptr;
7453 }
7454 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007455
7456#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7457 /* If we detected the next character needs to be redrawn, but the text
7458 * doesn't extend up to there, update the character here. */
7459 if (force_redraw_next && col < screen_Columns)
7460 {
7461# ifdef FEAT_MBYTE
7462 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7463 screen_char_2(off, row, col);
7464 else
7465# endif
7466 screen_char(off, row, col);
7467 }
7468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469}
7470
7471#ifdef FEAT_SEARCH_EXTRA
7472/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007473 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 */
7475 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007476start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477{
7478 if (p_hls && !no_hlsearch)
7479 {
7480 last_pat_prog(&search_hl.rm);
7481 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007482# ifdef FEAT_RELTIME
7483 /* Set the time limit to 'redrawtime'. */
7484 profile_setlimit(p_rdt, &search_hl.tm);
7485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 }
7487}
7488
7489/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007490 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 */
7492 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007493end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494{
7495 if (search_hl.rm.regprog != NULL)
7496 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007497 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 search_hl.rm.regprog = NULL;
7499 }
7500}
7501
7502/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007503 * Init for calling prepare_search_hl().
7504 */
7505 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007506init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007507{
7508 matchitem_T *cur;
7509
7510 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7511 * match */
7512 cur = wp->w_match_head;
7513 while (cur != NULL)
7514 {
7515 cur->hl.rm = cur->match;
7516 if (cur->hlg_id == 0)
7517 cur->hl.attr = 0;
7518 else
7519 cur->hl.attr = syn_id2attr(cur->hlg_id);
7520 cur->hl.buf = wp->w_buffer;
7521 cur->hl.lnum = 0;
7522 cur->hl.first_lnum = 0;
7523# ifdef FEAT_RELTIME
7524 /* Set the time limit to 'redrawtime'. */
7525 profile_setlimit(p_rdt, &(cur->hl.tm));
7526# endif
7527 cur = cur->next;
7528 }
7529 search_hl.buf = wp->w_buffer;
7530 search_hl.lnum = 0;
7531 search_hl.first_lnum = 0;
7532 /* time limit is set at the toplevel, for all windows */
7533}
7534
7535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 * Advance to the match in window "wp" line "lnum" or past it.
7537 */
7538 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007539prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007541 matchitem_T *cur; /* points to the match list */
7542 match_T *shl; /* points to search_hl or a match */
7543 int shl_flag; /* flag to indicate whether search_hl
7544 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007545 int pos_inprogress; /* marks that position match search is
7546 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 int n;
7548
7549 /*
7550 * When using a multi-line pattern, start searching at the top
7551 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007552 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007554 cur = wp->w_match_head;
7555 shl_flag = FALSE;
7556 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007558 if (shl_flag == FALSE)
7559 {
7560 shl = &search_hl;
7561 shl_flag = TRUE;
7562 }
7563 else
7564 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 if (shl->rm.regprog != NULL
7566 && shl->lnum == 0
7567 && re_multiline(shl->rm.regprog))
7568 {
7569 if (shl->first_lnum == 0)
7570 {
7571# ifdef FEAT_FOLDING
7572 for (shl->first_lnum = lnum;
7573 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7574 if (hasFoldingWin(wp, shl->first_lnum - 1,
7575 NULL, NULL, TRUE, NULL))
7576 break;
7577# else
7578 shl->first_lnum = wp->w_topline;
7579# endif
7580 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007581 if (cur != NULL)
7582 cur->pos.cur = 0;
7583 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007585 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7586 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007588 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7589 pos_inprogress = cur == NULL || cur->pos.cur == 0
7590 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 if (shl->lnum != 0)
7592 {
7593 shl->first_lnum = shl->lnum
7594 + shl->rm.endpos[0].lnum
7595 - shl->rm.startpos[0].lnum;
7596 n = shl->rm.endpos[0].col;
7597 }
7598 else
7599 {
7600 ++shl->first_lnum;
7601 n = 0;
7602 }
7603 }
7604 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007605 if (shl != &search_hl && cur != NULL)
7606 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 }
7608}
7609
7610/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007611 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 * Uses shl->buf.
7613 * Sets shl->lnum and shl->rm contents.
7614 * Note: Assumes a previous match is always before "lnum", unless
7615 * shl->lnum is zero.
7616 * Careful: Any pointers for buffer lines will become invalid.
7617 */
7618 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007619next_search_hl(
7620 win_T *win,
7621 match_T *shl, /* points to search_hl or a match */
7622 linenr_T lnum,
7623 colnr_T mincol, /* minimal column for a match */
7624 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625{
7626 linenr_T l;
7627 colnr_T matchcol;
7628 long nmatched;
7629
7630 if (shl->lnum != 0)
7631 {
7632 /* Check for three situations:
7633 * 1. If the "lnum" is below a previous match, start a new search.
7634 * 2. If the previous match includes "mincol", use it.
7635 * 3. Continue after the previous match.
7636 */
7637 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7638 if (lnum > l)
7639 shl->lnum = 0;
7640 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7641 return;
7642 }
7643
7644 /*
7645 * Repeat searching for a match until one is found that includes "mincol"
7646 * or none is found in this line.
7647 */
7648 called_emsg = FALSE;
7649 for (;;)
7650 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007651#ifdef FEAT_RELTIME
7652 /* Stop searching after passing the time limit. */
7653 if (profile_passed_limit(&(shl->tm)))
7654 {
7655 shl->lnum = 0; /* no match found in time */
7656 break;
7657 }
7658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 /* Three situations:
7660 * 1. No useful previous match: search from start of line.
7661 * 2. Not Vi compatible or empty match: continue at next character.
7662 * Break the loop if this is beyond the end of the line.
7663 * 3. Vi compatible searching: continue at end of previous match.
7664 */
7665 if (shl->lnum == 0)
7666 matchcol = 0;
7667 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7668 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007669 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007671 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007672
7673 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007674 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007675 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007677 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 shl->lnum = 0;
7679 break;
7680 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007681#ifdef FEAT_MBYTE
7682 if (has_mbyte)
7683 matchcol += mb_ptr2len(ml);
7684 else
7685#endif
7686 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 }
7688 else
7689 matchcol = shl->rm.endpos[0].col;
7690
7691 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007692 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007694 /* Remember whether shl->rm is using a copy of the regprog in
7695 * cur->match. */
7696 int regprog_is_copy = (shl != &search_hl && cur != NULL
7697 && shl == &cur->hl
7698 && cur->match.regprog == cur->hl.rm.regprog);
7699
Bram Moolenaarb3414592014-06-17 17:48:32 +02007700 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7701 matchcol,
7702#ifdef FEAT_RELTIME
7703 &(shl->tm)
7704#else
7705 NULL
7706#endif
7707 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007708 /* Copy the regprog, in case it got freed and recompiled. */
7709 if (regprog_is_copy)
7710 cur->match.regprog = cur->hl.rm.regprog;
7711
Bram Moolenaarb3414592014-06-17 17:48:32 +02007712 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007713 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007714 /* Error while handling regexp: stop using this regexp. */
7715 if (shl == &search_hl)
7716 {
7717 /* don't free regprog in the match list, it's a copy */
7718 vim_regfree(shl->rm.regprog);
7719 SET_NO_HLSEARCH(TRUE);
7720 }
7721 shl->rm.regprog = NULL;
7722 shl->lnum = 0;
7723 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7724 message */
7725 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007726 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007727 }
7728 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007729 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007730 else
7731 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 if (nmatched == 0)
7733 {
7734 shl->lnum = 0; /* no match found */
7735 break;
7736 }
7737 if (shl->rm.startpos[0].lnum > 0
7738 || shl->rm.startpos[0].col >= mincol
7739 || nmatched > 1
7740 || shl->rm.endpos[0].col > mincol)
7741 {
7742 shl->lnum += shl->rm.startpos[0].lnum;
7743 break; /* useful match found */
7744 }
7745 }
7746}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747
Bram Moolenaarb3414592014-06-17 17:48:32 +02007748 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007749next_search_hl_pos(
7750 match_T *shl, /* points to a match */
7751 linenr_T lnum,
7752 posmatch_T *posmatch, /* match positions */
7753 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007754{
7755 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007756 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007757
7758 shl->lnum = 0;
7759 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7760 {
7761 if (posmatch->pos[i].lnum == 0)
7762 break;
7763 if (posmatch->pos[i].col < mincol)
7764 continue;
7765 if (posmatch->pos[i].lnum == lnum)
7766 {
7767 if (shl->lnum == lnum)
7768 {
7769 /* partially sort positions by column numbers
7770 * on the same line */
7771 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7772 {
7773 llpos_T tmp = posmatch->pos[i];
7774
7775 posmatch->pos[i] = posmatch->pos[bot];
7776 posmatch->pos[bot] = tmp;
7777 }
7778 }
7779 else
7780 {
7781 bot = i;
7782 shl->lnum = lnum;
7783 }
7784 }
7785 }
7786 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007787 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007788 {
7789 colnr_T start = posmatch->pos[bot].col == 0
7790 ? 0 : posmatch->pos[bot].col - 1;
7791 colnr_T end = posmatch->pos[bot].col == 0
7792 ? MAXCOL : start + posmatch->pos[bot].len;
7793
7794 shl->rm.startpos[0].lnum = 0;
7795 shl->rm.startpos[0].col = start;
7796 shl->rm.endpos[0].lnum = 0;
7797 shl->rm.endpos[0].col = end;
7798 posmatch->cur = bot + 1;
7799 return TRUE;
7800 }
7801 return FALSE;
7802}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007803#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007806screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808 attrentry_T *aep = NULL;
7809
7810 screen_attr = attr;
7811 if (full_screen
7812#ifdef WIN3264
7813 && termcap_active
7814#endif
7815 )
7816 {
7817#ifdef FEAT_GUI
7818 if (gui.in_use)
7819 {
7820 char buf[20];
7821
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007822 /* The GUI handles this internally. */
7823 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 OUT_STR(buf);
7825 }
7826 else
7827#endif
7828 {
7829 if (attr > HL_ALL) /* special HL attr. */
7830 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007831 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 aep = syn_cterm_attr2entry(attr);
7833 else
7834 aep = syn_term_attr2entry(attr);
7835 if (aep == NULL) /* did ":syntax clear" */
7836 attr = 0;
7837 else
7838 attr = aep->ae_attr;
7839 }
7840 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7841 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007842 else if (aep != NULL && cterm_normal_fg_bold &&
7843#ifdef FEAT_TERMTRUECOLOR
7844 (p_guicolors ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02007845 (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007846#endif
7847 (t_colors > 1 && aep->ae_u.cterm.fg_color)
7848#ifdef FEAT_TERMTRUECOLOR
7849 )
7850#endif
7851 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007852 /* If the Normal FG color has BOLD attribute and the new HL
7853 * has a FG color defined, clear BOLD. */
7854 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7856 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007857 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7858 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 out_str(T_US);
7860 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7861 out_str(T_CZH);
7862 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7863 out_str(T_MR);
7864
7865 /*
7866 * Output the color or start string after bold etc., in case the
7867 * bold etc. override the color setting.
7868 */
7869 if (aep != NULL)
7870 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007871#ifdef FEAT_TERMTRUECOLOR
7872 if (p_guicolors)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {
Bram Moolenaar902647d2016-04-22 11:49:06 +02007874 if (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007875 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar902647d2016-04-22 11:49:06 +02007876 if (aep->ae_u.cterm.bg_rgb != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007877 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 }
7879 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007882 if (t_colors > 1)
7883 {
7884 if (aep->ae_u.cterm.fg_color)
7885 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7886 if (aep->ae_u.cterm.bg_color)
7887 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7888 }
7889 else
7890 {
7891 if (aep->ae_u.term.start != NULL)
7892 out_str(aep->ae_u.term.start);
7893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 }
7895 }
7896 }
7897 }
7898}
7899
7900 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007901screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
7903 int do_ME = FALSE; /* output T_ME code */
7904
7905 if (screen_attr != 0
7906#ifdef WIN3264
7907 && termcap_active
7908#endif
7909 )
7910 {
7911#ifdef FEAT_GUI
7912 if (gui.in_use)
7913 {
7914 char buf[20];
7915
7916 /* use internal GUI code */
7917 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7918 OUT_STR(buf);
7919 }
7920 else
7921#endif
7922 {
7923 if (screen_attr > HL_ALL) /* special HL attr. */
7924 {
7925 attrentry_T *aep;
7926
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007927 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {
7929 /*
7930 * Assume that t_me restores the original colors!
7931 */
7932 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007933 if (aep != NULL &&
7934#ifdef FEAT_TERMTRUECOLOR
7935 (p_guicolors ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02007936 (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR ||
7937 aep->ae_u.cterm.bg_rgb != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007938#endif
7939 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
7940#ifdef FEAT_TERMTRUECOLOR
7941 )
7942#endif
7943 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 do_ME = TRUE;
7945 }
7946 else
7947 {
7948 aep = syn_term_attr2entry(screen_attr);
7949 if (aep != NULL && aep->ae_u.term.stop != NULL)
7950 {
7951 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7952 do_ME = TRUE;
7953 else
7954 out_str(aep->ae_u.term.stop);
7955 }
7956 }
7957 if (aep == NULL) /* did ":syntax clear" */
7958 screen_attr = 0;
7959 else
7960 screen_attr = aep->ae_attr;
7961 }
7962
7963 /*
7964 * Often all ending-codes are equal to T_ME. Avoid outputting the
7965 * same sequence several times.
7966 */
7967 if (screen_attr & HL_STANDOUT)
7968 {
7969 if (STRCMP(T_SE, T_ME) == 0)
7970 do_ME = TRUE;
7971 else
7972 out_str(T_SE);
7973 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007974 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {
7976 if (STRCMP(T_UE, T_ME) == 0)
7977 do_ME = TRUE;
7978 else
7979 out_str(T_UE);
7980 }
7981 if (screen_attr & HL_ITALIC)
7982 {
7983 if (STRCMP(T_CZR, T_ME) == 0)
7984 do_ME = TRUE;
7985 else
7986 out_str(T_CZR);
7987 }
7988 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7989 out_str(T_ME);
7990
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007991#ifdef FEAT_TERMTRUECOLOR
7992 if (p_guicolors)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {
Bram Moolenaar902647d2016-04-22 11:49:06 +02007994 if (cterm_normal_fg_gui_color != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007995 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar902647d2016-04-22 11:49:06 +02007996 if (cterm_normal_bg_gui_color != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007997 term_bg_rgb_color(cterm_normal_bg_gui_color);
7998 }
7999 else
8000#endif
8001 {
8002 if (t_colors > 1)
8003 {
8004 /* set Normal cterm colors */
8005 if (cterm_normal_fg_color != 0)
8006 term_fg_color(cterm_normal_fg_color - 1);
8007 if (cterm_normal_bg_color != 0)
8008 term_bg_color(cterm_normal_bg_color - 1);
8009 if (cterm_normal_fg_bold)
8010 out_str(T_MD);
8011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 }
8013 }
8014 }
8015 screen_attr = 0;
8016}
8017
8018/*
8019 * Reset the colors for a cterm. Used when leaving Vim.
8020 * The machine specific code may override this again.
8021 */
8022 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008023reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008025 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 {
8027 /* set Normal cterm colors */
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008028#ifdef FEAT_TERMTRUECOLOR
8029 if (p_guicolors ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02008030 (cterm_normal_fg_gui_color != (long_u)INVALCOLOR
8031 || cterm_normal_bg_gui_color != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008032 (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
8033#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {
8037 out_str(T_OP);
8038 screen_attr = -1;
8039 }
8040 if (cterm_normal_fg_bold)
8041 {
8042 out_str(T_ME);
8043 screen_attr = -1;
8044 }
8045 }
8046}
8047
8048/*
8049 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8050 * using the attributes from ScreenAttrs["off"].
8051 */
8052 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008053screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
8055 int attr;
8056
8057 /* Check for illegal values, just in case (could happen just after
8058 * resizing). */
8059 if (row >= screen_Rows || col >= screen_Columns)
8060 return;
8061
Bram Moolenaar494838a2015-02-10 19:20:37 +01008062 /* Outputting a character in the last cell on the screen may scroll the
8063 * screen up. Only do it when the "xn" termcap property is set, otherwise
8064 * mark the character invalid (update it when scrolled up). */
8065 if (*T_XN == NUL
8066 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067#ifdef FEAT_RIGHTLEFT
8068 /* account for first command-line character in rightleft mode */
8069 && !cmdmsg_rl
8070#endif
8071 )
8072 {
8073 ScreenAttrs[off] = (sattr_T)-1;
8074 return;
8075 }
8076
8077 /*
8078 * Stop highlighting first, so it's easier to move the cursor.
8079 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008080#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 if (screen_char_attr != 0)
8082 attr = screen_char_attr;
8083 else
8084#endif
8085 attr = ScreenAttrs[off];
8086 if (screen_attr != attr)
8087 screen_stop_highlight();
8088
8089 windgoto(row, col);
8090
8091 if (screen_attr != attr)
8092 screen_start_highlight(attr);
8093
8094#ifdef FEAT_MBYTE
8095 if (enc_utf8 && ScreenLinesUC[off] != 0)
8096 {
8097 char_u buf[MB_MAXBYTES + 1];
8098
8099 /* Convert UTF-8 character to bytes and write it. */
8100
8101 buf[utfc_char2bytes(off, buf)] = NUL;
8102
8103 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008104 if (utf_ambiguous_width(ScreenLinesUC[off]))
8105 screen_cur_col = 9999;
8106 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 ++screen_cur_col;
8108 }
8109 else
8110#endif
8111 {
8112#ifdef FEAT_MBYTE
8113 out_flush_check();
8114#endif
8115 out_char(ScreenLines[off]);
8116#ifdef FEAT_MBYTE
8117 /* double-byte character in single-width cell */
8118 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8119 out_char(ScreenLines2[off]);
8120#endif
8121 }
8122
8123 screen_cur_col++;
8124}
8125
8126#ifdef FEAT_MBYTE
8127
8128/*
8129 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8130 * on the screen at position 'row' and 'col'.
8131 * The attributes of the first byte is used for all. This is required to
8132 * output the two bytes of a double-byte character with nothing in between.
8133 */
8134 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008135screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136{
8137 /* Check for illegal values (could be wrong when screen was resized). */
8138 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8139 return;
8140
8141 /* Outputting the last character on the screen may scrollup the screen.
8142 * Don't to it! Mark the character invalid (update it when scrolled up) */
8143 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8144 {
8145 ScreenAttrs[off] = (sattr_T)-1;
8146 return;
8147 }
8148
8149 /* Output the first byte normally (positions the cursor), then write the
8150 * second byte directly. */
8151 screen_char(off, row, col);
8152 out_char(ScreenLines[off + 1]);
8153 ++screen_cur_col;
8154}
8155#endif
8156
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008157#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158/*
8159 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8160 * This uses the contents of ScreenLines[] and doesn't change it.
8161 */
8162 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008163screen_draw_rectangle(
8164 int row,
8165 int col,
8166 int height,
8167 int width,
8168 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169{
8170 int r, c;
8171 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008172#ifdef FEAT_MBYTE
8173 int max_off;
8174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008176 /* Can't use ScreenLines unless initialized */
8177 if (ScreenLines == NULL)
8178 return;
8179
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 if (invert)
8181 screen_char_attr = HL_INVERSE;
8182 for (r = row; r < row + height; ++r)
8183 {
8184 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008185#ifdef FEAT_MBYTE
8186 max_off = off + screen_Columns;
8187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 for (c = col; c < col + width; ++c)
8189 {
8190#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008191 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {
8193 screen_char_2(off + c, r, c);
8194 ++c;
8195 }
8196 else
8197#endif
8198 {
8199 screen_char(off + c, r, c);
8200#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008201 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 ++c;
8203#endif
8204 }
8205 }
8206 }
8207 screen_char_attr = 0;
8208}
8209#endif
8210
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008211#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212/*
8213 * Redraw the characters for a vertically split window.
8214 */
8215 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008216redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217{
8218 int col;
8219 int width;
8220
8221# ifdef FEAT_CLIPBOARD
8222 clip_may_clear_selection(row, end - 1);
8223# endif
8224
8225 if (wp == NULL)
8226 {
8227 col = 0;
8228 width = Columns;
8229 }
8230 else
8231 {
8232 col = wp->w_wincol;
8233 width = wp->w_width;
8234 }
8235 screen_draw_rectangle(row, col, end - row, width, FALSE);
8236}
8237#endif
8238
8239/*
8240 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8241 * with character 'c1' in first column followed by 'c2' in the other columns.
8242 * Use attributes 'attr'.
8243 */
8244 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008245screen_fill(
8246 int start_row,
8247 int end_row,
8248 int start_col,
8249 int end_col,
8250 int c1,
8251 int c2,
8252 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253{
8254 int row;
8255 int col;
8256 int off;
8257 int end_off;
8258 int did_delete;
8259 int c;
8260 int norm_term;
8261#if defined(FEAT_GUI) || defined(UNIX)
8262 int force_next = FALSE;
8263#endif
8264
8265 if (end_row > screen_Rows) /* safety check */
8266 end_row = screen_Rows;
8267 if (end_col > screen_Columns) /* safety check */
8268 end_col = screen_Columns;
8269 if (ScreenLines == NULL
8270 || start_row >= end_row
8271 || start_col >= end_col) /* nothing to do */
8272 return;
8273
8274 /* it's a "normal" terminal when not in a GUI or cterm */
8275 norm_term = (
8276#ifdef FEAT_GUI
8277 !gui.in_use &&
8278#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008279 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 for (row = start_row; row < end_row; ++row)
8281 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008282#ifdef FEAT_MBYTE
8283 if (has_mbyte
8284# ifdef FEAT_GUI
8285 && !gui.in_use
8286# endif
8287 )
8288 {
8289 /* When drawing over the right halve of a double-wide char clear
8290 * out the left halve. When drawing over the left halve of a
8291 * double wide-char clear out the right halve. Only needed in a
8292 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008293 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008294 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008295 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008296 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008297 }
8298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 /*
8300 * Try to use delete-line termcap code, when no attributes or in a
8301 * "normal" terminal, where a bold/italic space is just a
8302 * space.
8303 */
8304 did_delete = FALSE;
8305 if (c2 == ' '
8306 && end_col == Columns
8307 && can_clear(T_CE)
8308 && (attr == 0
8309 || (norm_term
8310 && attr <= HL_ALL
8311 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8312 {
8313 /*
8314 * check if we really need to clear something
8315 */
8316 col = start_col;
8317 if (c1 != ' ') /* don't clear first char */
8318 ++col;
8319
8320 off = LineOffset[row] + col;
8321 end_off = LineOffset[row] + end_col;
8322
8323 /* skip blanks (used often, keep it fast!) */
8324#ifdef FEAT_MBYTE
8325 if (enc_utf8)
8326 while (off < end_off && ScreenLines[off] == ' '
8327 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8328 ++off;
8329 else
8330#endif
8331 while (off < end_off && ScreenLines[off] == ' '
8332 && ScreenAttrs[off] == 0)
8333 ++off;
8334 if (off < end_off) /* something to be cleared */
8335 {
8336 col = off - LineOffset[row];
8337 screen_stop_highlight();
8338 term_windgoto(row, col);/* clear rest of this screen line */
8339 out_str(T_CE);
8340 screen_start(); /* don't know where cursor is now */
8341 col = end_col - col;
8342 while (col--) /* clear chars in ScreenLines */
8343 {
8344 ScreenLines[off] = ' ';
8345#ifdef FEAT_MBYTE
8346 if (enc_utf8)
8347 ScreenLinesUC[off] = 0;
8348#endif
8349 ScreenAttrs[off] = 0;
8350 ++off;
8351 }
8352 }
8353 did_delete = TRUE; /* the chars are cleared now */
8354 }
8355
8356 off = LineOffset[row] + start_col;
8357 c = c1;
8358 for (col = start_col; col < end_col; ++col)
8359 {
8360 if (ScreenLines[off] != c
8361#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008362 || (enc_utf8 && (int)ScreenLinesUC[off]
8363 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364#endif
8365 || ScreenAttrs[off] != attr
8366#if defined(FEAT_GUI) || defined(UNIX)
8367 || force_next
8368#endif
8369 )
8370 {
8371#if defined(FEAT_GUI) || defined(UNIX)
8372 /* The bold trick may make a single row of pixels appear in
8373 * the next character. When a bold character is removed, the
8374 * next character should be redrawn too. This happens for our
8375 * own GUI and for some xterms. */
8376 if (
8377# ifdef FEAT_GUI
8378 gui.in_use
8379# endif
8380# if defined(FEAT_GUI) && defined(UNIX)
8381 ||
8382# endif
8383# ifdef UNIX
8384 term_is_xterm
8385# endif
8386 )
8387 {
8388 if (ScreenLines[off] != ' '
8389 && (ScreenAttrs[off] > HL_ALL
8390 || ScreenAttrs[off] & HL_BOLD))
8391 force_next = TRUE;
8392 else
8393 force_next = FALSE;
8394 }
8395#endif
8396 ScreenLines[off] = c;
8397#ifdef FEAT_MBYTE
8398 if (enc_utf8)
8399 {
8400 if (c >= 0x80)
8401 {
8402 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008403 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 }
8405 else
8406 ScreenLinesUC[off] = 0;
8407 }
8408#endif
8409 ScreenAttrs[off] = attr;
8410 if (!did_delete || c != ' ')
8411 screen_char(off, row, col);
8412 }
8413 ++off;
8414 if (col == start_col)
8415 {
8416 if (did_delete)
8417 break;
8418 c = c2;
8419 }
8420 }
8421 if (end_col == Columns)
8422 LineWraps[row] = FALSE;
8423 if (row == Rows - 1) /* overwritten the command line */
8424 {
8425 redraw_cmdline = TRUE;
8426 if (c1 == ' ' && c2 == ' ')
8427 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008428 if (start_col == 0)
8429 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 }
8431 }
8432}
8433
8434/*
8435 * Check if there should be a delay. Used before clearing or redrawing the
8436 * screen or the command line.
8437 */
8438 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008439check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440{
8441 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8442 && !did_wait_return
8443 && emsg_silent == 0)
8444 {
8445 out_flush();
8446 ui_delay(1000L, TRUE);
8447 emsg_on_display = FALSE;
8448 if (check_msg_scroll)
8449 msg_scroll = FALSE;
8450 }
8451}
8452
8453/*
8454 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008455 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 * Returns TRUE if there is a valid screen to write to.
8457 * Returns FALSE when starting up and screen not initialized yet.
8458 */
8459 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008460screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008462 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 return (ScreenLines != NULL);
8464}
8465
8466/*
8467 * Resize the shell to Rows and Columns.
8468 * Allocate ScreenLines[] and associated items.
8469 *
8470 * There may be some time between setting Rows and Columns and (re)allocating
8471 * ScreenLines[]. This happens when starting up and when (manually) changing
8472 * the shell size. Always use screen_Rows and screen_Columns to access items
8473 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8474 * final size of the shell is needed.
8475 */
8476 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008477screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478{
8479 int new_row, old_row;
8480#ifdef FEAT_GUI
8481 int old_Rows;
8482#endif
8483 win_T *wp;
8484 int outofmem = FALSE;
8485 int len;
8486 schar_T *new_ScreenLines;
8487#ifdef FEAT_MBYTE
8488 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008489 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008491 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492#endif
8493 sattr_T *new_ScreenAttrs;
8494 unsigned *new_LineOffset;
8495 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008496#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008497 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008498 tabpage_T *tp;
8499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008501 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008502#ifdef FEAT_AUTOCMD
8503 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008505retry:
8506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 /*
8508 * Allocation of the screen buffers is done only when the size changes and
8509 * when Rows and Columns have been set and we have started doing full
8510 * screen stuff.
8511 */
8512 if ((ScreenLines != NULL
8513 && Rows == screen_Rows
8514 && Columns == screen_Columns
8515#ifdef FEAT_MBYTE
8516 && enc_utf8 == (ScreenLinesUC != NULL)
8517 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008518 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519#endif
8520 )
8521 || Rows == 0
8522 || Columns == 0
8523 || (!full_screen && ScreenLines == NULL))
8524 return;
8525
8526 /*
8527 * It's possible that we produce an out-of-memory message below, which
8528 * will cause this function to be called again. To break the loop, just
8529 * return here.
8530 */
8531 if (entered)
8532 return;
8533 entered = TRUE;
8534
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008535 /*
8536 * Note that the window sizes are updated before reallocating the arrays,
8537 * thus we must not redraw here!
8538 */
8539 ++RedrawingDisabled;
8540
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 win_new_shellsize(); /* fit the windows in the new sized shell */
8542
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 comp_col(); /* recompute columns for shown command and ruler */
8544
8545 /*
8546 * We're changing the size of the screen.
8547 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8548 * - Move lines from the old arrays into the new arrays, clear extra
8549 * lines (unless the screen is going to be cleared).
8550 * - Free the old arrays.
8551 *
8552 * If anything fails, make ScreenLines NULL, so we don't do anything!
8553 * Continuing with the old ScreenLines may result in a crash, because the
8554 * size is wrong.
8555 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008556 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008558#ifdef FEAT_AUTOCMD
8559 if (aucmd_win != NULL)
8560 win_free_lsize(aucmd_win);
8561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562
8563 new_ScreenLines = (schar_T *)lalloc((long_u)(
8564 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8565#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008566 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 if (enc_utf8)
8568 {
8569 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8570 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008571 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008572 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8574 }
8575 if (enc_dbcs == DBCS_JPNU)
8576 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8577 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8578#endif
8579 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8580 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8581 new_LineOffset = (unsigned *)lalloc((long_u)(
8582 Rows * sizeof(unsigned)), FALSE);
8583 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008584#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008585 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008588 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 {
8590 if (win_alloc_lines(wp) == FAIL)
8591 {
8592 outofmem = TRUE;
8593#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008594 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595#endif
8596 }
8597 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008598#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008599 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8600 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008601 outofmem = TRUE;
8602#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008603#ifdef FEAT_WINDOWS
8604give_up:
8605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008607#ifdef FEAT_MBYTE
8608 for (i = 0; i < p_mco; ++i)
8609 if (new_ScreenLinesC[i] == NULL)
8610 break;
8611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 if (new_ScreenLines == NULL
8613#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008614 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8616#endif
8617 || new_ScreenAttrs == NULL
8618 || new_LineOffset == NULL
8619 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008620#ifdef FEAT_WINDOWS
8621 || new_TabPageIdxs == NULL
8622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 || outofmem)
8624 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008625 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008626 {
8627 /* guess the size */
8628 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8629
8630 /* Remember we did this to avoid getting outofmem messages over
8631 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008632 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 vim_free(new_ScreenLines);
8635 new_ScreenLines = NULL;
8636#ifdef FEAT_MBYTE
8637 vim_free(new_ScreenLinesUC);
8638 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008639 for (i = 0; i < p_mco; ++i)
8640 {
8641 vim_free(new_ScreenLinesC[i]);
8642 new_ScreenLinesC[i] = NULL;
8643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 vim_free(new_ScreenLines2);
8645 new_ScreenLines2 = NULL;
8646#endif
8647 vim_free(new_ScreenAttrs);
8648 new_ScreenAttrs = NULL;
8649 vim_free(new_LineOffset);
8650 new_LineOffset = NULL;
8651 vim_free(new_LineWraps);
8652 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008653#ifdef FEAT_WINDOWS
8654 vim_free(new_TabPageIdxs);
8655 new_TabPageIdxs = NULL;
8656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 }
8658 else
8659 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008660 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008661
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 for (new_row = 0; new_row < Rows; ++new_row)
8663 {
8664 new_LineOffset[new_row] = new_row * Columns;
8665 new_LineWraps[new_row] = FALSE;
8666
8667 /*
8668 * If the screen is not going to be cleared, copy as much as
8669 * possible from the old screen to the new one and clear the rest
8670 * (used when resizing the window at the "--more--" prompt or when
8671 * executing an external command, for the GUI).
8672 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008673 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 {
8675 (void)vim_memset(new_ScreenLines + new_row * Columns,
8676 ' ', (size_t)Columns * sizeof(schar_T));
8677#ifdef FEAT_MBYTE
8678 if (enc_utf8)
8679 {
8680 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8681 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008682 for (i = 0; i < p_mco; ++i)
8683 (void)vim_memset(new_ScreenLinesC[i]
8684 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 0, (size_t)Columns * sizeof(u8char_T));
8686 }
8687 if (enc_dbcs == DBCS_JPNU)
8688 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8689 0, (size_t)Columns * sizeof(schar_T));
8690#endif
8691 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8692 0, (size_t)Columns * sizeof(sattr_T));
8693 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008694 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 {
8696 if (screen_Columns < Columns)
8697 len = screen_Columns;
8698 else
8699 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008700#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008701 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008702 * may be invalid now. Also when p_mco changes. */
8703 if (!(enc_utf8 && ScreenLinesUC == NULL)
8704 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008705#endif
8706 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8707 ScreenLines + LineOffset[old_row],
8708 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008710 if (enc_utf8 && ScreenLinesUC != NULL
8711 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {
8713 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8714 ScreenLinesUC + LineOffset[old_row],
8715 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008716 for (i = 0; i < p_mco; ++i)
8717 mch_memmove(new_ScreenLinesC[i]
8718 + new_LineOffset[new_row],
8719 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 (size_t)len * sizeof(u8char_T));
8721 }
8722 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8723 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8724 ScreenLines2 + LineOffset[old_row],
8725 (size_t)len * sizeof(schar_T));
8726#endif
8727 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8728 ScreenAttrs + LineOffset[old_row],
8729 (size_t)len * sizeof(sattr_T));
8730 }
8731 }
8732 }
8733 /* Use the last line of the screen for the current line. */
8734 current_ScreenLine = new_ScreenLines + Rows * Columns;
8735 }
8736
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008737 free_screenlines();
8738
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 ScreenLines = new_ScreenLines;
8740#ifdef FEAT_MBYTE
8741 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008742 for (i = 0; i < p_mco; ++i)
8743 ScreenLinesC[i] = new_ScreenLinesC[i];
8744 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 ScreenLines2 = new_ScreenLines2;
8746#endif
8747 ScreenAttrs = new_ScreenAttrs;
8748 LineOffset = new_LineOffset;
8749 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008750#ifdef FEAT_WINDOWS
8751 TabPageIdxs = new_TabPageIdxs;
8752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753
8754 /* It's important that screen_Rows and screen_Columns reflect the actual
8755 * size of ScreenLines[]. Set them before calling anything. */
8756#ifdef FEAT_GUI
8757 old_Rows = screen_Rows;
8758#endif
8759 screen_Rows = Rows;
8760 screen_Columns = Columns;
8761
8762 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008763 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 screenclear2();
8765
8766#ifdef FEAT_GUI
8767 else if (gui.in_use
8768 && !gui.starting
8769 && ScreenLines != NULL
8770 && old_Rows != Rows)
8771 {
8772 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8773 /*
8774 * Adjust the position of the cursor, for when executing an external
8775 * command.
8776 */
8777 if (msg_row >= Rows) /* Rows got smaller */
8778 msg_row = Rows - 1; /* put cursor at last row */
8779 else if (Rows > old_Rows) /* Rows got bigger */
8780 msg_row += Rows - old_Rows; /* put cursor in same place */
8781 if (msg_col >= Columns) /* Columns got smaller */
8782 msg_col = Columns - 1; /* put cursor at last column */
8783 }
8784#endif
8785
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008787 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008788
8789#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008790 /*
8791 * Do not apply autocommands more than 3 times to avoid an endless loop
8792 * in case applying autocommands always changes Rows or Columns.
8793 */
8794 if (starting == 0 && ++retry_count <= 3)
8795 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008796 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008797 /* In rare cases, autocommands may have altered Rows or Columns,
8798 * jump back to check if we need to allocate the screen again. */
8799 goto retry;
8800 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802}
8803
8804 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008805free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008806{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008807#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008808 int i;
8809
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008810 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008811 for (i = 0; i < Screen_mco; ++i)
8812 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008813 vim_free(ScreenLines2);
8814#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008815 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008816 vim_free(ScreenAttrs);
8817 vim_free(LineOffset);
8818 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008819#ifdef FEAT_WINDOWS
8820 vim_free(TabPageIdxs);
8821#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008822}
8823
8824 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008825screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826{
8827 check_for_delay(FALSE);
8828 screenalloc(FALSE); /* allocate screen buffers if size changed */
8829 screenclear2(); /* clear the screen */
8830}
8831
8832 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008833screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
8835 int i;
8836
8837 if (starting == NO_SCREEN || ScreenLines == NULL
8838#ifdef FEAT_GUI
8839 || (gui.in_use && gui.starting)
8840#endif
8841 )
8842 return;
8843
8844#ifdef FEAT_GUI
8845 if (!gui.in_use)
8846#endif
8847 screen_attr = -1; /* force setting the Normal colors */
8848 screen_stop_highlight(); /* don't want highlighting here */
8849
8850#ifdef FEAT_CLIPBOARD
8851 /* disable selection without redrawing it */
8852 clip_scroll_selection(9999);
8853#endif
8854
8855 /* blank out ScreenLines */
8856 for (i = 0; i < Rows; ++i)
8857 {
8858 lineclear(LineOffset[i], (int)Columns);
8859 LineWraps[i] = FALSE;
8860 }
8861
8862 if (can_clear(T_CL))
8863 {
8864 out_str(T_CL); /* clear the display */
8865 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008866 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 }
8868 else
8869 {
8870 /* can't clear the screen, mark all chars with invalid attributes */
8871 for (i = 0; i < Rows; ++i)
8872 lineinvalid(LineOffset[i], (int)Columns);
8873 clear_cmdline = TRUE;
8874 }
8875
8876 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8877
8878 win_rest_invalid(firstwin);
8879 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008880#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008881 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 if (must_redraw == CLEAR) /* no need to clear again */
8884 must_redraw = NOT_VALID;
8885 compute_cmdrow();
8886 msg_row = cmdline_row; /* put cursor on last line for messages */
8887 msg_col = 0;
8888 screen_start(); /* don't know where cursor is now */
8889 msg_scrolled = 0; /* can't scroll back */
8890 msg_didany = FALSE;
8891 msg_didout = FALSE;
8892}
8893
8894/*
8895 * Clear one line in ScreenLines.
8896 */
8897 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008898lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
8900 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8901#ifdef FEAT_MBYTE
8902 if (enc_utf8)
8903 (void)vim_memset(ScreenLinesUC + off, 0,
8904 (size_t)width * sizeof(u8char_T));
8905#endif
8906 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8907}
8908
8909/*
8910 * Mark one line in ScreenLines invalid by setting the attributes to an
8911 * invalid value.
8912 */
8913 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008914lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915{
8916 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8917}
8918
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008919#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920/*
8921 * Copy part of a Screenline for vertically split window "wp".
8922 */
8923 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008924linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926 unsigned off_to = LineOffset[to] + wp->w_wincol;
8927 unsigned off_from = LineOffset[from] + wp->w_wincol;
8928
8929 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8930 wp->w_width * sizeof(schar_T));
8931# ifdef FEAT_MBYTE
8932 if (enc_utf8)
8933 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008934 int i;
8935
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8937 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008938 for (i = 0; i < p_mco; ++i)
8939 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8940 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 }
8942 if (enc_dbcs == DBCS_JPNU)
8943 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8944 wp->w_width * sizeof(schar_T));
8945# endif
8946 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8947 wp->w_width * sizeof(sattr_T));
8948}
8949#endif
8950
8951/*
8952 * Return TRUE if clearing with term string "p" would work.
8953 * It can't work when the string is empty or it won't set the right background.
8954 */
8955 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008956can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957{
8958 return (*p != NUL && (t_colors <= 1
8959#ifdef FEAT_GUI
8960 || gui.in_use
8961#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008962#ifdef FEAT_TERMTRUECOLOR
Bram Moolenaar902647d2016-04-22 11:49:06 +02008963 || (p_guicolors && cterm_normal_bg_gui_color != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8966}
8967
8968/*
8969 * Reset cursor position. Use whenever cursor was moved because of outputting
8970 * something directly to the screen (shell commands) or a terminal control
8971 * code.
8972 */
8973 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008974screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975{
8976 screen_cur_row = screen_cur_col = 9999;
8977}
8978
8979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 * Move the cursor to position "row","col" in the screen.
8981 * This tries to find the most efficient way to move, minimizing the number of
8982 * characters sent to the terminal.
8983 */
8984 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008985windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008987 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 int i;
8989 int plan;
8990 int cost;
8991 int wouldbe_col;
8992 int noinvcurs;
8993 char_u *bs;
8994 int goto_cost;
8995 int attr;
8996
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008997#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8999
9000#define PLAN_LE 1
9001#define PLAN_CR 2
9002#define PLAN_NL 3
9003#define PLAN_WRITE 4
9004 /* Can't use ScreenLines unless initialized */
9005 if (ScreenLines == NULL)
9006 return;
9007
9008 if (col != screen_cur_col || row != screen_cur_row)
9009 {
9010 /* Check for valid position. */
9011 if (row < 0) /* window without text lines? */
9012 row = 0;
9013 if (row >= screen_Rows)
9014 row = screen_Rows - 1;
9015 if (col >= screen_Columns)
9016 col = screen_Columns - 1;
9017
9018 /* check if no cursor movement is allowed in highlight mode */
9019 if (screen_attr && *T_MS == NUL)
9020 noinvcurs = HIGHL_COST;
9021 else
9022 noinvcurs = 0;
9023 goto_cost = GOTO_COST + noinvcurs;
9024
9025 /*
9026 * Plan how to do the positioning:
9027 * 1. Use CR to move it to column 0, same row.
9028 * 2. Use T_LE to move it a few columns to the left.
9029 * 3. Use NL to move a few lines down, column 0.
9030 * 4. Move a few columns to the right with T_ND or by writing chars.
9031 *
9032 * Don't do this if the cursor went beyond the last column, the cursor
9033 * position is unknown then (some terminals wrap, some don't )
9034 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009035 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 * characters to move the cursor to the right.
9037 */
9038 if (row >= screen_cur_row && screen_cur_col < Columns)
9039 {
9040 /*
9041 * If the cursor is in the same row, bigger col, we can use CR
9042 * or T_LE.
9043 */
9044 bs = NULL; /* init for GCC */
9045 attr = screen_attr;
9046 if (row == screen_cur_row && col < screen_cur_col)
9047 {
9048 /* "le" is preferred over "bc", because "bc" is obsolete */
9049 if (*T_LE)
9050 bs = T_LE; /* "cursor left" */
9051 else
9052 bs = T_BC; /* "backspace character (old) */
9053 if (*bs)
9054 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9055 else
9056 cost = 999;
9057 if (col + 1 < cost) /* using CR is less characters */
9058 {
9059 plan = PLAN_CR;
9060 wouldbe_col = 0;
9061 cost = 1; /* CR is just one character */
9062 }
9063 else
9064 {
9065 plan = PLAN_LE;
9066 wouldbe_col = col;
9067 }
9068 if (noinvcurs) /* will stop highlighting */
9069 {
9070 cost += noinvcurs;
9071 attr = 0;
9072 }
9073 }
9074
9075 /*
9076 * If the cursor is above where we want to be, we can use CR LF.
9077 */
9078 else if (row > screen_cur_row)
9079 {
9080 plan = PLAN_NL;
9081 wouldbe_col = 0;
9082 cost = (row - screen_cur_row) * 2; /* CR LF */
9083 if (noinvcurs) /* will stop highlighting */
9084 {
9085 cost += noinvcurs;
9086 attr = 0;
9087 }
9088 }
9089
9090 /*
9091 * If the cursor is in the same row, smaller col, just use write.
9092 */
9093 else
9094 {
9095 plan = PLAN_WRITE;
9096 wouldbe_col = screen_cur_col;
9097 cost = 0;
9098 }
9099
9100 /*
9101 * Check if any characters that need to be written have the
9102 * correct attributes. Also avoid UTF-8 characters.
9103 */
9104 i = col - wouldbe_col;
9105 if (i > 0)
9106 cost += i;
9107 if (cost < goto_cost && i > 0)
9108 {
9109 /*
9110 * Check if the attributes are correct without additionally
9111 * stopping highlighting.
9112 */
9113 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9114 while (i && *p++ == attr)
9115 --i;
9116 if (i != 0)
9117 {
9118 /*
9119 * Try if it works when highlighting is stopped here.
9120 */
9121 if (*--p == 0)
9122 {
9123 cost += noinvcurs;
9124 while (i && *p++ == 0)
9125 --i;
9126 }
9127 if (i != 0)
9128 cost = 999; /* different attributes, don't do it */
9129 }
9130#ifdef FEAT_MBYTE
9131 if (enc_utf8)
9132 {
9133 /* Don't use an UTF-8 char for positioning, it's slow. */
9134 for (i = wouldbe_col; i < col; ++i)
9135 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9136 {
9137 cost = 999;
9138 break;
9139 }
9140 }
9141#endif
9142 }
9143
9144 /*
9145 * We can do it without term_windgoto()!
9146 */
9147 if (cost < goto_cost)
9148 {
9149 if (plan == PLAN_LE)
9150 {
9151 if (noinvcurs)
9152 screen_stop_highlight();
9153 while (screen_cur_col > col)
9154 {
9155 out_str(bs);
9156 --screen_cur_col;
9157 }
9158 }
9159 else if (plan == PLAN_CR)
9160 {
9161 if (noinvcurs)
9162 screen_stop_highlight();
9163 out_char('\r');
9164 screen_cur_col = 0;
9165 }
9166 else if (plan == PLAN_NL)
9167 {
9168 if (noinvcurs)
9169 screen_stop_highlight();
9170 while (screen_cur_row < row)
9171 {
9172 out_char('\n');
9173 ++screen_cur_row;
9174 }
9175 screen_cur_col = 0;
9176 }
9177
9178 i = col - screen_cur_col;
9179 if (i > 0)
9180 {
9181 /*
9182 * Use cursor-right if it's one character only. Avoids
9183 * removing a line of pixels from the last bold char, when
9184 * using the bold trick in the GUI.
9185 */
9186 if (T_ND[0] != NUL && T_ND[1] == NUL)
9187 {
9188 while (i-- > 0)
9189 out_char(*T_ND);
9190 }
9191 else
9192 {
9193 int off;
9194
9195 off = LineOffset[row] + screen_cur_col;
9196 while (i-- > 0)
9197 {
9198 if (ScreenAttrs[off] != screen_attr)
9199 screen_stop_highlight();
9200#ifdef FEAT_MBYTE
9201 out_flush_check();
9202#endif
9203 out_char(ScreenLines[off]);
9204#ifdef FEAT_MBYTE
9205 if (enc_dbcs == DBCS_JPNU
9206 && ScreenLines[off] == 0x8e)
9207 out_char(ScreenLines2[off]);
9208#endif
9209 ++off;
9210 }
9211 }
9212 }
9213 }
9214 }
9215 else
9216 cost = 999;
9217
9218 if (cost >= goto_cost)
9219 {
9220 if (noinvcurs)
9221 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009222 if (row == screen_cur_row && (col > screen_cur_col)
9223 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 term_cursor_right(col - screen_cur_col);
9225 else
9226 term_windgoto(row, col);
9227 }
9228 screen_cur_row = row;
9229 screen_cur_col = col;
9230 }
9231}
9232
9233/*
9234 * Set cursor to its position in the current window.
9235 */
9236 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009237setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238{
9239 if (redrawing())
9240 {
9241 validate_cursor();
9242 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9243 W_WINCOL(curwin) + (
9244#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009245 /* With 'rightleft' set and the cursor on a double-wide
9246 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9248# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009249 (has_mbyte
9250 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9251 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252# endif
9253 1)) :
9254#endif
9255 curwin->w_wcol));
9256 }
9257}
9258
9259
9260/*
9261 * insert 'line_count' lines at 'row' in window 'wp'
9262 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9263 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9264 * scrolling.
9265 * Returns FAIL if the lines are not inserted, OK for success.
9266 */
9267 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009268win_ins_lines(
9269 win_T *wp,
9270 int row,
9271 int line_count,
9272 int invalid,
9273 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274{
9275 int did_delete;
9276 int nextrow;
9277 int lastrow;
9278 int retval;
9279
9280 if (invalid)
9281 wp->w_lines_valid = 0;
9282
9283 if (wp->w_height < 5)
9284 return FAIL;
9285
9286 if (line_count > wp->w_height - row)
9287 line_count = wp->w_height - row;
9288
9289 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9290 if (retval != MAYBE)
9291 return retval;
9292
9293 /*
9294 * If there is a next window or a status line, we first try to delete the
9295 * lines at the bottom to avoid messing what is after the window.
9296 * If this fails and there are following windows, don't do anything to avoid
9297 * messing up those windows, better just redraw.
9298 */
9299 did_delete = FALSE;
9300#ifdef FEAT_WINDOWS
9301 if (wp->w_next != NULL || wp->w_status_height)
9302 {
9303 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9304 line_count, (int)Rows, FALSE, NULL) == OK)
9305 did_delete = TRUE;
9306 else if (wp->w_next)
9307 return FAIL;
9308 }
9309#endif
9310 /*
9311 * if no lines deleted, blank the lines that will end up below the window
9312 */
9313 if (!did_delete)
9314 {
9315#ifdef FEAT_WINDOWS
9316 wp->w_redr_status = TRUE;
9317#endif
9318 redraw_cmdline = TRUE;
9319 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9320 lastrow = nextrow + line_count;
9321 if (lastrow > Rows)
9322 lastrow = Rows;
9323 screen_fill(nextrow - line_count, lastrow - line_count,
9324 W_WINCOL(wp), (int)W_ENDCOL(wp),
9325 ' ', ' ', 0);
9326 }
9327
9328 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9329 == FAIL)
9330 {
9331 /* deletion will have messed up other windows */
9332 if (did_delete)
9333 {
9334#ifdef FEAT_WINDOWS
9335 wp->w_redr_status = TRUE;
9336#endif
9337 win_rest_invalid(W_NEXT(wp));
9338 }
9339 return FAIL;
9340 }
9341
9342 return OK;
9343}
9344
9345/*
9346 * delete "line_count" window lines at "row" in window "wp"
9347 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9348 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9349 * scrolling
9350 * Return OK for success, FAIL if the lines are not deleted.
9351 */
9352 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009353win_del_lines(
9354 win_T *wp,
9355 int row,
9356 int line_count,
9357 int invalid,
9358 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359{
9360 int retval;
9361
9362 if (invalid)
9363 wp->w_lines_valid = 0;
9364
9365 if (line_count > wp->w_height - row)
9366 line_count = wp->w_height - row;
9367
9368 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9369 if (retval != MAYBE)
9370 return retval;
9371
9372 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9373 (int)Rows, FALSE, NULL) == FAIL)
9374 return FAIL;
9375
9376#ifdef FEAT_WINDOWS
9377 /*
9378 * If there are windows or status lines below, try to put them at the
9379 * correct place. If we can't do that, they have to be redrawn.
9380 */
9381 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9382 {
9383 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9384 line_count, (int)Rows, NULL) == FAIL)
9385 {
9386 wp->w_redr_status = TRUE;
9387 win_rest_invalid(wp->w_next);
9388 }
9389 }
9390 /*
9391 * If this is the last window and there is no status line, redraw the
9392 * command line later.
9393 */
9394 else
9395#endif
9396 redraw_cmdline = TRUE;
9397 return OK;
9398}
9399
9400/*
9401 * Common code for win_ins_lines() and win_del_lines().
9402 * Returns OK or FAIL when the work has been done.
9403 * Returns MAYBE when not finished yet.
9404 */
9405 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009406win_do_lines(
9407 win_T *wp,
9408 int row,
9409 int line_count,
9410 int mayclear,
9411 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412{
9413 int retval;
9414
9415 if (!redrawing() || line_count <= 0)
9416 return FAIL;
9417
9418 /* only a few lines left: redraw is faster */
9419 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009420#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 && wp->w_width == Columns
9422#endif
9423 )
9424 {
9425 screenclear(); /* will set wp->w_lines_valid to 0 */
9426 return FAIL;
9427 }
9428
9429 /*
9430 * Delete all remaining lines
9431 */
9432 if (row + line_count >= wp->w_height)
9433 {
9434 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9435 W_WINCOL(wp), (int)W_ENDCOL(wp),
9436 ' ', ' ', 0);
9437 return OK;
9438 }
9439
9440 /*
9441 * when scrolling, the message on the command line should be cleared,
9442 * otherwise it will stay there forever.
9443 */
9444 clear_cmdline = TRUE;
9445
9446 /*
9447 * If the terminal can set a scroll region, use that.
9448 * Always do this in a vertically split window. This will redraw from
9449 * ScreenLines[] when t_CV isn't defined. That's faster than using
9450 * win_line().
9451 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009452 * a character in the lower right corner of the scroll region may cause a
9453 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 */
9455 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009456#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 || W_WIDTH(wp) != Columns
9458#endif
9459 )
9460 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009461#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9463#endif
9464 scroll_region_set(wp, row);
9465 if (del)
9466 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9467 wp->w_height - row, FALSE, wp);
9468 else
9469 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9470 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009471#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9473#endif
9474 scroll_region_reset();
9475 return retval;
9476 }
9477
9478#ifdef FEAT_WINDOWS
9479 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9480 return FAIL;
9481#endif
9482
9483 return MAYBE;
9484}
9485
9486/*
9487 * window 'wp' and everything after it is messed up, mark it for redraw
9488 */
9489 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009490win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491{
9492#ifdef FEAT_WINDOWS
9493 while (wp != NULL)
9494#else
9495 if (wp != NULL)
9496#endif
9497 {
9498 redraw_win_later(wp, NOT_VALID);
9499#ifdef FEAT_WINDOWS
9500 wp->w_redr_status = TRUE;
9501 wp = wp->w_next;
9502#endif
9503 }
9504 redraw_cmdline = TRUE;
9505}
9506
9507/*
9508 * The rest of the routines in this file perform screen manipulations. The
9509 * given operation is performed physically on the screen. The corresponding
9510 * change is also made to the internal screen image. In this way, the editor
9511 * anticipates the effect of editing changes on the appearance of the screen.
9512 * That way, when we call screenupdate a complete redraw isn't usually
9513 * necessary. Another advantage is that we can keep adding code to anticipate
9514 * screen changes, and in the meantime, everything still works.
9515 */
9516
9517/*
9518 * types for inserting or deleting lines
9519 */
9520#define USE_T_CAL 1
9521#define USE_T_CDL 2
9522#define USE_T_AL 3
9523#define USE_T_CE 4
9524#define USE_T_DL 5
9525#define USE_T_SR 6
9526#define USE_NL 7
9527#define USE_T_CD 8
9528#define USE_REDRAW 9
9529
9530/*
9531 * insert lines on the screen and update ScreenLines[]
9532 * 'end' is the line after the scrolled part. Normally it is Rows.
9533 * When scrolling region used 'off' is the offset from the top for the region.
9534 * 'row' and 'end' are relative to the start of the region.
9535 *
9536 * return FAIL for failure, OK for success.
9537 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009538 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009539screen_ins_lines(
9540 int off,
9541 int row,
9542 int line_count,
9543 int end,
9544 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546 int i;
9547 int j;
9548 unsigned temp;
9549 int cursor_row;
9550 int type;
9551 int result_empty;
9552 int can_ce = can_clear(T_CE);
9553
9554 /*
9555 * FAIL if
9556 * - there is no valid screen
9557 * - the screen has to be redrawn completely
9558 * - the line count is less than one
9559 * - the line count is more than 'ttyscroll'
9560 */
9561 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9562 return FAIL;
9563
9564 /*
9565 * There are seven ways to insert lines:
9566 * 0. When in a vertically split window and t_CV isn't set, redraw the
9567 * characters from ScreenLines[].
9568 * 1. Use T_CD (clear to end of display) if it exists and the result of
9569 * the insert is just empty lines
9570 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9571 * present or line_count > 1. It looks better if we do all the inserts
9572 * at once.
9573 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9574 * insert is just empty lines and T_CE is not present or line_count >
9575 * 1.
9576 * 4. Use T_AL (insert line) if it exists.
9577 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9578 * just empty lines.
9579 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9580 * just empty lines.
9581 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9582 * the 'da' flag is not set or we have clear line capability.
9583 * 8. redraw the characters from ScreenLines[].
9584 *
9585 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9586 * the scrollbar for the window. It does have insert line, use that if it
9587 * exists.
9588 */
9589 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009590#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9592 type = USE_REDRAW;
9593 else
9594#endif
9595 if (can_clear(T_CD) && result_empty)
9596 type = USE_T_CD;
9597 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9598 type = USE_T_CAL;
9599 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9600 type = USE_T_CDL;
9601 else if (*T_AL != NUL)
9602 type = USE_T_AL;
9603 else if (can_ce && result_empty)
9604 type = USE_T_CE;
9605 else if (*T_DL != NUL && result_empty)
9606 type = USE_T_DL;
9607 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9608 type = USE_T_SR;
9609 else
9610 return FAIL;
9611
9612 /*
9613 * For clearing the lines screen_del_lines() is used. This will also take
9614 * care of t_db if necessary.
9615 */
9616 if (type == USE_T_CD || type == USE_T_CDL ||
9617 type == USE_T_CE || type == USE_T_DL)
9618 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9619
9620 /*
9621 * If text is retained below the screen, first clear or delete as many
9622 * lines at the bottom of the window as are about to be inserted so that
9623 * the deleted lines won't later surface during a screen_del_lines.
9624 */
9625 if (*T_DB)
9626 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9627
9628#ifdef FEAT_CLIPBOARD
9629 /* Remove a modeless selection when inserting lines halfway the screen
9630 * or not the full width of the screen. */
9631 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009632# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 || (wp != NULL && wp->w_width != Columns)
9634# endif
9635 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009636 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 else
9638 clip_scroll_selection(-line_count);
9639#endif
9640
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641#ifdef FEAT_GUI
9642 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9643 * scrolling is actually carried out. */
9644 gui_dont_update_cursor();
9645#endif
9646
9647 if (*T_CCS != NUL) /* cursor relative to region */
9648 cursor_row = row;
9649 else
9650 cursor_row = row + off;
9651
9652 /*
9653 * Shift LineOffset[] line_count down to reflect the inserted lines.
9654 * Clear the inserted lines in ScreenLines[].
9655 */
9656 row += off;
9657 end += off;
9658 for (i = 0; i < line_count; ++i)
9659 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009660#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 if (wp != NULL && wp->w_width != Columns)
9662 {
9663 /* need to copy part of a line */
9664 j = end - 1 - i;
9665 while ((j -= line_count) >= row)
9666 linecopy(j + line_count, j, wp);
9667 j += line_count;
9668 if (can_clear((char_u *)" "))
9669 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9670 else
9671 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9672 LineWraps[j] = FALSE;
9673 }
9674 else
9675#endif
9676 {
9677 j = end - 1 - i;
9678 temp = LineOffset[j];
9679 while ((j -= line_count) >= row)
9680 {
9681 LineOffset[j + line_count] = LineOffset[j];
9682 LineWraps[j + line_count] = LineWraps[j];
9683 }
9684 LineOffset[j + line_count] = temp;
9685 LineWraps[j + line_count] = FALSE;
9686 if (can_clear((char_u *)" "))
9687 lineclear(temp, (int)Columns);
9688 else
9689 lineinvalid(temp, (int)Columns);
9690 }
9691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
9693 screen_stop_highlight();
9694 windgoto(cursor_row, 0);
9695
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009696#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 /* redraw the characters */
9698 if (type == USE_REDRAW)
9699 redraw_block(row, end, wp);
9700 else
9701#endif
9702 if (type == USE_T_CAL)
9703 {
9704 term_append_lines(line_count);
9705 screen_start(); /* don't know where cursor is now */
9706 }
9707 else
9708 {
9709 for (i = 0; i < line_count; i++)
9710 {
9711 if (type == USE_T_AL)
9712 {
9713 if (i && cursor_row != 0)
9714 windgoto(cursor_row, 0);
9715 out_str(T_AL);
9716 }
9717 else /* type == USE_T_SR */
9718 out_str(T_SR);
9719 screen_start(); /* don't know where cursor is now */
9720 }
9721 }
9722
9723 /*
9724 * With scroll-reverse and 'da' flag set we need to clear the lines that
9725 * have been scrolled down into the region.
9726 */
9727 if (type == USE_T_SR && *T_DA)
9728 {
9729 for (i = 0; i < line_count; ++i)
9730 {
9731 windgoto(off + i, 0);
9732 out_str(T_CE);
9733 screen_start(); /* don't know where cursor is now */
9734 }
9735 }
9736
9737#ifdef FEAT_GUI
9738 gui_can_update_cursor();
9739 if (gui.in_use)
9740 out_flush(); /* always flush after a scroll */
9741#endif
9742 return OK;
9743}
9744
9745/*
9746 * delete lines on the screen and update ScreenLines[]
9747 * 'end' is the line after the scrolled part. Normally it is Rows.
9748 * When scrolling region used 'off' is the offset from the top for the region.
9749 * 'row' and 'end' are relative to the start of the region.
9750 *
9751 * Return OK for success, FAIL if the lines are not deleted.
9752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009754screen_del_lines(
9755 int off,
9756 int row,
9757 int line_count,
9758 int end,
9759 int force, /* even when line_count > p_ttyscroll */
9760 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761{
9762 int j;
9763 int i;
9764 unsigned temp;
9765 int cursor_row;
9766 int cursor_end;
9767 int result_empty; /* result is empty until end of region */
9768 int can_delete; /* deleting line codes can be used */
9769 int type;
9770
9771 /*
9772 * FAIL if
9773 * - there is no valid screen
9774 * - the screen has to be redrawn completely
9775 * - the line count is less than one
9776 * - the line count is more than 'ttyscroll'
9777 */
9778 if (!screen_valid(TRUE) || line_count <= 0 ||
9779 (!force && line_count > p_ttyscroll))
9780 return FAIL;
9781
9782 /*
9783 * Check if the rest of the current region will become empty.
9784 */
9785 result_empty = row + line_count >= end;
9786
9787 /*
9788 * We can delete lines only when 'db' flag not set or when 'ce' option
9789 * available.
9790 */
9791 can_delete = (*T_DB == NUL || can_clear(T_CE));
9792
9793 /*
9794 * There are six ways to delete lines:
9795 * 0. When in a vertically split window and t_CV isn't set, redraw the
9796 * characters from ScreenLines[].
9797 * 1. Use T_CD if it exists and the result is empty.
9798 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9799 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9800 * none of the other ways work.
9801 * 4. Use T_CE (erase line) if the result is empty.
9802 * 5. Use T_DL (delete line) if it exists.
9803 * 6. redraw the characters from ScreenLines[].
9804 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009805#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9807 type = USE_REDRAW;
9808 else
9809#endif
9810 if (can_clear(T_CD) && result_empty)
9811 type = USE_T_CD;
9812#if defined(__BEOS__) && defined(BEOS_DR8)
9813 /*
9814 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9815 * its internal termcap... this works okay for tests which test *T_DB !=
9816 * NUL. It has the disadvantage that the user cannot use any :set t_*
9817 * command to get T_DB (back) to empty_option, only :set term=... will do
9818 * the trick...
9819 * Anyway, this hack will hopefully go away with the next OS release.
9820 * (Olaf Seibert)
9821 */
9822 else if (row == 0 && T_DB == empty_option
9823 && (line_count == 1 || *T_CDL == NUL))
9824#else
9825 else if (row == 0 && (
9826#ifndef AMIGA
9827 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9828 * up, so use delete-line command */
9829 line_count == 1 ||
9830#endif
9831 *T_CDL == NUL))
9832#endif
9833 type = USE_NL;
9834 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9835 type = USE_T_CDL;
9836 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009837#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 && (wp == NULL || wp->w_width == Columns)
9839#endif
9840 )
9841 type = USE_T_CE;
9842 else if (*T_DL != NUL && can_delete)
9843 type = USE_T_DL;
9844 else if (*T_CDL != NUL && can_delete)
9845 type = USE_T_CDL;
9846 else
9847 return FAIL;
9848
9849#ifdef FEAT_CLIPBOARD
9850 /* Remove a modeless selection when deleting lines halfway the screen or
9851 * not the full width of the screen. */
9852 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009853# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 || (wp != NULL && wp->w_width != Columns)
9855# endif
9856 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009857 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 else
9859 clip_scroll_selection(line_count);
9860#endif
9861
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862#ifdef FEAT_GUI
9863 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9864 * scrolling is actually carried out. */
9865 gui_dont_update_cursor();
9866#endif
9867
9868 if (*T_CCS != NUL) /* cursor relative to region */
9869 {
9870 cursor_row = row;
9871 cursor_end = end;
9872 }
9873 else
9874 {
9875 cursor_row = row + off;
9876 cursor_end = end + off;
9877 }
9878
9879 /*
9880 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9881 * Clear the inserted lines in ScreenLines[].
9882 */
9883 row += off;
9884 end += off;
9885 for (i = 0; i < line_count; ++i)
9886 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009887#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 if (wp != NULL && wp->w_width != Columns)
9889 {
9890 /* need to copy part of a line */
9891 j = row + i;
9892 while ((j += line_count) <= end - 1)
9893 linecopy(j - line_count, j, wp);
9894 j -= line_count;
9895 if (can_clear((char_u *)" "))
9896 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9897 else
9898 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9899 LineWraps[j] = FALSE;
9900 }
9901 else
9902#endif
9903 {
9904 /* whole width, moving the line pointers is faster */
9905 j = row + i;
9906 temp = LineOffset[j];
9907 while ((j += line_count) <= end - 1)
9908 {
9909 LineOffset[j - line_count] = LineOffset[j];
9910 LineWraps[j - line_count] = LineWraps[j];
9911 }
9912 LineOffset[j - line_count] = temp;
9913 LineWraps[j - line_count] = FALSE;
9914 if (can_clear((char_u *)" "))
9915 lineclear(temp, (int)Columns);
9916 else
9917 lineinvalid(temp, (int)Columns);
9918 }
9919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920
9921 screen_stop_highlight();
9922
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009923#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 /* redraw the characters */
9925 if (type == USE_REDRAW)
9926 redraw_block(row, end, wp);
9927 else
9928#endif
9929 if (type == USE_T_CD) /* delete the lines */
9930 {
9931 windgoto(cursor_row, 0);
9932 out_str(T_CD);
9933 screen_start(); /* don't know where cursor is now */
9934 }
9935 else if (type == USE_T_CDL)
9936 {
9937 windgoto(cursor_row, 0);
9938 term_delete_lines(line_count);
9939 screen_start(); /* don't know where cursor is now */
9940 }
9941 /*
9942 * Deleting lines at top of the screen or scroll region: Just scroll
9943 * the whole screen (scroll region) up by outputting newlines on the
9944 * last line.
9945 */
9946 else if (type == USE_NL)
9947 {
9948 windgoto(cursor_end - 1, 0);
9949 for (i = line_count; --i >= 0; )
9950 out_char('\n'); /* cursor will remain on same line */
9951 }
9952 else
9953 {
9954 for (i = line_count; --i >= 0; )
9955 {
9956 if (type == USE_T_DL)
9957 {
9958 windgoto(cursor_row, 0);
9959 out_str(T_DL); /* delete a line */
9960 }
9961 else /* type == USE_T_CE */
9962 {
9963 windgoto(cursor_row + i, 0);
9964 out_str(T_CE); /* erase a line */
9965 }
9966 screen_start(); /* don't know where cursor is now */
9967 }
9968 }
9969
9970 /*
9971 * If the 'db' flag is set, we need to clear the lines that have been
9972 * scrolled up at the bottom of the region.
9973 */
9974 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9975 {
9976 for (i = line_count; i > 0; --i)
9977 {
9978 windgoto(cursor_end - i, 0);
9979 out_str(T_CE); /* erase a line */
9980 screen_start(); /* don't know where cursor is now */
9981 }
9982 }
9983
9984#ifdef FEAT_GUI
9985 gui_can_update_cursor();
9986 if (gui.in_use)
9987 out_flush(); /* always flush after a scroll */
9988#endif
9989
9990 return OK;
9991}
9992
9993/*
9994 * show the current mode and ruler
9995 *
9996 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9997 * If clear_cmdline is FALSE there may be a message there that needs to be
9998 * cleared only if a mode is shown.
9999 * Return the length of the message (0 if no message).
10000 */
10001 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010002showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
10004 int need_clear;
10005 int length = 0;
10006 int do_mode;
10007 int attr;
10008 int nwr_save;
10009#ifdef FEAT_INS_EXPAND
10010 int sub_attr;
10011#endif
10012
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010013 do_mode = ((p_smd && msg_silent == 0)
10014 && ((State & INSERT)
10015 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010016 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 if (do_mode || Recording)
10018 {
10019 /*
10020 * Don't show mode right now, when not redrawing or inside a mapping.
10021 * Call char_avail() only when we are going to show something, because
10022 * it takes a bit of time.
10023 */
10024 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10025 {
10026 redraw_cmdline = TRUE; /* show mode later */
10027 return 0;
10028 }
10029
10030 nwr_save = need_wait_return;
10031
10032 /* wait a bit before overwriting an important message */
10033 check_for_delay(FALSE);
10034
10035 /* if the cmdline is more than one line high, erase top lines */
10036 need_clear = clear_cmdline;
10037 if (clear_cmdline && cmdline_row < Rows - 1)
10038 msg_clr_cmdline(); /* will reset clear_cmdline */
10039
10040 /* Position on the last line in the window, column 0 */
10041 msg_pos_mode();
10042 cursor_off();
10043 attr = hl_attr(HLF_CM); /* Highlight mode */
10044 if (do_mode)
10045 {
10046 MSG_PUTS_ATTR("--", attr);
10047#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010048 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010049# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010050 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010051# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010052 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010053# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010054 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010055# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 MSG_PUTS_ATTR(" IM", attr);
10057# else
10058 MSG_PUTS_ATTR(" XIM", attr);
10059# endif
10060#endif
10061#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10062 if (gui.in_use)
10063 {
10064 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010065 {
10066 /* HANGUL */
10067 if (enc_utf8)
10068 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10069 else
10070 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 }
10073#endif
10074#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010075 /* CTRL-X in Insert mode */
10076 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 {
10078 /* These messages can get long, avoid a wrap in a narrow
10079 * window. Prefer showing edit_submode_extra. */
10080 length = (Rows - msg_row) * Columns - 3;
10081 if (edit_submode_extra != NULL)
10082 length -= vim_strsize(edit_submode_extra);
10083 if (length > 0)
10084 {
10085 if (edit_submode_pre != NULL)
10086 length -= vim_strsize(edit_submode_pre);
10087 if (length - vim_strsize(edit_submode) > 0)
10088 {
10089 if (edit_submode_pre != NULL)
10090 msg_puts_attr(edit_submode_pre, attr);
10091 msg_puts_attr(edit_submode, attr);
10092 }
10093 if (edit_submode_extra != NULL)
10094 {
10095 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10096 if ((int)edit_submode_highl < (int)HLF_COUNT)
10097 sub_attr = hl_attr(edit_submode_highl);
10098 else
10099 sub_attr = attr;
10100 msg_puts_attr(edit_submode_extra, sub_attr);
10101 }
10102 }
10103 length = 0;
10104 }
10105 else
10106#endif
10107 {
10108#ifdef FEAT_VREPLACE
10109 if (State & VREPLACE_FLAG)
10110 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10111 else
10112#endif
10113 if (State & REPLACE_FLAG)
10114 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10115 else if (State & INSERT)
10116 {
10117#ifdef FEAT_RIGHTLEFT
10118 if (p_ri)
10119 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10120#endif
10121 MSG_PUTS_ATTR(_(" INSERT"), attr);
10122 }
10123 else if (restart_edit == 'I')
10124 MSG_PUTS_ATTR(_(" (insert)"), attr);
10125 else if (restart_edit == 'R')
10126 MSG_PUTS_ATTR(_(" (replace)"), attr);
10127 else if (restart_edit == 'V')
10128 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10129#ifdef FEAT_RIGHTLEFT
10130 if (p_hkmap)
10131 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10132# ifdef FEAT_FKMAP
10133 if (p_fkmap)
10134 MSG_PUTS_ATTR(farsi_text_5, attr);
10135# endif
10136#endif
10137#ifdef FEAT_KEYMAP
10138 if (State & LANGMAP)
10139 {
10140# ifdef FEAT_ARABIC
10141 if (curwin->w_p_arab)
10142 MSG_PUTS_ATTR(_(" Arabic"), attr);
10143 else
10144# endif
10145 MSG_PUTS_ATTR(_(" (lang)"), attr);
10146 }
10147#endif
10148 if ((State & INSERT) && p_paste)
10149 MSG_PUTS_ATTR(_(" (paste)"), attr);
10150
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 if (VIsual_active)
10152 {
10153 char *p;
10154
10155 /* Don't concatenate separate words to avoid translation
10156 * problems. */
10157 switch ((VIsual_select ? 4 : 0)
10158 + (VIsual_mode == Ctrl_V) * 2
10159 + (VIsual_mode == 'V'))
10160 {
10161 case 0: p = N_(" VISUAL"); break;
10162 case 1: p = N_(" VISUAL LINE"); break;
10163 case 2: p = N_(" VISUAL BLOCK"); break;
10164 case 4: p = N_(" SELECT"); break;
10165 case 5: p = N_(" SELECT LINE"); break;
10166 default: p = N_(" SELECT BLOCK"); break;
10167 }
10168 MSG_PUTS_ATTR(_(p), attr);
10169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 MSG_PUTS_ATTR(" --", attr);
10171 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010172
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 need_clear = TRUE;
10174 }
10175 if (Recording
10176#ifdef FEAT_INS_EXPAND
10177 && edit_submode == NULL /* otherwise it gets too long */
10178#endif
10179 )
10180 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010181 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 need_clear = TRUE;
10183 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010184
10185 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 if (need_clear || clear_cmdline)
10187 msg_clr_eos();
10188 msg_didout = FALSE; /* overwrite this message */
10189 length = msg_col;
10190 msg_col = 0;
10191 need_wait_return = nwr_save; /* never ask for hit-return for this */
10192 }
10193 else if (clear_cmdline && msg_silent == 0)
10194 /* Clear the whole command line. Will reset "clear_cmdline". */
10195 msg_clr_cmdline();
10196
10197#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 /* In Visual mode the size of the selected area must be redrawn. */
10199 if (VIsual_active)
10200 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201
10202 /* If the last window has no status line, the ruler is after the mode
10203 * message and must be redrawn */
10204 if (redrawing()
10205# ifdef FEAT_WINDOWS
10206 && lastwin->w_status_height == 0
10207# endif
10208 )
10209 win_redr_ruler(lastwin, TRUE);
10210#endif
10211 redraw_cmdline = FALSE;
10212 clear_cmdline = FALSE;
10213
10214 return length;
10215}
10216
10217/*
10218 * Position for a mode message.
10219 */
10220 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010221msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222{
10223 msg_col = 0;
10224 msg_row = Rows - 1;
10225}
10226
10227/*
10228 * Delete mode message. Used when ESC is typed which is expected to end
10229 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010230 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 */
10232 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010233unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234{
10235 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010236 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 */
10238 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10239 redraw_cmdline = TRUE; /* delete mode later */
10240 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010241 clearmode();
10242}
10243
10244/*
10245 * Clear the mode message.
10246 */
10247 void
10248clearmode()
10249{
10250 msg_pos_mode();
10251 if (Recording)
10252 recording_mode(hl_attr(HLF_CM));
10253 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254}
10255
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010256 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010257recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010258{
10259 MSG_PUTS_ATTR(_("recording"), attr);
10260 if (!shortmess(SHM_RECORDING))
10261 {
10262 char_u s[4];
10263 sprintf((char *)s, " @%c", Recording);
10264 MSG_PUTS_ATTR(s, attr);
10265 }
10266}
10267
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010268#if defined(FEAT_WINDOWS)
10269/*
10270 * Draw the tab pages line at the top of the Vim window.
10271 */
10272 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010273draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010274{
10275 int tabcount = 0;
10276 tabpage_T *tp;
10277 int tabwidth;
10278 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010279 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010280 int attr;
10281 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010282 win_T *cwp;
10283 int wincount;
10284 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010285 int c;
10286 int len;
10287 int attr_sel = hl_attr(HLF_TPS);
10288 int attr_nosel = hl_attr(HLF_TP);
10289 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010290 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010291 int room;
10292 int use_sep_chars = (t_colors < 8
10293#ifdef FEAT_GUI
10294 && !gui.in_use
10295#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010296#ifdef FEAT_TERMTRUECOLOR
10297 && !p_guicolors
10298#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010299 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010300
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010301 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010302
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010303#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010304 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010305 if (gui_use_tabline())
10306 {
10307 gui_update_tabline();
10308 return;
10309 }
10310#endif
10311
10312 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010313 return;
10314
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010315#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010316
10317 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10318 for (scol = 0; scol < Columns; ++scol)
10319 TabPageIdxs[scol] = 0;
10320
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010321 /* Use the 'tabline' option if it's set. */
10322 if (*p_tal != NUL)
10323 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010324 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010325
10326 /* Check for an error. If there is one we would loop in redrawing the
10327 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010328 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010329 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010330 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010331 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010332 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010333 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010334 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010335 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010336#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010337 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010338 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10339 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010340
Bram Moolenaar238a5642006-02-21 22:12:05 +000010341 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10342 if (tabwidth < 6)
10343 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010344
Bram Moolenaar238a5642006-02-21 22:12:05 +000010345 attr = attr_nosel;
10346 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010347 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010348 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10349 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010350 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010351 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010352
Bram Moolenaar238a5642006-02-21 22:12:05 +000010353 if (tp->tp_topframe == topframe)
10354 attr = attr_sel;
10355 if (use_sep_chars && col > 0)
10356 screen_putchar('|', 0, col++, attr);
10357
10358 if (tp->tp_topframe != topframe)
10359 attr = attr_nosel;
10360
10361 screen_putchar(' ', 0, col++, attr);
10362
10363 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010364 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010365 cwp = curwin;
10366 wp = firstwin;
10367 }
10368 else
10369 {
10370 cwp = tp->tp_curwin;
10371 wp = tp->tp_firstwin;
10372 }
10373
10374 modified = FALSE;
10375 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10376 if (bufIsChanged(wp->w_buffer))
10377 modified = TRUE;
10378 if (modified || wincount > 1)
10379 {
10380 if (wincount > 1)
10381 {
10382 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010383 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010384 if (col + len >= Columns - 3)
10385 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010386 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010387#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010388 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010389#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010390 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010391#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010392 );
10393 col += len;
10394 }
10395 if (modified)
10396 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10397 screen_putchar(' ', 0, col++, attr);
10398 }
10399
10400 room = scol - col + tabwidth - 1;
10401 if (room > 0)
10402 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010403 /* Get buffer name in NameBuff[] */
10404 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010405 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010406 len = vim_strsize(NameBuff);
10407 p = NameBuff;
10408#ifdef FEAT_MBYTE
10409 if (has_mbyte)
10410 while (len > room)
10411 {
10412 len -= ptr2cells(p);
10413 mb_ptr_adv(p);
10414 }
10415 else
10416#endif
10417 if (len > room)
10418 {
10419 p += len - room;
10420 len = room;
10421 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010422 if (len > Columns - col - 1)
10423 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010424
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010425 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010426 col += len;
10427 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010428 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010429
10430 /* Store the tab page number in TabPageIdxs[], so that
10431 * jump_to_mouse() knows where each one is. */
10432 ++tabcount;
10433 while (scol < col)
10434 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010435 }
10436
Bram Moolenaar238a5642006-02-21 22:12:05 +000010437 if (use_sep_chars)
10438 c = '_';
10439 else
10440 c = ' ';
10441 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010442
10443 /* Put an "X" for closing the current tab if there are several. */
10444 if (first_tabpage->tp_next != NULL)
10445 {
10446 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10447 TabPageIdxs[Columns - 1] = -999;
10448 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010449 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010450
10451 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10452 * set. */
10453 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010454}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010455
10456/*
10457 * Get buffer name for "buf" into NameBuff[].
10458 * Takes care of special buffer names and translates special characters.
10459 */
10460 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010461get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010462{
10463 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010464 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010465 else
10466 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10467 trans_characters(NameBuff, MAXPATHL);
10468}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010469#endif
10470
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10472/*
10473 * Get the character to use in a status line. Get its attributes in "*attr".
10474 */
10475 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010476fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477{
10478 int fill;
10479 if (is_curwin)
10480 {
10481 *attr = hl_attr(HLF_S);
10482 fill = fill_stl;
10483 }
10484 else
10485 {
10486 *attr = hl_attr(HLF_SNC);
10487 fill = fill_stlnc;
10488 }
10489 /* Use fill when there is highlighting, and highlighting of current
10490 * window differs, or the fillchars differ, or this is not the
10491 * current window */
10492 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10493 || !is_curwin || firstwin == lastwin)
10494 || (fill_stl != fill_stlnc)))
10495 return fill;
10496 if (is_curwin)
10497 return '^';
10498 return '=';
10499}
10500#endif
10501
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010502#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503/*
10504 * Get the character to use in a separator between vertically split windows.
10505 * Get its attributes in "*attr".
10506 */
10507 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010508fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
10510 *attr = hl_attr(HLF_C);
10511 if (*attr == 0 && fill_vert == ' ')
10512 return '|';
10513 else
10514 return fill_vert;
10515}
10516#endif
10517
10518/*
10519 * Return TRUE if redrawing should currently be done.
10520 */
10521 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010522redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523{
10524 return (!RedrawingDisabled
10525 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10526}
10527
10528/*
10529 * Return TRUE if printing messages should currently be done.
10530 */
10531 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010532messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533{
10534 return (!(p_lz && char_avail() && !KeyTyped));
10535}
10536
10537/*
10538 * Show current status info in ruler and various other places
10539 * If always is FALSE, only show ruler if position has changed.
10540 */
10541 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010542showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543{
10544 if (!always && !redrawing())
10545 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010546#ifdef FEAT_INS_EXPAND
10547 if (pum_visible())
10548 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010549# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010550 /* Don't redraw right now, do it later. */
10551 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010552# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010553 return;
10554 }
10555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010557 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010558 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010559 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 else
10562#endif
10563#ifdef FEAT_CMDL_INFO
10564 win_redr_ruler(curwin, always);
10565#endif
10566
10567#ifdef FEAT_TITLE
10568 if (need_maketitle
10569# ifdef FEAT_STL_OPT
10570 || (p_icon && (stl_syntax & STL_IN_ICON))
10571 || (p_title && (stl_syntax & STL_IN_TITLE))
10572# endif
10573 )
10574 maketitle();
10575#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010576#ifdef FEAT_WINDOWS
10577 /* Redraw the tab pages line if needed. */
10578 if (redraw_tabline)
10579 draw_tabline();
10580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581}
10582
10583#ifdef FEAT_CMDL_INFO
10584 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010585win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010587#define RULER_BUF_LEN 70
10588 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 int row;
10590 int fillchar;
10591 int attr;
10592 int empty_line = FALSE;
10593 colnr_T virtcol;
10594 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010595 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010597#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 int this_ru_col;
10599 int off = 0;
10600 int width = Columns;
10601# define WITH_OFF(x) x
10602# define WITH_WIDTH(x) x
10603#else
10604# define WITH_OFF(x) 0
10605# define WITH_WIDTH(x) Columns
10606# define this_ru_col ru_col
10607#endif
10608
10609 /* If 'ruler' off or redrawing disabled, don't do anything */
10610 if (!p_ru)
10611 return;
10612
10613 /*
10614 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10615 * after deleting lines, before cursor.lnum is corrected.
10616 */
10617 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10618 return;
10619
10620#ifdef FEAT_INS_EXPAND
10621 /* Don't draw the ruler while doing insert-completion, it might overwrite
10622 * the (long) mode message. */
10623# ifdef FEAT_WINDOWS
10624 if (wp == lastwin && lastwin->w_status_height == 0)
10625# endif
10626 if (edit_submode != NULL)
10627 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010628 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10629 if (pum_visible())
10630 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631#endif
10632
10633#ifdef FEAT_STL_OPT
10634 if (*p_ruf)
10635 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010636 int save_called_emsg = called_emsg;
10637
10638 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010640 if (called_emsg)
10641 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010642 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010643 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 return;
10645 }
10646#endif
10647
10648 /*
10649 * Check if not in Insert mode and the line is empty (will show "0-1").
10650 */
10651 if (!(State & INSERT)
10652 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10653 empty_line = TRUE;
10654
10655 /*
10656 * Only draw the ruler when something changed.
10657 */
10658 validate_virtcol_win(wp);
10659 if ( redraw_cmdline
10660 || always
10661 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10662 || wp->w_cursor.col != wp->w_ru_cursor.col
10663 || wp->w_virtcol != wp->w_ru_virtcol
10664#ifdef FEAT_VIRTUALEDIT
10665 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10666#endif
10667 || wp->w_topline != wp->w_ru_topline
10668 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10669#ifdef FEAT_DIFF
10670 || wp->w_topfill != wp->w_ru_topfill
10671#endif
10672 || empty_line != wp->w_ru_empty)
10673 {
10674 cursor_off();
10675#ifdef FEAT_WINDOWS
10676 if (wp->w_status_height)
10677 {
10678 row = W_WINROW(wp) + wp->w_height;
10679 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 off = W_WINCOL(wp);
10681 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 }
10683 else
10684#endif
10685 {
10686 row = Rows - 1;
10687 fillchar = ' ';
10688 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010689#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 width = Columns;
10691 off = 0;
10692#endif
10693 }
10694
10695 /* In list mode virtcol needs to be recomputed */
10696 virtcol = wp->w_virtcol;
10697 if (wp->w_p_list && lcs_tab1 == NUL)
10698 {
10699 wp->w_p_list = FALSE;
10700 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10701 wp->w_p_list = TRUE;
10702 }
10703
10704 /*
10705 * Some sprintfs return the length, some return a pointer.
10706 * To avoid portability problems we use strlen() here.
10707 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010708 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10710 ? 0L
10711 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010712 len = STRLEN(buffer);
10713 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10715 (int)virtcol + 1);
10716
10717 /*
10718 * Add a "50%" if there is room for it.
10719 * On the last line, don't print in the last column (scrolls the
10720 * screen up on some terminals).
10721 */
10722 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010723 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 o = i + vim_strsize(buffer + i + 1);
10725#ifdef FEAT_WINDOWS
10726 if (wp->w_status_height == 0) /* can't use last char of screen */
10727#endif
10728 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010729#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 this_ru_col = ru_col - (Columns - width);
10731 if (this_ru_col < 0)
10732 this_ru_col = 0;
10733#endif
10734 /* Never use more than half the window/screen width, leave the other
10735 * half for the filename. */
10736 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10737 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10738 if (this_ru_col + o < WITH_WIDTH(width))
10739 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010740 /* need at least 3 chars left for get_rel_pos() + NUL */
10741 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 {
10743#ifdef FEAT_MBYTE
10744 if (has_mbyte)
10745 i += (*mb_char2bytes)(fillchar, buffer + i);
10746 else
10747#endif
10748 buffer[i++] = fillchar;
10749 ++o;
10750 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010751 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 }
10753 /* Truncate at window boundary. */
10754#ifdef FEAT_MBYTE
10755 if (has_mbyte)
10756 {
10757 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010758 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 {
10760 o += (*mb_ptr2cells)(buffer + i);
10761 if (this_ru_col + o > WITH_WIDTH(width))
10762 {
10763 buffer[i] = NUL;
10764 break;
10765 }
10766 }
10767 }
10768 else
10769#endif
10770 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10771 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10772
10773 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10774 i = redraw_cmdline;
10775 screen_fill(row, row + 1,
10776 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10777 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10778 fillchar, fillchar, attr);
10779 /* don't redraw the cmdline because of showing the ruler */
10780 redraw_cmdline = i;
10781 wp->w_ru_cursor = wp->w_cursor;
10782 wp->w_ru_virtcol = wp->w_virtcol;
10783 wp->w_ru_empty = empty_line;
10784 wp->w_ru_topline = wp->w_topline;
10785 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10786#ifdef FEAT_DIFF
10787 wp->w_ru_topfill = wp->w_topfill;
10788#endif
10789 }
10790}
10791#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010792
10793#if defined(FEAT_LINEBREAK) || defined(PROTO)
10794/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010795 * Return the width of the 'number' and 'relativenumber' column.
10796 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010797 * Otherwise it depends on 'numberwidth' and the line count.
10798 */
10799 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010800number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010801{
10802 int n;
10803 linenr_T lnum;
10804
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010805 if (wp->w_p_rnu && !wp->w_p_nu)
10806 /* cursor line shows "0" */
10807 lnum = wp->w_height;
10808 else
10809 /* cursor line shows absolute line number */
10810 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010811
Bram Moolenaar6b314672015-03-20 15:42:10 +010010812 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010813 return wp->w_nrwidth_width;
10814 wp->w_nrwidth_line_count = lnum;
10815
10816 n = 0;
10817 do
10818 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010819 lnum /= 10;
10820 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010821 } while (lnum > 0);
10822
10823 /* 'numberwidth' gives the minimal width plus one */
10824 if (n < wp->w_p_nuw - 1)
10825 n = wp->w_p_nuw - 1;
10826
10827 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010828 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010829 return n;
10830}
10831#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010832
10833/*
10834 * Return the current cursor column. This is the actual position on the
10835 * screen. First column is 0.
10836 */
10837 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010838screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010839{
10840 return screen_cur_col;
10841}
10842
10843/*
10844 * Return the current cursor row. This is the actual position on the screen.
10845 * First row is 0.
10846 */
10847 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010848screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010849{
10850 return screen_cur_row;
10851}